marcelinalinaa / CarSimulator

Autonomous car driving simulation in Java which implements object-oriented programming.
0 stars 2 forks source link

Pedals.java #3

Closed marcelinalinaa closed 3 years ago

marcelinalinaa commented 3 years ago

public class Pedals {

Speedometer speedometer;
public void accelerate(int n){ // parameternya adalah berapa banyak kecepatan yang ingin ditambahkan
    while(speedometer.speed < Speedometer.MAX_SPEED) {
        speedometer.speed++;
    }   
}
public void reduceSpeed(int n){     //parameternya adalah berapa banyak kecepatan yang ingin dikurangi
    while(speedometer.speed>0){
        speedometer.speed--; 
    }
}

}

TEricJ commented 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);
        }
    }
}

}

marcelinalinaa commented 3 years ago

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";
    }

}