0% found this document useful (0 votes)
21 views7 pages

EXP 10 Report 22z433 Final

The document describes 3 Java programs that handle exceptions: 1. A program that reads an integer from the user and prints its reciprocal, handling the exception when the user enters 0. 2. A program that demonstrates the flow of an exception through multiple method calls. 3. A student mark management program that uses custom exceptions for invalid register numbers greater than 6 digits and marks greater than 100. It gets student data, calculates totals and averages, and displays results.

Uploaded by

T.K. Santhosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

EXP 10 Report 22z433 Final

The document describes 3 Java programs that handle exceptions: 1. A program that reads an integer from the user and prints its reciprocal, handling the exception when the user enters 0. 2. A program that demonstrates the flow of an exception through multiple method calls. 3. A student mark management program that uses custom exceptions for invalid register numbers greater than 6 digits and marks greater than 100. It gets student data, calculates totals and averages, and displays results.

Uploaded by

T.K. Santhosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Name : T K Santhosh

Roll No : 22Z433
Date : 03/11/2023

Experiment 10

Object Oriented Programming Laboratory

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.

// Program to handle the exception.


package EXP_10;

import java.util.Scanner;

public class HandlingExceptions {

static double reciprocal(int value) throws Exception{


if(value == 0){
throw new Exception();
}
else{
return (1.0 / value);
}
}

public static void main(String[] args){


// Create an instance of the scanner.

int value;

Scanner scan = new Scanner(System.in);


System.out.println("Enter an integer : ");
value = scan.nextInt();

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.

Aim : To write a java program to demonstrate the flow of exception.

// Program to play with Exceptions.

package EXP_10;

public class HandlingExceptionsTwo {

Page 2
Name : T K Santhosh
Roll No : 22Z433
Date : 03/11/2023

static void method_p() throws Exception{


System.out.println("In method `p` calling method `q`,");
method_q();
}

static void method_q() throws Exception{


System.out.println("In method `q` calling method `r`,");
method_r();
}

static void method_r() throws Exception{


System.out.println("In method `r` calling method `s`,");
method_s();
}

static void method_s() throws Exception{


int[] array = new int[6];
System.out.println(array[10]);
}

public static void main(String[] args){


try{
method_p();
}
catch (Exception e){
System.out.println("Exception: " + e);
}
}

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.

Aim : To create a java program to create a student’s mark management with


user defined exceptions `InvalidRegisterNoException` and
`MarkOutOfBoundException`.
package EXP_10;

import java.util.Scanner;

// Exception for invalid register number exception.


class InvalidRegisterNoException extends Exception{
InvalidRegisterNoException(){
super("Register number should be 6 digits.");
}
}

// Exception for mark out of bound.


class MarkOutOfBoundException extends Exception{
MarkOutOfBoundException(){
super("Mark shouldn't be greater than 100.");
}
}

class Student{

String name;
int register_no;
double[] marks = new double[3];
double total_marks;
double average;

Scanner scan = new Scanner(System.in);

// Constructor for the class student.


Student(String name, int register_no) throws Exception{

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;
}

// Check the mark


void check_mark(double mark) throws Exception{
if(mark > 100.0){
throw new MarkOutOfBoundException();
}
}

// Check the register number


void check_register_no(int register_no) throws Exception{
String reg_no = Integer.toString(register_no);

if(reg_no.length() > 6){


throw new InvalidRegisterNoException();
}
}

// Method to get marks of the student.


void get_marks() throws Exception{
double mark;
for(int i = 0; i < 3; i++){
System.out.println("Enter mark " + (i + 1) + " : ");
mark = this.scan.nextDouble();
check_mark(mark);
this.marks[i] = mark;
}
}

// Method to calculate the total marks of the student.


void calculate_total(){
double total = 0;

for(int i = 0; i < 3; i++){


total += this.marks[i];
}

Page 5
Name : T K Santhosh
Roll No : 22Z433
Date : 03/11/2023
this.total_marks = total;
}

// Method to calculate the average of the student's mark.


void calculate_average(){
this.average = this.total_marks / 3;
}

// Method to display information of the student.


void display(){
System.out.println("Name : " + this.name);
System.out.println("Register No : " + this.register_no);
System.out.println("Total marks : " + this.total_marks);
System.out.println("Average marks : " + this.average);
}
}

public class StudentManagement {


public static void main(String[] args) throws Exception{
Student s = new Student("Santhosh", 123456);
s.get_marks();
s.calculate_total();
s.calculate_average();
s.display();
}
}

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

You might also like