0

这是我的Rational课:

package lab18f;

import static java.lang.System.*;

class Rational implements Comparable<Rational>
 {
//add two instance variables
private int num, den;

//write two constructors
public Rational ()
{
    num = 1;
    den = 1;
}

public Rational (int numer, int deno)
{
    num = numer;
    den = deno;
}

//write a setRational method
public void setRational(int numer, int deno)
{
    num = numer;
    den = deno;
}


//write  a set method for numerator and denominator
public void setNumerator(int numer)
{
    num = numer;
    
}

public void setDenominator(int deno)
{
    den = deno;
}
public void add(Rational  other)
{
    //num1/den1 + num2/den2 
    //new numerator = (num1 * den2 + num2 * den1)
    //new denominator = (den1 * den2)
    
    int newNumer = (this.getNumer()*other.getDeno() + other.getNumer()*this.getDeno());
    int newDeno = this.getDeno()*other.getDeno();
    this.setRational(newNumer, newDeno);
    
    reduce();
}

private void reduce()
{
    int gcd = 0;
    int numer = num;
    int deno = den;
    
    if(numer > deno)
        gcd = gcd(numer,deno);
    else if(numer < deno)
        gcd = gcd(deno, numer);
    else
        gcd = numer;
    
    this.setRational(num/gcd, den/gcd);
    
}

private int gcd(int numOne, int numTwo)
{
    
    int one = numOne;
    int two = numTwo;
    int gcd = two;
    
    
    if(two==0)
        return one;
    return gcd(two, one%two);
}

public Object clone ()
{
    return new Rational(num, den);
}


//ACCESSORS

//write get methods for numerator and denominator
public int getNumer()
{
    return num;
}

public int getDeno()
{
    return den;
}

public boolean equals( Object obj)
{
    Rational lhs = (Rational)obj;
    if((getNumer()/getNumer()== lhs.getNumer()/lhs.getNumer()) && getDeno()/getNumer() == lhs.getDeno()/lhs.getNumer())
        return true;
    return false;
}

public int compareTo(Rational other)
{


    return -1;
}




//write  toString() method
public String toString()
{
    return "" + getNumer() + "/" + getDeno();
}

}

以及该课程的跑步者Rational课程:

import static java.lang.System.*;

public class Lab18f
 {
public static void main( String args[] )
{
    Rational test = new Rational();
    out.println("test = " + test);

    Rational newOne = new Rational(3,4);
    out.println("newOne = "+newOne);

    out.println("test.equals(newOne) = "+test.equals(newOne));

    newOne = (Rational)test.clone();
    out.println("\n\nnewOne after test.clone() = "+newOne);
    out.println("test.equals(newOne) = "+test.equals(newOne));
    
    Rational rOne = new Rational(1,2);
    Rational rTwo = new Rational(2,3);
    out.println("1/2.equals(2/3) = "+rOne.equals(rTwo));
    test.setRational(4,6);
    out.println("2/3.equals(4/6) = "+rTwo.equals(test));
            
    out.println("\n\nrOne = "+rOne);
    out.println("rTwo = "+rTwo);

    out.println("rOne.compareTo(rTwo) = "+rOne.compareTo(rTwo));
    out.println("rTwo.compareTo(rOne) = "+rTwo.compareTo(rOne));

    rOne.add(rTwo);
    out.println("\n\nrOne.add(rTwo) = "+ rOne);

    rOne.setRational(1,2);
    rTwo.setRational(1,3);
    rOne.add(rTwo);
    out.println("\n\n1/2.add(1/3) = "+ rOne);
    
    rOne.setRational(4,10);
    rTwo.setRational(3,5);
    rOne.add(rTwo);
    out.println("\n\n4/10.add(3/5) = "+ rOne);  

    rOne.setRational(2,10);
    rTwo.setRational(3,6);
    rOne.add(rTwo);
    out.println("\n\n2/10.add(3/6) = "+ rOne);      

    //1/4 + 2/8 = 1/2
    rOne.setRational(1,4);
    rTwo.setRational(2,8);
    out.println("\n\n1/4.equals(2/8) = "+rOne.equals(rTwo));        
    rOne.add(rTwo);
    out.println("\n\n1/4.add(2/8) = "+ rOne);   
    
    //1/6 + 2/8 = 5/12
    rOne.setRational(1,6);
    rTwo.setRational(2,8);
    out.println("\n\n1/6.equals(2/8) = "+rOne.equals(rTwo));                
    rOne.add(rTwo);
    out.println("\n\n1/6.add(2/8) = "+ rOne);           
}
}

我手头的问题是,当我运行 runner 类(显然运行Rational该类)时,我的输出中有这样的语句:

测试 = 1/1

新一 = 3/4

test.equals(newOne) = true

这显然分数 1/1 不等于 3/4

4

1 回答 1

1

您的 equals 方法似乎没有做它应该做的事情。一方面,您正在进行整数除法,并且您正在将分子除以自身。尝试将一个的分子与另一个的分母相乘,并将其与另一个的分子与第一个的分母的乘积进行比较。如果这两个产品相等,则分数相等

public boolean equals(Object obj)
{
    Rational lhs = (Rational) obj;
    return ((getNumer() * lhs.getDeno()) == (getDeno() * lhs.getNumer()));
}
于 2013-02-20T23:47:06.863 回答