In: Computer Science
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”
Implementation in JAVA:
import java.util.ArrayList;
public class Car_Test {
public static void main(String[] args) {
// initilise arraylist
ArrayList<Car> list= new ArrayList<>();
// creating instance of no argument constructor
Car c1= new Car();
//and add instance to arraylist
list.add(c1);
//creating instance of 1 argument constructor of car class
Car c2= new Car("Chevy");
// and add instance to arraylist
list.add(c2);
//creating instance of 2 argument constructor
Car c3= new Car("Tesla","Green");
//and add instance to arraylist
list.add(c3);
//creating instance of 3 argument constructor
Car c4= new Car("Toyota","Blue",28);
//and add instance to arraylist
list.add(c4);
System.out.println();
// traverse list and call DisplayCarInformation() method from car
class
for(int i=0;i<list.size();i++) {
Car c= list.get(i);
// display information
c.DisplayCarInformation();
}
}
}
/////////////// CAR CLASS //////////////
class Car{
// initialize variables
private String brand;
private String color;
private int mpg;
// no argument constructor
// default values are brand=ford, color=red,
mpg=30.
Car(){
brand="Ford";
color="Red";
mpg=30;
}
// 1 argument constructor (argument is brand)
// default values are color=red, mpg=30.
Car(String brand){
this.brand=brand;
color="Red";
mpg=30;
}
// 2 argument constructor (argument is
brand,color)
// default value is mpg=30.
Car(String brand,String color){
this.brand=brand;
this.color=color;
mpg=30;
}
// 3 argument constructor (argument is
brand,color,mpg=30)
Car(String brand,String color,int mpg){
this.brand=brand;
this.color=color;
this.mpg=mpg;
}
// method to display information
public void DisplayCarInformation() {
System.out.print("\""+brand+",
"+color+" colour, "+mpg+" mpg\" ");
}
}
SAMPLE OUTPUT:
You can print them in new line just by adding "ln" very next to "System.out.print" in DisplayCarInformation() method which is in Car class.
If you have any doubt regarding this question please ask me in comments
//THANK YOU:-)