Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I NEED HELP WITH THIS most : toString() function returns a string consisting of

ID: 3537775 • Letter: I

Question


I NEED HELP WITH THIS most : toString() function returns a string consisting of the numerator only if the numerator is 0 or denominator is 1

If an exception is thrown while calculating fractions the main class has to catch the exception, print an error message, and continue with unaffected calculations.




Write a program that prompts the user to enter numerators and denominators of 2 fractions and outputs the sum, the difference, the multiplication, and the division of these fractions.

The main class stub is provided.

The input of numbers has to be handled by a dedicated function. The function has to print a prompt and ask user to enter a numerator and a denominator. If the user enters a string that is not an integer number the function has to catch the appropriate exception, print an error message, skip invalid string, and ask the user to enter the data again.

The program has to use a modified Fraction class from PE8. The Fraction class has to be modified in such a way that

%uF0B7 all functions that can be static are static

%uF0B7 it throws an exception if an attempt is made to set the denominator to 0

%uF0B7 it does not print any error messages %uF0B7 gcd() function returns correct result if the numerator is 0

%uF0B7 toString() function returns a string consisting of the numerator only if the numerator is 0 or denominator is 1

If an exception is thrown while calculating fractions the main class has to catch the exception, print an error message, and continue with unaffected calculations.








import java.io.*;

import java.util.*;

class Fraction {

private int a;

private int b;

            public Fraction parseint;

            public Fraction parsestring;

public Fraction() {

a = 1;

b = 1;

}

public Fraction(int a, int b) {

this.a = a;

this.b = b;

}

public int getNumerator() {

return a;

}

public int getDenominator() {

return b;

}

public void setNumerator(int a) {

this.a = a;

}

public void setDenominator(int b) {

this.b = b;

}

public String toString() {

return a + "/" + b;

}

public int gcd(int a, int b) {

          //ToDo implement Euclide algorithm

if(b == 0)

{

return a;

}

else

{

           return gcd(b, a % b);

}

}

public void lowestTerms() {

int g = gcd(a, b);

a = a / g;

b = b / g;

}

public static Fraction add(Fraction first, Fraction second) {

Fraction result = new Fraction();

result.setNumerator(first.getNumerator() * second.getDenominator()

+ first.getDenominator() * second.getNumerator());

result.setDenominator(first.getDenominator() * second.getDenominator());

result.lowestTerms();

return result;

}

public static Fraction sub(Fraction first, Fraction second) {

Fraction result = new Fraction();

result.setNumerator(first.getNumerator() * second.getDenominator()

- first.getDenominator() * second.getNumerator());

result.setDenominator(first.getDenominator() * second.getDenominator());

result.lowestTerms();

return result;

}

public static Fraction mult(Fraction first, Fraction second) {

Fraction result = new Fraction();

result.setNumerator(first.getNumerator() * second.getNumerator());

result.setDenominator(first.getDenominator() * second.getDenominator());

result.lowestTerms();

return result;

}

public static Fraction div(Fraction first, Fraction second) {

Fraction result = new Fraction();

result.setNumerator(first.getNumerator() * second.getDenominator());

result.setDenominator(first.getDenominator() * second.getNumerator());

result.lowestTerms();

return result;

}

public static boolean lessThan(Fraction first,Fraction second)

{

if(first.getNumerator()*second.getDenominator()<first.getDenominator()*second.getNumerator())

return true;

else

return false;

}

}

////

import java.io.*;

import java.util.*;

public class TestFraction {

            static Scanner console = new Scanner(System.in);

            private static int getNumber() throws InputMismatchException {

                        // try {

                        return console.nextInt();

                        // }

                        // catch (InputMismatchException e)

           

            // {

                        // System.out.println("Exception inside getNumber"+e.toString());

                        // throw new InputMismatchException();

                        // }

            }

            private static Fraction getFraction(String text) {

                        int numerator;

                        int denominator;

                        while (true) {

                                    System.out.println(" Please enter the Numerator and Denominator");

                                    try {

                                                numerator = getNumber();

                                                denominator = getNumber();

                                                if( denominator == 0)

                                                            throw new ArithmeticException();

                                    } catch (InputMismatchException e) {

                                                System.out.println(e.toString());

                                                console.nextLine();

                                                continue;

                                    } catch (ArithmeticException e){

                                                System.out.println("Cannot Divide by zero");

                                   

            System.out.println(e.toString());

                                               

                                                console.nextLine();

                                                continue;

                                    }

                                    break;

                                    // prompt to enter a numerator and a denominator

                                    // input the numerator and the denominator using getNumber()

                                    // return new Fraction if OK

                                    // otherwise print an error message

                        }

                        Fraction result = new Fraction(numerator, denominator);

                        return (result);

            }

            public static void main(String[] args) {

                        Fraction fr1 = getFraction("1st");

                        Fraction fr2 = getFraction("2nd");

                        Fraction res = new Fraction();

                        // define other variables including res and fr2

           

                       

           

           

                        res = Fraction.add(fr1, fr2);

                        System.out.println(fr1 + "+" + fr2 + " = " + res);

                        res = Fraction.sub(fr1, fr2);

                        System.out.println(fr1 + "-" + fr2 + " = " + res);

                        res = Fraction.mult(fr1, fr2);

                        System.out.println(fr1 + "*" + fr2 + " = " + res);

                        res = Fraction.div(fr1, fr2);

                        System.out.println(fr1 + "/" + fr2 + " = " + res);

                        boolean r;

                        r = Fraction.lessThan(fr1, fr2);

                        System.out.println(fr1 + " less than " + fr2 + " = " + r);

                        // test subtract, multiply, divide, lessThan methods

                        // each test has to print a description, a result,

                        // and, possibly, a error message if the calculation fails

            }

}

Explanation / Answer

The toString() method should be channged to the following


public String toString() {

        if (a == 0 && b == 1)
            return "" + a;
        else if (b == 0) {
            throw new RuntimeException("Invalid denominator");
        } else

            return a + "/" + b;

    }