Restore
This commit is contained in:
10
Labs/Lab5/Lab5_2230026071/Question5/.classpath
Normal file
10
Labs/Lab5/Lab5_2230026071/Question5/.classpath
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Labs/Lab5/Lab5_2230026071/Question5/.project
Normal file
17
Labs/Lab5/Lab5_2230026071/Question5/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question5</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Animal.class
Normal file
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Animal.class
Normal file
Binary file not shown.
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Bird.class
Normal file
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Bird.class
Normal file
Binary file not shown.
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Cat.class
Normal file
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Cat.class
Normal file
Binary file not shown.
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Dog.class
Normal file
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Dog.class
Normal file
Binary file not shown.
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Start.class
Normal file
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Start.class
Normal file
Binary file not shown.
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Student.class
Normal file
BIN
Labs/Lab5/Lab5_2230026071/Question5/bin/Student.class
Normal file
Binary file not shown.
31
Labs/Lab5/Lab5_2230026071/Question5/src/Animal.java
Normal file
31
Labs/Lab5/Lab5_2230026071/Question5/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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
26
Labs/Lab5/Lab5_2230026071/Question5/src/Bird.java
Normal file
26
Labs/Lab5/Lab5_2230026071/Question5/src/Bird.java
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
public class Bird extends Animal{
|
||||
//instance variable
|
||||
private double altitude;
|
||||
|
||||
//the name and weight are stored in Bird class as well
|
||||
//constructor
|
||||
public Bird (String name, double weight, double altitude) {
|
||||
super(name, weight);
|
||||
this.altitude = altitude;
|
||||
}
|
||||
|
||||
//method
|
||||
public double getAltitude() {return this.altitude;}
|
||||
|
||||
//test
|
||||
public static void testBird() {
|
||||
Bird b = new Bird("Twitter", 0.5, 200.5);
|
||||
// The methods getName and getWeight are inherited from Animal.
|
||||
// The method getAltitude comes from the Bird class itself.
|
||||
System.out.println(b.getName() == "Twitter");
|
||||
System.out.println(b.getWeight() == 0.5);
|
||||
System.out.println(b.getAltitude() == 200.5);
|
||||
}
|
||||
|
||||
}
|
||||
27
Labs/Lab5/Lab5_2230026071/Question5/src/Cat.java
Normal file
27
Labs/Lab5/Lab5_2230026071/Question5/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/Question5/src/Dog.java
Normal file
27
Labs/Lab5/Lab5_2230026071/Question5/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);
|
||||
}
|
||||
|
||||
}
|
||||
42
Labs/Lab5/Lab5_2230026071/Question5/src/Start.java
Normal file
42
Labs/Lab5/Lab5_2230026071/Question5/src/Start.java
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
//I suppose the student can get the altitude of his/her bird
|
||||
public static void main(String[] args) {
|
||||
// Unit tests
|
||||
// !!! Codes here written by your self !!!
|
||||
// Partial codes of System tests, to test both classes together.
|
||||
// Creating a new student with a cat as pet:
|
||||
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);
|
||||
Bird b = new Bird("Twitter", 0.5, 200.5);
|
||||
Student s3 = new Student("Mr. Wang", b);
|
||||
System.out.println(s3.getPet().getName() == "Twitter");
|
||||
System.out.println(s3.getPet().getWeight() == 0.5);
|
||||
s3.getPet().setWeight(3.0);
|
||||
System.out.println(s3.getPet().getName() == "Twitter");
|
||||
System.out.println(s3.getPet().getWeight() == 3.0);
|
||||
}
|
||||
|
||||
}
|
||||
37
Labs/Lab5/Lab5_2230026071/Question5/src/Student.java
Normal file
37
Labs/Lab5/Lab5_2230026071/Question5/src/Student.java
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
private Animal pet;
|
||||
|
||||
public Student(String name, Animal pet) {
|
||||
this.name = name;
|
||||
this.pet = pet;
|
||||
}
|
||||
|
||||
public String getName() {return this.name;}
|
||||
|
||||
public Animal getPet() {return this.pet;}
|
||||
|
||||
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);
|
||||
// We can now also use a Bird as a pet:
|
||||
Bird b = new Bird("Twitter", 0.5, 200.5);
|
||||
Student s3 = new Student("Mr. Wang", b);
|
||||
System.out.println(s3.getName() == "Mr. Wang");
|
||||
System.out.println(s3.getPet() == b);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user