Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Two files are required, a data class named Car and an executable class named Tes

ID: 3667744 • Letter: T

Question

Two files are required, a data class named Car and an executable class named TestCar.

Class Car (data class, no main method)

five instance data members, all private: String make, String model, int year, double price, String color.

one static, or class, member named unitsSold assigned a value of zero. Make it public.

a no-arg constructor, and another parameterized constructor that sets values in all data members when called. Both constructors should increment the unitsSold static data member.

has setters and getters for each data member.

a toString() method that returns the state of all data members of a Car instance

In Class TestCar

must hold a main method and a method named evaluateCars

In main:

create two Car instances using the parameterized constructor.

create a third Car instance with the no-arg constructor

use all of the Car class setter methods to assign values to the third Car's data members.

use the "array initializer" syntax to make an array to hold these three Car objects.

call the method named evaluateCars with the array as its sole argument.

Make sure you catch the value returned by evaluateCars.

print the value in the unitsSold data member and the value returned by evaluateCars

In evaluateCars

use a foreach loop to process the array passed in to the method

print the state of each Car object using the toString method.

calculate the total value of the Car objects in the array.

return the total value to main.

Example Output

make=Lamborghini, model=Aventador, year=2016, price=405000.0, color=yellow
make=Honda, model=Odyssey, year=2015, price=33560.0, color=cherry
make=Dodge, model=Hellcat, year=2016, price=76500.0, color=orange
Your 3 cars are worth $515,060.00

Explanation / Answer

Car.java


public class Car {

   private String make;
   private String model;
   private int year;
   private double price;
   private String color;

   public static int unitsSold = 0;

   public Car() {
       unitsSold++;
   }

   public Car(String carMake, String carModel, int mYear,
           double carPrice, String carColor) {
       make = carMake;
       model = carModel;
       year = mYear;
       price = carPrice;
       color = carColor;

       unitsSold++;
   }

   public String getMake() {
       return make;
   }

   public void setMake(String make) {
       this.make = make;
   }

   public String getModel() {
       return model;
   }

   public void setModel(String model) {
       this.model = model;
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }

   public String getColor() {
       return color;
   }

   public void setColor(String color) {
       this.color = color;
   }

   public String toString() {
       String s = "make=" + make + ", model=" + model + ", year=" + year +
                   ", price=" + price + ", color=" + color;
       return s;
   }

}


TestCar.java


public class TestCar {

   public static void main(String[] args) {
      
       Car c1=new Car("Lamborghini","Aventador",2016,405000,"yellow");
       Car c2=new Car("Honda","Odyssey",2015,33560,"cherry");
      
       Car c3=new Car();
       c3.setMake("Dodge");
       c3.setModel("Hellcat");
       c3.setYear(2016);
       c3.setPrice(76500);
       c3.setColor("orange");
      
       Car cars[]={c1,c2,c3};
      
       double totalValue=evaluateCars(cars);
      
       System.out.println("Your "+Car.unitsSold+" cars are worth $"+totalValue);
      

   }
  
   public static double evaluateCars(Car cars[])
   {
       double totalValue=0;
       for(Car c:cars)
       {
           System.out.println(c.toString());
           totalValue+=c.getPrice();
       }
       return totalValue;
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote