The Activity2 class does not do any exception checking. Modify it so that you ha
ID: 2247629 • Letter: T
Question
The Activity2 class does not do any exception checking. Modify it so that you handle any exceptions that could occur. Note that you should not change Fraction.java
public class Activity2 {
public static void main(String[] args) {
Fraction n = new Fraction(8,3);
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the numerator: ");
n.setNumerator(scan.nextInt());
System.out.print ("Enter the denominator: ");
n.setDenominator(scan.nextInt());
System.out.println ("The fraction " + n.getNumerator() + "/" +
n.getDenominator() + " is equal to " + n.toMixedNumber());
scan.close();
}
}
package activity2;
/**
* Fraction class, including the ability to represent improper fractions
* as mixed numbers.
*
* Activity - add exception handling.
*
*
*/
public class Fraction {
private int numerator;
private int denominator;
/**
* Class constructor - default to 1
*/
Fraction() {
numerator = 1;
denominator= 1;
}
/**
* Class constructor specifying values for numerator & denominator
*
* @param numerator numerator of fraction
* @param denominator denominator of fraction
*/
Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator= denominator;
}
/**
* @return numerator the numerator of the fraction
*/
public int getNumerator() {
return numerator;
}
/**
* @param numerator the numerator of the fraction
*/
public void setNumerator(int numerator) {
this.numerator = numerator;
}
/**
* @return denominator the denominator of the fraction
*/
public int getDenominator() {
return denominator;
}
/**
* @param denominator the denominator of the fraction
*/
public void setDenominator(int denominator) {
this.denominator = denominator;
}
/**
* Generate a string representing the fraction in mixed number format.
*
* @return string representation of the fraction as a 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;
}
/**
* @return string representation of the fraction as a mixed number
*/
public String toString() {
String ret = Integer.toString(numerator) + "/" + denominator;
return ret;
}
}
Explanation / Answer
package activity2;
import java.util.Scanner;
public class Activity2 {
public static void main(String[] args) {
Fraction n = new Fraction(8,3);
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the numerator: ");
n.setNumerator(scan.nextInt());
System.out.print ("Enter the denominator: ");
int den;
den = scan.nextInt(); // storing the input number as denominator
try // try-catch block to catch divide by 0 exception
{
n.setDenominator(den);
System.out.println ("The fraction " + n.getNumerator() + "/" +
n.getDenominator() + " is equal to " + n.toMixedNumber());
}
catch(ArithmeticException ex)
{
System.out.println("Exception: Denominator can not be zero");
}
scan.close();
}
}
Modified File Activity2.javapackage activity2;
import java.util.Scanner;
public class Activity2 {
public static void main(String[] args) {
Fraction n = new Fraction(8,3);
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the numerator: ");
n.setNumerator(scan.nextInt());
System.out.print ("Enter the denominator: ");
int den;
den = scan.nextInt(); // storing the input number as denominator
try // try-catch block to catch divide by 0 exception
{
n.setDenominator(den);
System.out.println ("The fraction " + n.getNumerator() + "/" +
n.getDenominator() + " is equal to " + n.toMixedNumber());
}
catch(ArithmeticException ex)
{
System.out.println("Exception: Denominator can not be zero");
}
scan.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.