Close: #1 Lab 7
This commit is contained in:
64
lab/lab7/Question1/Animal.java
Normal file
64
lab/lab7/Question1/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
31
lab/lab7/Question1/Start.java
Normal file
31
lab/lab7/Question1/Start.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
}
|
||||
}
|
||||
64
lab/lab7/Question2/Animal.java
Normal file
64
lab/lab7/Question2/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question2/Dog.java
Normal file
47
lab/lab7/Question2/Dog.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Dog. Dog has 4 legs and can not fly.
|
||||
*/
|
||||
|
||||
public class Dog extends Animal {
|
||||
/**
|
||||
* Constructor of Dog.
|
||||
*
|
||||
* @param name String
|
||||
*/
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog has 4 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog can not fly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Jack");
|
||||
System.out.println(d.getName() == "Jack");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
|
||||
32
lab/lab7/Question2/Start.java
Normal file
32
lab/lab7/Question2/Start.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
}
|
||||
}
|
||||
64
lab/lab7/Question3/Animal.java
Normal file
64
lab/lab7/Question3/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
75
lab/lab7/Question3/Bird.java
Normal file
75
lab/lab7/Question3/Bird.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
*/
|
||||
|
||||
public abstract class Bird extends Animal {
|
||||
/**
|
||||
* An integer indicate how many eggs the bird has.
|
||||
*/
|
||||
private int numOfEggs;
|
||||
|
||||
/**
|
||||
* A constructor of Bird.
|
||||
*
|
||||
* @param name The name of the bird.
|
||||
* @param numOfEggs The number of eggs the bird has.
|
||||
*/
|
||||
public Bird(String name, int numOfEggs) {
|
||||
super(name);
|
||||
this.numOfEggs = numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of numOfEggs.
|
||||
*
|
||||
* @return The number of eggs the bird has.
|
||||
*/
|
||||
public int getNumOfEggs() {
|
||||
return numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bird have 2 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method of canFly. Some birds can fly (for example magpies) and some
|
||||
* birds cannot fly (for example ostriches).
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testBird() {
|
||||
Bird b1 = new Bird("magpie", 1) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
System.out.println(b1.getName() == "magpie");
|
||||
System.out.println(b1.getNumOfEggs() == 1);
|
||||
System.out.println(b1.canFly() == true);
|
||||
System.out.println(b1.getLegs() == 2);
|
||||
|
||||
Bird b2 = new Bird("ostriches", 2) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(b2.getName() == "ostriches");
|
||||
System.out.println(b2.getNumOfEggs() == 2);
|
||||
System.out.println(b2.canFly() == false);
|
||||
System.out.println(b2.getLegs() == 2);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question3/Dog.java
Normal file
47
lab/lab7/Question3/Dog.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Dog. Dog has 4 legs and can not fly.
|
||||
*/
|
||||
|
||||
public class Dog extends Animal {
|
||||
/**
|
||||
* Constructor of Dog.
|
||||
*
|
||||
* @param name String
|
||||
*/
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog has 4 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog can not fly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Jack");
|
||||
System.out.println(d.getName() == "Jack");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
|
||||
33
lab/lab7/Question3/Start.java
Normal file
33
lab/lab7/Question3/Start.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
Bird.testBird();
|
||||
}
|
||||
}
|
||||
64
lab/lab7/Question4/Animal.java
Normal file
64
lab/lab7/Question4/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
75
lab/lab7/Question4/Bird.java
Normal file
75
lab/lab7/Question4/Bird.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
*/
|
||||
|
||||
public abstract class Bird extends Animal {
|
||||
/**
|
||||
* An integer indicate how many eggs the bird has.
|
||||
*/
|
||||
private int numOfEggs;
|
||||
|
||||
/**
|
||||
* A constructor of Bird.
|
||||
*
|
||||
* @param name The name of the bird.
|
||||
* @param numOfEggs The number of eggs the bird has.
|
||||
*/
|
||||
public Bird(String name, int numOfEggs) {
|
||||
super(name);
|
||||
this.numOfEggs = numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of numOfEggs.
|
||||
*
|
||||
* @return The number of eggs the bird has.
|
||||
*/
|
||||
public int getNumOfEggs() {
|
||||
return numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bird have 2 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method of canFly. Some birds can fly (for example magpies) and some
|
||||
* birds cannot fly (for example ostriches).
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testBird() {
|
||||
Bird b1 = new Bird("magpie", 1) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
System.out.println(b1.getName() == "magpie");
|
||||
System.out.println(b1.getNumOfEggs() == 1);
|
||||
System.out.println(b1.canFly() == true);
|
||||
System.out.println(b1.getLegs() == 2);
|
||||
|
||||
Bird b2 = new Bird("ostriches", 2) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(b2.getName() == "ostriches");
|
||||
System.out.println(b2.getNumOfEggs() == 2);
|
||||
System.out.println(b2.canFly() == false);
|
||||
System.out.println(b2.getLegs() == 2);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question4/Dog.java
Normal file
47
lab/lab7/Question4/Dog.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Dog. Dog has 4 legs and can not fly.
|
||||
*/
|
||||
|
||||
public class Dog extends Animal {
|
||||
/**
|
||||
* Constructor of Dog.
|
||||
*
|
||||
* @param name String
|
||||
*/
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog has 4 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog can not fly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Jack");
|
||||
System.out.println(d.getName() == "Jack");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
|
||||
38
lab/lab7/Question4/Magpie.java
Normal file
38
lab/lab7/Question4/Magpie.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Magpie, which is a child class of Brid.
|
||||
*/
|
||||
|
||||
public class Magpie extends Bird {
|
||||
/**
|
||||
* Constructor. Magpie always have 6 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Magpie(String name) {
|
||||
super(name, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magpie can fly.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMagpie() {
|
||||
Bird bird = new Magpie("Magpie");
|
||||
System.out.println(bird.getName() == "Magpie");
|
||||
System.out.println(bird.getNumOfEggs() == 6);
|
||||
System.out.println(bird.canFly() == true);
|
||||
System.out.println(bird.getLegs() == 2);
|
||||
|
||||
}
|
||||
}
|
||||
34
lab/lab7/Question4/Start.java
Normal file
34
lab/lab7/Question4/Start.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
Bird.testBird();
|
||||
Magpie.testMagpie();
|
||||
}
|
||||
}
|
||||
64
lab/lab7/Question5/Animal.java
Normal file
64
lab/lab7/Question5/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
75
lab/lab7/Question5/Bird.java
Normal file
75
lab/lab7/Question5/Bird.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
*/
|
||||
|
||||
public abstract class Bird extends Animal {
|
||||
/**
|
||||
* An integer indicate how many eggs the bird has.
|
||||
*/
|
||||
private int numOfEggs;
|
||||
|
||||
/**
|
||||
* A constructor of Bird.
|
||||
*
|
||||
* @param name The name of the bird.
|
||||
* @param numOfEggs The number of eggs the bird has.
|
||||
*/
|
||||
public Bird(String name, int numOfEggs) {
|
||||
super(name);
|
||||
this.numOfEggs = numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of numOfEggs.
|
||||
*
|
||||
* @return The number of eggs the bird has.
|
||||
*/
|
||||
public int getNumOfEggs() {
|
||||
return numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bird have 2 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method of canFly. Some birds can fly (for example magpies) and some
|
||||
* birds cannot fly (for example ostriches).
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testBird() {
|
||||
Bird b1 = new Bird("magpie", 1) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
System.out.println(b1.getName() == "magpie");
|
||||
System.out.println(b1.getNumOfEggs() == 1);
|
||||
System.out.println(b1.canFly() == true);
|
||||
System.out.println(b1.getLegs() == 2);
|
||||
|
||||
Bird b2 = new Bird("ostriches", 2) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(b2.getName() == "ostriches");
|
||||
System.out.println(b2.getNumOfEggs() == 2);
|
||||
System.out.println(b2.canFly() == false);
|
||||
System.out.println(b2.getLegs() == 2);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question5/Dog.java
Normal file
47
lab/lab7/Question5/Dog.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Dog. Dog has 4 legs and can not fly.
|
||||
*/
|
||||
|
||||
public class Dog extends Animal {
|
||||
/**
|
||||
* Constructor of Dog.
|
||||
*
|
||||
* @param name String
|
||||
*/
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog has 4 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog can not fly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Jack");
|
||||
System.out.println(d.getName() == "Jack");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
|
||||
38
lab/lab7/Question5/Magpie.java
Normal file
38
lab/lab7/Question5/Magpie.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Magpie, which is a child class of Brid.
|
||||
*/
|
||||
|
||||
public class Magpie extends Bird {
|
||||
/**
|
||||
* Constructor. Magpie always have 6 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Magpie(String name) {
|
||||
super(name, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magpie can fly.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMagpie() {
|
||||
Bird bird = new Magpie("Magpie");
|
||||
System.out.println(bird.getName() == "Magpie");
|
||||
System.out.println(bird.getNumOfEggs() == 6);
|
||||
System.out.println(bird.canFly() == true);
|
||||
System.out.println(bird.getLegs() == 2);
|
||||
|
||||
}
|
||||
}
|
||||
38
lab/lab7/Question5/Ostrich.java
Normal file
38
lab/lab7/Question5/Ostrich.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Ostrich, which is a child class of Bird.
|
||||
*/
|
||||
|
||||
public class Ostrich extends Bird {
|
||||
/**
|
||||
* Constructor. Ostrich always has 10 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Ostrich(String name) {
|
||||
super(name, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ostrich can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testOstrich() {
|
||||
Ostrich o1 = new Ostrich("Ostrich1");
|
||||
System.out.println(o1.canFly() == false);
|
||||
System.out.println(o1.getName() == "Ostrich1");
|
||||
System.out.println(o1.getNumOfEggs() == 10);
|
||||
System.out.println(o1.getLegs() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
35
lab/lab7/Question5/Start.java
Normal file
35
lab/lab7/Question5/Start.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
Bird.testBird();
|
||||
Magpie.testMagpie();
|
||||
Ostrich.testOstrich();
|
||||
}
|
||||
}
|
||||
64
lab/lab7/Question6/Animal.java
Normal file
64
lab/lab7/Question6/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
75
lab/lab7/Question6/Bird.java
Normal file
75
lab/lab7/Question6/Bird.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
*/
|
||||
|
||||
public abstract class Bird extends Animal {
|
||||
/**
|
||||
* An integer indicate how many eggs the bird has.
|
||||
*/
|
||||
private int numOfEggs;
|
||||
|
||||
/**
|
||||
* A constructor of Bird.
|
||||
*
|
||||
* @param name The name of the bird.
|
||||
* @param numOfEggs The number of eggs the bird has.
|
||||
*/
|
||||
public Bird(String name, int numOfEggs) {
|
||||
super(name);
|
||||
this.numOfEggs = numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of numOfEggs.
|
||||
*
|
||||
* @return The number of eggs the bird has.
|
||||
*/
|
||||
public int getNumOfEggs() {
|
||||
return numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bird have 2 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method of canFly. Some birds can fly (for example magpies) and some
|
||||
* birds cannot fly (for example ostriches).
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testBird() {
|
||||
Bird b1 = new Bird("magpie", 1) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
System.out.println(b1.getName() == "magpie");
|
||||
System.out.println(b1.getNumOfEggs() == 1);
|
||||
System.out.println(b1.canFly() == true);
|
||||
System.out.println(b1.getLegs() == 2);
|
||||
|
||||
Bird b2 = new Bird("ostriches", 2) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(b2.getName() == "ostriches");
|
||||
System.out.println(b2.getNumOfEggs() == 2);
|
||||
System.out.println(b2.canFly() == false);
|
||||
System.out.println(b2.getLegs() == 2);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question6/Dog.java
Normal file
47
lab/lab7/Question6/Dog.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Dog. Dog has 4 legs and can not fly.
|
||||
*/
|
||||
|
||||
public class Dog extends Animal {
|
||||
/**
|
||||
* Constructor of Dog.
|
||||
*
|
||||
* @param name String
|
||||
*/
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog has 4 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog can not fly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Jack");
|
||||
System.out.println(d.getName() == "Jack");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
|
||||
38
lab/lab7/Question6/Magpie.java
Normal file
38
lab/lab7/Question6/Magpie.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Magpie, which is a child class of Brid.
|
||||
*/
|
||||
|
||||
public class Magpie extends Bird {
|
||||
/**
|
||||
* Constructor. Magpie always have 6 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Magpie(String name) {
|
||||
super(name, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magpie can fly.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMagpie() {
|
||||
Bird bird = new Magpie("Magpie");
|
||||
System.out.println(bird.getName() == "Magpie");
|
||||
System.out.println(bird.getNumOfEggs() == 6);
|
||||
System.out.println(bird.canFly() == true);
|
||||
System.out.println(bird.getLegs() == 2);
|
||||
|
||||
}
|
||||
}
|
||||
38
lab/lab7/Question6/Ostrich.java
Normal file
38
lab/lab7/Question6/Ostrich.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Ostrich, which is a child class of Bird.
|
||||
*/
|
||||
|
||||
public class Ostrich extends Bird {
|
||||
/**
|
||||
* Constructor. Ostrich always has 10 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Ostrich(String name) {
|
||||
super(name, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ostrich can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testOstrich() {
|
||||
Ostrich o1 = new Ostrich("Ostrich1");
|
||||
System.out.println(o1.canFly() == false);
|
||||
System.out.println(o1.getName() == "Ostrich1");
|
||||
System.out.println(o1.getNumOfEggs() == 10);
|
||||
System.out.println(o1.getLegs() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
57
lab/lab7/Question6/Pegasus.java
Normal file
57
lab/lab7/Question6/Pegasus.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the Pegasus class which is a subclass of the Bird class.
|
||||
*/
|
||||
|
||||
public class Pegasus extends Bird {
|
||||
/**
|
||||
* Constructor. Pegasus do not lay eggs, so the eggCount is set to 0.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Pegasus(String name) {
|
||||
super(name, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasus have 4 legs.
|
||||
*
|
||||
* @return 4
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasi do not lay eggs. So the getNumOfEggs method returns 0. And print a
|
||||
* message to console.
|
||||
*/
|
||||
@Override
|
||||
public int getNumOfEggs() {
|
||||
System.out.println("Pegasi do not lay eggs!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasus can fly. So the getFlyingAbility method returns true.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testPegasus() {
|
||||
Pegasus p = new Pegasus("Pegasus");
|
||||
System.out.println(p.getName() == "Pegasus");
|
||||
System.out.println(p.getLegs() == 4);
|
||||
System.out.println(p.getNumOfEggs() == 0);
|
||||
System.out.println(p.canFly() == true);
|
||||
}
|
||||
}
|
||||
36
lab/lab7/Question6/Start.java
Normal file
36
lab/lab7/Question6/Start.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
Bird.testBird();
|
||||
Magpie.testMagpie();
|
||||
Ostrich.testOstrich();
|
||||
Pegasus.testPegasus();
|
||||
}
|
||||
}
|
||||
64
lab/lab7/Question7/Animal.java
Normal file
64
lab/lab7/Question7/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
76
lab/lab7/Question7/Bird.java
Normal file
76
lab/lab7/Question7/Bird.java
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
* The bird class implements the Flyer interface.
|
||||
*/
|
||||
|
||||
public abstract class Bird extends Animal implements Flyer {
|
||||
/**
|
||||
* An integer indicate how many eggs the bird has.
|
||||
*/
|
||||
private int numOfEggs;
|
||||
|
||||
/**
|
||||
* A constructor of Bird.
|
||||
*
|
||||
* @param name The name of the bird.
|
||||
* @param numOfEggs The number of eggs the bird has.
|
||||
*/
|
||||
public Bird(String name, int numOfEggs) {
|
||||
super(name);
|
||||
this.numOfEggs = numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of numOfEggs.
|
||||
*
|
||||
* @return The number of eggs the bird has.
|
||||
*/
|
||||
public int getNumOfEggs() {
|
||||
return numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bird have 2 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method of canFly. Some birds can fly (for example magpies) and some
|
||||
* birds cannot fly (for example ostriches).
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testBird() {
|
||||
Bird b1 = new Bird("magpie", 1) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
System.out.println(b1.getName() == "magpie");
|
||||
System.out.println(b1.getNumOfEggs() == 1);
|
||||
System.out.println(b1.canFly() == true);
|
||||
System.out.println(b1.getLegs() == 2);
|
||||
|
||||
Bird b2 = new Bird("ostriches", 2) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(b2.getName() == "ostriches");
|
||||
System.out.println(b2.getNumOfEggs() == 2);
|
||||
System.out.println(b2.canFly() == false);
|
||||
System.out.println(b2.getLegs() == 2);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question7/Dog.java
Normal file
47
lab/lab7/Question7/Dog.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Dog. Dog has 4 legs and can not fly.
|
||||
*/
|
||||
|
||||
public class Dog extends Animal {
|
||||
/**
|
||||
* Constructor of Dog.
|
||||
*
|
||||
* @param name String
|
||||
*/
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog has 4 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog can not fly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Jack");
|
||||
System.out.println(d.getName() == "Jack");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
|
||||
10
lab/lab7/Question7/Flyer.java
Normal file
10
lab/lab7/Question7/Flyer.java
Normal file
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the interface of Flyer.
|
||||
*/
|
||||
|
||||
public interface Flyer {
|
||||
public String getName();
|
||||
public boolean canFly();
|
||||
}
|
||||
38
lab/lab7/Question7/Magpie.java
Normal file
38
lab/lab7/Question7/Magpie.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Magpie, which is a child class of Brid.
|
||||
*/
|
||||
|
||||
public class Magpie extends Bird {
|
||||
/**
|
||||
* Constructor. Magpie always have 6 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Magpie(String name) {
|
||||
super(name, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magpie can fly.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMagpie() {
|
||||
Bird bird = new Magpie("Magpie");
|
||||
System.out.println(bird.getName() == "Magpie");
|
||||
System.out.println(bird.getNumOfEggs() == 6);
|
||||
System.out.println(bird.canFly() == true);
|
||||
System.out.println(bird.getLegs() == 2);
|
||||
|
||||
}
|
||||
}
|
||||
38
lab/lab7/Question7/Ostrich.java
Normal file
38
lab/lab7/Question7/Ostrich.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Ostrich, which is a child class of Bird.
|
||||
*/
|
||||
|
||||
public class Ostrich extends Bird {
|
||||
/**
|
||||
* Constructor. Ostrich always has 10 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Ostrich(String name) {
|
||||
super(name, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ostrich can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testOstrich() {
|
||||
Ostrich o1 = new Ostrich("Ostrich1");
|
||||
System.out.println(o1.canFly() == false);
|
||||
System.out.println(o1.getName() == "Ostrich1");
|
||||
System.out.println(o1.getNumOfEggs() == 10);
|
||||
System.out.println(o1.getLegs() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
57
lab/lab7/Question7/Pegasus.java
Normal file
57
lab/lab7/Question7/Pegasus.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the Pegasus class which is a subclass of the Bird class.
|
||||
*/
|
||||
|
||||
public class Pegasus extends Bird {
|
||||
/**
|
||||
* Constructor. Pegasus do not lay eggs, so the eggCount is set to 0.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Pegasus(String name) {
|
||||
super(name, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasus have 4 legs.
|
||||
*
|
||||
* @return 4
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasi do not lay eggs. So the getNumOfEggs method returns 0. And print a
|
||||
* message to console.
|
||||
*/
|
||||
@Override
|
||||
public int getNumOfEggs() {
|
||||
System.out.println("Pegasi do not lay eggs!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasus can fly. So the getFlyingAbility method returns true.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testPegasus() {
|
||||
Pegasus p = new Pegasus("Pegasus");
|
||||
System.out.println(p.getName() == "Pegasus");
|
||||
System.out.println(p.getLegs() == 4);
|
||||
System.out.println(p.getNumOfEggs() == 0);
|
||||
System.out.println(p.canFly() == true);
|
||||
}
|
||||
}
|
||||
56
lab/lab7/Question7/Start.java
Normal file
56
lab/lab7/Question7/Start.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*
|
||||
* - Q: Can you test object from the Animal, Dog, and Bird classes through the
|
||||
* Flyer interface? Why or Why not?
|
||||
*
|
||||
* - A: No. Because the they didn't implement the Flyer interface. For Animal
|
||||
* class, you can't because the canFly() method is abstract. For Dog class, you
|
||||
* can't because it didn't declare it has implement the Flyer interface. For
|
||||
* Bird class, you can't because the canFly() method is abstract.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
Bird.testBird();
|
||||
Magpie.testMagpie();
|
||||
Ostrich.testOstrich();
|
||||
Pegasus.testPegasus();
|
||||
|
||||
Flyer magpie = new Magpie("Mumomomo");
|
||||
System.out.println(magpie.getName() == "Mumomomo");
|
||||
System.out.println(magpie.canFly() == true);
|
||||
|
||||
Flyer ostrich = new Ostrich("Ohheyeyeye");
|
||||
System.out.println(ostrich.getName() == "Ohheyeyeye");
|
||||
System.out.println(ostrich.canFly() == false);
|
||||
|
||||
Flyer pegasi = new Pegasus("Prprprpr");
|
||||
System.out.println(pegasi.getName() == "Prprprpr");
|
||||
System.out.println(pegasi.canFly() == true);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question8/Airplane.java
Normal file
47
lab/lab7/Question8/Airplane.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
* The Airplane class implements the Flyer interface.
|
||||
*/
|
||||
|
||||
public class Airplane implements Flyer {
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Airplane(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the airplane.
|
||||
*
|
||||
* @return name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the airplane can fly.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAirplane() {
|
||||
Airplane airplane = new Airplane("Airbus A380");
|
||||
System.out.println(airplane.getName() == "Airbus A380");
|
||||
System.out.println(airplane.canFly() == true);
|
||||
}
|
||||
|
||||
}
|
||||
64
lab/lab7/Question8/Animal.java
Normal file
64
lab/lab7/Question8/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
76
lab/lab7/Question8/Bird.java
Normal file
76
lab/lab7/Question8/Bird.java
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
* The bird class implements the Flyer interface.
|
||||
*/
|
||||
|
||||
public abstract class Bird extends Animal implements Flyer {
|
||||
/**
|
||||
* An integer indicate how many eggs the bird has.
|
||||
*/
|
||||
private int numOfEggs;
|
||||
|
||||
/**
|
||||
* A constructor of Bird.
|
||||
*
|
||||
* @param name The name of the bird.
|
||||
* @param numOfEggs The number of eggs the bird has.
|
||||
*/
|
||||
public Bird(String name, int numOfEggs) {
|
||||
super(name);
|
||||
this.numOfEggs = numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of numOfEggs.
|
||||
*
|
||||
* @return The number of eggs the bird has.
|
||||
*/
|
||||
public int getNumOfEggs() {
|
||||
return numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bird have 2 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method of canFly. Some birds can fly (for example magpies) and some
|
||||
* birds cannot fly (for example ostriches).
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testBird() {
|
||||
Bird b1 = new Bird("magpie", 1) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
System.out.println(b1.getName() == "magpie");
|
||||
System.out.println(b1.getNumOfEggs() == 1);
|
||||
System.out.println(b1.canFly() == true);
|
||||
System.out.println(b1.getLegs() == 2);
|
||||
|
||||
Bird b2 = new Bird("ostriches", 2) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(b2.getName() == "ostriches");
|
||||
System.out.println(b2.getNumOfEggs() == 2);
|
||||
System.out.println(b2.canFly() == false);
|
||||
System.out.println(b2.getLegs() == 2);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question8/Dog.java
Normal file
47
lab/lab7/Question8/Dog.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Dog. Dog has 4 legs and can not fly.
|
||||
*/
|
||||
|
||||
public class Dog extends Animal {
|
||||
/**
|
||||
* Constructor of Dog.
|
||||
*
|
||||
* @param name String
|
||||
*/
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog has 4 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog can not fly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Jack");
|
||||
System.out.println(d.getName() == "Jack");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
|
||||
10
lab/lab7/Question8/Flyer.java
Normal file
10
lab/lab7/Question8/Flyer.java
Normal file
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the interface of Flyer.
|
||||
*/
|
||||
|
||||
public interface Flyer {
|
||||
public String getName();
|
||||
public boolean canFly();
|
||||
}
|
||||
38
lab/lab7/Question8/Magpie.java
Normal file
38
lab/lab7/Question8/Magpie.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Magpie, which is a child class of Brid.
|
||||
*/
|
||||
|
||||
public class Magpie extends Bird {
|
||||
/**
|
||||
* Constructor. Magpie always have 6 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Magpie(String name) {
|
||||
super(name, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magpie can fly.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMagpie() {
|
||||
Bird bird = new Magpie("Magpie");
|
||||
System.out.println(bird.getName() == "Magpie");
|
||||
System.out.println(bird.getNumOfEggs() == 6);
|
||||
System.out.println(bird.canFly() == true);
|
||||
System.out.println(bird.getLegs() == 2);
|
||||
|
||||
}
|
||||
}
|
||||
38
lab/lab7/Question8/Ostrich.java
Normal file
38
lab/lab7/Question8/Ostrich.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Ostrich, which is a child class of Bird.
|
||||
*/
|
||||
|
||||
public class Ostrich extends Bird {
|
||||
/**
|
||||
* Constructor. Ostrich always has 10 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Ostrich(String name) {
|
||||
super(name, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ostrich can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testOstrich() {
|
||||
Ostrich o1 = new Ostrich("Ostrich1");
|
||||
System.out.println(o1.canFly() == false);
|
||||
System.out.println(o1.getName() == "Ostrich1");
|
||||
System.out.println(o1.getNumOfEggs() == 10);
|
||||
System.out.println(o1.getLegs() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
57
lab/lab7/Question8/Pegasus.java
Normal file
57
lab/lab7/Question8/Pegasus.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the Pegasus class which is a subclass of the Bird class.
|
||||
*/
|
||||
|
||||
public class Pegasus extends Bird {
|
||||
/**
|
||||
* Constructor. Pegasus do not lay eggs, so the eggCount is set to 0.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Pegasus(String name) {
|
||||
super(name, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasus have 4 legs.
|
||||
*
|
||||
* @return 4
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasi do not lay eggs. So the getNumOfEggs method returns 0. And print a
|
||||
* message to console.
|
||||
*/
|
||||
@Override
|
||||
public int getNumOfEggs() {
|
||||
System.out.println("Pegasi do not lay eggs!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasus can fly. So the getFlyingAbility method returns true.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testPegasus() {
|
||||
Pegasus p = new Pegasus("Pegasus");
|
||||
System.out.println(p.getName() == "Pegasus");
|
||||
System.out.println(p.getLegs() == 4);
|
||||
System.out.println(p.getNumOfEggs() == 0);
|
||||
System.out.println(p.canFly() == true);
|
||||
}
|
||||
}
|
||||
61
lab/lab7/Question8/Start.java
Normal file
61
lab/lab7/Question8/Start.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*
|
||||
* - Q: Can you test object from the Animal, Dog, and Bird classes through the
|
||||
* Flyer interface? Why or Why not?
|
||||
*
|
||||
* - A: No. Because the they didn't implement the Flyer interface. For Animal
|
||||
* class, you can't because the canFly() method is abstract. For Dog class, you
|
||||
* can't because it didn't declare it has implement the Flyer interface. For
|
||||
* Bird class, you can't because the canFly() method is abstract.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
Bird.testBird();
|
||||
Magpie.testMagpie();
|
||||
Ostrich.testOstrich();
|
||||
Pegasus.testPegasus();
|
||||
Airplane.testAirplane();
|
||||
|
||||
Flyer magpie = new Magpie("Mumomomo");
|
||||
System.out.println(magpie.getName() == "Mumomomo");
|
||||
System.out.println(magpie.canFly() == true);
|
||||
|
||||
Flyer ostrich = new Ostrich("Ohheyeyeye");
|
||||
System.out.println(ostrich.getName() == "Ohheyeyeye");
|
||||
System.out.println(ostrich.canFly() == false);
|
||||
|
||||
Flyer pegasi = new Pegasus("Prprprpr");
|
||||
System.out.println(pegasi.getName() == "Prprprpr");
|
||||
System.out.println(pegasi.canFly() == true);
|
||||
|
||||
Flyer airplane = new Airplane("Shakaraka");
|
||||
System.out.println(airplane.getName() == "Shakaraka");
|
||||
System.out.println(airplane.canFly() == true);
|
||||
}
|
||||
}
|
||||
57
lab/lab7/Question9/Airplane.java
Normal file
57
lab/lab7/Question9/Airplane.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
* The Airplane class implements the Flyer interface.
|
||||
*/
|
||||
|
||||
public class Airplane implements Flyer {
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Airplane(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the airplane.
|
||||
*
|
||||
* @return name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the airplane can fly.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAirplane() {
|
||||
Airplane airplane = new Airplane("Airbus A380");
|
||||
System.out.println(airplane.getName() == "Airbus A380");
|
||||
System.out.println(airplane.canFly());
|
||||
System.out.println(airplane.isDangerous() == false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wheterh the object is dangerous.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isDangerous() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
64
lab/lab7/Question9/Animal.java
Normal file
64
lab/lab7/Question9/Animal.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract animal class.
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
/**
|
||||
* The name for the animal.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param String. The name for the animal.
|
||||
*/
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the name.
|
||||
*
|
||||
* @return String. The name of the animal.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract getLegs methods returns as result the animal's number of legs.
|
||||
*
|
||||
* @return int. The number of legs.
|
||||
*/
|
||||
public abstract int getLegs();
|
||||
|
||||
/**
|
||||
* Abstract canFly methods returns as result the animal's ability to fly.
|
||||
*
|
||||
* @return boolean. The ability to fly.
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testAnimal() {
|
||||
Animal a = new Animal("Animal") {
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
System.out.println(a.getName() == "Animal");
|
||||
System.out.println(a.getLegs() == 4);
|
||||
System.out.println(a.canFly() == false);
|
||||
}
|
||||
}
|
||||
97
lab/lab7/Question9/Bird.java
Normal file
97
lab/lab7/Question9/Bird.java
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the abstract class of Bird.
|
||||
* The bird class implements the Flyer interface.
|
||||
*/
|
||||
|
||||
public abstract class Bird extends Animal implements Flyer {
|
||||
/**
|
||||
* An integer indicate how many eggs the bird has.
|
||||
*/
|
||||
private int numOfEggs;
|
||||
|
||||
/**
|
||||
* A constructor of Bird.
|
||||
*
|
||||
* @param name The name of the bird.
|
||||
* @param numOfEggs The number of eggs the bird has.
|
||||
*/
|
||||
public Bird(String name, int numOfEggs) {
|
||||
super(name);
|
||||
this.numOfEggs = numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of numOfEggs.
|
||||
*
|
||||
* @return The number of eggs the bird has.
|
||||
*/
|
||||
public int getNumOfEggs() {
|
||||
return numOfEggs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bird have 2 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method of canFly. Some birds can fly (for example magpies) and some
|
||||
* birds cannot fly (for example ostriches).
|
||||
*/
|
||||
public abstract boolean canFly();
|
||||
|
||||
/**
|
||||
* Wheterh the object is dangerous.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isDangerous() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testBird() {
|
||||
Bird b1 = new Bird("magpie", 1) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
System.out.println(b1.getName() == "magpie");
|
||||
System.out.println(b1.getNumOfEggs() == 1);
|
||||
System.out.println(b1.canFly() == true);
|
||||
System.out.println(b1.getLegs() == 2);
|
||||
System.out.println(b1.isDangerous() == false);
|
||||
|
||||
Bird b2 = new Bird("ostriches", 2) {
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wheterh the object is dangerous. Ostriches is in dangerous.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean isDangerous() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
System.out.println(b2.getName() == "ostriches");
|
||||
System.out.println(b2.getNumOfEggs() == 2);
|
||||
System.out.println(b2.canFly() == false);
|
||||
System.out.println(b2.getLegs() == 2);
|
||||
System.out.println(b2.isDangerous() == true);
|
||||
}
|
||||
}
|
||||
47
lab/lab7/Question9/Dog.java
Normal file
47
lab/lab7/Question9/Dog.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Dog. Dog has 4 legs and can not fly.
|
||||
*/
|
||||
|
||||
public class Dog extends Animal {
|
||||
/**
|
||||
* Constructor of Dog.
|
||||
*
|
||||
* @param name String
|
||||
*/
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog has 4 legs.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dog can not fly.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Jack");
|
||||
System.out.println(d.getName() == "Jack");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
|
||||
11
lab/lab7/Question9/Flyer.java
Normal file
11
lab/lab7/Question9/Flyer.java
Normal file
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the interface of Flyer.
|
||||
*/
|
||||
|
||||
public interface Flyer {
|
||||
public String getName();
|
||||
public boolean canFly();
|
||||
public boolean isDangerous();
|
||||
}
|
||||
38
lab/lab7/Question9/Magpie.java
Normal file
38
lab/lab7/Question9/Magpie.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Magpie, which is a child class of Brid.
|
||||
*/
|
||||
|
||||
public class Magpie extends Bird {
|
||||
/**
|
||||
* Constructor. Magpie always have 6 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Magpie(String name) {
|
||||
super(name, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magpie can fly.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMagpie() {
|
||||
Bird bird = new Magpie("Magpie");
|
||||
System.out.println(bird.getName() == "Magpie");
|
||||
System.out.println(bird.getNumOfEggs() == 6);
|
||||
System.out.println(bird.canFly() == true);
|
||||
System.out.println(bird.getLegs() == 2);
|
||||
System.out.println(bird.isDangerous() == false);
|
||||
}
|
||||
}
|
||||
49
lab/lab7/Question9/Ostrich.java
Normal file
49
lab/lab7/Question9/Ostrich.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the class of Ostrich, which is a child class of Bird.
|
||||
*/
|
||||
|
||||
public class Ostrich extends Bird {
|
||||
/**
|
||||
* Constructor. Ostrich always has 10 eggs.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Ostrich(String name) {
|
||||
super(name, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ostrich can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ostrich is in dangerous.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean isDangerous() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testOstrich() {
|
||||
Ostrich o1 = new Ostrich("Ostrich1");
|
||||
System.out.println(o1.canFly() == false);
|
||||
System.out.println(o1.getName() == "Ostrich1");
|
||||
System.out.println(o1.getNumOfEggs() == 10);
|
||||
System.out.println(o1.getLegs() == 2);
|
||||
System.out.println(o1.isDangerous() == true);
|
||||
}
|
||||
|
||||
}
|
||||
58
lab/lab7/Question9/Pegasus.java
Normal file
58
lab/lab7/Question9/Pegasus.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/04/03
|
||||
* Description: This is the Pegasus class which is a subclass of the Bird class.
|
||||
*/
|
||||
|
||||
public class Pegasus extends Bird {
|
||||
/**
|
||||
* Constructor. Pegasus do not lay eggs, so the eggCount is set to 0.
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Pegasus(String name) {
|
||||
super(name, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasus have 4 legs.
|
||||
*
|
||||
* @return 4
|
||||
*/
|
||||
@Override
|
||||
public int getLegs() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasi do not lay eggs. So the getNumOfEggs method returns 0. And print a
|
||||
* message to console.
|
||||
*/
|
||||
@Override
|
||||
public int getNumOfEggs() {
|
||||
System.out.println("Pegasi do not lay eggs!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pegasus can fly. So the getFlyingAbility method returns true.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testPegasus() {
|
||||
Pegasus p = new Pegasus("Pegasus");
|
||||
System.out.println(p.getName() == "Pegasus");
|
||||
System.out.println(p.getLegs() == 4);
|
||||
System.out.println(p.getNumOfEggs() == 0);
|
||||
System.out.println(p.canFly() == true);
|
||||
System.out.println(p.isDangerous() == false);
|
||||
}
|
||||
}
|
||||
65
lab/lab7/Question9/Start.java
Normal file
65
lab/lab7/Question9/Start.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Answer to Question.
|
||||
*
|
||||
* Answer to Question 1:
|
||||
*
|
||||
* - Q: Should the getLegs methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different number of legs. In animal
|
||||
* class we didn't define how many legs an animal has.
|
||||
*
|
||||
* - Q: Should the canFly methods be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different ability to fly. In animal
|
||||
* class we didn't define how an animal can fly.
|
||||
*
|
||||
* - Q: Should the Animal class be abstract? Why or why not?
|
||||
*
|
||||
* - A: Yes. Because different animal have different properties. In animal class
|
||||
* we didn't define how an animal is.
|
||||
*
|
||||
* - Q: What kinds of test can you write inside the testAnimal method?
|
||||
*
|
||||
* - A: You can write test for the getLegs and canFly methods. Before that, you
|
||||
* need to inherit the Animal class and override the getLegs and canFly methods.
|
||||
* Because the Animal class is abstract, you can't create an object of it.
|
||||
*
|
||||
* - Q: Can you test object from the Animal, Dog, and Bird classes through the
|
||||
* Flyer interface? Why or Why not?
|
||||
*
|
||||
* - A: No. Because the they didn't implement the Flyer interface. For Animal
|
||||
* class, you can't because the canFly() method is abstract. For Dog class, you
|
||||
* can't because it didn't declare it has implement the Flyer interface. For
|
||||
* Bird class, you can't because the canFly() method is abstract.
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
Bird.testBird();
|
||||
Magpie.testMagpie();
|
||||
Ostrich.testOstrich();
|
||||
Pegasus.testPegasus();
|
||||
Airplane.testAirplane();
|
||||
|
||||
Flyer magpie = new Magpie("Mumomomo");
|
||||
System.out.println(magpie.getName() == "Mumomomo");
|
||||
System.out.println(magpie.canFly() == true);
|
||||
System.out.println(magpie.isDangerous() == false);
|
||||
|
||||
Flyer ostrich = new Ostrich("Ohheyeyeye");
|
||||
System.out.println(ostrich.getName() == "Ohheyeyeye");
|
||||
System.out.println(ostrich.canFly() == false);
|
||||
System.out.println(ostrich.isDangerous() == true);
|
||||
|
||||
Flyer pegasi = new Pegasus("Prprprpr");
|
||||
System.out.println(pegasi.getName() == "Prprprpr");
|
||||
System.out.println(pegasi.canFly() == true);
|
||||
System.out.println(pegasi.isDangerous() == false);
|
||||
|
||||
Flyer airplane = new Airplane("Shakaraka");
|
||||
System.out.println(airplane.getName() == "Shakaraka");
|
||||
System.out.println(airplane.canFly() == true);
|
||||
System.out.println(airplane.isDangerous() == false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user