Create an Automobile class for a dealership. Include fields for an ID number, make, model, color, year, vin number, miles per gallon, and speed. Include get and set methods for each field. Do not allow the ID to be negative or more than 9999; if it is, set the ID to 0. Do not allow the year to be earlier than 2000 or later than 2017; if it is, set the year to 0. Do not allow the miles per gallon to be less than 10 or more than 60; if it is, set the miles per gallon to 0. Car speed should be initialized as 0. Include a constructor that accepts arguments for each field value and uses the set methods to assign the values. Also, write two methods, Accelerate () and Brake (). Whenever Accelerate () is called, increase the speed by 5, and whenever Brake () is called, decrease the speed by 5. To allow users to specify the speed to increase (or decrease), create two overloading methods for Accelerate () and Brake () that accept a single parameter specifying the increasing (or decreasing) speed. Write an application that declares several Automobile objects and demonstrates that all the methods work correctly. Save the files as Automobile.java and TestAutomobiles.java.

Respuesta :

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 variables are declared as private members.

Their setters and getter are created.

Two accelearate and two break methods are created.

One constructor taking all variables as parameter is created.

In the main, I tested a few functions to check if they work properly.