1

I'm working on a program where the user inputs two different fractions and the computer then runs them through a Fraction method to test if they're equal or not. An additional step in the assignment is to throw an IllegalArgumentException to identify if one of the denominators is 0 and in turn restart the program. The Fraction and testFraction classes are as follows:

Fraction class:

class Fraction {

    private int numerator;
    private int denominator;

    Fraction() {
        numerator = 0;
        denominator = 1;

    }

    Fraction (int num, int den) {
        numerator = num;
        denominator = den;
        if (denominator == 0){
            throw new IllegalArgumentException (
            "Denominator cannot be zero");
        }
    }

    public void setNum(int num){
        numerator = num;
    }

    public void setDen(int den){
        denominator = den;

        }

    public int getNum(){
        return numerator;
    }

    public int getDen(){
        return denominator;
    }

    public String toString() {
        return numerator + "/" + denominator;
        }

    public boolean equals(Fraction other){
    if (numerator * other.getDen() == denominator * other.getNum()){
            return true;
         } 
        else {
            return false;
        }
    }
}

testFraction Class:

import java.util.Scanner;

public class testFraction {

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in); 
    int a = 1;
    int b = 1;
    int c = 1;
    int d = 1;

    Fraction f1 = new Fraction(a, b);
    Fraction f2 = new Fraction(c, d);


    System.out.println("Enter the numerator of the first fraction: ");
    f1.setNum(keyboard.nextInt());

    System.out.println("Enter the denominator of the first fraction: ");
    f1.setDen(keyboard.nextInt());

    try{
        int fraction = a/b; 
    }
    catch (IllegalArgumentException e){
        System.out.println(e.getMessage);
        main(args);
    }

    System.out.println("\nEnter the numerator of the second fraction: ");
    f2.setNum(keyboard.nextInt());

    System.out.println("Enter the denominator of the second fraction: ");
    f2.setDen(keyboard.nextInt());

    System.out.println("\nFraction 1: " + f1);
    System.out.println("Fraction 2: " + f2);

    System.out.println("\nAre fractions equal? " + f1.equals(f2));

    System.out.println(("\nWould you like to run the counter with a different integer? \n(1 for YES - 0 for NO): "));
    int answer = keyboard.nextInt();

    if (answer == 1){
        System.out.println("\nLet's Rock 'n Roll! ");
        main(args);
    }

    else if (answer == 0){
        System.out.println("\nLater Dude! ");
        System.exit(0);
    }       
}
}

I feel like I'm incorrectly working in the try/catch. Thanks for your help.

  • 1
    divide by `zero` is `ArithmeticException` – thegauravmahawar Oct 22 '15 at 12:21
  • As a side note you should avoid restarting the program by calling main again. This will just start another instance of main and when this finished it will progress where the old calling programm stopped. – Aron_dc Oct 22 '15 at 12:23
  • Additionally it will increase the stack size. Type the wrong input often enough and you get a stackoverflow. Use a loop instead. Also: the constructor throws a exception, but the setter doesn't???? – fabian Oct 22 '15 at 12:27

1 Answers1

0

You're throwing an IllegalArgumentException when initializing your Fraction, yet you try to catch it when you do an actual division in testFraction. Your catch will never be reached, since the exception at initialization will stop the code from reaching the incorrect division. You should instead put your try/catch around the code creating Fractions.

Moreover, you initialize your Fractions with mock values : 1. This isn't really useful, and pose a problem : your constructor will never throw its IllegalArgumentException, since it is called with correct values. You should raise the IllegalArgumentException in your setDenominator instead, and call it from your constructor.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Awesome this got the program to throw the exception thank you! Is there a way I can call the method over again after the exception is thrown? Or run main over again? – Nick Petrantoni Oct 22 '15 at 12:56
  • @NickPetrantoni currently you're using recursion (main calls itself), which is generally useful when you can split a problem into smaller parts where each step is based on the result of the previous (or next) one. As others have mentionned, it comes at a cost, and here you really do not need recursion. So the alternative for repeating a task is using iteration : you should use a while loop that will handle input/output , Fraction creation and usage as long as the user do not ask to stop. – Aaron Oct 22 '15 at 13:15
  • @NickPetrantoni If you do so there should be nothing after the catch part so if the error is raised, the loop will carry on its next iteration. You should put the code using a correct Fraction inside the try block – Aaron Oct 22 '15 at 13:16
  • I'm getting lost trying to work out the while loop. Are you saying that in the testFraction class, I should say "while denominator = 0, throw the exception and run the method again?" – Nick Petrantoni Oct 22 '15 at 14:18
  • @NickPetrantoni no, you should have something like this : `boolean carryOn = true; while (carryOn) { /* ask for input, then try to create and use a Fraction ; if an error is catched, signal it to the user */ /* then ask the user if he wants to carry on ; if not, set carryOn = false*/ }` I'm affraid comments aren't great to write code, hope you'll understand ! – Aaron Oct 22 '15 at 14:29