Restore
This commit is contained in:
23
Labs/Lab7/Lab7_2230026071/Question8/src/Airplane.java
Normal file
23
Labs/Lab7/Lab7_2230026071/Question8/src/Airplane.java
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
public class Airplane implements Flyer{
|
||||
private String name;
|
||||
//constructor
|
||||
public Airplane(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//methods override
|
||||
@Override
|
||||
public String getName() {return this.name;}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {return true;}
|
||||
|
||||
//test
|
||||
public static void testAirplane() {
|
||||
Airplane a = new Airplane("Aircar");
|
||||
System.out.println(a.getName() == "Aircar");
|
||||
System.out.println(a.canFly() == true);
|
||||
}
|
||||
|
||||
}
|
||||
21
Labs/Lab7/Lab7_2230026071/Question8/src/Animal.java
Normal file
21
Labs/Lab7/Lab7_2230026071/Question8/src/Animal.java
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
public abstract class Animal {
|
||||
//instance variable
|
||||
private String name;
|
||||
|
||||
//constructor
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//methods
|
||||
public String getName() {return this.name;}
|
||||
|
||||
//abstract methods
|
||||
public abstract int getLegs();
|
||||
|
||||
public abstract boolean canFly();
|
||||
|
||||
//test
|
||||
public static void testAnimal() {}
|
||||
}
|
||||
24
Labs/Lab7/Lab7_2230026071/Question8/src/Bird.java
Normal file
24
Labs/Lab7/Lab7_2230026071/Question8/src/Bird.java
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
public abstract class Bird extends Animal implements Flyer{
|
||||
//instance variable
|
||||
private int numOfEggs;
|
||||
|
||||
//constructor
|
||||
public Bird(String name, int numOfEggs) {
|
||||
super(name);
|
||||
this.numOfEggs = numOfEggs;
|
||||
}
|
||||
|
||||
//methods
|
||||
public int getNumOfEggs() {return this.numOfEggs;}
|
||||
|
||||
//methods override
|
||||
@Override
|
||||
public int getLegs() {return 2;}
|
||||
|
||||
@Override
|
||||
public abstract boolean canFly();
|
||||
|
||||
//test
|
||||
public static void testBird() {}
|
||||
}
|
||||
24
Labs/Lab7/Lab7_2230026071/Question8/src/Dog.java
Normal file
24
Labs/Lab7/Lab7_2230026071/Question8/src/Dog.java
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
public class Dog extends Animal{
|
||||
//constructor
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
//methods override
|
||||
@Override
|
||||
public int getLegs() {return 4;}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {return false;}
|
||||
|
||||
//test
|
||||
public static void testDog() {
|
||||
Dog d = new Dog("Nice Doggy");
|
||||
// The getName method is inherited from Animal.
|
||||
// The getLegs and canFly methods come from Dog itself.
|
||||
System.out.println(d.getName() == "Nice Doggy");
|
||||
System.out.println(d.getLegs() == 4);
|
||||
System.out.println(d.canFly() == false);
|
||||
}
|
||||
}
|
||||
5
Labs/Lab7/Lab7_2230026071/Question8/src/Flyer.java
Normal file
5
Labs/Lab7/Lab7_2230026071/Question8/src/Flyer.java
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
public interface Flyer {
|
||||
public String getName();
|
||||
public boolean canFly();
|
||||
}
|
||||
21
Labs/Lab7/Lab7_2230026071/Question8/src/Magpie.java
Normal file
21
Labs/Lab7/Lab7_2230026071/Question8/src/Magpie.java
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
public class Magpie extends Bird{
|
||||
//constructor
|
||||
public Magpie(String name) {
|
||||
super(name, 6);
|
||||
}
|
||||
|
||||
//method
|
||||
@Override
|
||||
public boolean canFly() {return true;}
|
||||
|
||||
//test
|
||||
public static void testMagpie() {
|
||||
Magpie m = new Magpie("Maggie");
|
||||
System.out.println(m.getName() == "Maggie");
|
||||
System.out.println(m.getLegs() == 2);
|
||||
System.out.println(m.getNumOfEggs() == 6);
|
||||
System.out.println(m.canFly() == true);
|
||||
}
|
||||
|
||||
}
|
||||
21
Labs/Lab7/Lab7_2230026071/Question8/src/Ostrich.java
Normal file
21
Labs/Lab7/Lab7_2230026071/Question8/src/Ostrich.java
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
public class Ostrich extends Bird{
|
||||
//constructor
|
||||
public Ostrich(String name) {
|
||||
super(name, 10);
|
||||
}
|
||||
|
||||
//method
|
||||
@Override
|
||||
public boolean canFly() {return false;}
|
||||
|
||||
//test
|
||||
public static void testOstrich() {
|
||||
Ostrich o = new Ostrich("Ossie");
|
||||
System.out.println(o.getName() == "Ossie");
|
||||
System.out.println(o.getLegs() == 2);
|
||||
System.out.println(o.getNumOfEggs() == 10);
|
||||
System.out.println(o.canFly() == false);
|
||||
}
|
||||
|
||||
}
|
||||
29
Labs/Lab7/Lab7_2230026071/Question8/src/Pegasus.java
Normal file
29
Labs/Lab7/Lab7_2230026071/Question8/src/Pegasus.java
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
public class Pegasus extends Bird{
|
||||
//constructor
|
||||
public Pegasus(String name) {
|
||||
super(name, 0);
|
||||
}
|
||||
|
||||
//method override
|
||||
@Override
|
||||
public int getLegs() {return 4;}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {return true;}
|
||||
|
||||
@Override
|
||||
public int getNumOfEggs() {
|
||||
System.out.println("Pegasi do not lay eggs!"); //message
|
||||
return 0;
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testPegasus() {
|
||||
Pegasus p = new Pegasus("Peggie");
|
||||
System.out.println(p.getName() == "Peggie");
|
||||
System.out.println(p.getLegs() == 4);
|
||||
System.out.println(p.getNumOfEggs() == 0); // Message printed here.
|
||||
System.out.println(p.canFly() == true);
|
||||
}
|
||||
}
|
||||
14
Labs/Lab7/Lab7_2230026071/Question8/src/Start.java
Normal file
14
Labs/Lab7/Lab7_2230026071/Question8/src/Start.java
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Animal.testAnimal();
|
||||
Dog.testDog();
|
||||
Bird.testBird();
|
||||
Magpie.testMagpie();
|
||||
Ostrich.testOstrich();
|
||||
Pegasus.testPegasus();
|
||||
Airplane.testAirplane();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user