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>Question1</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.
@@ -0,0 +1,6 @@
|
||||
|
||||
public class BadSpeedSetting extends Exception {
|
||||
public BadSpeedSetting(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -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 ExceedSpeedLimit, NotEnoughSpeed;
|
||||
}
|
||||
@@ -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,8 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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) { //determine whether speed & speed limit >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{
|
||||
int tmp = this.speed + amount; //a temporary speed that implements the accelerated speed
|
||||
if(tmp >= 0 && tmp <= this.speedLimit) { //if satisfied
|
||||
this.speed = tmp;
|
||||
return this.speed;
|
||||
}
|
||||
else {
|
||||
if(tmp < 0)
|
||||
throw new ExceedSpeedLimit(speed, amount);
|
||||
else
|
||||
throw new NotEnoughSpeed(speed);
|
||||
}
|
||||
}
|
||||
|
||||
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