Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an Automobile class for a dealership. Include fields for an ID number, ma

ID: 3741323 • Letter: C

Question

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.

Explanation / Answer

Automobile.java

public class Automobile {

@Override

public String toString() {

return "Automobile [id=" + id + ", make=" + make + ", model=" + model + ", color=" + color + ", year=" + year

+ ", vin=" + vin + ", miles=" + miles + ", speed=" + speed + "]";

}

private int id;

private String make, model, color;

private int year, vin;

private double miles, speed;

public int getId() {

return id;

}

public void setId(int id) {

if(id > 0 && id < 9999)

this.id = id;

else

this.id = 0;

}

public String getMake() {

return make;

}

public void setMake(String make) {

this.make = make;

}

public String getModel() {

return model;

}

public void setModel(String model) {

this.model = model;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public int getYear() {

return year;

}

public void setYear(int year) {

if(year >= 2000 && year <= 2017)

this.year = year;

else

this.year = 0;

}

public int getVin() {

return vin;

}

public void setVin(int vin) {

this.vin = vin;

}

public double getMiles() {

return miles;

}

public void setMiles(double miles) {

if(miles >= 10 && miles >= 60)

this.miles = miles;

else

this.miles = 0;

}

public double getSpeed() {

return speed;

}

public void setSpeed(double speed) {

this.speed = speed;

}

public void Accelerate() {

speed += 5;

}

public void Accelerate(double increaseSpeed) {

speed += increaseSpeed;

}

public void Brake() {

speed -= 5;

}

public Automobile(int id, String make, String model, String color, int year, int vin, double miles) {

this.setId(id);

this.setMake(make);

this.setModel(model);

this.setColor(color);

this.setYear(year);

this.setYear(year);

this.setMiles(miles);

this.speed = 0;

}

public void Brake(double decreaseSpeed) {

speed -= decreaseSpeed;

}

}

TestAutomobiles.java

public class TestAutomobiles {

public static void main(String[] args){

Automobile auto1 = new Automobile(1, "Honda", "Civi", "Red", 2014, 143, 4.5);

auto1.Accelerate();

System.out.println(auto1.getSpeed());

auto1.Accelerate(10);

System.out.println(auto1.getSpeed());

auto1.Brake();

System.out.println(auto1.getSpeed());

auto1.Brake(3);

System.out.println(auto1.getSpeed());

System.out.println(auto1);

Automobile auto2 = new Automobile(-4, "Toyoto", "Vento", "Black", 1999, 37, 9);

System.out.println(auto2);

}

}

**Comment for any further queries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote