Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Airline class The constructor in the Airline class sets name, initializes the to

ID: 3853638 • Letter: A

Question

Airline class The constructor in the Airline class sets name, initializes the totalPlanes to 0, and initializes the array to hold. 10 planes. The Airline toString method returns the name of the Airline, total number of planes, and the total number of passengers that have flown on all their planes. The addPlane method adds a plane object to the array of Planes that the Airline has percentageFull returns numberOfAllPassengers/numberOfAllSeats in the Planes in the airline to see how full all the planes are Plane class The constructor sets the name of the plane (flight name), the total number of seats on the plane and sets numPassengers to 0 addPassenger method increases the numPassengers by the number num (do appropriate checks in this method) hasMoreSeats (Plane p) compares two Plane objects (this Plane and Plane p) to see which one has more seats on the plane(ie., see which one is bigger). Properly test your classes by performing the following operations in a demo class, at minimum. Create an Airline class with at least 3 Plane objects. Add passengers to all the Planes. Add the planes to your airline. Print out the totalNumber of planes that the airline has. Print out the total number of passengers on all the Planes. (use the method getTotalPassengers in the Airline class). Add some more passengers to your second plane. Print out the percentageFull (totalPassengers from all planes/totalSeats from all planes) on all the Planes. Print out the plane object that has the most seats. Call the toString method in the Airline Class

Explanation / Answer

Below is your code: -

Plane.java

public class Plane {

private String planeName;

private int numSeats;

private int numPassengers;

public Plane(String name, int numSeats) {

this.planeName = name;

this.numSeats = numSeats;

this.numPassengers = 0;

}

public int getNumSeats() {

return numSeats;

}

public void setNumSeats(int num) {

this.numSeats = num;

}

public int getNumPassengers() {

return numPassengers;

}

public void addPassengers(int num) {

if ((this.numPassengers + num) <= this.numSeats) {

this.numPassengers = this.numPassengers + num;

} else {

System.out.println("Plane is full.");

}

}

public boolean hasMoreSeats(Plane p) {

if (this.numSeats > p.getNumSeats()) {

return true;

}

return false;

}

public String toString() {

return "Plane [planeName=" + planeName + ", numSeats=" + numSeats + ", numPassengers=" + numPassengers + "]";

}

}

Airline.java

public class Airline {

private int totalPlanes;

private String name;

private Plane[] allPlanes;

public Airline(String name) {

this.name = name;

this.totalPlanes = 0;

allPlanes = new Plane[10];

}

public String getName() {

return name;

}

public void setName(String n) {

this.name = n;

}

public int getTotalPlanes() {

return totalPlanes;

}

public Plane[] getAllPlanes() {

return allPlanes;

}

public int getTotalPassengers() {

int numPass = 0;

for (int i = 0; i < this.totalPlanes; i++) {

numPass = numPass + this.allPlanes[i].getNumPassengers();

}

return numPass;

}

public void addPlane(Plane p) {

if (this.totalPlanes < this.allPlanes.length) {

this.allPlanes[this.totalPlanes] = p;

this.totalPlanes++;

} else {

System.out.println("Airline has maximum planes.");

}

}

public double percentageFull() {

int numPass = 0, numSeats = 0;

for (int i = 0; i < this.totalPlanes; i++) {

numPass = numPass + this.allPlanes[i].getNumPassengers();

numSeats = numSeats + this.allPlanes[i].getNumSeats();

}

return (double) numPass / (double) numSeats;

}

@Override

public String toString() {

return "Airline [name=" + name + "totalPlanes=" + totalPlanes + ", total number of passengers flown="

+ this.getTotalPassengers() + "]";

}

public static void main(String[] args) {

Plane p1 = new Plane("A310", 300);

Plane p2 = new Plane("A330", 350);

Plane p3 = new Plane("B200", 120);

Airline al = new Airline("Indian Airlines");

System.out.println("Total number of planes in Airlines: " + al.getTotalPlanes());

p1.addPassengers(200);

p2.addPassengers(250);

p3.addPassengers(120);

al.addPlane(p1);

al.addPlane(p2);

al.addPlane(p3);

System.out.println("Total number of planes in Airlines after adding planes: " + al.getTotalPlanes());

System.out.println("Total number of passengers in all planes: " + al.getTotalPassengers());

System.out.println("Percentage full of airline: " + al.percentageFull());

p2.addPassengers(50);

System.out.println("Percentage full of airline after adding passengers: " + al.percentageFull());

System.out.println("Plane with most seats: ");

if(p1.hasMoreSeats(p2) && p1.hasMoreSeats(p3)) {

System.out.println(p1);

} else if(p2.hasMoreSeats(p1) && p2.hasMoreSeats(p3)) {

System.out.println(p2);

} else if(p3.hasMoreSeats(p1) && p3.hasMoreSeats(p2)) {

System.out.println(p3);

}

System.out.println("Airline Details: ");

System.out.println(al);

}

}

Sample Run: -

Total number of planes in Airlines: 0
Total number of planes in Airlines after adding planes: 3
Total number of passengers in all planes: 570
Percentage full of airline: 0.7402597402597403
Percentage full of airline after adding passengers: 0.8051948051948052
Plane with most seats:
Plane [planeName=A330, numSeats=350, numPassengers=300]
Airline Details:
Airline [name=Indian AirlinestotalPlanes=3, total number of passengers flown=620]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote