lab5
This commit is contained in:
63
lab/lab5/Question3/Dog.java
Normal file
63
lab/lab5/Question3/Dog.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user