diff --git a/lab/lab5/Question1/Cat.java b/lab/lab5/Question1/Cat.java new file mode 100644 index 0000000..98aa832 --- /dev/null +++ b/lab/lab5/Question1/Cat.java @@ -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); + } +} diff --git a/lab/lab5/Question1/Start.java b/lab/lab5/Question1/Start.java new file mode 100644 index 0000000..55435a5 --- /dev/null +++ b/lab/lab5/Question1/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Cat.testCat(); + } +} diff --git a/lab/lab5/Question2/Cat.java b/lab/lab5/Question2/Cat.java new file mode 100644 index 0000000..98aa832 --- /dev/null +++ b/lab/lab5/Question2/Cat.java @@ -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); + } +} diff --git a/lab/lab5/Question2/Dog.java b/lab/lab5/Question2/Dog.java new file mode 100644 index 0000000..7a2bdf7 --- /dev/null +++ b/lab/lab5/Question2/Dog.java @@ -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); + } +} diff --git a/lab/lab5/Question2/Start.java b/lab/lab5/Question2/Start.java new file mode 100644 index 0000000..50809a9 --- /dev/null +++ b/lab/lab5/Question2/Start.java @@ -0,0 +1,6 @@ +public class Start { + public static void main(String[] args) { + Cat.testCat(); + Dog.testDog(); + } +} diff --git a/lab/lab5/Question3/Cat.java b/lab/lab5/Question3/Cat.java new file mode 100644 index 0000000..98aa832 --- /dev/null +++ b/lab/lab5/Question3/Cat.java @@ -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); + } +} diff --git a/lab/lab5/Question3/Dog.java b/lab/lab5/Question3/Dog.java new file mode 100644 index 0000000..7a2bdf7 --- /dev/null +++ b/lab/lab5/Question3/Dog.java @@ -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); + } +} diff --git a/lab/lab5/Question3/Start.java b/lab/lab5/Question3/Start.java new file mode 100644 index 0000000..6c4abf7 --- /dev/null +++ b/lab/lab5/Question3/Start.java @@ -0,0 +1,7 @@ +public class Start { + public static void main(String[] args) { + Cat.testCat(); + Dog.testDog(); + Student.testStudent(); + } +} diff --git a/lab/lab5/Question3/Student.java b/lab/lab5/Question3/Student.java new file mode 100644 index 0000000..5c4ac61 --- /dev/null +++ b/lab/lab5/Question3/Student.java @@ -0,0 +1,56 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: Student class + */ + +public class Student { + /** + * Student's name + */ + private String name; + + /** + * Student's pet + */ + private Cat pet; + + /** + * Constructor + */ + public Student(String name, Cat pet) { + this.name = name; + this.pet = pet; + } + + /** + * Getter for name + * + * @return String + */ + public String getName() { + return name; + } + + /** + * Getter for pet + * + * @return Cat + */ + public Cat getPet() { + return pet; + } + + /** + * Test + */ + public static void testStudent() { + Cat cat = new Cat("Tom", 1.0); + Student student = new Student("Walter", cat); + System.out.println(student.getName() == "Walter"); + System.out.println(student.getPet() == cat); + System.out.println(student.getPet().getName() == "Tom"); + System.out.println(student.getPet().getWeight() == 1.0); + } + +} diff --git a/lab/lab5/Question4/Animal.java b/lab/lab5/Question4/Animal.java new file mode 100644 index 0000000..5d49613 --- /dev/null +++ b/lab/lab5/Question4/Animal.java @@ -0,0 +1,66 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: This is the class of Animal. + */ + +public class Animal { + /** + * Animal's name + */ + private String name; + + /** + * Animal's weight + */ + private double weight; + + /** + * Constructor. Initialize the name and weight of the animal. + */ + public Animal(String name, double weight) { + this.name = name; + this.weight = weight; + } + + /** + * Get the name of the animal. + * + * @return the name of the animal + */ + public String getName() { + return name; + } + + /** + * Get the weight of the animal. + * + * @return the weight of the animal + */ + public double getWeight() { + return weight; + } + + /** + * Set the weight of the animal. + * + * @param weight + * the weight of the animal + */ + public void setWeight(double weight) { + this.weight = weight; + } + + /** + * Test + */ + public static void testAnimal() { + Animal animal = new Animal("cat", 1.0); + System.out.println(animal.getName() == "cat"); + System.out.println(animal.getWeight() == 1.0); + animal.setWeight(2.0); + System.out.println(animal.getName() == "cat"); + System.out.println(animal.getWeight() == 2.0); + } +} + diff --git a/lab/lab5/Question4/Cat.java b/lab/lab5/Question4/Cat.java new file mode 100644 index 0000000..487688f --- /dev/null +++ b/lab/lab5/Question4/Cat.java @@ -0,0 +1,35 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: Cat class + */ + +public class Cat extends Animal { + /** + * Constructor. Initialize the name and weight of the cat + */ + public Cat(String name, double weight) { + super(name, weight); + } + + /** + * Feeding a cat adds 1.0 to its weight + */ + public void feed() { + setWeight(getWeight() + 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); + } +} diff --git a/lab/lab5/Question4/Dog.java b/lab/lab5/Question4/Dog.java new file mode 100644 index 0000000..0ac9628 --- /dev/null +++ b/lab/lab5/Question4/Dog.java @@ -0,0 +1,34 @@ +/* + * 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); + } +} diff --git a/lab/lab5/Question4/Start.java b/lab/lab5/Question4/Start.java new file mode 100644 index 0000000..b7d1a17 --- /dev/null +++ b/lab/lab5/Question4/Start.java @@ -0,0 +1,27 @@ +/** + * Answer to question. + * + * # Question 4 + * + * Q: Now that the Student class uses an animal as pet, can you still use a cat + * object as the pet of a student? Why or why not? + * + * A: Yes, you can. The Student class has a method getPet() that returns the pet + * which is Animal type. + * + * Q: Can you now use a dog object as the pet of a student? Why or why not? + * + * A: Yes, you can. But only attributes and methods definded in the Animal class + * can be accessed. When you do so, the Dog object will be converted to an + * Animal object. Assign a child object to a parent object is called upcasting + * in Java. Upcasting is implicit. + * + */ +public class Start { + public static void main(String[] args) { + Cat.testCat(); + Dog.testDog(); + Student.testStudent(); + Animal.testAnimal(); + } +} diff --git a/lab/lab5/Question4/Student.java b/lab/lab5/Question4/Student.java new file mode 100644 index 0000000..8e4cfb2 --- /dev/null +++ b/lab/lab5/Question4/Student.java @@ -0,0 +1,69 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: Student class + */ + +public class Student { + /** + * Student's name + */ + private String name; + + /** + * Student's pet + */ + private Animal pet; + + /** + * Constructor + */ + public Student(String name, Animal pet) { + this.name = name; + this.pet = pet; + } + + /** + * Getter for name + * + * @return String + */ + public String getName() { + return name; + } + + /** + * Getter for pet + * + * @return Animal + */ + public Animal getPet() { + return pet; + } + + /** + * Test + */ + public static void testStudent() { + Cat pet = new Cat("Tom", 1.0); + Student student = new Student("Walter", pet); + System.out.println(student.getName() == "Walter"); + System.out.println(student.getPet() == pet); + System.out.println(student.getPet().getName() == "Tom"); + System.out.println(student.getPet().getWeight() == 1.0); + + Dog pet2 = new Dog("Jerry", 1.0); + Student student2 = new Student("Walter", pet2); + System.out.println(student2.getName() == "Walter"); + System.out.println(student2.getPet() == pet2); + System.out.println(student2.getPet().getName() == "Jerry"); + System.out.println(student2.getPet().getWeight() == 1.0); + + Animal pet3 = new Animal("Unknown animal", 4.0); + Student student3 = new Student("Walter", pet3); + System.out.println(student3.getName() == "Walter"); + System.out.println(student3.getPet() == pet3); + System.out.println(student3.getPet().getName() == "Unknown animal"); + System.out.println(student3.getPet().getWeight() == 4.0); + } +} diff --git a/lab/lab5/Question5/Animal.java b/lab/lab5/Question5/Animal.java new file mode 100644 index 0000000..5d49613 --- /dev/null +++ b/lab/lab5/Question5/Animal.java @@ -0,0 +1,66 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: This is the class of Animal. + */ + +public class Animal { + /** + * Animal's name + */ + private String name; + + /** + * Animal's weight + */ + private double weight; + + /** + * Constructor. Initialize the name and weight of the animal. + */ + public Animal(String name, double weight) { + this.name = name; + this.weight = weight; + } + + /** + * Get the name of the animal. + * + * @return the name of the animal + */ + public String getName() { + return name; + } + + /** + * Get the weight of the animal. + * + * @return the weight of the animal + */ + public double getWeight() { + return weight; + } + + /** + * Set the weight of the animal. + * + * @param weight + * the weight of the animal + */ + public void setWeight(double weight) { + this.weight = weight; + } + + /** + * Test + */ + public static void testAnimal() { + Animal animal = new Animal("cat", 1.0); + System.out.println(animal.getName() == "cat"); + System.out.println(animal.getWeight() == 1.0); + animal.setWeight(2.0); + System.out.println(animal.getName() == "cat"); + System.out.println(animal.getWeight() == 2.0); + } +} + diff --git a/lab/lab5/Question5/Bird.java b/lab/lab5/Question5/Bird.java new file mode 100644 index 0000000..caff8b4 --- /dev/null +++ b/lab/lab5/Question5/Bird.java @@ -0,0 +1,43 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: This is the class of Bird. + */ + +public class Bird extends Animal { + /** + * Altitude of the bird. + */ + private double altitude; + + /** + * Constructor of Bird. + * + * @param name name of the bird + * @param weight weight of the bird + * @param altitude altitude of the bird + */ + public Bird(String name, double weight, double altitude) { + super(name, weight); + this.altitude = altitude; + } + + /** + * Get the altitude of the bird. + * + * @return altitude of the bird + */ + public double getAltitude() { + return altitude; + } + + /** + * Test + */ + public static void testBird() { + Bird bird = new Bird("Bird", 1.0, 10.0); + System.out.println(bird.getName() == "Bird"); + System.out.println(bird.getWeight() == 1.0); + System.out.println(bird.getAltitude() == 10.0); + } +} diff --git a/lab/lab5/Question5/Cat.java b/lab/lab5/Question5/Cat.java new file mode 100644 index 0000000..487688f --- /dev/null +++ b/lab/lab5/Question5/Cat.java @@ -0,0 +1,35 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: Cat class + */ + +public class Cat extends Animal { + /** + * Constructor. Initialize the name and weight of the cat + */ + public Cat(String name, double weight) { + super(name, weight); + } + + /** + * Feeding a cat adds 1.0 to its weight + */ + public void feed() { + setWeight(getWeight() + 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); + } +} diff --git a/lab/lab5/Question5/Dog.java b/lab/lab5/Question5/Dog.java new file mode 100644 index 0000000..0ac9628 --- /dev/null +++ b/lab/lab5/Question5/Dog.java @@ -0,0 +1,34 @@ +/* + * 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); + } +} diff --git a/lab/lab5/Question5/Start.java b/lab/lab5/Question5/Start.java new file mode 100644 index 0000000..f43ae81 --- /dev/null +++ b/lab/lab5/Question5/Start.java @@ -0,0 +1,43 @@ +/** + * Answer to question. + * + * # Question 4 + * + * Q: Now that the Student class uses an animal as pet, can you still use a cat + * object as the pet of a student? Why or why not? + * + * A: Yes, you can. The Student class has a method getPet() that returns the pet + * which is Animal type. + * + * Q: Can you now use a dog object as the pet of a student? Why or why not? + * + * A: Yes, you can. But only attributes and methods definded in the Animal class + * can be accessed. When you do so, the Dog object will be converted to an + * Animal object. Assign a child object to a parent object is called upcasting + * in Java. Upcasting is implicit. + * + * # Question 5 + * + * Q: Where are the name and weight of the bird stored? How? + * + * A: The Bird class has a private instance variable name and weight which are + * inherited from the Animal class. + * + * Q: Suppose a student has a bird as a pet. Can the student get the altitude of + * his pet? + * + * A: No. The student's pet is an Animal object. The Bird object is a subclass + * of Animal. getAltitude is only avaliable in Animal object. When we assign a + * Bird to an Animal variable, upcasting happend and only attributes and methods + * in the super class can be accessed. + * + */ +public class Start { + public static void main(String[] args) { + Cat.testCat(); + Dog.testDog(); + Student.testStudent(); + Animal.testAnimal(); + Bird.testBird(); + } +} diff --git a/lab/lab5/Question5/Student.java b/lab/lab5/Question5/Student.java new file mode 100644 index 0000000..9b03c56 --- /dev/null +++ b/lab/lab5/Question5/Student.java @@ -0,0 +1,76 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: Student class + */ + +public class Student { + /** + * Student's name + */ + private String name; + + /** + * Student's pet + */ + private Animal pet; + + /** + * Constructor + */ + public Student(String name, Animal pet) { + this.name = name; + this.pet = pet; + } + + /** + * Getter for name + * + * @return String + */ + public String getName() { + return name; + } + + /** + * Getter for pet + * + * @return Animal + */ + public Animal getPet() { + return pet; + } + + /** + * Test + */ + public static void testStudent() { + Cat pet = new Cat("Tom", 1.0); + Student student = new Student("Walter", pet); + System.out.println(student.getName() == "Walter"); + System.out.println(student.getPet() == pet); + System.out.println(student.getPet().getName() == "Tom"); + System.out.println(student.getPet().getWeight() == 1.0); + + Dog pet2 = new Dog("Jerry", 1.0); + Student student2 = new Student("Walter", pet2); + System.out.println(student2.getName() == "Walter"); + System.out.println(student2.getPet() == pet2); + System.out.println(student2.getPet().getName() == "Jerry"); + System.out.println(student2.getPet().getWeight() == 1.0); + + Animal pet3 = new Animal("Unknown animal", 4.0); + Student student3 = new Student("Walter", pet3); + System.out.println(student3.getName() == "Walter"); + System.out.println(student3.getPet() == pet3); + System.out.println(student3.getPet().getName() == "Unknown animal"); + System.out.println(student3.getPet().getWeight() == 4.0); + + Bird pet4 = new Bird("bird", 1.0, 39.0); + Student student4 = new Student("Walter", pet4); + System.out.println(student4.getName() == "Walter"); + System.out.println(student4.getPet() == pet4); + System.out.println(student4.getPet().getName() == "bird"); + System.out.println(student4.getPet().getWeight() == 1.0); + } +} diff --git a/lab/lab5/Question6/Animal.java b/lab/lab5/Question6/Animal.java new file mode 100644 index 0000000..5d49613 --- /dev/null +++ b/lab/lab5/Question6/Animal.java @@ -0,0 +1,66 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: This is the class of Animal. + */ + +public class Animal { + /** + * Animal's name + */ + private String name; + + /** + * Animal's weight + */ + private double weight; + + /** + * Constructor. Initialize the name and weight of the animal. + */ + public Animal(String name, double weight) { + this.name = name; + this.weight = weight; + } + + /** + * Get the name of the animal. + * + * @return the name of the animal + */ + public String getName() { + return name; + } + + /** + * Get the weight of the animal. + * + * @return the weight of the animal + */ + public double getWeight() { + return weight; + } + + /** + * Set the weight of the animal. + * + * @param weight + * the weight of the animal + */ + public void setWeight(double weight) { + this.weight = weight; + } + + /** + * Test + */ + public static void testAnimal() { + Animal animal = new Animal("cat", 1.0); + System.out.println(animal.getName() == "cat"); + System.out.println(animal.getWeight() == 1.0); + animal.setWeight(2.0); + System.out.println(animal.getName() == "cat"); + System.out.println(animal.getWeight() == 2.0); + } +} + diff --git a/lab/lab5/Question6/Bird.java b/lab/lab5/Question6/Bird.java new file mode 100644 index 0000000..caff8b4 --- /dev/null +++ b/lab/lab5/Question6/Bird.java @@ -0,0 +1,43 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: This is the class of Bird. + */ + +public class Bird extends Animal { + /** + * Altitude of the bird. + */ + private double altitude; + + /** + * Constructor of Bird. + * + * @param name name of the bird + * @param weight weight of the bird + * @param altitude altitude of the bird + */ + public Bird(String name, double weight, double altitude) { + super(name, weight); + this.altitude = altitude; + } + + /** + * Get the altitude of the bird. + * + * @return altitude of the bird + */ + public double getAltitude() { + return altitude; + } + + /** + * Test + */ + public static void testBird() { + Bird bird = new Bird("Bird", 1.0, 10.0); + System.out.println(bird.getName() == "Bird"); + System.out.println(bird.getWeight() == 1.0); + System.out.println(bird.getAltitude() == 10.0); + } +} diff --git a/lab/lab5/Question6/Cat.java b/lab/lab5/Question6/Cat.java new file mode 100644 index 0000000..487688f --- /dev/null +++ b/lab/lab5/Question6/Cat.java @@ -0,0 +1,35 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: Cat class + */ + +public class Cat extends Animal { + /** + * Constructor. Initialize the name and weight of the cat + */ + public Cat(String name, double weight) { + super(name, weight); + } + + /** + * Feeding a cat adds 1.0 to its weight + */ + public void feed() { + setWeight(getWeight() + 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); + } +} diff --git a/lab/lab5/Question6/Chicken.java b/lab/lab5/Question6/Chicken.java new file mode 100644 index 0000000..e4d2f0a --- /dev/null +++ b/lab/lab5/Question6/Chicken.java @@ -0,0 +1,28 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: This is the Chicken class. + */ + +public class Chicken extends Bird { + /** + * Constructor of Chicken class. A chicken always has a weight of 5.0 and an + * altitude of 0.0. Chicken spend all their time on the ground. + * + * @param name + */ + public Chicken(String name) { + super(name, 5.0, 0.0); + } + + /** + * Test. + */ + public static void testChicken() { + Chicken chk1 = new Chicken("chicken1"); + + System.out.println(chk1.getName() == "chicken1"); + System.out.println(chk1.getWeight() == 5.0); + System.out.println(chk1.getAltitude() == 0.0); + } +} diff --git a/lab/lab5/Question6/Dog.java b/lab/lab5/Question6/Dog.java new file mode 100644 index 0000000..0ac9628 --- /dev/null +++ b/lab/lab5/Question6/Dog.java @@ -0,0 +1,34 @@ +/* + * 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); + } +} diff --git a/lab/lab5/Question6/Start.java b/lab/lab5/Question6/Start.java new file mode 100644 index 0000000..7630f2f --- /dev/null +++ b/lab/lab5/Question6/Start.java @@ -0,0 +1,44 @@ +/** + * Answer to question. + * + * # Question 4 + * + * Q: Now that the Student class uses an animal as pet, can you still use a cat + * object as the pet of a student? Why or why not? + * + * A: Yes, you can. The Student class has a method getPet() that returns the pet + * which is Animal type. + * + * Q: Can you now use a dog object as the pet of a student? Why or why not? + * + * A: Yes, you can. But only attributes and methods definded in the Animal class + * can be accessed. When you do so, the Dog object will be converted to an + * Animal object. Assign a child object to a parent object is called upcasting + * in Java. Upcasting is implicit. + * + * # Question 5 + * + * Q: Where are the name and weight of the bird stored? How? + * + * A: The Bird class has a private instance variable name and weight which are + * inherited from the Animal class. + * + * Q: Suppose a student has a bird as a pet. Can the student get the altitude of + * his pet? + * + * A: No. The student's pet is an Animal object. The Bird object is a subclass + * of Animal. getAltitude is only avaliable in Animal object. When we assign a + * Bird to an Animal variable, upcasting happend and only attributes and methods + * in the super class can be accessed. + * + */ +public class Start { + public static void main(String[] args) { + Cat.testCat(); + Dog.testDog(); + Student.testStudent(); + Animal.testAnimal(); + Bird.testBird(); + Chicken.testChicken(); + } +} diff --git a/lab/lab5/Question6/Student.java b/lab/lab5/Question6/Student.java new file mode 100644 index 0000000..4ed8ce4 --- /dev/null +++ b/lab/lab5/Question6/Student.java @@ -0,0 +1,83 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: Student class + */ + +public class Student { + /** + * Student's name + */ + private String name; + + /** + * Student's pet + */ + private Animal pet; + + /** + * Constructor + */ + public Student(String name, Animal pet) { + this.name = name; + this.pet = pet; + } + + /** + * Getter for name + * + * @return String + */ + public String getName() { + return name; + } + + /** + * Getter for pet + * + * @return Animal + */ + public Animal getPet() { + return pet; + } + + /** + * Test + */ + public static void testStudent() { + Cat pet = new Cat("Tom", 1.0); + Student student = new Student("Walter", pet); + System.out.println(student.getName() == "Walter"); + System.out.println(student.getPet() == pet); + System.out.println(student.getPet().getName() == "Tom"); + System.out.println(student.getPet().getWeight() == 1.0); + + Dog pet2 = new Dog("Jerry", 1.0); + Student student2 = new Student("Walter", pet2); + System.out.println(student2.getName() == "Walter"); + System.out.println(student2.getPet() == pet2); + System.out.println(student2.getPet().getName() == "Jerry"); + System.out.println(student2.getPet().getWeight() == 1.0); + + Animal pet3 = new Animal("Unknown animal", 4.0); + Student student3 = new Student("Walter", pet3); + System.out.println(student3.getName() == "Walter"); + System.out.println(student3.getPet() == pet3); + System.out.println(student3.getPet().getName() == "Unknown animal"); + System.out.println(student3.getPet().getWeight() == 4.0); + + Bird pet4 = new Bird("bird", 1.0, 39.0); + Student student4 = new Student("Walter", pet4); + System.out.println(student4.getName() == "Walter"); + System.out.println(student4.getPet() == pet4); + System.out.println(student4.getPet().getName() == "bird"); + System.out.println(student4.getPet().getWeight() == 1.0); + + Chicken pet5 = new Chicken("chicken"); + Student student5 = new Student("Walter", pet5); + System.out.println(student5.getName() == "Walter"); + System.out.println(student5.getPet() == pet5); + System.out.println(student5.getPet().getName() == "chicken"); + System.out.println(student5.getPet().getWeight() == 5.0); + } +} diff --git a/lab/lab5/Question7/Animal.java b/lab/lab5/Question7/Animal.java new file mode 100644 index 0000000..d5b4c56 --- /dev/null +++ b/lab/lab5/Question7/Animal.java @@ -0,0 +1,52 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: This is the class of Animal. + */ + +public class Animal extends LivingThing { + /** + * Animal's weight + */ + private double weight; + + /** + * Constructor. Initialize the name and weight of the animal. + */ + public Animal(String name, double weight) { + super(name); + this.weight = weight; + } + + /** + * Get the weight of the animal. + * + * @return the weight of the animal + */ + public double getWeight() { + return weight; + } + + /** + * Set the weight of the animal. + * + * @param weight + * the weight of the animal + */ + public void setWeight(double weight) { + this.weight = weight; + } + + /** + * Test + */ + public static void testAnimal() { + Animal animal = new Animal("cat", 1.0); + System.out.println(animal.getName() == "cat"); + System.out.println(animal.getWeight() == 1.0); + animal.setWeight(2.0); + System.out.println(animal.getName() == "cat"); + System.out.println(animal.getWeight() == 2.0); + } +} + diff --git a/lab/lab5/Question7/Bird.java b/lab/lab5/Question7/Bird.java new file mode 100644 index 0000000..caff8b4 --- /dev/null +++ b/lab/lab5/Question7/Bird.java @@ -0,0 +1,43 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: This is the class of Bird. + */ + +public class Bird extends Animal { + /** + * Altitude of the bird. + */ + private double altitude; + + /** + * Constructor of Bird. + * + * @param name name of the bird + * @param weight weight of the bird + * @param altitude altitude of the bird + */ + public Bird(String name, double weight, double altitude) { + super(name, weight); + this.altitude = altitude; + } + + /** + * Get the altitude of the bird. + * + * @return altitude of the bird + */ + public double getAltitude() { + return altitude; + } + + /** + * Test + */ + public static void testBird() { + Bird bird = new Bird("Bird", 1.0, 10.0); + System.out.println(bird.getName() == "Bird"); + System.out.println(bird.getWeight() == 1.0); + System.out.println(bird.getAltitude() == 10.0); + } +} diff --git a/lab/lab5/Question7/Cat.java b/lab/lab5/Question7/Cat.java new file mode 100644 index 0000000..487688f --- /dev/null +++ b/lab/lab5/Question7/Cat.java @@ -0,0 +1,35 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: Cat class + */ + +public class Cat extends Animal { + /** + * Constructor. Initialize the name and weight of the cat + */ + public Cat(String name, double weight) { + super(name, weight); + } + + /** + * Feeding a cat adds 1.0 to its weight + */ + public void feed() { + setWeight(getWeight() + 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); + } +} diff --git a/lab/lab5/Question7/Chicken.java b/lab/lab5/Question7/Chicken.java new file mode 100644 index 0000000..e4d2f0a --- /dev/null +++ b/lab/lab5/Question7/Chicken.java @@ -0,0 +1,28 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: This is the Chicken class. + */ + +public class Chicken extends Bird { + /** + * Constructor of Chicken class. A chicken always has a weight of 5.0 and an + * altitude of 0.0. Chicken spend all their time on the ground. + * + * @param name + */ + public Chicken(String name) { + super(name, 5.0, 0.0); + } + + /** + * Test. + */ + public static void testChicken() { + Chicken chk1 = new Chicken("chicken1"); + + System.out.println(chk1.getName() == "chicken1"); + System.out.println(chk1.getWeight() == 5.0); + System.out.println(chk1.getAltitude() == 0.0); + } +} diff --git a/lab/lab5/Question7/Dog.java b/lab/lab5/Question7/Dog.java new file mode 100644 index 0000000..0ac9628 --- /dev/null +++ b/lab/lab5/Question7/Dog.java @@ -0,0 +1,34 @@ +/* + * 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); + } +} diff --git a/lab/lab5/Question7/LivingThing.java b/lab/lab5/Question7/LivingThing.java new file mode 100644 index 0000000..232e5b9 --- /dev/null +++ b/lab/lab5/Question7/LivingThing.java @@ -0,0 +1,38 @@ +/* + * AutoRobot: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-19 + * Description: This is the class of LivingThing. + */ + +public class LivingThing { + /** + * Name of the LivingThing. + */ + private String name; + + /** + * Constructor of LivingThing. + * + * @param name Name of the LivingThing. + */ + public LivingThing(String name) { + this.name = name; + } + + /** + * Get the name of the LivingThing. + * + * @return The name of the LivingThing. + */ + public String getName() { + return name; + } + + /** + * Test. + */ + public static void testLiving() { + LivingThing living = new LivingThing("LivingThing"); + System.out.println(living.getName() == "LivingThing"); + } +} diff --git a/lab/lab5/Question7/Start.java b/lab/lab5/Question7/Start.java new file mode 100644 index 0000000..f1eea17 --- /dev/null +++ b/lab/lab5/Question7/Start.java @@ -0,0 +1,45 @@ +/** + * Answer to question. + * + * # Question 4 + * + * Q: Now that the Student class uses an animal as pet, can you still use a cat + * object as the pet of a student? Why or why not? + * + * A: Yes, you can. The Student class has a method getPet() that returns the pet + * which is Animal type. + * + * Q: Can you now use a dog object as the pet of a student? Why or why not? + * + * A: Yes, you can. But only attributes and methods definded in the Animal class + * can be accessed. When you do so, the Dog object will be converted to an + * Animal object. Assign a child object to a parent object is called upcasting + * in Java. Upcasting is implicit. + * + * # Question 5 + * + * Q: Where are the name and weight of the bird stored? How? + * + * A: The Bird class has a private instance variable name and weight which are + * inherited from the Animal class. + * + * Q: Suppose a student has a bird as a pet. Can the student get the altitude of + * his pet? + * + * A: No. The student's pet is an Animal object. The Bird object is a subclass + * of Animal. getAltitude is only avaliable in Animal object. When we assign a + * Bird to an Animal variable, upcasting happend and only attributes and methods + * in the super class can be accessed. + * + */ +public class Start { + public static void main(String[] args) { + Cat.testCat(); + Dog.testDog(); + Student.testStudent(); + Animal.testAnimal(); + Bird.testBird(); + Chicken.testChicken(); + LivingThing.testLiving(); + } +} diff --git a/lab/lab5/Question7/Student.java b/lab/lab5/Question7/Student.java new file mode 100644 index 0000000..28a6e11 --- /dev/null +++ b/lab/lab5/Question7/Student.java @@ -0,0 +1,69 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/19 + * Description: Student class + */ + +public class Student extends LivingThing { + /** + * Student's pet + */ + private Animal pet; + + /** + * Constructor + */ + public Student(String name, Animal pet) { + super(name); + this.pet = pet; + } + + /** + * Getter for pet + * + * @return Animal + */ + public Animal getPet() { + return pet; + } + + /** + * Test + */ + public static void testStudent() { + Cat pet = new Cat("Tom", 1.0); + Student student = new Student("Walter", pet); + System.out.println(student.getName() == "Walter"); + System.out.println(student.getPet() == pet); + System.out.println(student.getPet().getName() == "Tom"); + System.out.println(student.getPet().getWeight() == 1.0); + + Dog pet2 = new Dog("Jerry", 1.0); + Student student2 = new Student("Walter", pet2); + System.out.println(student2.getName() == "Walter"); + System.out.println(student2.getPet() == pet2); + System.out.println(student2.getPet().getName() == "Jerry"); + System.out.println(student2.getPet().getWeight() == 1.0); + + Animal pet3 = new Animal("Unknown animal", 4.0); + Student student3 = new Student("Walter", pet3); + System.out.println(student3.getName() == "Walter"); + System.out.println(student3.getPet() == pet3); + System.out.println(student3.getPet().getName() == "Unknown animal"); + System.out.println(student3.getPet().getWeight() == 4.0); + + Bird pet4 = new Bird("bird", 1.0, 39.0); + Student student4 = new Student("Walter", pet4); + System.out.println(student4.getName() == "Walter"); + System.out.println(student4.getPet() == pet4); + System.out.println(student4.getPet().getName() == "bird"); + System.out.println(student4.getPet().getWeight() == 1.0); + + Chicken pet5 = new Chicken("chicken"); + Student student5 = new Student("Walter", pet5); + System.out.println(student5.getName() == "Walter"); + System.out.println(student5.getPet() == pet5); + System.out.println(student5.getPet().getName() == "chicken"); + System.out.println(student5.getPet().getWeight() == 5.0); + } +}