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