Question

In: Computer Science

A car dealer wants to use a software to display his or her cars’ information. As...

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”

Solutions

Expert Solution

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:-)


Related Solutions

Problem Statement A car dealer wants to use a software to display his or her cars’...
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...
ASAP PLEASE Problem Statement A car dealer wants to use a software to display his or...
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....
C# A car dealer wants an application that calculates the cost of a car. The GUI...
C# A car dealer wants an application that calculates the cost of a car. The GUI application should link the “BuildYourCar.accdb” database and display all the data in four different “ListBox” based on the category. Each “ListBox” should display all the items in that category. The user can only choose one item from each “ListBox” to add an item to a car. As each item is selected, the application displays the item in a separate “ListBox” to display. If user...
A car dealer leases a small computer with software for $5,000 per year. As an alterative...
A car dealer leases a small computer with software for $5,000 per year. As an alterative he could buy the computer for $7,500 and lease the software for $3,500 per year. Any time he would decide to switch to some other computer he could cancel software lease and sell the computer for $500.   A. If he buys the computer and leases the software, what is the payback period? B. If he kept the computer and software for 8 years, what...
The owner of a car wash wants to see if the arrival rate of cars follows...
The owner of a car wash wants to see if the arrival rate of cars follows a Poisson distribution. In order to test the assumption of a Poisson distribution, a random sample of 150 ten-minute intervals was taken. You are given the following observed frequencies: Number of Cars Arriving in a 10-Minute Interval Frequency 0 3 1 10 2 15 3 23 4 30 5 24 6 20 7 13 8 8 9 or more 4 150 The calculated value...
Peter wants to sell one of his cars that has a diesel motor to his nephew...
Peter wants to sell one of his cars that has a diesel motor to his nephew Brett. Brett is eager to purchase the car but Brett does not realise that one of his uncle’s cars is diesel. Peter writes a letter to Brett as follows:          “Dear Brett, I know that you are interested in buying one of my cars. I would like to give it to you but I know that you won’t accept charity. Therefore I will offer...
Peter wants to sell one of his cars that has a diesel motor to his nephew...
Peter wants to sell one of his cars that has a diesel motor to his nephew Brett. Brett is eager to purchase the car but Brett does not realise that one of his uncle’s cars is diesel. Peter writes a letter to Brett as follows:          “Dear Brett, I know that you are interested in buying one of my cars. I would like to give it to you but I know that you won’t accept charity. Therefore I will offer...
A small car dealer, who is eager to estimate his inventory cost, can hold up to...
A small car dealer, who is eager to estimate his inventory cost, can hold up to 4 cars in the showroom. The periodic demand for the cars is following a Poisson distribution with mean 2, except for the maximum inventory level. In the case of maximum inventory level, the dealer makes a special discount by offering a price much below the market which results in depletion of its entire inventory. Once all the cars are sold, the dealer immediately orders...
A small car dealer, who is eager to estimate his inventory cost, can hold up to...
A small car dealer, who is eager to estimate his inventory cost, can hold up to 4 cars in the showroom. The periodic demand for the cars is following a Poisson distribution with mean 2, except for the maximum inventory level. In the case of maximum inventory level, the dealer makes a special discount by offering a price much below the market which results in depletion of its entire inventory. Once all the cars are sold, the dealer immediately orders...
Java - Write a program to calculate a user’s BMI and display his/her weight status. The...
Java - Write a program to calculate a user’s BMI and display his/her weight status. The status is defined as Underweight, Normal, Overweight and obese if BMI is less than 18.5, 25, 30 or otherwise respectively. You need to read weight (in kilogram) and height (in metre) from the user as inputs. The BMI is defined as the ratio of the weight and the square of the height. [10 marks]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT