Restore
This commit is contained in:
31
Labs/Lab5/Lab5_2230026071/Question4/src/Animal.java
Normal file
31
Labs/Lab5/Lab5_2230026071/Question4/src/Animal.java
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
public class Animal {
|
||||
//instance variables
|
||||
private String name;
|
||||
private double weight;
|
||||
|
||||
//constructor
|
||||
public Animal (String name, double weight) {
|
||||
this.name = name;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
//methods
|
||||
public String getName() {return this.name;}
|
||||
|
||||
public double getWeight() {return this.weight;}
|
||||
|
||||
public void setWeight (double weight) {this.weight = weight;}
|
||||
|
||||
//test
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Blob", 2.0);
|
||||
System.out.println(a.getName() == "Blob");
|
||||
System.out.println(a.getWeight() == 2.0);
|
||||
a.setWeight(3.0);
|
||||
System.out.println(a.getName() == "Blob");
|
||||
System.out.println(a.getWeight() == 3.0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
27
Labs/Lab5/Lab5_2230026071/Question4/src/Cat.java
Normal file
27
Labs/Lab5/Lab5_2230026071/Question4/src/Cat.java
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
public class Cat extends Animal {
|
||||
//constructor
|
||||
public Cat (String name, double weight) {
|
||||
super(name, weight);
|
||||
}
|
||||
|
||||
//method
|
||||
public void feed() {super.setWeight(getWeight() + 1.0);}
|
||||
|
||||
//test
|
||||
public static void testCat() {
|
||||
Cat c = new Cat("Meow", 2.0);
|
||||
// The getName and getWeight methods are inherited from Animal.
|
||||
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);
|
||||
// The setWeight method is inherited too.
|
||||
c.setWeight(2.0);
|
||||
System.out.println(c.getName() == "Meow");
|
||||
System.out.println(c.getWeight() == 2.0);
|
||||
}
|
||||
|
||||
}
|
||||
27
Labs/Lab5/Lab5_2230026071/Question4/src/Dog.java
Normal file
27
Labs/Lab5/Lab5_2230026071/Question4/src/Dog.java
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
public class Dog extends Animal {
|
||||
//constructor
|
||||
public Dog (String name, double weight) {
|
||||
super(name, weight);
|
||||
}
|
||||
|
||||
//method
|
||||
public void feed() {super.setWeight(getWeight() + 2.0);}
|
||||
|
||||
//test
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Woof", 2.0);
|
||||
// The getName and getWeight methods are inherited from Animal.
|
||||
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);
|
||||
// The setWeight method is inherited too.
|
||||
d.setWeight(2.0);
|
||||
System.out.println(d.getName() == "Woof");
|
||||
System.out.println(d.getWeight() == 2.0);
|
||||
}
|
||||
|
||||
}
|
||||
34
Labs/Lab5/Lab5_2230026071/Question4/src/Start.java
Normal file
34
Labs/Lab5/Lab5_2230026071/Question4/src/Start.java
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
//no, because cat inherits animal and the student's pet type are undetermined
|
||||
public static void main(String[] args) {
|
||||
// Unit tests
|
||||
// !!! Codes here written by your self !!!
|
||||
// Partial codes of System tests, to test both classes together.
|
||||
Cat c = new Cat("Meow", 2.0);
|
||||
Student s0 = new Student("Philippe", c);
|
||||
System.out.println(s0.getPet().getName() == "Meow");
|
||||
System.out.println(s0.getPet().getWeight() == 2.0);
|
||||
s0.getPet().setWeight(3.0);
|
||||
System.out.println(s0.getPet().getName() == "Meow");
|
||||
System.out.println(s0.getPet().getWeight() == 3.0);
|
||||
// Creating a new student with a dog as pet:
|
||||
Dog d = new Dog("Woof", 2.0);
|
||||
Student s1 = new Student("Mr. Li", d);
|
||||
System.out.println(s1.getPet().getName() == "Woof");
|
||||
System.out.println(s1.getPet().getWeight() == 2.0);
|
||||
s1.getPet().setWeight(3.0);
|
||||
System.out.println(s1.getPet().getName() == "Woof");
|
||||
System.out.println(s1.getPet().getWeight() == 3.0);
|
||||
// We can now also use an Animal object as a pet:
|
||||
Animal a = new Animal("Blob", 5.0);
|
||||
Student s2 = new Student("Ms. Chen", a);
|
||||
System.out.println(s2.getPet().getName() == "Blob");
|
||||
System.out.println(s2.getPet().getWeight() == 5.0);
|
||||
s2.getPet().setWeight(3.0);
|
||||
System.out.println(s2.getPet().getName() == "Blob");
|
||||
System.out.println(s2.getPet().getWeight() == 3.0);
|
||||
}
|
||||
|
||||
}
|
||||
36
Labs/Lab5/Lab5_2230026071/Question4/src/Student.java
Normal file
36
Labs/Lab5/Lab5_2230026071/Question4/src/Student.java
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
public class Student {
|
||||
//instance variables
|
||||
private String name;
|
||||
private Animal pet;
|
||||
|
||||
//constructor
|
||||
public Student(String name, Animal pet) {
|
||||
this.name = name;
|
||||
this.pet = pet;
|
||||
}
|
||||
|
||||
//methods
|
||||
public String getName() {return this.name;}
|
||||
|
||||
public Animal getPet() {return this.pet;}
|
||||
|
||||
//test
|
||||
public static void testStudent() {
|
||||
Cat c = new Cat("Meow", 2.0);
|
||||
Student s0 = new Student("Philippe", c);
|
||||
System.out.println(s0.getName() == "Philippe");
|
||||
System.out.println(s0.getPet() == c);
|
||||
// Creating a new student with a dog as pet:
|
||||
Dog d = new Dog("Woof", 2.0);
|
||||
Student s1 = new Student("Mr. Li", d);
|
||||
System.out.println(s1.getName() == "Mr. Li");
|
||||
System.out.println(s1.getPet() == d);
|
||||
// We can now also use an Animal object as a pet:
|
||||
Animal a = new Animal("Blob", 5.0);
|
||||
Student s2 = new Student("Ms. Chen", a);
|
||||
System.out.println(s2.getName() == "Ms. Chen");
|
||||
System.out.println(s2.getPet() == a);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user