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

Pages: 1-

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-18 23:35

Name: Anonymous 2011-02-18 23:41

>>2
Back to /b/.

Name: Anonymous 2011-02-19 0:45

It's worth noting that it runs perfectly fine without the "implements Comparable" in the pet class

Name: Anonymous 2011-02-19 0:48

Java methods are abstract by default. Why do they bother even having an abstract keyword?

Name: Chin Chan !!j7TFje21TaEnaOr 2011-02-19 0:51


▲ ▲

Name: Anonymous 2011-02-19 0:51

>>6
Back to /b/.

Name: Chin Chan !!j7TFje21TaEnaOr 2011-02-19 0:54

8 get!

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();

}

Name: Anonymous 2011-02-19 1:27

>>9

Thanks, the problem with this is that the compareTo override for the implementation of Comparable wasn't really explained in any of the class notes or videos

Name: Anonymous 2011-02-19 1:29

>>10
That's your professor's fault, unless you've been missing some lectures.

Name: Anonymous 2011-02-19 1:33

Yeah, there aren't any lectures. We meet in-class once a week to work on shit and to ask any questions we have, but other than that all the learning is through vids/notes.

Name: Anonymous 2011-02-19 1:35

>>12
That's horrible. What country/university are you in?

Name: Anonymous 2011-02-19 1:43

New York, the "hybrid" class is basically an online class, but the way the guy teaches there's hardly any real "learning" to be done, just copy/pasting his work and modifying it to suit your own purpose(s).

Name: Anonymous 2011-02-19 1:57

I just looked through the videos relevant to this assignment, and he basically went over interfaces, inner-classes and exception handling. Implementing the Comparable class wasn't covered at all, aside from being listed as one of the requirements for the assignment.

Name: Anonymous 2011-02-19 1:59

>>15
Implementing the Comparable class
Comparable interface

Name: Anonymous 2011-02-19 2:05

>>16 My mistake

Name: Anonymous 2011-02-19 2:23

Well, if you have any question as to how the code in >>9 works, go ahead. I'm soon going to sleep() because I'm tired.

Name: Anonymous 2011-02-19 9:19

This, gentlemen, is what happens when OP uses code tags!

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