public class Fish {
private int weight;
private String color;
public Fish(int weight, String color) {
this.color = color;
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class EqualsTest {
public static void main(String[] args) {
Fish f1 = new Fish(1, "blue");
Fish f2 = new Fish(1, "blue");
System.out.println(f1 == f2);
System.out.println(f1.equals(f2));
}
}
public class Fish {
private int weight;
private String color;
public Fish(int weight, String color) {
this.color = color;
this.weight = weight;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + weight;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Fish other = (Fish) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (weight != other.weight)
return false;
return true;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class EqualsTest {
public static void main(String[] args) {
Fish f1 = new Fish(1, "blue");
Fish f2 = new Fish(1, "blue");
System.out.println(f1 == f2);
System.out.println(f1.equals(f2));
}
}
public class Fish {
private int weight;
private String color;
public Fish(int weight, String color) {
this.color = color;
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class EqualsTest {
public static void main(String[] args) {
Fish f1 = new Fish(1, "blue");
Fish f2 = new Fish(1, "blue");
System.out.println(f1 == f2);
System.out.println(f1.equals(f2));
}
}
public class Fish {
private int weight;
private String color;
public Fish(int weight, String color) {
this.color = color;
this.weight = weight;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + weight;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Fish other = (Fish) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (weight != other.weight)
return false;
return true;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class EqualsTest {
public static void main(String[] args) {
Fish f1 = new Fish(1, "blue");
Fish f2 = new Fish(1, "blue");
System.out.println(f1 == f2);
System.out.println(f1.equals(f2));
}
}