Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ICS 201 Assignment 2 Q1. Write a Java program that: 1. Reads two numbers from the user. 2. Write a try-throw-catch bloc to calculate the division of the two entered numbers. An exception should be handled if the second entered number is zero. Use the Exception class. Solution: import java.util.*; public class ex1{ public static void main (String[] args) { try{ Scanner input =new Scanner(System.in); System.out.println("enter n1:"); int n1=input.nextInt(); System.out.println("enter n2:"); int n2=input.nextInt(); if(n2==0)throw new Exception("Zero is not allowed as a delimiter"); int result=n1/n2; System.out.println(result);} catch(Exception e){ System.out.print(e.getMessage()); } }} Q2. 2. Create a class that contains: A method named divide that accepts two variable as arguments and returns their division. The method should throw an Exception exception that prints this message “Zero is not allowed as a delimiter”. The main method to prompt the user to enter two numbers and then to call the method divide (Insert a try-catch block to handle the Exception exception). Solution: import java.util.*; public class ex2{ public static int divide(int n1,int n2)throws Exception{ (1) if(n2==0)throw new Exception("Zero is not allowed as a delimiter"); int div=n1/n2; return div; } public static void main (String[] args) { Scanner input =new Scanner(System.in); System.out.println("enter n1:"); int n1=input.nextInt(); System.out.println("enter n2:"); int n2=input.nextInt(); try{ System.out.println(divide(n1,n2));} catch(Exception e){ System.out.println(e.getMessage()); } } } OR import java.util.*; public class ex4{ public int divide(int n1,int n2)throws Exception{ if(n2==0)throw new Exception("Zero is not allowed as a delimiter"); int div=n1/n2; return div; } public static void main (String[] args) { Scanner input =new Scanner(System.in); System.out.println("enter n1:"); int n1=input.nextInt(); System.out.println("enter n2:"); (2) int n2=input.nextInt(); try{ ex3 obj=new ex3(); System.out.println( obj.divide(n1,n2));} catch(Exception e){ System.out.println(e.getMessage()); } } } Q3. 1.Create an exception class named ArithmeticException extended from the class Exception. This class should contain a parameterless constructor to call the Exception class constructor with the message “Zero is not allowed as a delimiter”. 2. Create a class that contains: A method named divide that accepts two variable as arguments and returns their division. The method should throw the ArithmeticException exception defined above. The main method to prompt the user to enter two numbers and then to call the method divide (Insert a try-catch block to handle the ArithmeticException exception). Solution: class ArithmeticException extends Exception { public ArithmeticException () {super("Zero is not allowed as a delimiter"); } public ArithmeticException (String message) { super(message); } } (3) import java.util.*; public class ex3{ public static int divide(int n1,int n2)throws ArithmeticException{ if(n2==0)throw new ArithmeticException(); int div=n1/n2; return div; } public static void main (String[] args) { Scanner input =new Scanner(System.in); System.out.println("enter n1:"); int n1=input.nextInt(); System.out.println("enter n2:"); int n2=input.nextInt(); try{ System.out.println(divide(n1,n2));} catch(ArithmeticException e){ System.out.println(e.getMessage()); } } } Best Wishes (4)