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?

What is the use-case for this Coin class (i.e. client code)? I think that this class could be much better modeled as an enum (limited and finite states) in which case you automatically get a good equals and hashCode implementation. Also, when you say 'weight', is it the physical weight of the coin or something else?

On a related note, since you are already using Eclipse, you can ask Eclipse to generate a equals() and hashCode() implementation for you. ALT + SHIFT + S then h.

Can anyone guide me on how to create the tests for this?

In eclipse, right click on the .java file and say New->JUnit Testcase.. :)
Select the methods you want to test, then implement the methods.

What is the use-case for this Coin class (i.e. client code)? I think that this class could be much better modeled as an enum (limited and finite states) in which case you automatically get a good equals and hashCode implementation. Also, when you say 'weight', is it the physical weight of the coin or something else?

On a related note, since you are already using Eclipse, you can ask Eclipse to generate a equals() and hashCode() implementation for you. ALT + SHIFT + S then h.

This worked great, I think, did not know it automates it like this.

But consider thinking about the enum approach which I mentioned for the reasons already stated above.

For the JUnit test, they give you the start of the tests, but not sure how to develop them further.

I tried to create a compareTo:

public int compareTo(Coin firstobj, Coin secondobj) {
		Coin first = (Coin) firstobj;
		Coin second = (Coin) secondobj;
		if(first.getValue()< second.getValue()) return -1;
		if(first.getValue()== second.getValue()) return 0;
		return 1;
	}

but Eclipse still wants to put on in with one argument.

The usual convention for compareTo is that it takes 1 param and compares that to the current object ("this").

like this:
@Override

public int compareTo(Coin that) {
    if (this.value != that.weight) return -1;
    else if (this.value == that.weight) return 0;
    return 1;
}
@Override
    public int compareTo(Coin that) {
        if (this.value != that.weight) {
            return -1;
        } else if (this.value == that.weight) {
            return 0;
        } else {
            return 1;
        }        
    }

Previous 2 examples both wrong. Code can never return 1 as result. Line 3 should be <= not !=.
It would also be lot more realistic if it compared the same fields from this and that!

Oh, where's your sense of adventure, James? What's wrong with a compareTo that sometimes returns coin1>coin2>coin1, anyway? I think it adds a little spice to your coding.

Oh dear. Oops! I should have said "<" and not "<=". Maybe nobody noticed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.