Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class named Car that represents a car. Your Car class should have the fo

ID: 3540436 • Letter: W

Question

Write a class named Car that represents a car. Your Car class should have the followning:

1. Instance variables

i. make and model of the car

ii. Color of the car

iii. The car's price

iv. The car's mileage (total miles traveled, not miles per gallon)

2. Two overloaded constructors

i. A constructor that takes no parameters. This constructor should set the mileage to 0 and set some default values (use whatever you want ) for the make, model, color and price.

ii. A constructor that allows you to set values for the make, model, color and price. This constructor should also set the mileage to 0.

3. Methods

public void setPrice(double p)

This should update the instance variable for price with the specified value.

public void paint(String c)

This should "paint " the car by changing the instance variable for color with the specified value.

public void displayCarInfo()

This should display all available information on the car, including its make, model, coor, price, and mileage.

public void travel(double distance)

This should display a message saying that the car is traveling for the specified distance. This method should also increase the value of the mileage attribute accordingly.

After the Car class, write a separate client program containing a main method that does the following actions:

1. Create two new Car objects: a black Porsche Cayman S with a price of  $ 63,800, and a blue Nissan Leaf with a price of $ 28,800.

2. Display information for each object by calling its displayCarInfo method.

3. Paint the Cayman purple and the Leaf green.

4. Make the Cayman travel 12,000 miles and the Leaf 8,000 miles.

5. Change the price of the Cayman to $50,000 and the price of the Leaf to $ 24,500.

6. Call displayCarInfo on each object again to see how the instance variables have changed.

Explanation / Answer



import java.util.*;


public class CarClient {


public static void main(String[] args) {

CarClient e = new CarClient();

//1 ques

Car car1 = new Car("Porsche Cayman S", "black", 63800, 0);

Car car2 = new Car("Nisson Leaf", "blue", 28800, 0);

  

//2 ques

System.out.println("car1 "+car1.displayCarInfo());

System.out.println("car2 "+car2.displayCarInfo());

  

//3 ques

car1.paint("purple");

car2.paint("green");

  

//4 ques

  

car1.travel(12000);

car2.travel(8000);

  

  

//5 ques

car1.setPrice(50000);

car2.setPrice(245000);

  

//6 ques

System.out.println("car1 "+car1.displayCarInfo());

System.out.println("car2 "+car2.displayCarInfo());

  

  

}

}


class Car{


String makeAndModel;

String color;

double price;

double mileage;


public Car() {

this.color = "black";

this.makeAndModel="Audi Q5";

this.price = 98000;

this.mileage=0;

}


  

public Car(String makeAndModel, String color, double price, long mileage) {

this.makeAndModel = makeAndModel;

this.color = color;

this.price = price;

this.mileage = mileage;

}


public void paint(String color) {

this.color = color;

}


public void setPrice(double price) {

this.price = price;

}


public void travel(double distance) {

System.out.println("The car is travelling "+distance);

this.mileage = this.mileage + distance;

}


public String displayCarInfo() {

return "Car{" + "makeAndModel=" + makeAndModel + ", color=" + color + ", price=" + price + ", mileage=" + mileage + '}';

}

  

  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote