This commit is contained in:
2022-03-19 17:47:52 +08:00
parent 6bf672bd5b
commit 1c8f95c4b3
35 changed files with 1597 additions and 0 deletions

View 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);
}
}

View 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);
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Cat.testCat();
Dog.testDog();
}
}