-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3.java
More file actions
193 lines (168 loc) · 6.23 KB
/
Assignment3.java
File metadata and controls
193 lines (168 loc) · 6.23 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.util.InputMismatchException;
import java.util.Scanner;
class InvalidMarksException extends Exception {
public InvalidMarksException(String message) {
super(message);
}
}
class Student {
private int rollNumber;
private String studentName;
private int[] marks;
public Student(int rollNumber, String studentName, int[] marks) throws InvalidMarksException {
this.rollNumber = rollNumber;
this.studentName = studentName;
this.marks = marks;
validateMarks();
}
private void validateMarks() throws InvalidMarksException {
for (int i = 0; i < marks.length; i++) {
if (marks[i] < 0 || marks[i] > 100) {
throw new InvalidMarksException("Invalid marks for subject " + (i + 1) + ": " + marks[i]);
}
}
}
public double calculateAverage() {
if (marks == null || marks.length == 0) {
return 0.0;
}
int sum = 0;
for (int mark : marks) {
sum += mark;
}
return (double) sum / marks.length;
}
public void displayResult() {
System.out.println("Roll Number: " + rollNumber);
System.out.println("Student Name: " + studentName);
System.out.print("Marks: ");
for (int mark : marks) {
System.out.print(mark + " ");
}
System.out.println();
double avg = calculateAverage();
System.out.println("Average: " + avg);
String resultStatus = avg >= 40.0 ? "Pass" : "Fail";
System.out.println("Result: " + resultStatus);
}
public int getRollNumber() {
return rollNumber;
}
}
public class Main {
private Student[] students;
private int studentCount;
private Scanner scanner;
private static final int MAX_STUDENTS = 10;
public Main() {
students = new Student[MAX_STUDENTS];
studentCount = 0;
scanner = new Scanner(System.in);
}
public void addStudent() {
int rollNumber = 0;
String studentName = "";
int[] marks = new int[3];
System.out.print("Enter Roll Number: ");
try {
rollNumber = scanner.nextInt();
scanner.nextLine();
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input for Roll Number. Must be an integer.");
scanner.nextLine();
return;
}
System.out.print("Enter Student Name: ");
try {
studentName = scanner.nextLine();
} catch (Exception e) {
System.out.println("Error: Could not read student name.");
return;
}
for (int i = 0; i < marks.length; i++) {
System.out.print("Enter marks for subject " + (i + 1) + ": ");
try {
marks[i] = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input for marks. Must be an integer.");
scanner.nextLine();
return;
}
}
scanner.nextLine();
try {
Student newStudent = new Student(rollNumber, studentName, marks);
students[studentCount++] = newStudent;
System.out.println("Student added successfully. Returning to main menu...");
} catch (InvalidMarksException e) {
System.out.println("Error: " + e.getMessage() + " Returning to main menu...");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Maximum student limit reached (" + MAX_STUDENTS + ").");
}
}
public void showStudentDetails() {
System.out.print("Enter Roll Number to search: ");
try {
int searchRoll = scanner.nextInt();
scanner.nextLine();
Student foundStudent = null;
for (int i = 0; i < studentCount; i++) {
if (students[i].getRollNumber() == searchRoll) {
foundStudent = students[i];
break;
}
}
if (foundStudent != null) {
foundStudent.displayResult();
} else {
System.out.println("Error: Student with Roll Number " + searchRoll + " not found.");
}
System.out.println("Search completed.");
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input for Roll Number. Must be an integer.");
scanner.nextLine();
}
}
public void mainMenu() {
int choice = 0;
do {
System.out.println("\n===== Student Result Management System =====");
System.out.println("1. Add Student");
System.out.println("2. Show Student Details");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
try {
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addStudent();
break;
case 2:
showStudentDetails();
break;
case 3:
System.out.println("Exiting program. Thank you!");
break;
default:
System.out.println("Invalid choice. Please enter 1, 2, or 3.");
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Please enter a number (1, 2, or 3).");
choice = 0;
scanner.nextLine();
}
} while (choice != 3);
}
public static void main(String[] args) {
Main manager = new Main();
try {
manager.mainMenu();
} finally {
if (manager.scanner != null) {
manager.scanner.close();
System.out.println("Scanner resource released.");
}
}
}
}