Design a class named Car to represent a car. The class should have the following
ID: 3732632 • Letter: D
Question
Design a class named Car to represent a car. The class should have the following data fields (name each data field appropriately):
A private string data field to specify the make of the car (the default is "Make")
A private string data field to specify the model of the car (the default is "Model")
A private int data field to specify the year of the car (the default is 2018)
A private double data field to specify the cost of the car (the default is 30,000)
A private string data field to specify the color of the car (the default is "White")
The class should have the following methods:
Accessor and mutator methods for all data fields
A no-arg constructor that creates a default car
A constructor that takes in values for all five data fields and sets them
A method named toString() that returns a string description for the car
Draw the UML diagram for the Car class and implement the class. Write a test program (separate class, but can be in the same file) that creates two cars:
Car 1
Create this car using the no-arg constructor
Use the appropriate mutator method to set the car's make to "Toyota"
Use the appropriate accessor method to display the car's new make value
Use the appropriate mutator method to set the car's model to "Highlander"
Use the appropriate accessor method to display the car's new model value
Use the appropriate mutator method to set the car's cost to $31,695
Use the appropriate accessor method to display the car's new cost value
Car 2Create this car using the constructor that accepts values
Make: Kia
Model: Optima
Year: 2016
Cost: $22,500
Color: Red
Print out the values for Car 2 using the toString() method
Explanation / Answer
Car.java
public class Car {
//Declaring instance variables
private String make;
private String model;
private int year;
private double cost;
private String color;
//Zero argumented constructor
public Car()
{
this.make="Make";
this.model="Model";
this.year=2018;
this.cost=30000;
this.color="White";
}
//Parameterized constructor
public Car(String make, String model, int year, double cost,
String color) {
this.make = make;
this.model = model;
this.year = year;
this.cost = cost;
this.color = color;
}
// getters and setters
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 int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Car [make=" + make + ", model=" + model + ", year=" + year
+ ", cost=" + cost + ", color=" + color + "]";
}
public static void main(String[] args) {
//Creating an instance of Car class
Car car1=new Car();
//calling the setters on the Car1
car1.setMake("Toyota");
car1.setModel("Highlander");
car1.setCost(31695);
//calling the getters on the Car1
System.out.println("Car#1 ");
System.out.println("New Make:"+car1.getMake());
System.out.println("New Model:"+car1.getModel());
System.out.println("New Cost:"+car1.getCost());
//Creating an instance of Car class using parameterized constructor
Car car2=new Car("Kia","Optima",2016,22500,"Red");
System.out.println(" Car#2 ");
System.out.println(car2);
}
}
______________________
Output:
Car#1
New Make:Toyota
New Model:Highlander
New Cost:31695.0
Car#2
Car [make=Kia, model=Optima, year=2016, cost=22500.0, color=Red]
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.