lab5
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user