72 lines
1.7 KiB
Java
72 lines
1.7 KiB
Java
/*
|
|
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
|
* Date: 2022-04-24
|
|
* Description: Train class inherited from Vehicle class
|
|
*/
|
|
|
|
public class Train extends Vehicle {
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param speedLimit the speed limit of the train
|
|
* @param speed the speed of the train
|
|
* @throws BadSpeedSetting
|
|
*/
|
|
public Train(int speedLimit, int speed) throws BadSpeedSetting {
|
|
super(speedLimit, speed);
|
|
}
|
|
|
|
/**
|
|
* Train can not fly.
|
|
*
|
|
* @return false
|
|
*/
|
|
@Override
|
|
public boolean canFly() {
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Tain speed can not be changed.
|
|
*
|
|
* @throws TrainSpeedChange
|
|
*/
|
|
@Override
|
|
public int accelerate(int amount) throws TrainSpeedChange {
|
|
throw new TrainSpeedChange("Do not try to accelerate or decelerate the train!");
|
|
}
|
|
|
|
/**
|
|
* Test.
|
|
*/
|
|
public static void testTrain() {
|
|
try {
|
|
Train train;
|
|
try {
|
|
train = new Train(-1, -1);
|
|
} catch (BadSpeedSetting e) {
|
|
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
|
}
|
|
try {
|
|
train = new Train(100, -1);
|
|
} catch (BadSpeedSetting e) {
|
|
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
|
}
|
|
try {
|
|
train = new Train(100, 101);
|
|
} catch (BadSpeedSetting e) {
|
|
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
|
|
}
|
|
train = new Train(100, 50);
|
|
System.out.println(train.getSpeed() == 50);
|
|
try {
|
|
train.accelerate(1);
|
|
} catch (TrainSpeedChange e) {
|
|
System.out.println(e.getMessage().equals("Do not try to accelerate or decelerate the train!"));
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println("Error: uncaught exception " + e.getClass() + ": " + e.getMessage());
|
|
}
|
|
}
|
|
}
|