Closed marcelinalinaa closed 3 years ago
public class Pedals { private Speedometer speedometer;
public void accelerate(double n){
for(double i = 1.0; i < n; i++){
if(speedometer.getSpeed() < Speedometer.MAX_SPEED){
speedometer.setSpeed(speedometer.getSpeed() + 1);
} else {
speedometer.setSpeed(200.0);
break;
}
}
}
public void accelerateToN(double n){
while(speedometer.getSpeed() < n){
speedometer.setSpeed(speedometer.getSpeed() + 1);
if(speedometer.getSpeed() > n){
speedometer.setSpeed(n);
}
}
}
public void decelerate(double n){
for(double i = 1.0; i < n; i++){
if(speedometer.getSpeed() > 0){
speedometer.setSpeed(speedometer.getSpeed() - 1);
} else {
speedometer.setSpeed(0);
break;
}
}
}
public void decelerateToN(double n){
while(speedometer.getSpeed() > n){
speedometer.setSpeed(speedometer.getSpeed() - 1);
if(speedometer.getSpeed() < n){
speedometer.setSpeed(n);
}
}
}
}
package project;
public class Speedometer { int speed; private int miles;
public static final int MAX_SPEED = 200;
public Speedometer() {
speed = 0;
miles = 0;
}
public Speedometer(int speed, int miles) {
setSpeed(speed);
setMiles(miles);
}
public void setSpeed(int speed) {
if(speed<=MAX_SPEED && speed>=0)
this.speed = speed;
}
public int getSpeed() {
return this.speed;
}
public int getMiles() {
return miles;
}
public void setMiles(int miles) {
this.miles = miles;
}
public String displaySpeed() {
return "Your car's current speed is" + this.getSpeed() + "km/ h";
}
}
public class Pedals {
}