Tester
public class Tester {
public static void main(String[] args) {
Animal creature = new Animal(10, 5);
Animal dog = new Dog(10, 4);
Animal boy1 = new Man(10, 2, 50);
Animal boy2 = new Man(10, 2, 50);
int result = 0;
if (creature.equals(dog))
result = 5;
else
result = 5*2;
if(dog.equals(boy1))
result = result + 5*10;
else
result = result + 5*20;
if (boy1.equals(boy2))
result = result + 5*100;
else
result = result + 5*200;
}
}
Man.java
public class Man extends Animal{
private double weight;
public static final int NUM_OF_LEGS = 2;
public static final int VENERABLE_AGE = 50;
public Man(int age, int legs, double weight) {
super(age, legs);
this.weight = weight;
}
public double getWeight() {
return weight;
}
public double computeSpeed() {
return super.computeSpeed() * 10 / weight;
}
public String speak () {
if (getAge() < VENERABLE_AGE)
return "How are you doing?";
else
return "How are you doing, honey?";
}
public String toString() {
return super.toString() + "[weight=" + weight + "]";
}
public boolean equals(Object anObject) {
Man man = (Man) anObject;
return super.equals(anObject) && this.weight == man.weight;
}
}
Animal.java
public class Animal {
public static final int MINIMUM_SPEED = 10;
private int age;
private int numOfLegs;
public Animal(int age, int legs) {
this.age = age;
numOfLegs = legs;
}
public int getAge() {
return age;
}
public int getNumOfLegs() {
return numOfLegs;
}
public double computeSpeed() {
return MINIMUM_SPEED * numOfLegs;
}
public String speak(){
return "gibberish";
}
public String toString() {
return getClass().getName() + "[age=" + age + "][legs=" + numOfLegs + "]";
}
public boolean equals(Object anObject) {
Animal animal = (Animal) anObject;
return this.age == animal.age && this.numOfLegs == animal.numOfLegs;
}
}
Dog.java
public class Dog extends Animal {
public static final int NUM_OF_LEGS = 4;
public static final int VENERABLE_AGE = 10;
public Dog(int age, int legs) {
super(age, legs);
}
public double computeSpeed() {
if (getAge() < VENERABLE_AGE)
return super.computeSpeed();
else
return super.computeSpeed() / 2.0;
}
public String speak () {
return "bow-wow";
}
}