Public boolean equals (object is different)

This is a program for fractions. I have private ints num and den, Fraction and FractionInterface - a standard homework problem. I did almost everything and now am stuck for several hours using the equals method. Since the other object is Object, I cannot equate it to a fraction. Here is what I have:

public boolean equals(Object other){        
     if (other == this){
         return true;
     } else {
         return false;
     }
}

This compiles, but it gives incorrect results:

1/2 eq 1/2 = true
1/2 eq 1/2 = true
1/2 eq 1/2 = false
1/2 eq 1/2 = false

If I try another == Fraction, it does not compile. Thanks for any help!

+5
source share
9 answers

Try the following:

First draw an object other

Fraction otherFraction = (Fraction) other;

. , ( getNum() getDen() otherFraction.

+1

, other FractionInterface :

public boolean equals(Object other){        
     if (other == this){
         return true;
     } else if (other instanceof FractionInterface) {
         FractionInterface fOther = (FractionInterface) other;
         // compare numerator and denominator...
     } else {
         return false;
     }
}

, instanceof false, other == null, .

+2

, false, , , . equals() :

public boolean equals(Object obj) {
    if (!(obj instanceof Fraction)) {
        return false;
    }
    Fraction that = (Fraction) obj;
    ... // Your algorithm to compare two fractions: this and that.
}

, , equals() .

+1

, , , :

if (other.getClass() != Fraction.class) {
    return false;
}
Fraction otherFraction = (Fraction) other;
// compare the fields of this and otherFraction

, , null.

0

, .

, other Fraction, Fraction. Fraction.

0

, , . , , , . , , ...

Fraction a = // this is however you're making a fraction object...
Fraction b = // do EXACT same thing here that you did for a

// And then, this will illustrate what is wrong with your program...
if(a.equals(b)) {
  System.out.println("This won't print");
} else {
  System.out.println("This will print because your method just checks for reference");
}

, , :

  • == equals
  • , ""

...

public boolean equals(Object other){        
     if (other == this){
         return true;
     } else {
         return false;
     }
}

"equals" Java. == , this.equals(foo) .

, , , instanceof. ...

== , .

, . , ...

if(other instanceOf Fraction) {
  // do stuff...
}

, , . ...

public boolean equals(Fraction other) {
  // do something like this (you will have to define toDouble)
  if(this == other || this.toDouble() == other.toDouble()) {
    return true;
  }

  return false;
}

...

public boolean equals(Object other) {/* ... */}

. , ...

Fraction fractionA = new Fraction("2/4");
Fraction fractionB = new Fraction("1/2");
Fraction fractionC = new Fraction("1/3");
Object trollObject = new Object();

// And then call random equals objects...
if(fractionA.equals(fractionB)) {
  // should be true...
}

if(fractionB.equals(fractionA)) {
  // should be true...
}

// This avoids having to do any casting because
// since you've only defined a Fraction.equals(Fraction) method
// it should instead default to the Object.equals method
if(trollObject.equals(fractionB)) {

}
0

:

public boolean equals(Object other) {
    if (other == this) return true;
    if (other == null) return false;
    if (other.getClass() != this.getClass()) return false;

    Fraction o = (Fraction) other;
    // now you compare their num, den, and possibly sign
}

, getClass() instanceof. , Fraction , , , . , equals() a.equals(b) , b.equals(a), null, equals(), .

0

== - . , , :

public boolean equals(Object other){        
     if (other instanceof Fraction){
         return ((Fraction)other).getNum == this.num && ((Fraction)other).getDen == this.den;
     } else {
         return false;
     }
}
-1

, .try .

import java.io.*;

class Cast

{

public static void main(String args[]) throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

byte a=20;

short s=31468;

int i=12345678;

char c=’c';

float f=3.56f;

//Widening or promotion [java question bank][1]

System.out.println("a=(short)  "+(short) a);

System.out.println("a=(int)  "+(int) a);

System.out.println("a=(long)  "+(long)a);

System.out.println("a=(float)  "+(float)a);

System.out.println();

System.out.println();

System.out.println("s=(int) "+(int)s);

System.out.println("s=(long)  "+(long)s);

System.out.println("s=(float)  "+(float)s);

System.out.println();

System.out.println();

System.out.println("i=(long)  "+(long)i);

System.out.println("i=(float)  "+(float)i);

System.out.println("i=(double)  "+(double)i);


//Narrowing using [java question bank][2]

System.out.println("f=(byte)  "+(byte)f);

System.out.println("f=(short)  "+(short)f);

System.out.println("f=(char)  "+(char)f);

System.out.println("f=(long)  "+(long)f);

System.out.println();

System.out.println();

System.out.println("i=(byte)  "+(byte)i);

System.out.println("i=(short)  "+(short)i);

System.out.println();

System.out.println();

System.out.println("s=(byte)  "+(byte)s);


}

}
-1

All Articles