Create an Automobile class for a dealership. Include fields for an ID number, ma
ID: 3740847 • 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.
Note: Please make this as simple as possible. We're up to Chapter 5 in Object Oriented Programming. Also, please include a documentation as to what each line is and what it does. Thank you.
Explanation / Answer
Automobile.java
public class Automobile
{
//Declaring instance variables
private int id;
private String make;
private String model;
private String color;
private int year;
private int vin_number;
private int miles_per_gallon;
private int speed;
//Parameterized constructor
public Automobile(int id, String make, String model, String color,
int year, int vin_number, int miles_per_gallon, int speed) {
super();
//Calling the setters methods by passing the parameters
setId(id);
setYear(year);
setMiles_per_gallon(miles_per_gallon);
setMake(make);
setModel(model);
setColor(color);
setVin_number(vin_number);
setSpeed(0);
}
//Setters and getters
public int getId() {
return id;
}
public void setId(int id) {
if(id<0 || id >9999)
{
this.id=0;
}
else
{
this.id = id;
}
}
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=0;
}
else
{
this.year = year;
}
}
public int getVin_number() {
return vin_number;
}
public void setVin_number(int vin_number) {
this.vin_number = vin_number;
}
public int getMiles_per_gallon() {
return miles_per_gallon;
}
public void setMiles_per_gallon(int miles_per_gallon) {
if(miles_per_gallon<10 || miles_per_gallon>60)
{
this.miles_per_gallon=0;
}
else
{
this.miles_per_gallon = miles_per_gallon;
}
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed =speed;
}
//accelerate() method is used to increase the speed of the car by 5
public int accelerate()
{
setSpeed(getSpeed()+5);
return getSpeed();
}
//brake() method is used to decrease the speed of the car by 5
public int brake()
{
setSpeed(getSpeed()-5);
return getSpeed();
}
/*accelerate() method is used to increase
* the speed of the car based on the supplied speed value
*/
public int accelerate(int speed)
{
setSpeed(getSpeed()+speed);
return getSpeed();
}
/*brake() method is used to decrease
* the speed of the car based on the supplied speed value
*/
public int brake(int speed)
{
//calling the setters method
setSpeed(getSpeed()-speed);
return getSpeed();
}
}
_______________________
TestAutomobiles.java
public class TestAutomobiles {
public static void main(String[] args) {
System.out.println("______CAR 1______");
//Creating the Automobile Class Object by passing the parameters as input
Automobile am1=new Automobile(111,"Ford","Ford Fiesta","Black",2014,233,20,50);
//Displaying the make of the car
System.out.println("Car Make:"+am1.getMake());
//Displaying the Model of the car
System.out.println("Car Model:"+am1.getModel());
//Displaying the year
System.out.println("Car Model:"+am1.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am1.accelerate());
System.out.println("After Acceleration, Speed :"+am1.accelerate());
System.out.println("After Acceleration, Speed :"+am1.accelerate());
//Displaying the Speed of the car after applying the brakes
System.out.println("After Applying Brakes, Speed :"+am1.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am1.accelerate(30));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am1.brake(15));
System.out.println(" ______CAR 2______");
//Creating the Automobile Class Object by passing the parameters as input
Automobile am2=new Automobile(222,"Honda","Verna","White",2016,450,18,30);
//Displaying the make of the car
System.out.println("Car Make:"+am2.getMake());
//Displaying the Model of the car
System.out.println("Car Model:"+am2.getModel());
//Displaying the year
System.out.println("Car Model:"+am2.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
//Displaying the Speed of the car after applying the brakes
System.out.println("After Applying Brakes, Speed :"+am2.brake());
System.out.println("After Applying Brakes, Speed :"+am2.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am2.accelerate(40));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am2.brake(20));
System.out.println(" ______CAR 3______");
Automobile am3=new Automobile(333,"Honda","BRIO","GREY",2015,347,15,45);
//Displaying the make of the car
System.out.println("Car Make:"+am3.getMake());
//Displaying the Model of the car
System.out.println("Car Model:"+am3.getModel());
//Displaying the year
System.out.println("Car Model:"+am3.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am3.accelerate());
System.out.println("After Acceleration, Speed :"+am3.accelerate());
System.out.println("After Acceleration, Speed :"+am3.accelerate());
//Displaying the Speed of the car after applying the brakes
System.out.println("After Applying Brakes, Speed :"+am3.brake());
System.out.println("After Applying Brakes, Speed :"+am3.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am3.accelerate(50));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am3.brake(35));
}
}
____________________
Output:
______CAR 1______
Car Make:Ford
Car Model:Ford Fiesta
Car Model:2014
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Applying Brakes, Speed :10
After Acceleration, Speed :40
After Applying Brakes, Speed :25
______CAR 2______
Car Make:Honda
Car Model:Verna
Car Model:2016
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Acceleration, Speed :20
After Applying Brakes, Speed :15
After Applying Brakes, Speed :10
After Acceleration, Speed :50
After Applying Brakes, Speed :30
______CAR 3______
Car Make:Honda
Car Model:BRIO
Car Model:2015
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Applying Brakes, Speed :10
After Applying Brakes, Speed :5
After Acceleration, Speed :55
After Applying Brakes, Speed :20
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.