This commit is contained in:
ldy
2026-03-01 23:18:55 -05:00
commit 67f753a5d1
3087 changed files with 218259 additions and 0 deletions

View File

@@ -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>

View File

@@ -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>

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@@ -0,0 +1,6 @@
public class BadSpeedSetting extends Exception {
public BadSpeedSetting(String msg) {
super(msg);
}
}

View File

@@ -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!");
}
}

View File

@@ -0,0 +1,4 @@
public interface Movable {
public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed;
}

View File

@@ -0,0 +1,6 @@
public class NotEnoughSpeed extends Exception {
public NotEnoughSpeed(int speed) {
super("Current speed is "+ speed +", not enough speed to decelerate!");
}
}

View File

@@ -0,0 +1,8 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
}
}

View File

@@ -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.
}
}