In: Computer Science
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
// Automobile.java
public class Automobile {
public Automobile(int id, int year, double milesPerGallon, double
speed, int make, String model, String color,
String vinNumber) {
setId(id);
setYear(year);
setMilesPerGallon(milesPerGallon);
setSpeed(speed);
setMake(make);
setModel(model);
setColor(color);
setVinNumber(vinNumber);
}
private int id;
private int year;
private double milesPerGallon;
private double speed = 0;
private int make;
private String model;
private String color;
private String vinNumber;
public int getId() {
return id;
}
public void setId(int id) {
if (id < 0 || id > 99999)
id = 0;
this.id = id;
}
public int getYear() {
return year;
}
public void setYear(int year) {
if (year < 2000 || year > 2017)
year = 0;
this.year = year;
}
public double getMilesPerGallon() {
return milesPerGallon;
}
public void setMilesPerGallon(double milesPerGallon) {
if (milesPerGallon < 10 || milesPerGallon > 60)
milesPerGallon = 0;
this.milesPerGallon = milesPerGallon;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public int getMake() {
return make;
}
public void setMake(int 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 String getVinNumber() {
return vinNumber;
}
public void setVinNumber(String vinNumber) {
this.vinNumber = vinNumber;
}
public void Accelerate()
{
setSpeed(getSpeed() + 5);
}
public void Brake()
{
setSpeed(getSpeed() - 5);
}
public void Accelerate(double speed)
{
setSpeed(getSpeed() + speed);
}
public void Brake(double speed)
{
setSpeed(getSpeed() - speed);
}
@Override
public String toString() {
return "Automobile [id=" + id + ", year=" + year + ",
milesPerGallon=" + milesPerGallon + ", speed=" + speed
+ ", make=" + make + ", model=" + model + ", color=" + color + ",
vinNumber=" + vinNumber + "]";
}
}
// TestAutomobiles.java
public class TestAutomobiles {
public static void main(String[] args)
{
Automobile auto1 = new Automobile(1, 2001, 20, 5, 0, "model1",
"color1", "vinNumber1");
Automobile auto2 = new Automobile(2, 2001, 20, 15, 0, "model2",
"color2", "vinNumber2");
Automobile auto3 = new Automobile(3, 2058, 80, 25, 0, "model3",
"color2", "vinNumber3");
Automobile auto4 = new Automobile(-2, 2001, 5, 35, 0, "model4",
"color1", "vinNumber4");
System.out.println("Automobile 1: " + auto1);
System.out.println("Automobile 2: " + auto1);
System.out.println("Automobile 3: " + auto3);
System.out.println("Automobile 4: " + auto4);
auto1.Accelerate();
System.out.println("Speed of automobile 1 after accelerate: " +
auto1.getSpeed());
auto1.Brake();
System.out.println("Speed of automobile 1 after break: " +
auto1.getSpeed());
auto1.Accelerate(20);
System.out.println("Speed of automobile 1 after accelerate with
speed 20: " + auto1.getSpeed());
auto1.Brake(22);
System.out.println("Speed of automobile 1 after break with speed
22: " + auto1.getSpeed());
}
}
/*
Sample run
$ java TestAutomobiles
Automobile 1: Automobile [id=1, year=2001, milesPerGallon=20.0,
speed=5.0, make=0, model=model1, color=color1,
vinNumber=vinNumber1]
Automobile 2: Automobile [id=1, year=2001, milesPerGallon=20.0,
speed=5.0, make=0, model=model1, color=color1,
vinNumber=vinNumber1]
Automobile 3: Automobile [id=3, year=0, milesPerGallon=0.0,
speed=25.0, make=0, model=model3, color=color2,
vinNumber=vinNumber3]
Automobile 4: Automobile [id=0, year=2001, milesPerGallon=0.0,
speed=35.0, make=0, model=model4, color=color1,
vinNumber=vinNumber4]
Speed of automobile 1 after accelerate: 10.0
Speed of automobile 1 after break: 5.0
Speed of automobile 1 after accelerate with speed 20: 25.0
Speed of automobile 1 after break with speed 22: 3.0
*/