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

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

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,9 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
}
}

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) {
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;
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.
}
}