In: Computer Science
ASAP PLEASE
Problem Statement A car dealer wants to use a software to display his or her cars’ information. As a software developer, you are going to develop a simple program for the dealer. You will have a Car class which has the following member variables:
Car’s Brand
Car’s Color
Car’s MPG (Mile Per Gallon)
This class has setters and getter for all member variables. This class has four different constructors:
1. A constructor with no argument.
2. A constructor with only one argument which is brand
3. A constructor with only two arguments which are brand and color
4. A constructor with only three argument which are brand, color and MPG.
If the caller of the constructor does not give a value for a particular member variable, a default value should be assigned when a constructor is called: The default value for brand is Ford. The default value for color is Red. The default value for MPG is 30. This class (Car Class) has one public method named DisplayCarInformation().
When the method is called, it prints the car’s brand, color, and MPG values. In another class named CarTest, you will have a main() method. Within the main(), you need to create at least 4 car instances. You must call the four different constructors in your program. Since you have 4 or more Car objects, you MUST use ArrayList data structure to store them. For each object, you need to call its DisplayCarInformation() method which will print the brand, color, and MPG on the console and write to an actual file named carInfo.txt. Your output format should be easy to understand.
Sample Output “Ford, Red color, 30 mpg” “Chevy, Red color, 30 mpg” “Tesla, Green color, 30 mpg” “Toyota, Blue color, 28 mpg”
Input None Output Print cars’ information on console and to an actual file named carInfo.txt
Car.java :
public class Car {
/*
Creating three instance (member) variables
to hold brand, color and mpg.
*/
private String brand;
private String color;
private int mpg;
// Creating constructor with no arguments. and initalizing instance variables with default values.
public Car() {
this.brand = "Ford";
this.color = "Red";
this.mpg = 30;
}
// Creating constructor with one argument and initalizing instance variables
// here color and mpg variables will initialize with default values provided
public Car(String brand) {
this.brand = brand;
this.color = "Red";
this.mpg = 30;
}
// Creating constructor with two arguments and initalizing instance variables.
// here mpg will initialize with default value provivded
public Car(String brand,String color) {
this.brand = brand;
this.color = color;
this.mpg = 30;
}
// Creating constructor with three arguments and initalizing instance variables.
public Car(String brand,String color,int mpg) {
this.brand = brand;
this.color = color;
this.mpg = mpg;
}
/*
Getter and setter method for brand, color and mpg instance variables.
*/
public void setBrand(String brand) {
this.brand = brand;
}
public void setColor(String color) {
this.color = color;
}
public void setMpg(int mpg) {
this.mpg = mpg;
}
public String getBrand() {
return brand;
}
public String getColor() {
return color;
}
public int getMpg() {
return mpg;
}
/*
Creating method to print Car information on console.
*/
public void DisplayCarInformation() {
System.out.println(brand+","+color+","+mpg+" mpg");
}
}
CarTest.java :
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.IOException;
/*
Class to display information of Car
*/
public class CarTest
{
public static void main(String[] args) {
// creating ArrayList of type Car to hold object of class Car.
ArrayList<Car> cars = new ArrayList<Car>();
// Creating four object of Car and adding in ArrayList.
cars.add(new Car());
cars.add(new Car("Brand 1"));
cars.add(new Car("Brand 2","BLack"));
cars.add(new Car("Brand 3","Blue",28));
// diplay information of each Car object created above using for each loop.
for(Car car:cars) {
car.DisplayCarInformation(); // calling method to print car's info.
}
// print to a file named carInfo.txt
try {
// creating FileWriter object to write to file named carInfo.txt.
FileWriter fileWriter = new FileWriter("carInfo.txt");
/*
iterating through all Car object created above and
store each Car object's info in single LinkageError
*/
for(Car car: cars) {
fileWriter.write(car.getBrand()+","+car.getColor()+","+car.getMpg()+" mpg"+"\n");
}
// close FileWriter object
fileWriter.close();
}
catch(IOException e) { // this catch will execute if above try block thrown any IOException.
e.printStackTrace(); // printStackTrace on console.
}
}
}
Sample Output :
Console Output :
carInfo.txt Output :
Ford,Red,30 mpg
Brand 1,Red,30 mpg
Brand 2,BLack,30 mpg
Brand 3,Blue,28 mpg
Please refer to the Screenshot of the code given below to understand indentation of the code :
Car.java :
CarTest.java :
carInfo.txt :