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 2x2 seating layout
public Car(int numDoors, int numWindows){
super(2,2); // Calls the Vehicle constructor with 2 rows and 2 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 Car(int numDoors, int numWindows, int numSeatsPerRow){
super(2, numSeatsPerRow); // Calls the Vehicle constructor with 2 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 Car(int numDoors, int numWindows, int[] numSeatsPerRow){
super(numSeatsPerRow); // 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 Car(int numDoors, int numWindows, Person driver, int[] numSeatsPerRow) throws InvalidDriverException {
super(driver, 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 loadPassenger(Person p){
// Loop through each row
for (int i =0; i < getNumberOfRows(); i++){
// Loop through each seat in the row
for (int j =0; j < getNumSeatsPerRow(i); j++){
// Check if the seat is empty
if (getPersonInSeat(i, j)== null){
// Set the person in the empty seat
setPersonInSeat(i, 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 loadPassengers(Person[] people){
int loaded =0; // Counter for the number of passengers successfully loaded
for (Person p : people){
if (loadPassenger(p)){// 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 canOpenDoor(Person p){
int[] location = getLocationOfPersonInVehicle(p); // Get the passenger's seat location
// Check if the seat is at the end of the row (door seat)
return location[0]!=-1 && (location[1]==0|| location[1]== getNumSeatsPerRow(location[0])-1);
}
// Method to check if a passenger can open a window based on their seat location
public boolean canOpenWindow(Person p){
int[] location = getLocationOfPersonInVehicle(p); // Get the passenger's seat location
// Check if the seat is next to the window (seat at the ends of a row)
return location[0]!=-1 && (location[1]==0|| location[1]== getNumSeatsPerRow(location[0])-1);
}
// 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!