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

Suppose we have a class called Car that models cars. This class has instance var

ID: 3679958 • Letter: S

Question

Suppose we have a class called Car that models cars. This class has instance variables year, make, model, and price. It also provides appropriate observer methods, including one called getPrice that returns a value of type int indicating the price of "this" car. Implement a client method totalPrice, which accepts a list (ArrayUnsortedList carList) of cars and returns an integer equal to the total cost of the cars on the list.

I have made the car clas but do not know how to implement the client method that returns the total cost of cars on the list (NOTE: MUST BE an ARRAYUNSORTEDLIST). Thank you.

Explanation / Answer

java code:

import java.io.*;
import java.util.Scanner;
public class Car {
  
private int year;
private String make;
private String model;
private double price;
public Car()
{
this.year = 0;
this.make ="";
this.model ="";
this.price =0.0;
  
}
public Car(int year, String make, String model, double price) {
this.year = year;
this.make = make;
this.model = model;
this.price = price;
}

public int getYear() {
return year;
}

public String getMake() {
return make;
}

public String getModel() {
return model;
}

public double getPrice() {
return price;
}
public double client(Car[] car)
{
double totalPrice=0.0;
for(int i=0;i<car.length;i++)
{
totalPrice=totalPrice+car[i].getPrice();
}
return totalPrice;
}
@Override
public String toString() {
  
return String.format( "%4d %s %s - $%,.2f", getYear(), getMake(), getModel(), getPrice() );
  
}   
public static void main(String[] args)throws IOException
{
int year;
String make;
String model;
double price;
  
  
Scanner scan=new Scanner(System.in);
System.out.println("Enter no of cars you want to enter ");
int size=scan.nextInt();
  
  
Car []car =new Car[size];
for(int i=0;i<size;i++)
{
System.out.println("Enter year of car"+(i+1)+" design:");
year=scan.nextInt();
System.out.println("Enter car"+(i+1)+" makers Name:");
make=scan.next();
System.out.println("Enter car"+(i+1)+" model no: ");
model=scan.next();
System.out.println("Enter car"+(i+1)+" price ");
price=scan.nextDouble();
car[i]=new Car(year,make,model,price);
  
}
for(int i=0;i<car.length;i++)
{
System.out.println("Car "+(i+1)+" "+car[i].toString());
  
}
System.out.println("Total Price:$"+car[0].client(car));
}
}

output:

run:
Enter no of cars you want to enter
3
Enter year of car1 design:
2009
Enter car1 makers Name:
hound
Enter car1 model no:
12xx500
Enter car1 price
550000
Enter year of car2 design:
2010
Enter car2 makers Name:
maruti
Enter car2 model no:
ma12xx5
Enter car2 price
455000
Enter year of car3 design:
2011
Enter car3 makers Name:
mahindra
Enter car3 model no:
mh1200
Enter car3 price
955555
Car 1 2009 hound 12xx500 - $550,000.00
Car 2 2010 maruti ma12xx5 - $455,000.00
Car 3 2011 mahindra mh1200 - $955,555.00
Total Price:1960555.0
BUILD SUCCESSFUL (total time: 1 minute 20 seconds)

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