AssertionError - imaginary and real numbers adder - java

I'm getting error:
java.lang.AssertionError: expected: learning.java.advancedoop2.MyComplex<(2.0+10.0i)> but was: learning.java.advancedoop2.MyComplex<(2.0+10.0i)>
Expected :learning.java.advancedoop2.MyComplex<(2.0+10.0i)>
Actual :learning.java.advancedoop2.MyComplex<(2.0+10.0i)>
I'm now working on MyComplex Class like shown on 3.1:
http://www.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html#zz-2
Here's a part of the code that's relevant:
package learning.java.advancedoop2;
public class MyComplex {
private double real = 0.0;
private double imag = 0.0;
public MyComplex() {
}
public MyComplex add(MyComplex right) {
this.imag += right.imag;
this.real += right.real;
return this;
}
}
I tried to make tests, and when I ran them, error that I've got is upside, here's part of my test code:
#Test
public void add() {
MyComplex myComplexFirst = new MyComplex(1, 5);
MyComplex myComplexSecond = new MyComplex(1, 5);
MyComplex myComplexThird = new MyComplex(myComplexFirst.getReal() + myComplexSecond.getReal(), myComplexFirst.getImag() + myComplexSecond.getImag());
myComplexFirst.add(myComplexSecond);
MyComplex newComplex = myComplexFirst;
assertEquals(newComplex, myComplexThird);
}

Did you override the equals method in your custom class ?
If you didn't, the default behavior is to compare the references. That would explain the error message you get.
It is confusing because you have overriden the toString method which displays both instances to have the same values.

You need to override the equals method so that java knows how to compare two MyComplex Objects. Right now it doesn't know that you want to compare the objects based on the real and imag values.
#Override
public boolean equals(Object o) {
// If the object is compared with itself then return true
if (o == this) {
return true;
}
// Check if o is an instance of MyComplex
if (!(o instanceof MyComplex)) {
return false;
}
// typecast o to MyComplex so that we can compare data members
MyComplex c = (MyComplex) o;
// Compare the data members and return accordingly
return Double.compare(real, c.getReal()) == 0
&& Double.compare(imag, c.getImag()) == 0;
}

Related

Why doesn't my custom object Java HashMap handle unique keys? [duplicate]

This question already has an answer here:
When do you need to override hashcode() and equals() when using a hashmap [duplicate]
(1 answer)
Closed 9 months ago.
Below code returns false. Trying to make Equation object act as a key in a custom HashMap. Pretty sure method overrides are implemented correctly, hashCode is the exact same between the two equation instances. What is exactly wrong?
class Solution {
public boolean maxPoints(int[][] points) {
Map<Equation, Integer> map = new HashMap<>();
Equation eq1 = new Equation(1, 2);
Equation eq2 = new Equation(1, 2);
map.put(eq1, 1);
if (map.containsKey(eq1)) return true;
return false;
}
class Equation {
private int slope;
private int yIntercept;
public Equation(int slope, int yIntercept) {
this.slope = slope;
this.yIntercept = yIntercept;
}
public boolean equals(Equation other) {
if (other == this) return true;
return (this.slope == other.slope && this.yIntercept == other.yIntercept);
}
public int hashCode() {
return Objects.hash(this.slope, this.yIntercept);
}
}
}
If you want to overwrite a method annotade it with #Override.
The compiler would give you an error for your equals because you do not overwrite but overload the method
#Override
public boolean equals(Object other) {
...
}
equals needs to be public boolean equals(Object other) to match the signature in Object.
Your equals method doesn't override the one that the code in HashMap is calling.
Usually an equals method looks like this:
#Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Equation)) {
return false;
}
// instanceof above also covers null
Equation otherEquation = (Equation) other;
return otherEquation.slope == this.slope
&& otherEquation.yIntercept == this.yIntercept;
}
...or use Project Lombok's #EqualsAndHashCode annotation.

HashMap ignoring overridden hashCode and equals methods

