lab5
This commit is contained in:
64
lab/lab5/Question3/Cat.java
Normal file
64
lab/lab5/Question3/Cat.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-03-19
|
||||
* Description: Cat class
|
||||
*/
|
||||
|
||||
public class Cat {
|
||||
/**
|
||||
* Cat's name
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Cat's weight
|
||||
*/
|
||||
private double weight;
|
||||
|
||||
/**
|
||||
* Constructor. Initialize the name and weight of the cat
|
||||
*/
|
||||
public Cat(String name, double weight) {
|
||||
this.name = name;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the cat
|
||||
*
|
||||
* @return name of the cat
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the weight of the cat
|
||||
*
|
||||
* @return weight of the cat
|
||||
*/
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Feeding a cat adds 1.0 to its weight
|
||||
*/
|
||||
public void feed() {
|
||||
weight += 1.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test
|
||||
*/
|
||||
public static void testCat() {
|
||||
Cat c = new Cat("Meow", 2.0);
|
||||
|
||||
System.out.println(c.getName() == "Meow");
|
||||
System.out.println(c.getWeight() == 2.0);
|
||||
c.feed();
|
||||
// The name is still the same but the weight increased by 1.0:
|
||||
System.out.println(c.getName() == "Meow");
|
||||
System.out.println(c.getWeight() == 3.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user