Upload files to "OOP/Java/Lab/Week10"
This commit is contained in:
parent
2b9a21ac62
commit
22f306573d
5 changed files with 160 additions and 0 deletions
51
OOP/Java/Lab/Week10/MarkOutOfBoundsHandling.java
Normal file
51
OOP/Java/Lab/Week10/MarkOutOfBoundsHandling.java
Normal file
|
@ -0,0 +1,51 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
class Students {
|
||||
String name;
|
||||
int marks[];
|
||||
|
||||
Students(String name, int marks[]) {
|
||||
this.name = name;
|
||||
this.marks = marks;
|
||||
}
|
||||
}
|
||||
|
||||
class arkOutOfBoundsHandling extends Exception {
|
||||
arkOutOfBoundsHandling(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
public class MarkOutOfBoundsHandling {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.print("Enter student name: ");
|
||||
String studentName = sc.nextLine();
|
||||
|
||||
System.out.print("Enter the number of subjects: ");
|
||||
int numSubjects = sc.nextInt();
|
||||
|
||||
int[] studentMarks = new int[numSubjects];
|
||||
|
||||
System.out.println("Enter marks for each subject:");
|
||||
|
||||
try {
|
||||
for (int i = 0; i < numSubjects; i++) {
|
||||
System.out.print("Subject " + (i + 1) + ": ");
|
||||
studentMarks[i] = sc.nextInt();
|
||||
if (studentMarks[i] < 0 || studentMarks[i] > 100) {
|
||||
throw new arkOutOfBoundsHandling("Marks should be between 0 and 100");
|
||||
}
|
||||
}
|
||||
Students student = new Students(studentName, studentMarks);
|
||||
System.out.println("Students details: " + student.name);
|
||||
System.out.print("Marks: ");
|
||||
for (int mark : student.marks) {
|
||||
System.out.print(mark + " ");
|
||||
}
|
||||
} catch (arkOutOfBoundsHandling e) {
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue