Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java Abstract Class Help

Name: Anonymous 2011-02-18 23:29

Can't figure out how to fix this Java error.



Abstract Pet Class:
public abstract class Pet implements Comparable
{
   
    private String name, breed;
  
    Pet(String name, String breed)
    {
        this.name = name;
        this.breed = breed;
    }
 
    public String getName()
    {
        return name;
    }
    public String getBreed()
    {
        return breed;
    }
 
    //Abstract get methods
    public abstract double getAge();
    public abstract double getWeight();

}


Cat Sub-Class:

public class Cat extends Pet
{

  private double age, weight;
 
  Cat(String name, String breed, double age, double weight)
  {
      super(name, breed);
      this.age = age;
      this.weight = weight;
  }
 
  public double getAge()
  {
      return age;
  }
  public double getWeight()
  {
      return weight;
  }

}


Error:
Error(3,8):  homework2.Cat is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable

What am I doing wrong? The only relevant detail given for this assignment is:
Create an abstract class called Pet. This class should have at least two abstract methods in it. Create two classes named Cat and Dog that implement these abstract methods. Make sure that your Pet class implements the Comparable interface.

Name: Anonymous 2011-02-19 1:24

Hi >>1. What your java compiler is telling you is that the interface "Comparable" requires you to implement a method "int     compareTo(Object o)".

In other words:
public abstract class Pet implements Comparable
{
      
    private String name, breed;

    Pet(String name, String breed)
    {
        this.name = name;
        this.breed = breed;
    }

    public String getName()
    {
        return name;
    }
    public String getBreed()
    {
        return breed;
    }
   
    // required for class Pet to implement the Comparable interface
    // it compares first the names, then breed, then age, then weight
    public int compareTo(Object o) {
        Pet p = (Pet)o;
        int c;
       
        c = this.name.compareTo(p.getName());
        if (c!=0) return c;
       
        c = this.breed.compareTo(p.getBreed());
        if (c!=0) return c;
       
        c = (new Double(this.getAge())).compareTo(new Double(p.getAge()));
        if (c!=0) return c;
       
        c = (new Double(this.getWeight())).compareTo(new Double(p.getWeight()));
        if (c!=0) return c;
       
        return 0; // they must be equal
    }
   
    //Abstract get methods
    public abstract double getAge();
    public abstract double getWeight();

}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List