Close: #1 Lab 7
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user