Question: Using java. Create two classes Person representing a person with fields name of type String and cars of type Car representing cars which they owns;
Using java.


Create two classes Person representing a person with fields name of type String and cars of type Car representing cars which they owns; this can be null if he/she doesn't have any car; Car representing a car with fields make (String), price (int) and next of type Car which is the reference to the next car possessed by a person (possibly null, then this car is the last). In other words, objects of class Car can be viewed as nodes of a singly-linked list All fields in both classes should be private! The class Car contains the following member functions constructors and methods): public Car (String m, int p, Carn) public Car(String make, int price) public String getMake() public int getPrice() public Car getNext() public void showCars() public void showCarsRev() @Override public String toString() where the first two member functions are constructors: one taking values of all fields, and one taking a make and a price and setting next to null the next three methods are just accessors which return values of the corresponding fields showCars prints (using System.out.print - see below) information on all cars: this car, then the one referenced to by its field next, then next of this next e.t.c, until null is encountered Information on consecutive cars should be printed in one line; showCarsRev prints (using System.out.print - see below) information on all cars, as does showCars, but in the reverse order: it has to be recursive and must not use any loops, auxiliary arrays or additional fields; toString overrides toString from class Object and returns a string with make and price of this car. In this way a reference to Car may be used as an argument to System.out.print yielding a sensible string The class Person contains the following member functions constructors and methods): public Person(String name) public Person buys(String make, int price) public String getName() public void showCars() . public void showCarsRevo public int getTotalPrice() public boolean hasCar (String make) public Car mostExpensive where the first member is the constructor taking name only; field cars will be initialized with null); . buys takes a string and an int, creates a Car with the given make and price and sets it as the first car possessed by this person; the car which was the first possibly null) becomes the second, i.e., it is referenced by the field next of the newly created car. The method returns the reference to this person (the one the method has been invoked on); getName is a simple getter method; showCars prints all cars owned by this person using the corresponding method in class Car; showCarsRev does the same thing, but utilizing the corresponding recursive method in Car; getTotalPrice returns the total price of all cars owned by this person; .hasCar takes a string and returns a boolean stating if this person owns a car of the given make use equalsignoreCase from String to make string comparison case insensitive); mostExpensive returns the most expensive car owned by this person (or null if this person doesn't have a car). The following main function (in another class) (Click here to dounload public static void main(String[] args) { Person john = new Person ("John"); john. buys ("Ford", 20000) .buys ("Opel", 16000) .buys ("Fiat", 12000) showCars(); System.out.println(); john. showCarsRev(); System.out.println(); System.out.println("Total price of john.getName() + "'s cars: john.getTotalPrice ()); System.out.println("Does + john.getName() + have a ford?" john.hasCar ("ford")); System.out.println("Does + john.getName() + " have a bmw? john.hasCar("bmw')); System.out.println(john.getName() + "'s most "expensive car is + john. mostExpensive()); } should print Fiat (12000) Opel (16000) Ford (20000) Ford (20000) Opel (16000) Fiat(12000) Total price of John's cars: 48000 Does John have a ford? true Does John have a bmw? false John's most expensive car is Ford (20000) + +
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
