Question: public class Car extends Vehicle implements Comparable, Announcements { / / Instance variables to store the number of doors and windows in the car private
public class Car extends Vehicle implements Comparable, Announcements
Instance variables to store the number of doors and windows in the car
private int numDoors;
private int numWindows;
Constructor: initializes a Car with a default x seating layout
public Carint numDoors, int numWindows
super; Calls the Vehicle constructor with rows and seats per row
this.numDoors numDoors; Assigns the number of doors
this.numWindows numWindows; Assigns the number of windows
Constructor: initializes a Car with a given number of seats per row
public Carint numDoors, int numWindows, int numSeatsPerRow
super numSeatsPerRow; Calls the Vehicle constructor with rows and variable seats per row
this.numDoors numDoors; Assigns the number of doors
this.numWindows numWindows; Assigns the number of windows
Constructor: initializes a Car with a custom number of seats per row array
public Carint numDoors, int numWindows, int numSeatsPerRow
supernumSeatsPerRow; Calls the Vehicle constructor with a specific seat layout
this.numDoors numDoors; Assigns the number of doors
this.numWindows numWindows; Assigns the number of windows
Constructor: initializes a Car with a driver and custom seating layout
public Carint numDoors, int numWindows, Person driver, int numSeatsPerRow throws InvalidDriverException
superdriver numSeatsPerRow; Calls the Vehicle constructor with a driver and seating layout
this.numDoors numDoors; Assigns the number of doors
this.numWindows numWindows; Assigns the number of windows
Overridden method from Vehicle: loads a passenger into the car
@Override
public boolean loadPassengerPerson p
Loop through each row
for int i ; i getNumberOfRows; i
Loop through each seat in the row
for int j ; j getNumSeatsPerRowi; j
Check if the seat is empty
if getPersonInSeati j null
Set the person in the empty seat
setPersonInSeati j p;
return true; Passenger successfully loaded
return false; If no seat is available, return false
Overridden method from Vehicle: loads multiple passengers into the car
@Override
public int loadPassengersPerson people
int loaded ; Counter for the number of passengers successfully loaded
for Person p : people
if loadPassengerp Load each passenger one by one
loaded; Increment the count if the passenger is loaded
else
break; Stop loading if no seats are available
return loaded; Return the total number of passengers loaded
Overridden method from Announcements interface: returns a departure message
@Override
public String departure
return "All Aboard
; Return departure announcement
Overridden method from Announcements interface: returns an arrival message
@Override
public String arrival
return "Everyone Out
; Return arrival announcement
Overridden method from Announcements interface: returns a message asking not to disturb the driver
@Override
public String doNotDisturbTheDriver
return No Backseat Driving
; Return no backseat driving' announcement
Method to check if a passenger can open a door based on their seat location
public boolean canOpenDoorPerson p
int location getLocationOfPersonInVehiclep; Get the passenger's seat location
Check if the seat is at the end of the row door seat
return location && location location getNumSeatsPerRowlocation;
Method to check if a passenger can open a window based on their seat location
public boolean canOpenWindowPerson p
int location getLocationOfPersonInVehiclep; Get the passenger's seat location
Check if the seat is next to the window seat at the ends of a row
return location && location location getNumSeatsPerRowlocation;
Getter method for the number of doors
public int getNumDoors
return numDoors;
Getter method for the number of windows
public int getNumWindows
return numWindows;
Overridden method from Comparable: compares cars by the number of people on board
@Override
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
