Java分数计算器减少方法

时间:2017-01-07 18:12:55

标签: java

我正在为我的AP计算机科学课做一个java分数计算器,我的reduce方法有问题。我认为它只是返回作为简化答案输入的第一个分数。例如,4/5 * 5/4返回20/20而不使用reduce方法调用。这个答案是正确的,但是当我应用我的reduce方法时,它返回4/5(第一个分数)而不是1(或1/1)。任何反馈总是有用的。这是我的代码:

// begin fraction class.
public class Fraction {
    // instance variables for the first fractions numerator and denominator.
    private  int numerator;
    private  int denominator;


    public Fraction(String fraction) {

        // This if statement checks for mixed numbers (indicated by the underscore)
        if (fraction.contains("_")) {
            // gets from 0 to before the first underscore is present to find the value of the whole number in the string.
            int wholeNumber1 = Integer.parseInt(fraction.substring(0, fraction.indexOf("_")));

            // does the same for numerator.
            numerator = Integer.parseInt(fraction.substring(fraction.indexOf("_") + 1, fraction.indexOf("/")));

            // does the same for denominator.
            denominator = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));

            // sets mixed numbers into improper fractions
            numerator = (wholeNumber1 * denominator) + numerator;


        } else if (fraction.contains("/")) {
            // this if statement checks for regular fractions.

            // locates the numerator in the string.
            numerator = Integer.parseInt(fraction.substring(0, fraction.indexOf("/")));

            //locates the denominator in the string
            denominator = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));

        } else {
            // this else statement assumes that the user enters a whole number.

            int wholeNumber2 = Integer.parseInt(fraction.substring(0));
            numerator = wholeNumber2;
            // puts the whole number over 1 to make an improper fraction. This will probably be reduced in another method.
            denominator = 1;

        }

    } // end of Fraction constructor


    // Starting methods for checkpoint 4+

    public Fraction (int numerator, int denominator) {

        // we used this.numerator and this.denominator to make it clear which variable is being referred to.
        this.numerator = numerator;
        this.denominator = denominator;

    } // end of second Fraction constructor.

    // method for formatting fractions.
    public  String toString() {
        return String.valueOf(numerator) + "/" + String.valueOf(denominator);
    }

    // multiply method.
    public Fraction multiply(Fraction secondFraction) {

        int productOfNumerators = numerator * secondFraction.numerator;
        int productOfDenominators = denominator * secondFraction.denominator;

        // object to call the second constructor.
        Fraction product = new Fraction(productOfNumerators, productOfDenominators);
        return product;

    } // end multiply method

    // divide method.
    public Fraction divide(Fraction secondFraction) {

        // product of numerators and denominators for cross multiplication.
        int productOfNumerators = numerator * secondFraction.denominator;
        int productOfDenominators = denominator * secondFraction.numerator;

        // object to call the second constructor.
        Fraction quotient = new Fraction(productOfNumerators, productOfDenominators);

        return quotient;
    } // end divide method

    // add method.
    public Fraction add(Fraction secondFraction) {

        if(denominator != secondFraction.denominator) {

            int sumOfDenominators = denominator * secondFraction.denominator;
            int newNumeratorOne = numerator * secondFraction.denominator;
            int newNumeratorTwo = secondFraction.numerator * denominator;
            int sumOfNumerators = newNumeratorOne + newNumeratorTwo;

            Fraction sum = new Fraction(sumOfNumerators, sumOfDenominators);
            return sum;
        } else {

            int sumOfNumerators = numerator + secondFraction.numerator;
            Fraction sum = new Fraction(sumOfNumerators, denominator);
            return sum;
        }

    } // end add method.

    // subtract method.
    public Fraction subtract(Fraction secondFraction) {

        if(denominator != secondFraction.denominator) {

            int differenceOfDenominators = denominator * secondFraction.denominator;
            int newNumeratorOne = numerator * secondFraction.denominator;
            int newNumeratorTwo = secondFraction.numerator * denominator;
            int differenceOfNumerators = newNumeratorOne - newNumeratorTwo;

            Fraction difference = new Fraction(differenceOfNumerators, differenceOfDenominators);
            return difference;
        } else {

            int sumOfNumerators = numerator - secondFraction.numerator;
            Fraction difference = new Fraction(sumOfNumerators, denominator);
            return difference;
        }

    } // end subtract method.

    // reduce method.
    public Fraction reduce (Fraction secondFraction) {
        int numerator_abs = Math.abs (numerator);
        int denominator_abs = Math.abs (denominator);

        int min_num = Math.min(numerator_abs, denominator_abs);

        Fraction reducedAnswer = new Fraction(numerator_abs, denominator_abs);

        for (int i = 1; i <= min_num; i++) {
            if (numerator%i == 0 && denominator%i == 0){
                i++;
            }//end if
        }//end for
        return reducedAnswer;

    }//end reduce



} // end class

3 个答案:

答案 0 :(得分:2)

在reduce功能中,你不会改变reducedAnswer,所以你不能减少分数。

另外,你要返回分数的abs值,女巫可能不是你想要的。

BTW - 您可能希望使用https://en.wikipedia.org/wiki/Euclidean_algorithm来查找缩减过程中最大的公约数。

答案 1 :(得分:0)

您可以像这样编写缩小方法

public void reduce ()
{
    BigInteger num = BigInteger.valueOf(numerator);
    int gcd = num.gcd(BigInteger.valueOf(denominator)).intValue();

    this.denominator /= gcd;
    this.numerator /= gcd;

}

答案 2 :(得分:0)

您的reduce方法只是在缩减循环之前设置结果,它本身似乎没有效果。鉴于减少纯粹是一个分数特征,它应该是你的分数类的一种方法。此外,Elucid的GCD对计算最大公分母更为有效。鉴于此,我建议如下。

NSTimeZone *tz = [[NSTimeZone timeZoneForSecondsFromGMT:(-8 * 3600)];
[dateFormatter setTimeZone:tz];
NSString* s = [dateFormatter stringFromDate:now];