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