加法,减法,乘法和&用Java中的分数划分(家庭作业)

时间:2011-12-10 00:48:00

标签: java

我已经编写了这段代码作为我在AP计算机科学中收到的作业的答案,我的老师正在重做这个,因为答案不起作用。我只能在我的机器上编译它,所以我依靠我的老师来确定现在产生的结果,如下所示。这项任务是在今晚午夜之前进行的,我拖延到现在为止在这里寻求帮助,但是,我非常感激。此外,这是我第一次来这里,很抱歉,有些代码格式不正确。谢谢你的帮助!

结果ATM

if a = 3/4 and b = 5/6

a.add(b) produces 35/24; This should be 19/12
a.subtract(b) produces 1/0; This should be -1/12
a.divide(b) produces 11/1; This should be 9/10
a.multiply(b) produces 6/-5; This should be 5/8

这是代码。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.TitledBorder;

class Fraction{
    private int numer;
    private int denom;
    private Fraction answer;

    //Definition
    public Fraction(int num, int den)
    {
        numer = num;
        denom = den;
        simplify();
    }


    //Simplify
    void simplify()
    {
        int gcd = findGCD(numer, denom);
        numer /= gcd;
        denom /= gcd;
    }


    //GCD Function
    int findGCD(int a, int b)
    { int temp;
        while(b != 0)
        {
            temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }


    //GetNumerator
    public int getNumer()
    {
        return numer;
    }

    //GetDenominator
    public int getDenom()
    {
        return denom;
    }

    //Fraction Add Method
    Fraction add(Fraction x)
    {
        Fraction answer;

        if(denom == x.denom)
        {
            answer = new Fraction(x.numer + numer, denom);
            }
        else
        {
          int den = denom * x.getDenom();
          int num = numer * x.getNumer();
          num += x.getNumer() * denom;
          answer = new Fraction(num, den);

        }
     return answer;
    }


    //Fraction Subtract Method
    Fraction subtract(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer - x.numer, denom);
        }
        else
        {
            int den = denom / x.getDenom();
            int num = numer / x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }

    //Fraction Multiply Method
    Fraction multiply(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer + x.numer, denom);
        }
        else
        {
            int den = denom + x.getDenom();
            int num = numer + x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }

    //Fraction Divide Method
    Fraction divide(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer - x.numer, denom);
        }
        else
        {
            int den = denom - x.getDenom();
            int num = numer - x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }


    //@Override
    public boolean equals(Fraction x)  
    {
        boolean answer = false;

        if(numer == x.numer && denom == x.denom)
      {
            answer = true;
        }

        return answer;
    }


    //ToString
    public String toString()
    {
    return (Integer.toString(numer) + "/" +
             Integer.toString(denom));
    }


    //Main Method
    public void main(String[]args)
    {
        Fraction a = new Fraction(3,4);
        Fraction b = new Fraction(5,6);
        System.out.println(a.toString());
        System.out.println(b.toString());

        Fraction c = a.add(b);
        System.out.println(c.toString());

        c = a.subtract(b);
        System.out.println(c.toString());

        c = a.divide(b);
        System.out.println(c.toString());

        c = a.multiply(b);
        System.out.println(c.toString());





    }

}

2 个答案:

答案 0 :(得分:5)

所以只需要开始一件事:一直使用getDenom(),因为在你的代码中有时候它是getDenom,有时候会这样说..

对于add()方法: 在denoms是不同的情况下,你错了。 你必须这样做:

denom = this.getDenom() * x.getDenom();
num = this.getNum() * x.getDenom() + x.getNumer() * this.getDenom();
//and check if simplification is possible.
return new Fraction(num,denom);

对于其他人来说,这是一回事。检查你是否乘以分母或分子,因为它似乎不是这样,不要忘记简化。

希望有所帮助

答案 1 :(得分:4)

好的,我对你的GUI感到烦恼,这就是你应该做的事情:

// Euclidean Algorithm
public int gcd(int a, int b)
{
    if (b==0) return a;
    return GCD(b,a%b);
}

void simplify()
{
    int gcd = gcd(numer, denom);
    numer /= gcd;
    denom /= gcd;
}

Fraction add(Fraction x)
{
    return new Fraction(numer * x.getDenom() + x.getNumer() * numer,
                        denom * x.getDenom();  
}

我猜这是你的gcd功能。 和 正如所指出的,添加不正确的denom