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
ICS201: Introduction to Computer Science Computer Science and Software Engineering Department University of Hail Prepared by: Ebtisam Hamed Lab 07 : Exception Handling Objectives LAB 07 Be able to write code that handles an exception Be able to write code that throws an exception Be able to write a custom exception class Exception that are thrown in methods. Exercise 1 1. Create a new Java file named exercise1 and add it to the lab7 project. 2. Write a Java program to: a. Read a number from the keyboard. b. Write a try-throw-catch bloc to calculate the square root of the entered number. An exception should be handled if the entered number is negative. Use the Exception class. (Hint: use Math.sqrt(d) to compute the square root of d ) Exercise 2 1. Create a new Java file named exercise2. 2. Create an exception class named AgeOutOfRangeException extended from the class Exception. This class should contain a constructor with no parameter to call the Exception class constructor with the message “You are older than the requested age (25 years)”. 3. Create an exception class named LowGpaException extended from the class Exception. This class should contain a constructor with no parameter to call the Exception class constructor with the message “Your GPA is not sufficient to apply for this job (2.5)”. Exercise 3 1. Create a new Java file named exercise3 and add it to the lab9 project. 2. Create an exception class named NegativeNumberException extended from the class Exception. This class should contain a parameterless constructor to call the Exception class constructor with the message “You should enter a positive number”. 3. Create a class named exercise3 that contains: A method named M1 that accepts a double variable as argument and returns its square root. The method should throw the NegativeNumberException exception defined above. The main method to prompt the user to enter a number and then to call the method M1 (insert a try-catch block to handle the NegativeNumberException exception). Solutio Exercise 1 : import java.util.* ; public class SqureTest { public static void main(String[] args) { Scanner key = new Scanner(System.in); //Write a try-throw-catch bloc to calculate the square root of the entered number. // An exception should be handled if the entered number is negative System.out.println("Please enter a number"); double n = key.nextInt(); try{ if (n < 0 )throw new Exception(" I cant calculate squre root for negative numbers"); double result = Math.sqrt(n) ; System.out.println("The square Root of " + n + " = " + result); } catch(Exception e ){ System.out.println(e.getMessage()); System.exit(0); } } } Exercise 2: Part 2 : public class AgeOutOfRangeException extends Exception { public AgeOutOfRangeException() {super("You are older than the requested age (25 years)") } public AgeOutOfRangeException(String message) { super(message); } } Part 3 public class LowGpaException extends Exception { public LowGpaException () {super("Your GPA is not sufficient to apply for this job (2.5)") } public LowGpaException (String message) { super(message); } } Exercise 3: import java.util.*; public class exercise3{ public static void main (String[] args) { Scanner key = new Scanner(System.in); try{ System.out.println("please enter a number:"); double n = key.nextInt(); System.out.println(M1(n)); } catch(NegativeNumberException e ){ System.out.println(e.getMessage()); } } public static double M1(double x)throws NegativeNumberException { if(x < 0) throw new NegativeNumberException(); return Math.sqrt(x);} } class NegativeNumberException } extends Exception { public NegativeNumberException } () {super("You should enter a positive number"); public NegativeNumberException super(message); } (String message) {