Java Add()和Multiply()方法返回0/0和1/1?

时间:2014-03-10 17:28:49

标签: java methods add fractions multiplying

对于作业,我应该使用以下的Driver来输出两个输入分数的和和乘积,分别为分数和双精度。

问题是我创建的Add()方法保持返回“0/0 NaN”并且Multiply()方法保持返回“1/1 1.0”。我一直在尝试很多不同的东西,但我无法弄清楚如何让方法覆盖驱动程序中的新分数。

这是我的司机:

import java.util.Scanner;

public class FractionDriver {

    /**
     * @param args
     */
    public static void main(String[] args) 
    { 
     Scanner stdIn = new Scanner(System.in); 
     Fraction c, d, x; // Fraction objects 

     System.out.println("Enter numerator; then denominator."); 
     c = new Fraction(stdIn.nextInt(), stdIn.nextInt()); 
     c.print(); 

     System.out.println("Enter numerator; then denominator."); 
     d = new Fraction(stdIn.nextInt(), stdIn.nextInt()); 
     d.print(); 

     x = new Fraction(); // create a fraction for number 0 

     System.out.println("Sum:"); 
     x.add(c).add(d); 
     x.print(); 
     x.printAsDouble(); 

     x = new Fraction(1, 1); // create a fraction for number 1 

     System.out.println("Product:"); 
     x.multiply(c).multiply(d); 
     x.print(); 
     x.printAsDouble(); 

     System.out.println("Enter numerator; then denominator."); 
     x = new Fraction(stdIn.nextInt(), stdIn.nextInt()); 
     x.printAsDouble(); 

     } // end main 

}  //end class

这是我的Fraction类:

package ch6;

public class Fraction 
{
    private int numerator;
    private int denominator;

    //*************************************************************

    public Fraction()
    {

    }

    //*************************************************************

    public Fraction(int numerator, int denominator)
    {
        this.numerator = numerator;
        this.denominator = denominator;
    }



//**************************************************************

    public int getNumerator()
    {
        return this.denominator;
    }

    //**************************************************************

    public int getDenominator()
    {
        return this.denominator;
    }

    //**************************************************************

    public Fraction add(Fraction c)
    {
        denominator = this.getDenominator() * c.getDenominator();
        numerator = this.getNumerator() * c.getDenominator() + c.getNumerator() * this.getDenominator();
        return new Fraction (numerator, denominator);
    }

    //**************************************************************

    public Fraction multiply(Fraction c)
    {
        int productNumerator = this.numerator * c.numerator;
        int productDenominator = this.denominator * c.denominator;
        return new Fraction(productNumerator, productDenominator);
    }


    //**************************************************************

    public void print()
    {
        System.out.println(this.numerator + "/" + this.denominator);
    }

    //**************************************************************

    public void printAsDouble()
    {
        System.out.println((double) this.numerator / this.denominator);
    }

} 

1 个答案:

答案 0 :(得分:1)

您错误地使用每个0的分子和分母来表示值0。该值0/0在数学中是不确定的,并且将在浮点运算中生成NaN

尝试数学上正确的零,分子表示为0,分母表示为1。

// Creates zero.
public Fraction()
{
    this.numerator = 0;  // This line is unnecessary, as default is 0.
    this.denominator = 1;
}

此外,在您的add方法中,您将返回新的Fraction,但您还要更改当前numerator的{​​{1}}和denominator 。与Fraction类似,您需要创建局部变量来存储新分子和新分母,以避免更改当前multiply

Fraction

顺便提一下,正如@MarcB已在评论中说明的那样,这是错误的:

// Declare them as "int" local variables.
int denominator = this.getDenominator() * c.getDenominator();
int numerator = this.getNumerator() * c.getDenominator() +
               c.getNumerator() * this.getDenominator();

更改它以返回分子!