Close: #1 Lab 7

This commit is contained in:
2022-04-03 15:14:47 +08:00
parent 85b623f65f
commit 80ae76b597
53 changed files with 2677 additions and 0 deletions

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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();
}
}