From 80ae76b597a8e116e68e9254be40fee8079276bc Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 3 Apr 2022 15:14:47 +0800 Subject: [PATCH] Close: #1 Lab 7 --- lab/lab7/Question1/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question1/Start.java | 31 ++++++++++ lab/lab7/Question2/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question2/Dog.java | 47 ++++++++++++++++ lab/lab7/Question2/Start.java | 32 +++++++++++ lab/lab7/Question3/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question3/Bird.java | 75 ++++++++++++++++++++++++ lab/lab7/Question3/Dog.java | 47 ++++++++++++++++ lab/lab7/Question3/Start.java | 33 +++++++++++ lab/lab7/Question4/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question4/Bird.java | 75 ++++++++++++++++++++++++ lab/lab7/Question4/Dog.java | 47 ++++++++++++++++ lab/lab7/Question4/Magpie.java | 38 +++++++++++++ lab/lab7/Question4/Start.java | 34 +++++++++++ lab/lab7/Question5/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question5/Bird.java | 75 ++++++++++++++++++++++++ lab/lab7/Question5/Dog.java | 47 ++++++++++++++++ lab/lab7/Question5/Magpie.java | 38 +++++++++++++ lab/lab7/Question5/Ostrich.java | 38 +++++++++++++ lab/lab7/Question5/Start.java | 35 ++++++++++++ lab/lab7/Question6/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question6/Bird.java | 75 ++++++++++++++++++++++++ lab/lab7/Question6/Dog.java | 47 ++++++++++++++++ lab/lab7/Question6/Magpie.java | 38 +++++++++++++ lab/lab7/Question6/Ostrich.java | 38 +++++++++++++ lab/lab7/Question6/Pegasus.java | 57 +++++++++++++++++++ lab/lab7/Question6/Start.java | 36 ++++++++++++ lab/lab7/Question7/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question7/Bird.java | 76 +++++++++++++++++++++++++ lab/lab7/Question7/Dog.java | 47 ++++++++++++++++ lab/lab7/Question7/Flyer.java | 10 ++++ lab/lab7/Question7/Magpie.java | 38 +++++++++++++ lab/lab7/Question7/Ostrich.java | 38 +++++++++++++ lab/lab7/Question7/Pegasus.java | 57 +++++++++++++++++++ lab/lab7/Question7/Start.java | 56 ++++++++++++++++++ lab/lab7/Question8/Airplane.java | 47 ++++++++++++++++ lab/lab7/Question8/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question8/Bird.java | 76 +++++++++++++++++++++++++ lab/lab7/Question8/Dog.java | 47 ++++++++++++++++ lab/lab7/Question8/Flyer.java | 10 ++++ lab/lab7/Question8/Magpie.java | 38 +++++++++++++ lab/lab7/Question8/Ostrich.java | 38 +++++++++++++ lab/lab7/Question8/Pegasus.java | 57 +++++++++++++++++++ lab/lab7/Question8/Start.java | 61 ++++++++++++++++++++ lab/lab7/Question9/Airplane.java | 57 +++++++++++++++++++ lab/lab7/Question9/Animal.java | 64 +++++++++++++++++++++ lab/lab7/Question9/Bird.java | 97 ++++++++++++++++++++++++++++++++ lab/lab7/Question9/Dog.java | 47 ++++++++++++++++ lab/lab7/Question9/Flyer.java | 11 ++++ lab/lab7/Question9/Magpie.java | 38 +++++++++++++ lab/lab7/Question9/Ostrich.java | 49 ++++++++++++++++ lab/lab7/Question9/Pegasus.java | 58 +++++++++++++++++++ lab/lab7/Question9/Start.java | 65 +++++++++++++++++++++ 53 files changed, 2677 insertions(+) create mode 100644 lab/lab7/Question1/Animal.java create mode 100644 lab/lab7/Question1/Start.java create mode 100644 lab/lab7/Question2/Animal.java create mode 100644 lab/lab7/Question2/Dog.java create mode 100644 lab/lab7/Question2/Start.java create mode 100644 lab/lab7/Question3/Animal.java create mode 100644 lab/lab7/Question3/Bird.java create mode 100644 lab/lab7/Question3/Dog.java create mode 100644 lab/lab7/Question3/Start.java create mode 100644 lab/lab7/Question4/Animal.java create mode 100644 lab/lab7/Question4/Bird.java create mode 100644 lab/lab7/Question4/Dog.java create mode 100644 lab/lab7/Question4/Magpie.java create mode 100644 lab/lab7/Question4/Start.java create mode 100644 lab/lab7/Question5/Animal.java create mode 100644 lab/lab7/Question5/Bird.java create mode 100644 lab/lab7/Question5/Dog.java create mode 100644 lab/lab7/Question5/Magpie.java create mode 100644 lab/lab7/Question5/Ostrich.java create mode 100644 lab/lab7/Question5/Start.java create mode 100644 lab/lab7/Question6/Animal.java create mode 100644 lab/lab7/Question6/Bird.java create mode 100644 lab/lab7/Question6/Dog.java create mode 100644 lab/lab7/Question6/Magpie.java create mode 100644 lab/lab7/Question6/Ostrich.java create mode 100644 lab/lab7/Question6/Pegasus.java create mode 100644 lab/lab7/Question6/Start.java create mode 100644 lab/lab7/Question7/Animal.java create mode 100644 lab/lab7/Question7/Bird.java create mode 100644 lab/lab7/Question7/Dog.java create mode 100644 lab/lab7/Question7/Flyer.java create mode 100644 lab/lab7/Question7/Magpie.java create mode 100644 lab/lab7/Question7/Ostrich.java create mode 100644 lab/lab7/Question7/Pegasus.java create mode 100644 lab/lab7/Question7/Start.java create mode 100644 lab/lab7/Question8/Airplane.java create mode 100644 lab/lab7/Question8/Animal.java create mode 100644 lab/lab7/Question8/Bird.java create mode 100644 lab/lab7/Question8/Dog.java create mode 100644 lab/lab7/Question8/Flyer.java create mode 100644 lab/lab7/Question8/Magpie.java create mode 100644 lab/lab7/Question8/Ostrich.java create mode 100644 lab/lab7/Question8/Pegasus.java create mode 100644 lab/lab7/Question8/Start.java create mode 100644 lab/lab7/Question9/Airplane.java create mode 100644 lab/lab7/Question9/Animal.java create mode 100644 lab/lab7/Question9/Bird.java create mode 100644 lab/lab7/Question9/Dog.java create mode 100644 lab/lab7/Question9/Flyer.java create mode 100644 lab/lab7/Question9/Magpie.java create mode 100644 lab/lab7/Question9/Ostrich.java create mode 100644 lab/lab7/Question9/Pegasus.java create mode 100644 lab/lab7/Question9/Start.java diff --git a/lab/lab7/Question1/Animal.java b/lab/lab7/Question1/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question1/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question1/Start.java b/lab/lab7/Question1/Start.java new file mode 100644 index 0000000..bb7b9c8 --- /dev/null +++ b/lab/lab7/Question1/Start.java @@ -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(); + } +} diff --git a/lab/lab7/Question2/Animal.java b/lab/lab7/Question2/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question2/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question2/Dog.java b/lab/lab7/Question2/Dog.java new file mode 100644 index 0000000..3b9a012 --- /dev/null +++ b/lab/lab7/Question2/Dog.java @@ -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); + } +} + diff --git a/lab/lab7/Question2/Start.java b/lab/lab7/Question2/Start.java new file mode 100644 index 0000000..07ea360 --- /dev/null +++ b/lab/lab7/Question2/Start.java @@ -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(); + } +} diff --git a/lab/lab7/Question3/Animal.java b/lab/lab7/Question3/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question3/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question3/Bird.java b/lab/lab7/Question3/Bird.java new file mode 100644 index 0000000..c3f5cc9 --- /dev/null +++ b/lab/lab7/Question3/Bird.java @@ -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); + } +} diff --git a/lab/lab7/Question3/Dog.java b/lab/lab7/Question3/Dog.java new file mode 100644 index 0000000..3b9a012 --- /dev/null +++ b/lab/lab7/Question3/Dog.java @@ -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); + } +} + diff --git a/lab/lab7/Question3/Start.java b/lab/lab7/Question3/Start.java new file mode 100644 index 0000000..bff3a3c --- /dev/null +++ b/lab/lab7/Question3/Start.java @@ -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(); + } +} diff --git a/lab/lab7/Question4/Animal.java b/lab/lab7/Question4/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question4/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question4/Bird.java b/lab/lab7/Question4/Bird.java new file mode 100644 index 0000000..c3f5cc9 --- /dev/null +++ b/lab/lab7/Question4/Bird.java @@ -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); + } +} diff --git a/lab/lab7/Question4/Dog.java b/lab/lab7/Question4/Dog.java new file mode 100644 index 0000000..3b9a012 --- /dev/null +++ b/lab/lab7/Question4/Dog.java @@ -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); + } +} + diff --git a/lab/lab7/Question4/Magpie.java b/lab/lab7/Question4/Magpie.java new file mode 100644 index 0000000..048b5f9 --- /dev/null +++ b/lab/lab7/Question4/Magpie.java @@ -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); + + } +} diff --git a/lab/lab7/Question4/Start.java b/lab/lab7/Question4/Start.java new file mode 100644 index 0000000..9c50f37 --- /dev/null +++ b/lab/lab7/Question4/Start.java @@ -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(); + } +} diff --git a/lab/lab7/Question5/Animal.java b/lab/lab7/Question5/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question5/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question5/Bird.java b/lab/lab7/Question5/Bird.java new file mode 100644 index 0000000..c3f5cc9 --- /dev/null +++ b/lab/lab7/Question5/Bird.java @@ -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); + } +} diff --git a/lab/lab7/Question5/Dog.java b/lab/lab7/Question5/Dog.java new file mode 100644 index 0000000..3b9a012 --- /dev/null +++ b/lab/lab7/Question5/Dog.java @@ -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); + } +} + diff --git a/lab/lab7/Question5/Magpie.java b/lab/lab7/Question5/Magpie.java new file mode 100644 index 0000000..048b5f9 --- /dev/null +++ b/lab/lab7/Question5/Magpie.java @@ -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); + + } +} diff --git a/lab/lab7/Question5/Ostrich.java b/lab/lab7/Question5/Ostrich.java new file mode 100644 index 0000000..4f33fb1 --- /dev/null +++ b/lab/lab7/Question5/Ostrich.java @@ -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); + } + +} diff --git a/lab/lab7/Question5/Start.java b/lab/lab7/Question5/Start.java new file mode 100644 index 0000000..a73bb80 --- /dev/null +++ b/lab/lab7/Question5/Start.java @@ -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(); + } +} diff --git a/lab/lab7/Question6/Animal.java b/lab/lab7/Question6/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question6/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question6/Bird.java b/lab/lab7/Question6/Bird.java new file mode 100644 index 0000000..c3f5cc9 --- /dev/null +++ b/lab/lab7/Question6/Bird.java @@ -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); + } +} diff --git a/lab/lab7/Question6/Dog.java b/lab/lab7/Question6/Dog.java new file mode 100644 index 0000000..3b9a012 --- /dev/null +++ b/lab/lab7/Question6/Dog.java @@ -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); + } +} + diff --git a/lab/lab7/Question6/Magpie.java b/lab/lab7/Question6/Magpie.java new file mode 100644 index 0000000..048b5f9 --- /dev/null +++ b/lab/lab7/Question6/Magpie.java @@ -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); + + } +} diff --git a/lab/lab7/Question6/Ostrich.java b/lab/lab7/Question6/Ostrich.java new file mode 100644 index 0000000..4f33fb1 --- /dev/null +++ b/lab/lab7/Question6/Ostrich.java @@ -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); + } + +} diff --git a/lab/lab7/Question6/Pegasus.java b/lab/lab7/Question6/Pegasus.java new file mode 100644 index 0000000..e551ff0 --- /dev/null +++ b/lab/lab7/Question6/Pegasus.java @@ -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); + } +} diff --git a/lab/lab7/Question6/Start.java b/lab/lab7/Question6/Start.java new file mode 100644 index 0000000..aabdd0b --- /dev/null +++ b/lab/lab7/Question6/Start.java @@ -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(); + } +} diff --git a/lab/lab7/Question7/Animal.java b/lab/lab7/Question7/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question7/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question7/Bird.java b/lab/lab7/Question7/Bird.java new file mode 100644 index 0000000..2ee2f01 --- /dev/null +++ b/lab/lab7/Question7/Bird.java @@ -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); + } +} diff --git a/lab/lab7/Question7/Dog.java b/lab/lab7/Question7/Dog.java new file mode 100644 index 0000000..3b9a012 --- /dev/null +++ b/lab/lab7/Question7/Dog.java @@ -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); + } +} + diff --git a/lab/lab7/Question7/Flyer.java b/lab/lab7/Question7/Flyer.java new file mode 100644 index 0000000..ffacd25 --- /dev/null +++ b/lab/lab7/Question7/Flyer.java @@ -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(); +} diff --git a/lab/lab7/Question7/Magpie.java b/lab/lab7/Question7/Magpie.java new file mode 100644 index 0000000..048b5f9 --- /dev/null +++ b/lab/lab7/Question7/Magpie.java @@ -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); + + } +} diff --git a/lab/lab7/Question7/Ostrich.java b/lab/lab7/Question7/Ostrich.java new file mode 100644 index 0000000..4f33fb1 --- /dev/null +++ b/lab/lab7/Question7/Ostrich.java @@ -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); + } + +} diff --git a/lab/lab7/Question7/Pegasus.java b/lab/lab7/Question7/Pegasus.java new file mode 100644 index 0000000..e551ff0 --- /dev/null +++ b/lab/lab7/Question7/Pegasus.java @@ -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); + } +} diff --git a/lab/lab7/Question7/Start.java b/lab/lab7/Question7/Start.java new file mode 100644 index 0000000..208799b --- /dev/null +++ b/lab/lab7/Question7/Start.java @@ -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); + } +} diff --git a/lab/lab7/Question8/Airplane.java b/lab/lab7/Question8/Airplane.java new file mode 100644 index 0000000..8a5de8b --- /dev/null +++ b/lab/lab7/Question8/Airplane.java @@ -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); + } + +} diff --git a/lab/lab7/Question8/Animal.java b/lab/lab7/Question8/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question8/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question8/Bird.java b/lab/lab7/Question8/Bird.java new file mode 100644 index 0000000..2ee2f01 --- /dev/null +++ b/lab/lab7/Question8/Bird.java @@ -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); + } +} diff --git a/lab/lab7/Question8/Dog.java b/lab/lab7/Question8/Dog.java new file mode 100644 index 0000000..3b9a012 --- /dev/null +++ b/lab/lab7/Question8/Dog.java @@ -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); + } +} + diff --git a/lab/lab7/Question8/Flyer.java b/lab/lab7/Question8/Flyer.java new file mode 100644 index 0000000..ffacd25 --- /dev/null +++ b/lab/lab7/Question8/Flyer.java @@ -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(); +} diff --git a/lab/lab7/Question8/Magpie.java b/lab/lab7/Question8/Magpie.java new file mode 100644 index 0000000..048b5f9 --- /dev/null +++ b/lab/lab7/Question8/Magpie.java @@ -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); + + } +} diff --git a/lab/lab7/Question8/Ostrich.java b/lab/lab7/Question8/Ostrich.java new file mode 100644 index 0000000..4f33fb1 --- /dev/null +++ b/lab/lab7/Question8/Ostrich.java @@ -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); + } + +} diff --git a/lab/lab7/Question8/Pegasus.java b/lab/lab7/Question8/Pegasus.java new file mode 100644 index 0000000..e551ff0 --- /dev/null +++ b/lab/lab7/Question8/Pegasus.java @@ -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); + } +} diff --git a/lab/lab7/Question8/Start.java b/lab/lab7/Question8/Start.java new file mode 100644 index 0000000..f17f24f --- /dev/null +++ b/lab/lab7/Question8/Start.java @@ -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); + } +} diff --git a/lab/lab7/Question9/Airplane.java b/lab/lab7/Question9/Airplane.java new file mode 100644 index 0000000..685da39 --- /dev/null +++ b/lab/lab7/Question9/Airplane.java @@ -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; + } + +} diff --git a/lab/lab7/Question9/Animal.java b/lab/lab7/Question9/Animal.java new file mode 100644 index 0000000..500f938 --- /dev/null +++ b/lab/lab7/Question9/Animal.java @@ -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); + } +} diff --git a/lab/lab7/Question9/Bird.java b/lab/lab7/Question9/Bird.java new file mode 100644 index 0000000..aa5405d --- /dev/null +++ b/lab/lab7/Question9/Bird.java @@ -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); + } +} diff --git a/lab/lab7/Question9/Dog.java b/lab/lab7/Question9/Dog.java new file mode 100644 index 0000000..3b9a012 --- /dev/null +++ b/lab/lab7/Question9/Dog.java @@ -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); + } +} + diff --git a/lab/lab7/Question9/Flyer.java b/lab/lab7/Question9/Flyer.java new file mode 100644 index 0000000..7119b11 --- /dev/null +++ b/lab/lab7/Question9/Flyer.java @@ -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(); +} diff --git a/lab/lab7/Question9/Magpie.java b/lab/lab7/Question9/Magpie.java new file mode 100644 index 0000000..1c37e5e --- /dev/null +++ b/lab/lab7/Question9/Magpie.java @@ -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); + } +} diff --git a/lab/lab7/Question9/Ostrich.java b/lab/lab7/Question9/Ostrich.java new file mode 100644 index 0000000..e1cdd60 --- /dev/null +++ b/lab/lab7/Question9/Ostrich.java @@ -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); + } + +} diff --git a/lab/lab7/Question9/Pegasus.java b/lab/lab7/Question9/Pegasus.java new file mode 100644 index 0000000..a83e1ea --- /dev/null +++ b/lab/lab7/Question9/Pegasus.java @@ -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); + } +} diff --git a/lab/lab7/Question9/Start.java b/lab/lab7/Question9/Start.java new file mode 100644 index 0000000..c541bfd --- /dev/null +++ b/lab/lab7/Question9/Start.java @@ -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); + } +}