Question
If anyone could show how to do the exception controlled loop, it would be much appreciated! Here is Fraction.java, including what I have added: public
If anyone could show how to do the exception controlled loop, it would be much appreciated!
Here is Fraction.java, including what I have added:
public class Fraction {
private int numerator; private int denominator;
/** * Constructor with parameter * * @param numerator numerator * @param denominator denominator */ Fraction(int numerator, int denominator) throws DenominatorIsZeroException { this.numerator = numerator; if(denominator == 0) { throw new DenominatorIsZeroException("Denominator is zero"); } this.denominator = denominator; }
/** * Get the numerator. * * @return numerator */ public int getNumerator() { return numerator; }
/** * Set the numerator. * * @param numerator numerator */ public void setNumerator(int numerator) { this.numerator = numerator; }
/** * Get the denominator. * * @return the denominator */ public int getDenominator() { return denominator; }
/** * Set the denominator. * * @param denominator denominator * @throws DenominatorIsZeroException */ public void setDenominator(int denominator) throws DenominatorIsZeroException { if(denominator == 0) { throw new DenominatorIsZeroException("Denominator is Zero"); } this.denominator = denominator; }
/** * Convert the fraction to a mixed number. * * @return The mixed number. */ public String toMixedNumber() {
String ret = ""; int remainder = numerator % denominator;
ret += numerator / denominator; ret += " "; if (remainder != 0) { ret += numerator % denominator; ret += "/"; ret += denominator; }
return ret; }
/** * Override of {@link java.lang.Object#toString} method. */ @Override public String toString() { String ret = Integer.toString(numerator) + "/" + denominator; return ret; } }
Creating a Custom Exception Class. Topics: Custom Exceptions, try catch finally, throw, throws and exception ontrolled loops. 1. Write DenominatorIsZeroException class by adding the appropriate constructors. 2. Modify the provided Fraction.java so that it throws the DenominatorIsZeroException if the denominator is set to zero. You need to see which methods can potentially throw this exception. 3. Create the tester class CustomException Tester.java that tests the new Fraction class, attempts to set the denominator to zero, and catches the DenominatorIsZeroException exception. Try invoking all of the methods of the fractions class. Here is Fraction.java Challenge: 5 Points Extra Credit Write an exeeption controlled loop that keeps prompting the user to reenter in case the user enters a zero denominator and an exception is thrown. Otherwise, display the mixed number form and exitStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started