当所有其他实例都正确时,为什么我得到错误的布尔值?

时间:2013-02-20 23:41:18

标签: java oop interface

这是我的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);           
}
}

我的问题在于,当我运行转轮类(显然运行Rational类)时,我在输出中有这样的声明:

  

test = 1/1

     

newOne = 3/4

     

test.equals(newOne)= true

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

1 个答案:

答案 0 :(得分:1)

您的equals方法似乎没有做它应该做的事情。首先,您正在进行整数除法,并且您将分子除以其自身。尝试将1的分子与另一个的分母相乘,并将其与另一个分子与第一个分母的乘积进行比较。 fractions are equal if those two products are equal

public boolean equals(Object obj)
{
    Rational lhs = (Rational) obj;
    return ((getNumer() * lhs.getDeno()) == (getDeno() * lhs.getNumer()));
}