So the assignment was to make a class that uses compareTo to compare two different Monsters. If Monster 1 is larger it returns 1, if Monster 2 is larger it should return -1. (We just got into the Comparable, so it's still somewhat new.)
We started with a skeleton of a class and I've filled it in as best I could.
CODE
import static java.lang.System.*;
public class Monster implements Comparable
{
private int myWeight;
private int myHeight;
private int myAge;
//write a default Constructor
public Monster()
{
myWeight=0;
myHeight=0;
myAge=0;
}
//write an initialization constructor with an int parameter ht
public Monster(int ht)
{
setMonster(0,ht,0);
}
//write an initialization constructor with int parameters ht and wt
public Monster(int ht, int wt)
{
setMonster (wt, ht, 0);
}
//write an initialization constructor with int parameters ht, wt, and age
public Monster(int ht, int wt, int age)
{
setMonster(wt,ht,age);
}
//modifiers - write set methods for height, weight, and age
public void setMonster(int ht, int wt, int age)
{
myWeight=0;
myHeight=0;
myAge=0;
}
//accessors - write get methods for height, weight, and age
public int getHeight()
{
return myHeight;
}
public int getWeight()
{
return myWeight;
}
public int getAge()
{
return myAge;
}
//creates a new copy of this Object
public Object clone()
{
return new Monster();
}
public boolean equals( Object obj )
{
Monster temp = (Monster)obj;
return (myWeight == obj.getWeight() && myHeight == obj.getHeight() && myAge== obj.getAge());
}
public int compareTo( Object obj )
{
Monster rhs = (Monster)obj;
if(myHeight > rhs.myHeight)
{
return 1;
}
else if(myHeight < rhs.myHeight)
{
return -1;
}
else
{
if(myWeight > rhs.myWeight)
{
return 1;
}
else if(myWeight < rhs.myWeight)
{
return -1;
}
else
{
if(myAge > rhs.myAge)
{
return 1;
}
else if(myAge< rhs.myAge)
{
return -1;
}
}
}
return -1;
}
//write a toString() method
public String toString()
{
return(""+myAge);
}
}
as you can see the comments from the original skeleton are still there, so I've followed instructions I think.
However when I compile, it gives me "Cannot find Symbol method getWeight()" and the same over for getHeight() and getAge(). I've also tried changing them to myWeight, myHeight, and myAge, but it still can't seem to find the variables.
What's wrong?
This post has been edited by Revitalized: 8 Feb, 2008 - 09:33 AM