Files
oop/lab/lab5/Question3/Dog.java
2022-03-19 17:47:52 +08:00

64 lines
1.0 KiB
Java

/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-03-19
* Description: Dog class
*/
public class Dog {
/**
* Dog's name
*/
private String name;
/**
* Dog's weight
*/
private double weight;
/**
* Constructor. Initialize the name and weight of the dog
*/
public Dog(String name, double weight) {
this.name = name;
this.weight = weight;
}
/**
* Get the name of the dog
*
* @return name of the dog
*/
public String getName() {
return name;
}
/**
* Get the weight of the dog
*
* @return weight of the dog
*/
public double getWeight() {
return weight;
}
/**
* Feeding a dog adds 2.0 to its weight
*/
public void feed() {
weight += 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);
}
}