0% found this document useful (0 votes)
2 views3 pages

Week 7 Lab

The document outlines a practical exercise for a Java programming class focused on creating a simple car dealership system. It includes the design of a Car class and a CarDealership class to manage car inventory, along with methods for adding cars, calculating total inventory value, and finding specific cars. A main class is also provided to demonstrate the functionality of the system with sample output examples.

Uploaded by

tuhafenijason05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Week 7 Lab

The document outlines a practical exercise for a Java programming class focused on creating a simple car dealership system. It includes the design of a Car class and a CarDealership class to manage car inventory, along with methods for adding cars, calculating total inventory value, and finding specific cars. A main class is also provided to demonstrate the functionality of the system with sample output examples.

Uploaded by

tuhafenijason05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming 2 (PRG620S) Practical Class Exercises, Week 07

24 to 28 March 2025
NO VPL
Classes, Objects, Methods and Data Structures

1. Simple Car Dealership System in Java

You are tasked with creating a simple system to manage a car dealership inventory.
The system should allow the dealership to store and manage a collection of cars.

Car Class (Car.java):


o Create a Car class that contains the following fields:
▪ Private Make – the make of the car (e.g., "Toyota", "Honda").
▪ Private model – the model of the car (e.g., "Corolla", "Civic").
▪ Private year – the year the car was manufactured.
▪ Private price – the price of the car.
o Provide a constructor to initialise these attributes.
o Implement the following methods:
▪ Getters and Setters for all private attributes
▪ getDetails() – a method that returns a string containing the details of
the car (make, model, year, price).
▪ getPrice() – a method that returns the price of the car.

Page 1 of 3
CarDealership Class (CarDealership.java):
o Create a CarDealership class that manages a collection of cars using an
ArrayList<Car>.
o The class should have the following methods:
▪ addCar(Car car) – adds a car to the dealership's inventory.
▪ getTotalInventoryValue() – calculates and returns the total value of all
cars in the inventory.
▪ listAllCars() – prints out the details of all the cars in the inventory.
▪ findCheapestCar() – returns the car with the lowest price.
▪ findCarsByMake(String make) – returns an ArrayList of cars that
match a given make.

2. Driver Program (Main Class). Call it TestCarDealership.java:


o Create a Main class with a main method that demonstrates the functionality
of the Car and CarDealership classes. In the main method:
▪ Create several Car objects with different makes, models, years, and
prices. Call the Car constructor to provide these values.
▪ Add these cars to a CarDealership object.
▪ Display all cars in the inventory.
▪ Display the total value of the inventory.
▪ Display the cheapest car.
▪ List all cars from a particular make (e.g., "Toyota").

Below is a sample output when running the Main class:

Page 2 of 3
Sample Output:
Inventory of Cars:
Toyota Corolla 2020 N$ 280000.0
Honda Civic 2019 N$ 220000.0
Ford Mustang 2021 N$ 600000.0
Toyota Camry 2021 N$ 310000.0

Total inventory value: N$ 1410000.0

The cheapest car: Honda Civic 2019 N$ 220000.0

Cars by make (Toyota):


Toyota Corolla 2020 N$ 280000.0
Toyota Camry 2021 N$ 310000.0

Page 3 of 3

You might also like