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>Question4</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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,91 @@
|
||||
|
||||
public class AirCraft extends Vehicle{
|
||||
//private instance
|
||||
private int altitude;
|
||||
|
||||
//constructor
|
||||
public AirCraft(int speedLimit, int speed, int altitude) throws BadSpeedSetting, BadAltitudeSetting {
|
||||
super(speedLimit, speed);
|
||||
if(altitude > 0)
|
||||
this.altitude = altitude;
|
||||
else
|
||||
throw new BadAltitudeSetting();
|
||||
}
|
||||
|
||||
//methods
|
||||
public int getAltitude() {return this.altitude;}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {return true;}
|
||||
|
||||
//test
|
||||
public static void testAirCraft() {
|
||||
// test case for constructors
|
||||
try {
|
||||
// Exception
|
||||
AirCraft a1 = new AirCraft(70,80,1000);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
|
||||
} catch (BadAltitudeSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
}
|
||||
try {
|
||||
// exception
|
||||
AirCraft a2 = new AirCraft(70,-10,1000);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
} catch (BadAltitudeSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
}
|
||||
try {
|
||||
// exception
|
||||
AirCraft a3 = new AirCraft(700,200,-10);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (BadAltitudeSetting e)
|
||||
{ System.out.println(e.getMessage().equals("Altitude cannot be negative!"));
|
||||
}
|
||||
// Test case for accelerate, canFly, getSpeed and getAltitude methods
|
||||
try {
|
||||
AirCraft a4 = new AirCraft(700,200,100);
|
||||
// test the getAltitude method
|
||||
System.out.println(a4.getAltitude()==100);
|
||||
// test the canFly method
|
||||
System.out.println(a4.canFly() == true);
|
||||
a4.accelerate(100);
|
||||
System.out.println(a4.getSpeed() == 300);
|
||||
// test the accelerate with positive amount
|
||||
a4.accelerate(-200);
|
||||
System.out.println(a4.getSpeed() == 100);
|
||||
// test the accelerate with negative amount
|
||||
// exception
|
||||
a4.accelerate(650);
|
||||
} catch(BadSpeedSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (BadAltitudeSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
|
||||
} catch (NotEnoughSpeed e) {
|
||||
}
|
||||
catch (TrainSpeedChange e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
}
|
||||
try {
|
||||
AirCraft a5 = new AirCraft(700,200,100);
|
||||
// exception
|
||||
a5.accelerate(-300);
|
||||
} catch(BadSpeedSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (BadAltitudeSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage().equals("Current speed is "+ 200 + ", not enough speed to decelerate!"));
|
||||
}
|
||||
catch (TrainSpeedChange e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
public class BadAltitudeSetting extends Exception {
|
||||
public BadAltitudeSetting() {
|
||||
super("Altitude cannot be negative!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
public class BadSpeedSetting extends Exception {
|
||||
public BadSpeedSetting(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
|
||||
public class Car extends Vehicle{
|
||||
//constructors
|
||||
public Car(int speedLimit, int speed) throws BadSpeedSetting {
|
||||
super(speedLimit, speed);
|
||||
}
|
||||
|
||||
public Car(int speed) throws BadSpeedSetting {
|
||||
super(120, speed);
|
||||
}
|
||||
|
||||
//method
|
||||
@Override
|
||||
public boolean canFly() {return false;}
|
||||
|
||||
//test
|
||||
public static void testCar() {
|
||||
// test case for constructors
|
||||
try {
|
||||
// exception
|
||||
Car c1 = new Car(70,80);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
|
||||
}
|
||||
try {
|
||||
// exception
|
||||
Car c2 = new Car(70,-10);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
// test case for accelerate, canFly and getSpeed methods
|
||||
try {
|
||||
Car c3 = new Car(100,80);
|
||||
// test the canFly method
|
||||
System.out.println(c3.canFly() == false);
|
||||
c3.accelerate(10);
|
||||
System.out.println(c3.getSpeed() == 90);
|
||||
// test the accelerate with positive amount
|
||||
c3.accelerate(-20);
|
||||
System.out.println(c3.getSpeed() == 70);
|
||||
// test the accelerate with negative amount
|
||||
c3.accelerate(35); // exception
|
||||
} catch(BadSpeedSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!"));
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
}
|
||||
catch (TrainSpeedChange e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
}
|
||||
try {
|
||||
Car c4 = new Car(100,80);
|
||||
// exception
|
||||
c4.accelerate(-90);
|
||||
} catch(BadSpeedSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage().equals("Current speed is "+ 80 + ", not enough speed to decelerate!"));
|
||||
}
|
||||
catch (TrainSpeedChange e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
public class ExceedSpeedLimit extends Exception {
|
||||
public ExceedSpeedLimit(int speed, int amount) {
|
||||
super("Current speed is "+ speed +". Accelerate "+ amount +" will exceed the speed limit!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
public interface Movable {
|
||||
public int accelerate(int amount) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
public class NotEnoughSpeed extends Exception {
|
||||
public NotEnoughSpeed(int speed) {
|
||||
super("Current speed is "+ speed + ", not enough speed to decelerate!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
AirCraft.testAirCraft();
|
||||
Train.testTrain();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
|
||||
public class Train extends Vehicle{
|
||||
//constructor
|
||||
public Train(int speedLimit, int speed) throws BadSpeedSetting {
|
||||
super(speedLimit, speed);
|
||||
}
|
||||
|
||||
//method
|
||||
@Override
|
||||
public int accelerate(int amount) throws TrainSpeedChange {
|
||||
//if called accelerate() in Train
|
||||
throw new TrainSpeedChange();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFly() {return false;}
|
||||
|
||||
//test
|
||||
public static void testTrain() {
|
||||
// test case for constructors
|
||||
try {
|
||||
// exception
|
||||
Train t1 = new Train(350,400);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
|
||||
}
|
||||
try {
|
||||
// exception
|
||||
Train t2 = new Train(350,-100);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
// test case for accelerate, canFly and getSpeed methods
|
||||
try {
|
||||
Train t3 = new Train(350,300);
|
||||
// test the canFly method
|
||||
System.out.println(t3.canFly() == false);
|
||||
// test the getSpeed
|
||||
System.out.println(t3.getSpeed() == 300);
|
||||
// exception
|
||||
t3.accelerate(50);
|
||||
} catch(BadSpeedSetting e) {
|
||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||
} catch (TrainSpeedChange e) {
|
||||
System.out.println(e.getMessage().equals("Do not try to accelerate or decelerate the train!"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
public class TrainSpeedChange extends Exception {
|
||||
public TrainSpeedChange() {
|
||||
super("Do not try to accelerate or decelerate the train!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
public abstract class Vehicle implements Movable{
|
||||
//instance variables
|
||||
private int speedLimit;
|
||||
private int speed;
|
||||
|
||||
//constructor
|
||||
public Vehicle(int speedLimit, int speed) throws BadSpeedSetting{
|
||||
if(speedLimit >= 0 && speed >= 0) {
|
||||
if(speed <= speedLimit) {
|
||||
this.speedLimit = speedLimit;
|
||||
this.speed = speed;
|
||||
}
|
||||
else throw new BadSpeedSetting("Speed cannot be greater than speed limit!");
|
||||
}
|
||||
else throw new BadSpeedSetting("Speed cannot be negative!");
|
||||
}
|
||||
|
||||
//methods
|
||||
public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed, TrainSpeedChange {
|
||||
int tmp = this.speed + amount;
|
||||
if(tmp >= 0 && tmp <= this.speedLimit) {
|
||||
this.speed = tmp;
|
||||
return this.speed;
|
||||
}
|
||||
else {
|
||||
if(tmp < 0)
|
||||
throw new NotEnoughSpeed(speed);
|
||||
else
|
||||
throw new ExceedSpeedLimit(speed, amount);
|
||||
}
|
||||
}
|
||||
|
||||
public int getSpeed() {return this.speed;}
|
||||
|
||||
public abstract boolean canFly();
|
||||
|
||||
//test
|
||||
public static void testVehicle() {
|
||||
//The Vehicle class is abstract. We cannot create objects from this class. Therefore we cannot test anything.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user