In this programming exercise, you will create a Car class definition and a CarTester client program.
The car must have the following properties: make, model, year, price, and two other properties of your own choosing. You must decide what variable type each of these properties should be declared as.
The car object must have at least three constructors. One constructor must be the default constructor. Another constructor must take all six properties and assign them. The third will be one of your own choosing. Any properties not sent to the constructor should be randomly assigned or given a default value.
In addition to the constructors, the car object should have a toString() method.
The CarTester program should ask the user to create three different cars, each using a different constructor. The program should display the information for each car once entered.
class Car {
private String make;
private String model;
private int year;
private double price;
private String color;
private double speed;
public Car() {
this.make = "Default";
this.model = "Default";
this.year = 2020;
this.price = 15000;
this.color = "Black";
this.speed = 100;
}
public Car(String make, String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.color = "Black";
this.speed = 100;
}
public Car(String make, String model, int year, double price, String color, double speed) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.color = make;
this.speed = speed;
}
public String toString() {
return "Make: " + make + "\n" + "Model: " + model + "\n" + "Year: " + year + "\n" + "Price: " + price + "\n"
+ "Color: " + color + "\n" + "Speed: " + speed + "\n\n";
}
}
public class CarTester {
public static void main(String[] args) {
Car Default = new Car();
System.out.println(Default.toString());
Car Car4 = new Car("Ford", "B5", 2022, 45000);
System.out.println(Car4.toString());
Car Car6 = new Car("Audi", "X5", 2022, 85000, "Red", 250);
System.out.println(Car6.toString());
}
}
Comments
Leave a comment