public abstract class Martian extends Object implements Comparable<Martian>
Modifier and Type | Field and Description |
---|---|
private int |
id
Every Martian has an id which is simply an integer.
|
private int |
volume
Every Martian has a volume which is simply an integer.
|
Constructor and Description |
---|
Martian(int id,
int volume)
Creates a Martian with the specified
id and volume . |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Martian m)
Implements Java’s
compareTo so that Martians (either red or green) are compared based on
their id ’s which results in the usual ascending sorted order. |
boolean |
equals(Object o)
Two Martians are equal if their
id ’s are equal, regardless of whether they are GreenMartian s
or RedMartians s. |
int |
getId()
Returns the
id for the Martian. |
int |
getVolume()
Returns the
volume for the Martian. |
void |
setVolume(int volume)
Set the
volume for the Martian |
abstract String |
speak()
Martian subclasses must implement this method to speak in the way that makes sense for them.
|
String |
toString()
OPTIONAL-NOT REQUIRED.
|
private int id
private int volume
public Martian(int id, int volume)
id
and volume
.id
- volume
- public int compareTo(Martian m)
compareTo
so that Martians (either red or green) are compared based on
their id
’s which results in the usual ascending sorted order.compareTo
in interface Comparable<Martian>
public boolean equals(Object o)
id
’s are equal, regardless of whether they are GreenMartian
s
or RedMartians
s.equals
in class Object
public int getId()
id
for the Martian.public int getVolume()
volume
for the Martian.public void setVolume(int volume)
volume
for the Martianvolume
- public abstract String speak()
public String toString()
id=xxx, vol=yyywhere xxx is the
id
, yyy is the volume
. Then, RedMartian and GreenMartian toString can call
this.toString
in class Object