I have a problem to Modify the Coin class to have it implement the Comparable interface. The following code is not working correctly and I don't know why.

public class Coin implements Comparable<Object>
{
	private double value;
	private String name;
	
	public Coin(double aValue, String aName)
	{
		value = aValue;
		name = aName;
	}
	
	public int Coinc1.compareTo(Object c2)
	{
		return (int) value;
	}
}
/**
   This program tests the use of the Comparable interface
   in the coin class.
*/
public class CoinTester
{
   public static void main(String[] args)
   {
      Coin c1 = new Coin(0.05, "nickel");
      Coin c2 = new Coin(0.01, "penny");

      int b = c1.compareTo(c2);
    
      if (b < 0)
         System.out.println("less");
      else if (b > 0)
         System.out.println("more");
      else
         System.out.println("equal");
      System.out.println("Expected: more");
   }
}

I fixed a couple of typos in your coin class:

public class Coin implements Comparable<Object>
{
	private double value;
	private String name;

	public Coin(double aValue, String aName)
	{
		value = aValue;
		name = aName;
	}

	public int compareTo(Object c2)
	{
		return (int) value;
	}
}

The whole point of compareTo is that it compares your object with the one passed as a parameter, and tells you which is "lower" or "higher" or "the same" (those terms depend on what the data is, and how you chose to sort it).
Simply returning a value from one of the objects MUST be a mistake.

ps: @thins01 - the OP would learn more if you pointed out where his mistakes are, and let him think through how to fix them. Just providing a corrected program encourages copy/paste rather than true understanding.

I made the abov changes which is how I had it set up intially anyway but I'm still getting the wrong output. It is supposed to output more but it outputs equal.

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.