I am loading data on network traffic from a file. The information I'm loading is attacker IP address, victim IP address, and date. I've combined these data into a Traffic object, for which I've defined the hashCode and equals functions. Despite this, the HashMap I'm loading them into treats identical Traffic objects as different keys. The entire Traffic object complete with some simple test code in the main method follows:
import java.util.HashMap;
public class Traffic {
public String attacker;
public String victim;
public int date;
//constructors, getters and setters
#Override
public int hashCode() {
long attackerHash = 1;
for (char c:attacker.toCharArray()) {
attackerHash = attackerHash * Character.getNumericValue(c) + 17;
}
long victimHash = 1;
for (char c:victim.toCharArray()) {
victimHash = victimHash * Character.getNumericValue(c) + 17;
}
int IPHash = (int)(attackerHash*victimHash % Integer.MAX_VALUE);
return (IPHash + 7)*(date + 37) + 17;
}
public boolean equals(Traffic t) {
return this.attacker.equals(t.getAttacker()) && this.victim.equals(t.getVictim()) && this.date == t.getDate();
}
public static void main(String[] args) {
Traffic a = new Traffic("209.167.099.071", "172.016.112.100", 7);
Traffic b = new Traffic("209.167.099.071", "172.016.112.100", 7);
System.out.println(a.hashCode());
System.out.println(b.hashCode());
HashMap<Traffic, Integer> h = new HashMap<Traffic, Integer>();
h.put(a, new Integer(1));
h.put(b, new Integer(2));
System.out.println(h);
}
}
I can't speak to the strength of my hash method, but the outputs of the first two prints are identical, meaning it at least holds for this case.
Since a and b are identical in data (and therefore equals returns true), and the hashes are identical, the HashMap should recognize them as the same and update the value from 1 to 2 instead of creating a second entry with value 2. Unfortunately, it does not recognize them as the same and the output of the final print is the following:
{packagename.Traffic#1c051=1, packagename.Traffic#1c051=2}
My best guess at this is that HashMap's internal workings are ignoring my custom hashCode and equals methods, but if that's the case then why? And if that guess is wrong then what is happening here?
The problem here is your equals method, which does not override Object#equals. To prove this, the following will not compile with the #Override annotation:
#Override
public boolean equals(Traffic t) {
return this.attacker.equals(t.getAttacker()) &&
this.victim.equals(t.getVictim()) &&
this.date == t.getDate();
}
The implementation of HashMap uses Object#equals and not your custom implementation. Your equals method should accept an Object as a parameter instead:
#Override
public boolean equals(Object o) {
if (!(o instanceof Traffic)) {
return false;
}
Traffic t = (Traffic) o;
return Objects.equals(attacker, t.attacker) &&
Objects.equals(victim, t.victim) &&
date == t.date;
}

HashMap doesn't work with user defined key class [duplicate]

This question already has answers here:
Overriding the java equals() method - not working?
(8 answers)
Closed 7 years ago.
I am using Processing language (derived from Java PApplet) version 3.01
Here is my code...
Pig a = new Pig(1);
Pig b = new Pig(1);
HashMap<Pig, String> m = new HashMap<Pig, String>();
m.put(a,"foo");
String c = m.get(b);
println(c);
class Pig {
int a;
Pig(int x) { a=x;}
boolean equals(Pig b) { return b.a==a;}
int hashCode() { return a;}
}
As you can see I am using Pig for key, I defined the equals and hashCode. I expect output "foo", however, I get output null.
Any idea what is going on here? Why doesn't this work?
Try that, you didn't override Object methods at all / correctly :
class Pig {
int a;
Pig(int x) {
a = x;
}
#Override
public boolean equals(Object b) {
if(b == null) return false;
if(!(b instanceof Pig)) return false;
if(b == this) return true;
return ((Pig) b).a == a;
}
#Override
public int hashCode() {
return a;
}
}
You didn't override equals(Object), but you did implement an unrelated equals(Pig) method. HashMap uses the former, your method isn't even called.

Not sure how to complete this equals method

Could someone help me with this question please? I've tried looking up other examples of this to find what I need to do and keep running into something called and EqualsBuilder, is that what I need to use? Do I need to have it call on equals again if it satisfies neither of the IFs?
The following code contains a class definition and an incomplete method definition. The equals method is used to compare Buildings.
It is intended to return true if Buildings have the same names and number of floors (but are not necessarily the same Building) and false otherwise.
public class Building {
private String name;
private int noOfFloors;
public boolean equals (Object rhs) {
if (this == rhs) {
return true;
}
if (!(rhs instanceof Building)) {
return false;
}
Building b = (Building) rhs;
// missing return statement
}
}
public boolean equals (Object rhs) {
if (this == rhs) {
return true;
}
if (!(rhs instanceof Building)) {
return false;
}
Building b = (Building) rhs;
// This is what you're supposed to add. It will return true only if both
// object's attributes (name and number of floors) are the same
return this.name.equals(b.name) && this.noOfFloors == b.noOfFloors;
}
The only thing that you have to test for now is the fields of both objects. If they are equal, then you should return true, if at least one of them is not then you should return false.
Since your fields in that case are int and Stringyou can use == for the integer field and .equals() for the String field.
Something like this should do the job just fine:
if(this.name.equals(b.name) && this.noOfFloors == b.noOfFloors){
return true ;
}
else{
return false;
}
After the instanceOf tests you want to compare the fields of the object to the other object. Something like Objects.deepEquals() should do the trick for you nicely.

removeAll from Interface Set

I want to compare database dump to xml and *.sql. In debagge toRemove and toAdd only differ in dimension. toRemove has size 3, toAdd has size 4. But after running the code, removeAll, toRemove has size 3 and toAdd has size 4. What's wrong?
final DBHashSet fromdb = new DBHashSet(strURL, strUser, strPassword);
final DBHashSet fromxml = new DBHashSet(namefile);
Set<DBRecord> toRemove = new HashSet<DBRecord>(fromdb);
toRemove.removeAll(fromxml);
Set<DBRecord> toAdd = new HashSet<DBRecord>(fromxml);
toAdd.removeAll(fromdb);
Update:
public class DBRecord {
public String depcode;
public String depjob;
public String description;
public DBRecord(String newdepcode, String newdepjobe, String newdesc) {
this.depcode = newdepcode;
this.depjob = newdepjobe;
this.description = newdesc;
}
public String getKey() {
return depcode + depjob;
}
public boolean IsEqualsKey(DBRecord rec) {
return (this.getKey().equals(rec.getKey()));
}
public boolean equals(Object o) {
if (o == this)
return true;
if (o == null)
return false;
if (!(getClass() == o.getClass()))
return false;
else {
DBRecord rec = (DBRecord) o;
if ((rec.depcode.equals(this.depcode)) && (rec.depjob.equals(this.depjob)))
return true;
else
return false;
}
}
}
In order to properly use HashSet (and HashMap, for that matter), you must implement a hashCode() as per the following contract:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
The code you've supplied for DBRecord does not overide it, hence the problem.
You'd probably want to override it in the following way, or something similar:
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + depcode.hashCode();
result = prime * result + depjob.hashCode());
return result;
}

Categories

Resources