Question

In: Computer Science

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.

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

Solutions

Expert Solution

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 :


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...
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...
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...
Description of problem domain: the customer goes to the car dealer to purchase a car. First,...
Description of problem domain: the customer goes to the car dealer to purchase a car. First, the customer works with the salesperson who lists available cars from the dealer’s stock. If the customer selects a car, he may test-drive the car. If he feels satisfied with the car, he will negotiate the price with the salesperson. The customer may bring his old vehicle for trade-in. If the customer and the salesperson agree upon the final price (including the trade-in price...
Use computer software packages, such as Excel, to solve this problem. The Jacobs Chemical Company wants...
Use computer software packages, such as Excel, to solve this problem. The Jacobs Chemical Company wants to estimate the mean time (minutes) required to mix a batch of material on machines produced by three different manufacturers. To limit the cost of testing, four batches of material were mixed on machines produced by each of the three manufacturers. The times needed to mix the material follow. Manufacturer 1 Manufacturer 2 Manufacturer 3 17 29 17 23 27 16 21 32 20...
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...
C PROGRAM Problem Specification: You are a software engineer of a Car Sale company. And Car...
C PROGRAM Problem Specification: You are a software engineer of a Car Sale company. And Car Sales is preparing for the end of the year sale. CEO wants to track the sales of your four sales people. You have been asked to help with the preparations for the sales event. The CEO asked for a program to quickly tabulate their sale. It will be your job to implement this program in C. You are not required to use functions in...
PLEASE DO BY HAND AND NOT EXCEL 1.A car dealer believes that average daily sales for...
PLEASE DO BY HAND AND NOT EXCEL 1.A car dealer believes that average daily sales for four different dealerships in four separate states are equal. A random sample of days results in the following data on daily sales: Ohio                New York       West Virginia              Pennsylvania                3                          10                         3                                  20                2                            0                         4                                  11                6                            7                         5                                  8                4                            8                                                             2                4                            0                                                             14                7...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT