Question

In: Computer Science

Create an Automobile class for a dealership. Include fields for an ID number, make, model, color,...

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

Solutions

Expert Solution

// 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

*/


Related Solutions

Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
Create a class named Student. Student has fields for an ID number, number of credit hours...
Create a class named Student. Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. Student also has a field for grade point average. Include a method to compute the grade point average field by dividing...
make a Pay class that had fields for a single employee's name, ID number, hourly pay,...
make a Pay class that had fields for a single employee's name, ID number, hourly pay, number of hours worked. use appropriate getter and setter methods, and a constructor accepting the employees name, ID number, hourly pay, and number of hours worked as arguments. The Pay class should also have a method that returns the employee's gross pay (gross pay = hourly pay* number of hours worked). In addition include a toString method that prints to screen the employee name,...
In Chapter 8, you created a Salesperson class with fields for an ID number and sales...
In Chapter 8, you created a Salesperson class with fields for an ID number and sales values. Now, create an application that allows a user to enter values for an array of seven Salesperson objects. Offer the user the choice of displaying the objects in order by either (I)D number or (S)ales value. ----------------------- Salesperson.java public class Salesperson {    private int id;    private double sales;    Salesperson(int idNum, double amt)    {       id = idNum;       sales = amt;    }    public int getId()...
1. Create a class named Mobile 2. Add IEMICode, processor, make, model and color properties to...
1. Create a class named Mobile 2. Add IEMICode, processor, make, model and color properties to the Mobile class 3. Create a methods connectBlueTooth, sendMessage, changeColor, displayInfo in the class Mobile. 4. Write a python script that creates two instances of the class Mobile, changes their colors to Black and Pink and prints a message to the console to display the object attributes using the displayInfo method 5. Run the program and observe the message in Console
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
Create a TeeShirt class for Toby’s Tee Shirt Company. Fields include: orderNumber - of type int...
Create a TeeShirt class for Toby’s Tee Shirt Company. Fields include: orderNumber - of type int size - of type String color - of type String price - of type double Create set methods for the order number, size, and color and get methods for all four fields. The price is determined by the size: $22.99 for XXL or XXXL, and $19.99 for all other sizes. Create a subclass named CustomTee that descends from TeeShirt and includes a field named...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy()...
This code in java: Create a class named car. A car has color, model, company, registration...
This code in java: Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car. Kindly add some other functionalities like refuel
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT