Restore
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question2</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,27 @@
|
||||
|
||||
public class DisplayBox {
|
||||
//instance variable
|
||||
private Dragonfly dragonfly;
|
||||
|
||||
//constructor
|
||||
public DisplayBox(Dragonfly dragonfly) {
|
||||
this.dragonfly = dragonfly;
|
||||
}
|
||||
|
||||
///methods
|
||||
public Dragonfly getDragonfly() {return this.dragonfly;}
|
||||
|
||||
//test
|
||||
public static void testDisplayBox() {
|
||||
// In the testDisplayBox method, create a male emperor dragonfly called med1...
|
||||
MaleEmperorDragonfly med1 = new MaleEmperorDragonfly();
|
||||
// then create a display box with this male emperor dragonfly in it.
|
||||
DisplayBox db = new DisplayBox(med1); // Implicit upcast from MaleEmperorDragongly to Dragonfly.
|
||||
// Then get the male emperor dragonfly from the display box...
|
||||
Dragonfly d = db.getDragonfly();
|
||||
// and store it into a local variable called med2 of type MaleEmperorDragongly.
|
||||
MaleEmperorDragonfly med2 = (MaleEmperorDragonfly)d; // Downcast mandatory!
|
||||
// Use the == operator to check that med1 and med2 are the same male emperor dragonfly.
|
||||
System.out.println(med1 == med2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
public class Dragonfly extends Insect{
|
||||
//instance variable
|
||||
private double length;
|
||||
private int age;
|
||||
|
||||
//constructor
|
||||
public Dragonfly(String name, double length, int age) {
|
||||
super(name);
|
||||
this.length = length;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
//methods
|
||||
public double getLength() {return this.length;}
|
||||
|
||||
public int getAge() {return this.age;}
|
||||
|
||||
//method override
|
||||
@Override
|
||||
public boolean canFly() {return true;}
|
||||
|
||||
//test
|
||||
public static void testDragonfly() {
|
||||
Dragonfly d = new Dragonfly("Frequent Fly", 20.0, 1);
|
||||
// getName is inherited from Insect.
|
||||
System.out.println(d.getName() == "Frequent Fly");
|
||||
System.out.println(d.getLength() == 20.0);
|
||||
System.out.println(d.canFly() == true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
public class DragonflyNymph extends Dragonfly{
|
||||
//constructor
|
||||
public DragonflyNymph() {
|
||||
super("Baby Dragon", 0.8, 1);
|
||||
}
|
||||
|
||||
//method override
|
||||
@Override
|
||||
public boolean canFly() {return false;}
|
||||
|
||||
//test
|
||||
public static void testDragonflyNymph () {
|
||||
DragonflyNymph dn = new DragonflyNymph();
|
||||
// getName is inherited from Insect.
|
||||
System.out.println(dn.getName() == "Baby Dragon");
|
||||
// getLength is inherited from Dragonfly.System.out.println(dn.getLength() == 0.8);
|
||||
// getAge is inherited from Dragonfly.
|
||||
System.out.println(dn.getAge() == 1);
|
||||
// canFly is from DragonflyNymph itself.
|
||||
System.out.println(dn.canFly() == false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
|
||||
public class EmperorDragonfly extends Dragonfly{
|
||||
//constructors
|
||||
public EmperorDragonfly(String name, double length, int age) {
|
||||
super(name, length, age);
|
||||
}
|
||||
|
||||
public EmperorDragonfly(String name) {
|
||||
this(name, 2.0, 3);
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testEmperorDragonfly() {
|
||||
// Testing the first constructor.
|
||||
EmperorDragonfly ed1 = new EmperorDragonfly ("king fly", 3.0,1);
|
||||
// getName is inherited from Insect.
|
||||
System.out.println(ed1.getName() == "king fly");
|
||||
// getLength is inherited from Dragonfly.
|
||||
System.out.println(ed1.getLength() == 3.0);
|
||||
// getAge is inherited from Dragonfly.
|
||||
System.out.println(ed1.getAge() == 1);
|
||||
// canFly is inherited from Dragonfly.
|
||||
System.out.println(ed1.canFly() == true);
|
||||
// Testing the second constructor.
|
||||
EmperorDragonfly ed2 = new EmperorDragonfly ("green fly");
|
||||
// getName is inherited from Insect.
|
||||
System.out.println(ed2.getName() == "green fly");
|
||||
// getLength is inherited from Dragonfly.
|
||||
System.out.println(ed2.getLength() == 2.0);
|
||||
// getAge is inherited from Dragonfly.
|
||||
System.out.println(ed2.getAge() == 3);
|
||||
// canFly is inherited from Dragonfly.
|
||||
System.out.println(ed2.canFly() == true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
public class Flee extends Insect{
|
||||
//constructor
|
||||
public Flee() {
|
||||
super("Fleet");
|
||||
}
|
||||
|
||||
//methods override
|
||||
@Override
|
||||
public boolean canFly() {return false;}
|
||||
|
||||
//test
|
||||
public static void testFlee() {
|
||||
Flee f = new Flee();
|
||||
// getName is inherited from Insect.
|
||||
System.out.println(f.getName() == "Fleet");
|
||||
System.out.println(f.canFly() == false); // No message printed.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
public class Insect {
|
||||
//instance variable
|
||||
private String name;
|
||||
|
||||
//constructor
|
||||
public Insect(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//methods
|
||||
public String getName() {return this.name;}
|
||||
|
||||
public boolean canFly() {
|
||||
System.out.println("Cannot fly!"); //message
|
||||
return false;
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testInsect() {
|
||||
Insect i = new Insect("some name");
|
||||
System.out.println(i.getName() == "some name");
|
||||
System.out.println(i.canFly() == false);
|
||||
// Message printed too.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
public class MaleEmperorDragonfly extends EmperorDragonfly{
|
||||
//constructor
|
||||
public MaleEmperorDragonfly() {
|
||||
super("Beautiful", 3.5, 4);
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testMaleEmperorDragonfly () {
|
||||
MaleEmperorDragonfly med = new MaleEmperorDragonfly();
|
||||
// getName is inherited from Insect.
|
||||
System.out.println(med.getName() == "Beautiful");
|
||||
// getLength is inherited from EmperorDragonfly.
|
||||
System.out.println(med.getLength() == 3.5);
|
||||
// getAge is inherited from EmperorDragonfly.
|
||||
System.out.println(med.getAge() == 4);
|
||||
// canFly is inherited from Dragonfly.
|
||||
System.out.println(med.canFly() == true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Insect.testInsect();
|
||||
Flee.testFlee();
|
||||
Dragonfly.testDragonfly();
|
||||
EmperorDragonfly.testEmperorDragonfly();
|
||||
MaleEmperorDragonfly.testMaleEmperorDragonfly();
|
||||
DragonflyNymph.testDragonflyNymph();
|
||||
DisplayBox.testDisplayBox();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user