180 lines
4.7 KiB
Java
180 lines
4.7 KiB
Java
/*
|
|
* Authro: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
|
* Date: 2022/03/09
|
|
* Description: This is the main class of AutoRobot.
|
|
*/
|
|
|
|
public class AutoRobot {
|
|
/**
|
|
* current speed of the robot.
|
|
*/
|
|
private double currentSpeed;
|
|
|
|
/**
|
|
* name of the robot
|
|
*/
|
|
private String name;
|
|
|
|
/**
|
|
* Constructor for objects of class AutoRobot. Default name is "xyz".
|
|
* Initial speed is 0.
|
|
*/
|
|
public AutoRobot() {
|
|
// initialise instance variables
|
|
this.currentSpeed = 0;
|
|
this.name = "xyz";
|
|
}
|
|
|
|
/**
|
|
* Getter method for name.
|
|
*
|
|
* @return name of the robot.
|
|
*/
|
|
public String getName() {
|
|
return this.name;
|
|
}
|
|
|
|
/**
|
|
* Getter method for current speed.
|
|
*
|
|
* @return current speed of the robot.
|
|
*/
|
|
public double getCurrentSpeed() {
|
|
return this.currentSpeed;
|
|
}
|
|
|
|
/**
|
|
* Increase the speed of the robot by 5. The speed exceed 20 will
|
|
* print "Dangerous" message and do nothing.
|
|
*
|
|
* @return current speed of the robot.
|
|
*/
|
|
public void accelerate() {
|
|
if (this.currentSpeed >= 20) {
|
|
System.out.println("Dangerous! Please stay within speed limit.");
|
|
return;
|
|
}
|
|
|
|
this.currentSpeed += 5;
|
|
}
|
|
|
|
/**
|
|
* Decrase the speed of the robot by 5. The speed can not be less than 0.
|
|
*
|
|
* @return current speed of the robot.
|
|
*/
|
|
public void brake() {
|
|
if (this.currentSpeed - 5 <= 0) {
|
|
this.currentSpeed = 0;
|
|
return;
|
|
}
|
|
|
|
this.currentSpeed -= 5;
|
|
}
|
|
|
|
/**
|
|
* Print out current status.
|
|
*
|
|
* @return void.
|
|
*/
|
|
public void printStatus() {
|
|
System.out.println(String.format("Hello there, my name is %s and my current speed is %.1f",
|
|
this.name, this.currentSpeed));
|
|
}
|
|
|
|
/**
|
|
* Setter method for name.
|
|
*
|
|
* @param name name of the robot.
|
|
* @return void.
|
|
*/
|
|
public void setName(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
/**
|
|
* Static test the class AutoRobot.
|
|
*
|
|
* @return void.
|
|
*/
|
|
public static void testAutoRobot() {
|
|
AutoRobot robot = new AutoRobot();
|
|
|
|
// print current status
|
|
robot.printStatus();
|
|
|
|
// print true for default value
|
|
System.out.println(robot.getName().equals("xyz"));
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 0));
|
|
System.out.println(robot.getCurrentSpeed() == 0);
|
|
|
|
// change name and print current status
|
|
robot.setName("ABC");
|
|
|
|
// print true for name
|
|
System.out.println(robot.getName().equals("ABC"));
|
|
|
|
// accelerate and print true for current speed
|
|
robot.accelerate();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 5));
|
|
|
|
// accelerate and print true for current speed
|
|
robot.accelerate();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 10));
|
|
|
|
// brake and print true for current speed
|
|
robot.brake();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 5));
|
|
|
|
// accelerate and print true for current speed
|
|
robot.accelerate();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 10));
|
|
|
|
// accelerate and print true for current speed
|
|
robot.accelerate();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 15));
|
|
|
|
// accelerate and print true for current speed
|
|
robot.accelerate();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 20));
|
|
|
|
// accelerate and print true for current speed
|
|
robot.accelerate();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 20));
|
|
|
|
// break and print true for current speed
|
|
robot.brake();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 15));
|
|
|
|
// brake and print true for current speed
|
|
robot.brake();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 10));
|
|
|
|
// brake and print true for current speed
|
|
robot.brake();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 5));
|
|
|
|
// brake and print true for current speed
|
|
robot.brake();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 0));
|
|
|
|
// brake and print true for current speed
|
|
robot.brake();
|
|
System.out.println(doubleEquals(robot.getCurrentSpeed(), 0));
|
|
|
|
// print current status
|
|
robot.printStatus();
|
|
}
|
|
|
|
/**
|
|
* Compare double value.
|
|
*
|
|
* @param a double value.
|
|
* @param b double value.
|
|
* @return true if a and b are equal.
|
|
*/
|
|
public static boolean doubleEquals(double a, double b) {
|
|
return Math.abs(a - b) < 0.00001;
|
|
}
|
|
}
|