-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentResultSystem.java
More file actions
94 lines (76 loc) · 2.95 KB
/
Copy pathStudentResultSystem.java
File metadata and controls
94 lines (76 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
// 1. Interface (As per diagram: Exam)
interface Exam {
void percentCal();
}
// 2. Base Class (As per diagram: Student)
class Student {
private String name;
private int rollNo;
private int marks1;
private int marks2;
// Parameterized Constructor
public Student(String name, int rollNo, int marks1, int marks2) {
this.name = name;
this.rollNo = rollNo;
this.marks1 = marks1;
this.marks2 = marks2;
}
// Protected getters allow the Result subclass to safely access the marks
protected int getMarks1() { return marks1; }
protected int getMarks2() { return marks2; }
public void show() {
System.out.println("Student Name: " + name);
System.out.println("Roll Number: " + rollNo);
System.out.println("Marks in Paper 1: " + marks1);
System.out.println("Marks in Paper 2: " + marks2);
}
}
// 3. Derived Class (As per diagram: Result)
class Result extends Student implements Exam {
private double percentage;
public Result(String name, int rollNo, int marks1, int marks2) {
super(name, rollNo, marks1, marks2); // Call the parent constructor
}
// Implementing the interface method
@Override
public void percentCal() {
// Using getters to maintain encapsulation from the parent class
percentage = (getMarks1() + getMarks2()) / 2.0;
}
// Displaying final result
public void perDisplay() {
show(); // Inherited method from Student
percentCal(); // Implemented method from Exam interface
System.out.println("Total Percentage: " + percentage + "%");
}
}
// 4. Main Driver Class (PascalCase)
public class StudentResultSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--- Student Result Calculation System ---");
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your roll number: ");
int rollNo = getValidInt(scanner);
System.out.print("Enter your marks for Paper 1: ");
int marks1 = getValidInt(scanner);
System.out.print("Enter your marks for Paper 2: ");
int marks2 = getValidInt(scanner);
// Instantiating the Result object
System.out.println("\n=====================================");
Result studentResult = new Result(name, rollNo, marks1, marks2);
studentResult.perDisplay();
System.out.println("=====================================");
scanner.close();
}
// Helper Method to prevent Scanner crashes during input
private static int getValidInt(Scanner scanner) {
while (!scanner.hasNextInt()) {
System.out.print("Invalid input. Please enter a valid number: ");
scanner.next(); // Consume invalid input
}
return scanner.nextInt();
}
}