I have a Coin class that compares weight and value and I need help developing a hashcode and Junit tests to test them, here is my class:
public class Coin implements Comparable<Coin>{
public static final int
CENT=1,
NICKEL=5,
DIME=10,
QUARTER=25;
private int value;
private double weight;
public double getWeight(){
return weight;
}
public void setWeight(double weight){
this.weight = weight;
}
public int getValue(){return value;}
public Coin(int value, double weight){
this.weight = weight;
this.value = value;
}
@Override
public String toString(){
return
value == CENT ? "cent"
: value == NICKEL ? "nickel"
: value == DIME ? "dime"
: value == QUARTER ? "quarter"
: "unknown";
}
@Override
public boolean equals(Object that){
if(that == null)
return false;
if(getValue() != ((Coin) that).getValue())
return false;
Coin t = (Coin) that;
return(
value == t.value
&& t.equals(t.weight));
}
@Override
public int hashCode(){
int h1 = new Double (value).hashCode();
int h2 = new Double (weight).hashCode();
final int HASH_MULTIPLIER = 29;
int h = HASH_MULTIPLIER * h1 + h2;
return h;
}
@Override
public int compareTo(Coin arg0) {
// TODO Auto-generated method stub
return 0;
}
}
As you can see I need help. My hash code is very bad and I do not even know how to approach the compareTo Eclipse put in. I also need help developing JUnit tests for this code. Can anyone guide me on how to create the tests for this?