Question: JAVA: In this lab you will be creating a new Exception, writing a class that uses this Exceptions, and add Exception handling code to a
JAVA:
In this lab you will be creating a new Exception, writing a class that uses this Exceptions, and add Exception handling code to a provided test driver.
Concepts Covered
Creating new Exceptions
Throwing Exceptions
Handling Checked and Unchecked Exceptions.
In your Lab4Packages project, add a new package called mypackages.types.exceptions. This sub-package will hold Exceptions that are used with your mypckages.types classes. Within this package create the ZeroDenominatorException class. This will be a new Checked Exception which inherits from the Exception class. The documentation for this class can be found here attached as an image below:


Within your mypackage.types package create and write a new class called Fraction. The setDenominator method must check if it is being attempted to set the denominator to 0, if so, it throws a new ZeroDenominatorException. This is a Checked Exception, therefore it must adhere to the catch or declare rule. Declare it using the throws keyword in the method header. This method should be called upon by the constructor, which should also declare it. It will be caught in the test driver. Documentation for Fraction can be found here




Finally, create a new project titled Lab7Exceptions. Place the Lab7ExceptionsTest.java file in the project. This is a test driver that does some simple tests on your Fraction class, however does not contain any Exception handling. You must add the handling code in where prompted by the comments in the file. To begin with, this file will not compile since it calls upon the Fraction constructor which throws a Checked Exception.
Lab7ExceptionsTest.java
-----------
import java.util.InputMismatchException; import java.util.Scanner;
import mypackages.types.Fraction; import mypackages.types.exceptions.ZeroDenominatorException;
/** * Test class for Fraction and handling Exceptions. */ public class Lab7ExceptionsTest { /** * Scanner used for input in getUserFraction(). */ private static Scanner input = new Scanner(System.in); /** * Main method for Fraction and Exception testing. * @param args Not used. */ public static void main(String[] args) { Fraction fraction1 = null; Fraction fraction2 = null; //This will throw a NullPointerException. Catch it and output "fraction1 is currently null" System.out.printf("Decimal value of fraction1 is %f ", fraction1.getDecimal());
fraction1 = getUserFraction(); fraction2 = getUserFraction(); System.out.printf("fraction1: %s fraction2: %s, ", fraction1, fraction2); System.out.printf("They are %s", fraction1.equals(fraction2) ? "Equal " : "Not Equal "); //This will throw an ArithmeticException if the whole part of fraction2 is 0. //Catch the ArithmeticException and output "Division by zero attempted" System.out.print("Dividing the whole parts of fraction1 and fraction2: "); System.out.printf("%d / %d = %d ", (int) fraction1.getDecimal(), (int) fraction2.getDecimal(), (int) fraction1.getDecimal() / (int) fraction2.getDecimal());
input.close(); } /** * Prompts user for numerator and denominator for creation of a new Fraction object. Ensures int input * and non-zero value for denominator by catching appropriate Exceptions. Reprompts user until valid * input is given. Returns reference of the new Fraction. * @return New Fraction created with user specified numerator and denominator. */ private static Fraction getUserFraction() { int num1, denom1; System.out.println(" ***Enter integer values for Fraction's numerator and denominator:"); //input.nextInt() will throw an InputMismatchException if user enters a non-int String. //Call to Fraction constructor throws ZeroDenominatorException which is a Checked Exception. //This means it will not compile if not handled. Create a loop that catches these Exceptions //and will repeat until the return statement is reached successfully. When catching //InputMismatchException output "Must enter integer values, try again " and clear the //input stream with input.nextLine(). When catching ZeroDenominatorException output //"Attempted to create Fraction with 0 for denominator, try again ". See example output //for expected behavior.
System.out.print("Numerator: "); num1 = input.nextInt(); System.out.print("Denominator: "); denom1 = input.nextInt(); return new Fraction(num1, denom1);
} }
----------
The test driver accepts input for you to give numerator and denominator values. Once the Exceptions have been properly handled, the prompt for input should repeat while invalid values (non-int Strings or 0 denominator) are entered. Be sure to test multiple bad inputs as well as correct inputs. See example output below.
EXAMPLE OUTPUTS (Bold lettering used for comments, not actually in output)
---Example 1---
fraction1 is currently null Calling upon method of a null reference causes NullPointerException, catch and report
***Enter integer values for Fraction's numerator and denominator: Numerator: nine This will cause an InputMismatchException, catch, report and repeat input Must enter integer values, try again
Numerator: 9 Denominator: four This will cause an InputMismatchException, catch, report and repeat input Must enter integer values, try again
Numerator: 9 Denominator: 4
***Enter integer values for Fraction's numerator and denominator: Numerator: 3 Denominator: 0 Setting 0 for denominator will cause ZeroDenominatorException, catch, report and repeat input Attempted to create Fraction with 0 for denominator, try again
Numerator: 3 Denominator: 2 fraction1: 9 / 4 fraction2: 3 / 2, They are Not Equal Dividing the whole parts of fraction1 and fraction2: 2 / 1 = 2
---Example 2---
fraction1 is currently null
***Enter integer values for Fraction's numerator and denominator: Numerator: 13 Denominator: 6
***Enter integer values for Fraction's numerator and denominator: Numerator: 0 Denominator: 5 fraction1: 13 / 6 fraction2: 0 / 5, They are Not Equal Dividing the whole parts of fraction1 and fraction2: Division by zero attempted fraction2 has no whole part, dividing by zero causes ArithmeticException, catch and report
OVERVIEW PACKAGE CLASS USE TREE DEPRECATED INDEX HELP PREV CLASS NEXT CLASS FRAMES NO FRAMES ALL CLASSES SUMMARY: NESTED | FIELD CONSTR | METHOD DETAIL: FIELD I CONSTR | METHOD mypackages.types.exceptions Class ZeroDenominatorException java.lang.Object java.lang.Throwable java.lang.Exception mypackages.types.exceptions.ZeroDenominatorException All Implemented Interfaces java.io.Serializable public class ZeroDenominatorException extends java.lang.Exception Exception used with Fraction class. To be thrown if it is attempted to set denominator to o. Inherits from Exception. See Also Serialized Form Constructor Summary Constructors Constructor and Description ZeroDenominatorException() ZeroDenominatorException constructor Method Summary Methods inherited from class java.lang.Throwable addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString 1 of 2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
