lab5
This commit is contained in:
64
lab/lab5/Question1/Cat.java
Normal file
64
lab/lab5/Question1/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);
|
||||
}
|
||||
}
|
||||
5
lab/lab5/Question1/Start.java
Normal file
5
lab/lab5/Question1/Start.java
Normal file
@@ -0,0 +1,5 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Cat.testCat();
|
||||
}
|
||||
}
|
||||
64
lab/lab5/Question2/Cat.java
Normal file
64
lab/lab5/Question2/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);
|
||||
}
|
||||
}
|
||||
63
lab/lab5/Question2/Dog.java
Normal file
63
lab/lab5/Question2/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);
|
||||
}
|
||||
}
|
||||
6
lab/lab5/Question2/Start.java
Normal file
6
lab/lab5/Question2/Start.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Cat.testCat();
|
||||
Dog.testDog();
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
7
lab/lab5/Question3/Start.java
Normal file
7
lab/lab5/Question3/Start.java
Normal file
@@ -0,0 +1,7 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Cat.testCat();
|
||||
Dog.testDog();
|
||||
Student.testStudent();
|
||||
}
|
||||
}
|
||||
56
lab/lab5/Question3/Student.java
Normal file
56
lab/lab5/Question3/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
66
lab/lab5/Question4/Animal.java
Normal file
66
lab/lab5/Question4/Animal.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
35
lab/lab5/Question4/Cat.java
Normal file
35
lab/lab5/Question4/Cat.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
34
lab/lab5/Question4/Dog.java
Normal file
34
lab/lab5/Question4/Dog.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
27
lab/lab5/Question4/Start.java
Normal file
27
lab/lab5/Question4/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
69
lab/lab5/Question4/Student.java
Normal file
69
lab/lab5/Question4/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
66
lab/lab5/Question5/Animal.java
Normal file
66
lab/lab5/Question5/Animal.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
43
lab/lab5/Question5/Bird.java
Normal file
43
lab/lab5/Question5/Bird.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
35
lab/lab5/Question5/Cat.java
Normal file
35
lab/lab5/Question5/Cat.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
34
lab/lab5/Question5/Dog.java
Normal file
34
lab/lab5/Question5/Dog.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
43
lab/lab5/Question5/Start.java
Normal file
43
lab/lab5/Question5/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
76
lab/lab5/Question5/Student.java
Normal file
76
lab/lab5/Question5/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
66
lab/lab5/Question6/Animal.java
Normal file
66
lab/lab5/Question6/Animal.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
43
lab/lab5/Question6/Bird.java
Normal file
43
lab/lab5/Question6/Bird.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
35
lab/lab5/Question6/Cat.java
Normal file
35
lab/lab5/Question6/Cat.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
28
lab/lab5/Question6/Chicken.java
Normal file
28
lab/lab5/Question6/Chicken.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
34
lab/lab5/Question6/Dog.java
Normal file
34
lab/lab5/Question6/Dog.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
44
lab/lab5/Question6/Start.java
Normal file
44
lab/lab5/Question6/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
83
lab/lab5/Question6/Student.java
Normal file
83
lab/lab5/Question6/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
52
lab/lab5/Question7/Animal.java
Normal file
52
lab/lab5/Question7/Animal.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
43
lab/lab5/Question7/Bird.java
Normal file
43
lab/lab5/Question7/Bird.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
35
lab/lab5/Question7/Cat.java
Normal file
35
lab/lab5/Question7/Cat.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
28
lab/lab5/Question7/Chicken.java
Normal file
28
lab/lab5/Question7/Chicken.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
34
lab/lab5/Question7/Dog.java
Normal file
34
lab/lab5/Question7/Dog.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
38
lab/lab5/Question7/LivingThing.java
Normal file
38
lab/lab5/Question7/LivingThing.java
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
45
lab/lab5/Question7/Start.java
Normal file
45
lab/lab5/Question7/Start.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
69
lab/lab5/Question7/Student.java
Normal file
69
lab/lab5/Question7/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user