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: 3869608 • Letter: W

Question

Write a class named Car that represents a car. Your Car class should have the following: Instance variables Make and model of the car Color of the car The car's price The car's mileage (total miles traveled, not miles per gallon) Two overloaded constructors 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. A constructor that allows you to set values for the make, model, color, and price. This constructor should also set the mileage to 0. Methods public void se(Price(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 displayCarlnfo() This should display all available information on the car, including its make, model, color, 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. Once you've finished your Car class, write a separate client program containing a main method that does the following actions: 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 $29,010. (If you'd like to buy me one of the former - preferably with a manual transmission - I'll consider a few extra credit points) Display information for each object by calling its displayCarInfo method. Paint the Cayman purple and the Leaf green. Both cars get stolen and taken for (really long) joyrides! Make the Cayman travel 12,000 miles and the Leaf travel 8,000 miles. Change the price of the Cayman to $50,000 and the price of the Leaf to $24,500. Call displayC'arInfo on each object again to see how the instance variables have changed.

Explanation / Answer

Hey. There are two classes, one is the Car class in a file named Car.java, and a Program.java containing a java class called Program, to test the Car class. (compile with "javac Car.java Program.java") and run with "java Program".

Contents of the class with helpful explanations:

Car.java:

public class Car {

// instance variables are set to private for data safety in object oriented programming.

private String make;

private String model;

private double price;

private double mileage;

private String color;

// below is a default, no-arguments constructor with some default values for the instance variables.

public Car() {

this.mileage = 0;

this.model = "Lamborghini Gallardo";

this.make = "LP640";

this.color = "Red";

this.price = 100000.0;

}

// this is a overloaded constructor allowing us to provide data for the instance variables.

public Car(String model, String make, String color, double price) {

this.mileage = 0;

this.model = model;

this.make = make;

this.color = color;

this.price = price;

}

// setPrice updates the instance variable to the passed value.

public void setPrice(double p) {

this.price = p;

}

// similarly this paint function sets the car color to the new passed value.

public void paint(String c) {

this.color = c;

}

// this display function prints out the current data of the instance variables when called.

public void displayCarInfo() {

System.out.println("Car model: " + this.model);

System.out.println("Car make: " + this.make);

System.out.println("Car color: " + this.color);

System.out.println("Car price: " + this.price);

System.out.println("Car mileage (total miles travelled): " + this.mileage);

}

// this function prints that a distance is being travelled and that distance is added to the current mileage (total miles travelled) of the car (stored in the mileage instance variable)

public void travel(double distance) {

System.out.println("The car '" + this.model + " " + this.make + "' is travelling a distance of " + distance + " miles.");

this.mileage = this.mileage + distance;

}

}

# ----------------------

Program.java:

public class Program {

// we use this separate class and program to test the Car class and its variables and functions.

public static void main(String[] args) {

System.out.println("Here we create two new Car objects, then display their information.");

Car porschecaymans = new Car("Porsche Cayman", "S", "Black", 63800);

Car nissanleaf = new Car("Nissan Leaf", "2014", "Blue", 29010);

porschecaymans.displayCarInfo();

nissanleaf.displayCarInfo();

System.out.println("Now we paint the Cayman purple and the Leaf green.");

porschecaymans.paint("Purple");

nissanleaf.paint("Green");

System.out.println("We've painted the cars, now we add travel distance to each.");

porschecaymans.travel(12000);

nissanleaf.travel(8000);

System.out.println("We update the prices of the Porsche and the Nissan now.");

porschecaymans.setPrice(50000);

nissanleaf.setPrice(24500);

System.out.println("Now we display each car's info to see how the variables have changed:");

porschecaymans.displayCarInfo();

nissanleaf.displayCarInfo();

}

}

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