Question: Need help with Java Exceptions The test driver accepts input for you to give numerator and denominator values. Once the Exceptions have been properly handled,

Need help with Java Exceptions

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.

Exceptions Test Class

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

}

}

zero demoniator exception class

package mypackages.types.exceptions;

/**

* Exception used with Fraction class.

* To be thrown if it is attempted to set denominator to 0.

* Inherits from Exception.

* @author Zachary Williams

*

*/

public class ZeroDenominatorException extends java.lang.Exception implements Serializable

{

/**

* ZeroDenominatorException constructor.

* Calls upon Exception constructor with message "Zero is an invalid denominator".

*/

public ZeroDenominatorException()

{

super("Zero Something");

}

}

Fraction class

package mypackages.types;

import mypackages.types.exceptions.ZeroDenominatorException;

/**

* Class representing a fraction, split into two parts, integer numerator and denominator.

* Fractions cannot have 0 as a denominator.

* @author Zachary Williams

*/

public class Fraction extends java.lang.Object

{

/**

* Denominator of Fraction.

*/

private int denominator;

/**

* Numerator of Fraction.

*/

private int numerator;

/**

* Fraction constructor, accepts numerator and denominator values.

* Ensures denominator is not 0 by calling upon setDenominator which throws ZeroDenominatorException,

* rethrows this Exceptions.

*

* @param numeratorIn Numerator of Fraction.

* @param denominatorIn Denominator of Fraction.

* @throws ZeroDenominatorException Rethrown Exception from setDenominator.

*/

public Fraction(int numeratorIn, int denominatorIn)

throws ZeroDenominatorException

{

setNumerator(numeratorIn);

setDenominator(denominatorIn);

}

/**

* Sets the numerator of the Fraction.

* @param numeratorIn Incoming value of the numerator.

*/

public void setNumerator(int numeratorIn)

{

numerator = numeratorIn;

}

/**

*

* @param denominatorIn Incoming value of the denominator.

* @throws ZeroDenominatorException Throws ZeroDenominator Exception if denominatorIn is 0.

*/

public void setDenominator(int denominatorIn)

throws ZeroDenominatorException

{

// if is zero then throw ZeroDenominatorException

// else then assign DenominatorIn to denominator

}

/**

* Returns the decimal value of this Fraction.

* @return Decimal value of the Fraction by dividing numerator by denominator.

*/

public double getDecimal()

{

double result = 0.0;

// numerator / denominator;

//promote explicitly

return result;

}

/**

* @param o2 Object

* @return Returns a boolean value

* Overrides equals in class java.lang.Object.

*/

public boolean equals(java.lang.Object o2)

{

boolean result = false;

// comparing two fraction numbers using a method

return result;

}

/**

* Creates and returns common representation of Fraction: numerator / denominator.

* Overrides toString in class java.lang.Object

* @return Fraction as a String.

*/

public java.lang.String toString()

{

String result = "Hello";

// numerator / denominator

return result;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!