So we're supposed to modify this class, which implements Comparable, so that it accepts any type of object. The Comparable interface contains the method signature for compareTo, which I need to implement. What I'm uncertain of is how to implement this method. I know that compareTo is supposed to return -1, 0, or 1 but I'm not sure what goes inside the if statements (-1 if object1 < object 2, 0 if object 1 == object 2, and 1 if object1 > object2)

Also, assume that we can only have the instance variable minimum and maximum and that the rest of the methods cannot be changed.

/**
   Computes the average of a set of data values.
   Determines the largest of a set of data values.
*/
public class DataSet implements Comparable
{
   /**
      Constructs an empty data set.
   */
   public DataSet()
   {
      maximum = null;
      minimum = null;
   }

   /**
      Adds a data value to the data set
      @param x a data value
   */
   public void add(Comparable x)
   {
		if (minimum == null || this.compareTo(x) < 0)
			minimum = x;

		if (maximum == null || this.compareTo(x) > 0)
			maximum = x;
   }
   /**
      Gets the largest of the added data.
      @return the maximum or 0 if no data has been added
   */
   public Comparable getMaximum()
   {
      return maximum;
   }
   public Comparable getMinimum()
   {
	  return minimum;
   }
   public int compareTo(Object o)
   {
	   if ()
	   		return -1;
	   else if ()
		   	return 0;
	   else
	   		return 1;
   }

   private Comparable minimum;
   private Comparable maximum;
}

Recommended Answers

All 5 Replies

It sounds like you mostly understand how compareTo(Object o) works. Your job is to compare the specific properties of the objects in your if() statements such that you return the proper int value. If you need to compare on some property value, say object.cost() for example, you write the if() statements to copare those properties such that if o1.cost()<o2.cost() then return -1, etc. If you aren't using generics to define the object types, then you will need to case Object to the correct type before you can access the properties.

I think you almost have it nailed well enough, but if my explanation is still confusing, just post back on what you don't understand.

It sounds like you mostly understand how compareTo(Object o) works. Your job is to compare the specific properties of the objects in your if() statements such that you return the proper int value. If you need to compare on some property value, say object.cost() for example, you write the if() statements to copare those properties such that if o1.cost()<o2.cost() then return -1, etc. If you aren't using generics to define the object types, then you will need to case Object to the correct type before you can access the properties.

I think you almost have it nailed well enough, but if my explanation is still confusing, just post back on what you don't understand.

See this is what I'm confused about: how do I know what I'm going to compare given that the compareTo() method is supposed to be able take any type of object argument? Different data types added to DataSet may require different methods of measurements (i.e. for strings you compare their length; numbers, magnitude; etc..). Somehow, I don't see a way to do this for generic objects, even with casting. There must be something I'm forgetting here.

edit: no emoticons -.-||

eh, I just went through my class notes again and figure it out.

See this is what I'm confused about: how do I know what I'm going to compare given that the compareTo() method is supposed to be able take any type of object argument? Different data types added to DataSet may require different methods of measurements (i.e. for strings you compare their length; numbers, magnitude; etc..). Somehow, I don't see a way to do this for generic objects, even with casting. There must be something I'm forgetting here.

edit: no emoticons -.-||

You could use instanceof to check the type of the object and operate appropriately. Any object that is of a numerical wrapper type (Integer, Long, etc.) extends Number, so that check becomes easy, as you can see here

Integer a = 3;
Integer b = 2;

if (a instanceof Number){
    if (a < b)
        System.out.println("-1");  // just printing here as example
    else if (a > b)
        System.out.println("1");
    else
        System.out.println("0");
}

If the object is an instanceof String, those operations can be separated the same way.

Since you have a parameter of Object to be compared to "this", you will also need to decide what to do if they are not compatible types. The compareTo() method says that it will throw ClassCastException if the types are not comparable, so you need to throw that exception unless you have a specific strategy on comparing disparate types (ie by their toString() values, or perhaps treating Strings as a numeric zero, etc). Most likely you would want to throw the exception, since the sets should probably be of uniform type.

You could use instanceof to check the type of the object and operate appropriately. Any object that is of a numerical wrapper type (Integer, Long, etc.) extends Number, so that check becomes easy, as you can see here

Integer a = 3;
Integer b = 2;

if (a instanceof Number){
    if (a < b)
        System.out.println("-1");  // just printing here as example
    else if (a > b)
        System.out.println("1");
    else
        System.out.println("0");
}

If the object is an instanceof String, those operations can be separated the same way.

Since you have a parameter of Object to be compared to "this", you will also need to decide what to do if they are not compatible types. The compareTo() method says that it will throw ClassCastException if the types are not comparable, so you need to throw that exception unless you have a specific strategy on comparing disparate types (ie by their toString() values, or perhaps treating Strings as a numeric zero, etc). Most likely you would want to throw the exception, since the sets should probably be of uniform type.

ohhh, ok ty. I wasn't aware of the instanceOf.

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.