Answer:
public class TestAutomobiles {
public static void main(String args[]) {
Automobile obj = new Automobile(1, "Wolkswagen", "Golf", 2007, "GR1GR", 20, 120, "Grey");
System.out.println("Initial Speed: " + obj.getSpeed());
System.out.println("Current Speed with acceleration 100: " + obj.accelerate(100));
System.out.println("Current Speed after using brake : " + obj.brake());
}
}
class Automobile {
private int idNumber, year, milesPerGallon, speed = 0;
private String model, vinNumber, make, color;
public void setIdNumber(int idNumber){
if (idNumber >= 0 && idNumber <= 9999)
this.idNumber = idNumber;
else this.idNumber = 0;
}
public void setModel(String model){
this.model = model;
}
public void setYear(int year){
if (year >= 2000 && year <= 2017)
this.idNumber = idNumber;
else this.year = 0;
}
public void setVinNumber(String vinNumber){
this.vinNumber = vinNumber;
}
public void setMilesPerGalon(int milesPerGallon){
if (milesPerGallon >= 10 && year <= 60)
this.milesPerGallon = milesPerGallon;
else this.milesPerGallon = 0;
}
public void setSpeed(int speed){
this.speed = speed;
}
public void setMake(String make){
this.make = make;
}
public void setColor(String color){
this.color = color;
}
public int getIdNumber(){return idNumber;}
public String getModel(){return model;}
public int getYear(){return year;}
public String getVinNumber(){return vinNumber;}
public int getMilesPerGallon(){return milesPerGallon;}
public int getSpeed(){return speed;}
public String getMake(){return make;}
public String getColor(){return color;}
public int accelerate() {
setSpeed(speed + 5);
return speed;
}
public int brake() {
setSpeed(speed - 5);
return speed;
}
public int accelerate(int s) {
setSpeed(speed + s);
return speed;
}
public int brake(int s) {
setSpeed(speed - s);
return speed;
}
public Automobile(int idNumber, String make, String model, int year, String vinNumber, int milesPerGallon, int speed, String color){
setIdNumber(idNumber);
setModel(model);
setYear(year);
setVinNumber(vinNumber);
setMilesPerGalon(milesPerGallon);
setSpeed(speed);
setMake(make);
setColor(color);
}
}
Explanation:
Required <em>variables</em> are declared as private members.
Their <em>setters</em> and <em>getter</em> are created.
Two <em>accelearate</em> and two <em>break</em> methods are created.
One <em>constructor</em> taking all variables as parameter is created.
In the main, I tested a few functions to check if they work properly.