Hi!

I am not that good in java and as a homework we had to write to classes: a partial abstract class Koerper and it's subclass Kugel but when I try to compile them the compiler always tells me this:

Kugel.java:9: Kugel is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
public class Kugel extends Koerper

I hope someone can help I'm just not able to figure it out... maybe it's nothing big, but i don't get it :)

The code of both classes is as follows:

abstract class Koerper implements Comparable 
{
	public double volumen;
	public double oberflaeche;
	public double PI=3.14159265;
	
	public abstract void volumen();
	public abstract void oberflaeche();
	
	public void setVolumen(double v)
	{
		volumen=v;
	}

	public void setOberflaeche(double o)
	{
		oberflaeche=o;
	}
	
	public double getVolumen()
	{
		return volumen;
	}
	
	public double getOberflaeche()
	{
		return oberflaeche;
	}
	
	public double compareTo(Koerper object)
	{
		double temp=0;
		
		if(object.volumen>volumen)
			temp=object.volumen;
		if(object.volumen<volumen)
			 temp=volumen;
		if(object.volumen==volumen)
			temp=volumen;
		
		return temp;
	}
public class Kugel extends Koerper 
	{	
		public double r;
		
		public Kugel(double radius){
			
			r=radius;
			
			volumen();
			oberflaeche();
		}
		
		public void volumen()
		{
			double V;
			
			V=(4/3)*Math.PI*r*r*r;
			
			setVolumen(V);
		}
		
		public void oberflaeche()
		{
			double O;
			
			O=4*Math.PI*r*r;
			
			setOberflaeche(O);
		}
	}

...

compareTo should return an int if you want to override the one inherited by the Comparable interface.

And it doesn't have to have specific value

public int compareTo(Koerper object)
	{
		
	}

It should return a positive number if "this" object is "greater than" the argument "Koerper object"
A negative if it is "smaller than".
And 0 if they are equal. Remember if compareTo returns 0 doesn't mean the the equals method will have to return true. So you don't need to implemented.

Check the API for the comparable interface

I wrote this example for you:

class Person implements Comparable {

private String name = null;
private int age = 0;

// get, set methods and constructor

public int compareTo(Person other) {
  if (other==null) {
     return 1;	
  }
     if (this.age>other.getAge()) {

         return 1; //something positive

    } else if (this.age<other.getAge()) {

           return -1; //something negative
    } else { // none is greater or lower than the other
          return 0;
    }	
}


//BUT the equals MAY use something else
public boolean equals(Person p) {
   if (p==null) {
      return false;
   }
   if (name==null) {
      return name == p.getName();
   }
   return name.equals(p.getName()); 
}
}

Thanks very much! works now :)

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.