35 lines
738 B
Java
35 lines
738 B
Java
/*
|
|
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
|
* Date: 2022-03-19
|
|
* Description: Dog class
|
|
*/
|
|
|
|
public class Dog extends Animal {
|
|
/**
|
|
* Constructor. Initialize the name and weight of the dog
|
|
*/
|
|
public Dog(String name, double weight) {
|
|
super(name, weight);
|
|
}
|
|
|
|
/**
|
|
* Feeding a dog adds 2.0 to its weight
|
|
*/
|
|
public void feed() {
|
|
setWeight(getWeight() + 2.0);
|
|
}
|
|
|
|
/**
|
|
* Test
|
|
*/
|
|
public static void testDog() {
|
|
Dog d = new Dog("Woof", 2.0);
|
|
System.out.println(d.getName() == "Woof");
|
|
System.out.println(d.getWeight() == 2.0);
|
|
d.feed();
|
|
// The name is still the same but the weight increased by 2.0:
|
|
System.out.println(d.getName() == "Woof");
|
|
System.out.println(d.getWeight() == 4.0);
|
|
}
|
|
}
|