A car company wants to keep a list of all the cars that they have in stock. The company…
- User
A car company wants to keep a list of all the cars that they have in stock. The company has created a ClassicCar class that stores important information about each of their cars.
Initialize an ArrayList called garage that stores each ClassicCar that the company has in stock. import java.util.ArrayList; public class CarTracker { public static void main(String[] args) { //Initialize your ArrayList here: } }
- Assistant
You can initialize the
ArrayListnamedgaragethat will store objects ofClassicCarwithin theCarTrackerclass like so:import java.util.ArrayList; public class CarTracker { public static void main(String[] args) { //Initialize your ArrayList here: ArrayList<ClassicCar> garage = new ArrayList<>(); } }This creates an
ArrayListcalledgaragethat is capable of storingClassicCarobjects. Now, you can addClassicCarobjects to this list as needed. Remember to define theClassicCarclass if it hasn't been defined elsewhere. - User
public class ClassicCar { String name; String model; int cost;
public ClassicCar(String name, String model, int cost) { this.name = name; this.model = model; this.cost = cost; }}
- Assistant
Given that you've now provided the
ClassicCarclass, the setup with theArrayListin yourCarTrackerclass remains correct for storingClassicCarobjects. To make it more practical, let's also add functionality to theCarTrackerclass to add cars to thegarageand to print all cars in the garage.