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