0% found this document useful (0 votes)
37 views5 pages

Lab Programs 9&10

Java program

Uploaded by

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

Lab Programs 9&10

Java program

Uploaded by

Madhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Lab Progran_9 Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero using try, catch, throw and finally. AIN: To develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero using try, catch, throw and finally. import java.util. Scanner; class DivisionByZeroException extends Exception ‘ public DivisionByZeroException(String message) : super(message); » public class CustomExceptionWithUserInput ‘ public static void main(String{] args) t try { int numerator, denominator; Scanner scanner = new Scanner(System.in); // Input from the user System.out.print("Enter the numerator: "); numerator = scanner.nextInt(); System.out. print("Enter the denominator: denominator = scanner.nextInt(); scanner. close(); if (denominator 5 0) ‘throw new DivisionByZeroException("Division by zero is not allowed.") + int result = numerator / denominatoi System.out.println("Result of division: + result); catch (DivisionByZeroException e) < System.err.printn("Custom Exception Caught: e-getMessage()); : catch (ArithmeticException e) Systemerr.printIn("Arithmetic Exception Caught: e-getMessage(}); finally { System. out. printIn("Finally block executed." , } output: Enter the numerator: 10 Enter the denominator: 2 Result of division: 5 Finally block executed. Enter the numerator: 20 Enter the denominator: 0 Finally block executed. Custom Exception Caught: Division by zero is not allowed. RESULT: Thus, the program to develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero using try, catch, throw and finally has been executed successfully and the output was verified. Lab Program 10 Develop a JAVA program to create a package naned mypack and import & implement it in a suitable class. AIM: To develop a JAVA program to create a package named mypack and import & implement it in a suitable class. To create a Java package named mypack and import it in a suitable class, you can follow these steps in Eclipse: 1, Open Eclipse and create @ new Java project: © Go to File > New > Java Project. © Enter the project name, e.g., "MyPackageDemo," and click Finish, 2. Create a package named mypack: © Right-click on the "src" folder within your project. o Choose New > Package. © Enter "mypack" as the package name and click Finish. 3. Create a class within the mypack package: e Right-click on the mypack package. © Choose New > Class. © Enter a class name, e.g., “Circle” and select the option to include the public static void main(String[] args) method. © Click Finish, 4.Now, you can implement the code to calculate the area of a circle in the Circle class: mypack/Circle,java Package mypack; // Inport the mypack package import java.util.Scanner; public class Circle { public static void main(Stringl] args) { Scanner scanner = new Scanner(System..in); System.out.print("Enter the radius of the circle: " double radius = scannersnextDouble(); double area = calculateCircleArea(radius); Systen.out.println("The area of the circle is: " + area); scanner. close(); + public static double calculateCircleArea(double radius) { return Math.PI * radius * radius; + } To use the mypack package in another class and calculate the area of a circle using the import statement, you can follow these steps: 1. Create a new Java class in your project. Let's call it MainClass in a different package (e.g., myapp). 2, Import the mypack package in your MainClass and use it to calculate the area of a circle. 3. To run the program, right-click on the MainClass and select "Run As" > "Java Application." nyapp/MainClass. java package myapp; // This is the package for MainClass import nypack.Circl // Inport the Circle class from mypack public class MainClass { public static void main(String{] args) { Circle circle = new Circle(); // Create an instance of the Cirele class double radius = 5.0; // You can set your own radius here double area = circle.calculateCircleArea(radius); // Use the method from the Circle class System.out.println("The area of the circle is: " + area); output: ‘The area of the circle is: 78.53981633974483, RESULT: Thus, the program to create a package named mypack and import & implement it in a suitable class has been executed successfully and the output was verified.

You might also like