Brandon Bischoff
Brandon Bischoff

Reputation: 81

Fraction Program - Java

I am currently working on a program that allows the user to select one of four methods, and test them on a list of 5 predefined fractions. The first method converts the fraction to a string, the second reduces the fraction, the third outputs the fraction as a mixed number, and the fourth represents the fraction in decimal form.

I have everything working with a couple minor flaws:

  1. If the user selects the second method before the first method, the first method will print out in reduced form rather than its original form.
  2. When a mixed number is negative it will print a "-" sign before each number in the mixed number (Example: -5 5/6 prints as -5 -5/-6)

Below is my code:

public class TestFractionBrandonBischoff
{

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    // Create fraction array
    Fraction [] fractionArray;
    fractionArray = new Fraction[5];

    for (int i=0; i<fractionArray.length; i++)
        fractionArray[i] = new Fraction();

    fractionArray[0].numerator = -125;
    fractionArray[0].denominator = 30;
    fractionArray[1].numerator = 100;
    fractionArray[1].denominator = -4;
    fractionArray[2].numerator = 23;
    fractionArray[2].denominator = 50;
    fractionArray[3].numerator = 60;
    fractionArray[3].denominator = 40;
    fractionArray[4].numerator = 30;
    fractionArray[4].denominator = 390;

    //Prompt user to select a method to test
    System.out.println("Select which method you would like to perform: ");
    System.out.println("1. Test the toString() method");
    System.out.println("2. Test the reduce() method");
    System.out.println("3. Test the toMixed() method");
    System.out.println("4. Test the getDecimal() method");
    System.out.println("5. Quit");

    Scanner method;
    method = new Scanner(System.in);
    int methodChoice;
    methodChoice = method.nextInt();

    while(methodChoice != 5){
    //Use user input to call specific method
    if (methodChoice == 1){
        System.out.println(fractionArray[0].toString());
        System.out.println(fractionArray[1].toString());
        System.out.println(fractionArray[2].toString());
        System.out.println(fractionArray[3].toString());
        System.out.println(fractionArray[4].toString());
        }
    else if(methodChoice == 2){
        fractionArray[0].reduce();
        fractionArray[1].reduce();
        fractionArray[2].reduce();
        fractionArray[3].reduce();
        fractionArray[4].reduce();

        System.out.println(fractionArray[0].toString());
        System.out.println(fractionArray[1].toString());
        System.out.println(fractionArray[2].toString());
        System.out.println(fractionArray[3].toString());
        System.out.println(fractionArray[4].toString());
    }
    else if (methodChoice == 3){
        System.out.println(fractionArray[0].toMixed());
        System.out.println(fractionArray[1].toMixed());
        System.out.println(fractionArray[2].toMixed());
        System.out.println(fractionArray[3].toMixed());
        System.out.println(fractionArray[4].toMixed());
    }
    else if (methodChoice == 4){
        System.out.println(fractionArray[0].getDecimal());
        System.out.println(fractionArray[1].getDecimal());
        System.out.println(fractionArray[2].getDecimal());
        System.out.println(fractionArray[3].getDecimal());
        System.out.println(fractionArray[4].getDecimal());
    }
    else

    //Prompt user to select a method to test
    System.out.println("Select which method you would like to perform: ");
    System.out.println("1. Test the toString() method");
    System.out.println("2. Test the reduce() method");
    System.out.println("3. Test the toMixed() method");
    System.out.println("4. Test the getDecimal() method");
    System.out.println("5. Quit");

    methodChoice = method.nextInt();

    }

}

}

class Fraction
{
    int numerator;
    int denominator;

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

//Method to return decimal form of fraction
public double getDecimal(){
    double result;
    result = 1.0 * numerator/denominator;
    return result;
}


//Method to reduce fraction to simplest form
public void reduce() {
int n = numerator;
int d = denominator;

while (d != 0) {
int t = d;
d = n % d;
n = t;
}

int gcd = n;

numerator /= gcd;
denominator /= gcd;
}

//Method to return mixed number form of fraction
public String toMixed(){
    String mixedNum;
    int wholeNum;
    wholeNum = Math.floorDiv(numerator,denominator);

    if (wholeNum == 0){
        mixedNum = numerator + "/" + denominator;
    }
    else if (numerator % denominator == 0)
        mixedNum = wholeNum + "";
    else
    mixedNum = wholeNum + " " +
            (numerator - wholeNum*denominator) + "/" + denominator;

    return mixedNum;
}

If you can help me in fixing these two small errors it would be greatly appreciated. Thank you in advance!

Upvotes: 0

Views: 759

Answers (1)

S. Carr
S. Carr

Reputation: 169

Error 1: It is most likely because you are modifying the instance variables in reduce; I suggest using a local variable.

Error 2: You are printing the individual string of the whole number and the fraction. So basically you get the whole number, which is going to be negative, and then you perform a separate operation on the fraction, which is also negative. Then you print these two negatives; I suggest determining the sign of the fraction before hand, and then absolute value everything and print the sign at the end of the function.

Upvotes: 1

Related Questions