EXP 10 Report 22z433 Final
EXP 10 Report 22z433 Final
Roll No : 22Z433
Date : 03/11/2023
Experiment 10
1. Consider a program that reads an integer from the user and prints its reciprocal. If the
user enters zero, the program will throw an exception. Write the code to handle this
exception using a try-catch block:
Aim : To create a java program to read an integer from the user and print its
reciprocal. When the user enters zero it’ll throw an exception.
import java.util.Scanner;
int value;
try{
Page 1
Name : T K Santhosh
Roll No : 22Z433
Date : 03/11/2023
double output = reciprocal(value);
System.out.println("Output : " + output);
}
catch(Exception e){
System.out.println("You entered zero.");
}
finally{
scan.close();
}
}
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Enter an integer :
100
Output : 0.01
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Enter an integer :
0
You entered zero.
2. Define a class that has static methods main(), p(), q(), r() and s(). The main() method
calls p(). Method p() calls q(). Method q() calls r(). Method r() calls s().Method s()
declares a local array with six integer type elements and then attempts to access the
element at position 10. Therefore, an exception of type
ArrayIndexOutOfBoundsException is generated. Each method has a catch block for this
type of exception. The catch blocks in q(), r() and s() contain a throw statement to
generate this type of exception. Indicate the flow of control during the execution by
displaying the suitable output messages.Also include a finally block with print
statements.
package EXP_10;
Page 2
Name : T K Santhosh
Roll No : 22Z433
Date : 03/11/2023
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
In method `p` calling method `q`,
In method `q` calling method `r`,
In method `r` calling method `s`,
Exception: java.lang.ArrayIndexOutOfBoundsException: Index 10
out of bounds for length 6
Page 3
Name : T K Santhosh
Roll No : 22Z433
Date : 03/11/2023
3. Create a class Student with the data members Name, Register Number, Mark1, Mark2,
Mark3, Total and Average. Using necessary member functions get the input, calculate Total
and Average and display the student information. If the register number exceeds 6 digits
then raise one user defined exception called InvalidRegNoException. Similarly if marks
value is greater than 100 throw an exception called MarkOutOfBoundsException.
import java.util.Scanner;
class Student{
String name;
int register_no;
double[] marks = new double[3];
double total_marks;
double average;
Page 4
Name : T K Santhosh
Roll No : 22Z433
Date : 03/11/2023
this.name = name;
this.check_register_no(register_no);
this.register_no = register_no;
}
Page 5
Name : T K Santhosh
Roll No : 22Z433
Date : 03/11/2023
this.total_marks = total;
}
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Exception in thread "main" EXP_10.InvalidRegisterNoException:
Register number should be 6 digits.
at
EXP_10.Student.check_register_no(StudentManagement.java:49)
at EXP_10.Student.<init>(StudentManagement.java:33)
at
EXP_10.StudentManagement.main(StudentManagement.java:91)
Page 6
Name : T K Santhosh
Roll No : 22Z433
Date : 03/11/2023
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Enter mark 1 :
100
Enter mark 2 :
99
Enter mark 3 :
200
Exception in thread "main" EXP_10.MarkOutOfBoundException: Mark
shouldn't be greater than 100.
at EXP_10.Student.check_mark(StudentManagement.java:40)
at EXP_10.Student.get_marks(StudentManagement.java:59)
at
EXP_10.StudentManagement.main(StudentManagement.java:92)
santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Enter mark 1 :
99
Enter mark 2 :
89
Enter mark 3 :
69
Name : Santhosh
Register No : 123456
Total marks : 257.0
Average marks : 85.66666666666667
Page 7