We want to realize a program for collecting statistical data about car sales. For each car, the following information is stored:
null
.
Part 1.
Write a class Car
that implements the following methods:
Car(String b, int hp)
: constructor that creates an
object representing an car whose brand is b
, whose horse power
is hp
, and which has not been sold yet;
String getBrand()
: that returns the brand of the car;
int getHP()
: that returns the horse power of the car;
double getPrice()
: that returns the price of the car, if
the car has been sold, and 0 otherwise;
String getOwner()
: that returns the name of the owner of
the car, if the car has been sold, and null
otherwise;
boolean isSold()
: that checks whether the car has been
sold;
void sell(String n, double p)
: that changes the status of
the car to sold, by setting the name of the owner of the car to
n
and the price to p
; if the car had already been
sold, the method should do nothing;
String toString()
: that returns a string with all the
information about the car.
Example program:
TestCar.Java
Part 2.
Write a class UseCar
, client of Car
, that contains
the following public static method:
static double averagePriceSold(Car[] ar)
: that, given an
archive ar
of cars, represented as an array of cars, calculates
the average price of the cars that have been sold. If there are no cars that
have been sold in the archive, the method should return 0.
Example program:
TestUseCar.java