Create an Automobile class for a dealership. Include fields for an ID number, ma
ID: 3741572 • 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. ***DO BOTH FILES NEED TO BE COPIED AND PASTED INTO ONE FILE?*** Thank You
Explanation / Answer
Please find my implementation:
################### Automobile.java ###################
//---------------Automobile.java----------
import java.util.Scanner;
public class Automobile
{
//Declare variables
int ID, year, vin_number, miles_p_gallon, speed;
String make, model, color;
Scanner scan = new Scanner(System.in);
//default constructor
Automobile()
{
ID = 0;
year = 0;
vin_number = 0;
miles_p_gallon = 0;
speed = 0;
make = "NO MAKE";
model = "NO MAKE";
color = "NO MAKE";
}
//parameterized constructor
Automobile(int id, int y, int vin_num, int m_p_g, int s, String mak, String mod, String col) {
ID = setID(id);
year = setYear(y);
vin_number = vin_num;
miles_p_gallon = setMiles_p_gallon(m_p_g);
speed = s;
make = mak;
model = mod;
color = col;
}
//mutator methods
//detUd method
int setID(int id)
{
if (id > 9999 || id < 0)
id = 0;
return id;
}
//setYear method
int setYear(int y) {
if (y < 2000 || y > 2017)
y = 0;
return y;
}
//setMiles_p_gallon method
int setMiles_p_gallon(int m_p_g)
{
if (m_p_g < 10 || m_p_g > 60)
m_p_g = 0;
return m_p_g;
}
//accessor methods
//getId() method
int getID()
{
return ID;
}
//getYear method
int getYear() {
return year;
}
//getVin_number method
int getVin_number()
{
return vin_number;
}
//getMiles_p_gallon method
int getMiles_p_gallon()
{
return miles_p_gallon;
}
//getSpeed method
int getSpeed()
{
return speed;
}
//getMake method
String getMake()
{
return make;
}
String getModel()
{
return model;
}
String getColor()
{
return color;
}
void Accelerate()
{
speed += 5;
}
void Brake()
{
speed -= 5;
}
//overriding method Brake method
void Brake(int deSpeed)
{
speed -= deSpeed;
}
//overriding method Accelarate method
public void Accelarate(int incSpeed)
{
speed += incSpeed;
}
}
################### TestAutomobiles.java ###################
class TestAutomobiles
{
//main method
public static void main (String[] args) throws java.lang.Exception
{
//create three automobile class objects
Automobile obj1=new Automobile(-1,2018,201511,50,0,"NISSAN","GTR","BLACK");
Automobile obj2=new Automobile(2,2012,201202,50,0,"ASTON MARTIN","DB2","WHITE");
Automobile obj3=new Automobile(3,2013,201303,30,0,"alejandria","DB3","Grey");
System.out.println(" -------------AUTOMOBILE 1-------------");
//call the methods through the object and print the //values
System.out.println("AUTOMOBILE NAME - "+obj1.getMake()+" "+obj1.getModel());
System.out.println("AUTOMOBILE COLOUR - "+obj1.getColor());
System.out.println("AUTOMOBILE ID - "+obj1.getID());
System.out.println("AUTOMOBILE MAKE YEAR - "+obj1.getYear());
System.out.println("AUTOMOBILE Vin Number - "+obj1.getVin_number());
System.out.println("AUTOMOBILE Miles per Galon - "+obj1.getMiles_p_gallon());
System.out.println("AUTOMOBILE Current Speed - "+obj1.getSpeed());
obj1.Accelerate();
System.out.println("AUTOMOBILE Accelerated Speed - "+obj1.getSpeed());
obj1.Brake();
System.out.println("AUTOMOBILE Speed after Brake - "+obj1.getSpeed());
//call the methods through the object and //print the values
System.out.println(" -------------AUTOMOBILE 2----- --------");
System.out.println("AUTOMOBILE NAME - "+obj2.getMake()+" "+obj2.getModel());
System.out.println("AUTOMOBILE COLOUR - "+obj2.getColor());
System.out.println("AUTOMOBILE ID - "+obj2.getID());
System.out.println("AUTOMOBILE MAKE YEAR - "+obj2.getYear());
System.out.println("AUTOMOBILE Vin Number - "+obj2.getVin_number());
System.out.println("AUTOMOBILE Miles per Galon - "+obj2.getMiles_p_gallon());
System.out.println("AUTOMOBILE Current Speed - "+obj2.getSpeed());
obj2.Accelarate(20); //overloaded function call System.out.println("AUTOMOBILE Accelerated Speed - "+obj2.getSpeed());
obj2.Brake(10); //overloaded function call
System.out.println("AUTOMOBILE Speed after Brake - "+obj2.getSpeed());
//call the methods through the object and print //the values
System.out.println(" -------------AUTOMOBILE 3-------------");
System.out.println("AUTOMOBILE NAME - "+obj3.getMake()+" "+obj3.getModel());
System.out.println("AUTOMOBILE COLOUR - "+obj3.getColor());
System.out.println("AUTOMOBILE ID - "+obj3.getID());
System.out.println("AUTOMOBILE MAKE YEAR - "+obj3.getYear());
System.out.println("AUTOMOBILE Vin Number - "+obj3.getVin_number());
System.out.println("AUTOMOBILE Miles per Galon - "+obj3.getMiles_p_gallon());
System.out.println("AUTOMOBILE Current Speed - "+obj3.getSpeed());
obj3.Accelarate(30); //overloaded function call
System.out.println("AUTOMOBILE Accelerated Speed - "+obj3.getSpeed());
obj3.Brake(10); //overloaded function call
System.out.println("AUTOMOBILE Speed after Brake - "+obj3.getSpeed());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.