Project Name the first main file Flight.java – contains the main method. Name th
ID: 3711641 • Letter: P
Question
Project
Name the first main file Flight.java – contains the main method.
Name the second file Passengers.java and use the supplied text file List.txt. – contains the class your array is based on.
Don’t use the try/catch array.
Carry the maximum of 16 passengers
Your project needs to store all the passengers in a 1 dimensional array of objects based on the class you create in the Passengers.java file.
Each object in the array will hold information about one passenger
1)Full Name (stored as one value)
2)Street Address
3)City (as a separate value from the State)
4)State
5)Weight of Passenger
6)Seat# (Seat numbers are 1A, 1B, 2A, 2B, 3A, 3B, 4A, 4B, 5A, 5B, 6A, 6B, 7A, 7B, 8A, 8B)
The program will be responsible for assigning each passenger a seat
The flight is sensitive to weight distribution. You need to have roughly the same weight on each side of the plane. All “A” seats are on the left of the plane and all “B” seats are on the right of the plane. The first passenger will be placed in seat 1A, the second passenger will be placed in seat 1B, the third passenger will be placed in the side with the least amount of total weight. This process will be repeated till everyone has been loaded. Make sure you load a maximum of 8 passengers per side.
Passenger input
1)No user input should be added to part 1 of the project. All 16 passengers will be read in from the file List.txt. You will need to modify the text file that I supplied. I supplied you with the 15 passengers. You are responsible for adding one more.
You are not allowed to modify the existing data in the text file.
i.Each value is delimited from the next value with a forward slash including the end of the line. Keep this in mind when adding your one passenger.
Once the data is collected, store it in an object in the array. Place the first passenger (you) at index location 0 and work your way up. This needs to be an object based on the class you create. It cannot be a simple String array.
Once the sub is full, output the full list of passengers from the array to the standard output window. Do this with a for loop. One passenger per line. List only their full name, their city, their state, and their seat assignment. Make sure you do this AFTER the array has been filled with the data. Do not do this as you are reading the data in from the file.
The Passengers Class
The Passengers Class will have the following:
1.Six class variables for the 6 values collected for each object. Note: The full name will go in one class variable, but the city and state will go in separate class variables.
2.A constructor method that assigns all 6 class variables a value.
3.A toString method that will print out just the full name, city, state, and seat for the object.
List.txt:
Explanation / Answer
Hi,
Based on the sample List.txt, I am uploading the files on the following link :
https://drive.google.com/drive/folders/1xWLhlukPHaMogv7OPh828BmVVEmreHvi?usp=sharing
(Please change the file path in Flight.java according to where you have put the file "List.txt" on your system.) :
In case the link doesn't work, I am pasting the code below:
//[Flight.java]
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Flight {
static double leftWtSum = 0.0;
static double rightWtSum = 0.0;
public static void main(String[] args) throws IOException {
String filePath = "C:\Users\shash\Desktop\20180419\List.txt";
String myFullName = "John Doe";
String myStreetAddress = "123 Pine Tree Boulevard";
String myCity = "New York City";
String myState = "Ny";
double myWeight = 151.2;
String mySeat = "1A";
leftWtSum += myWeight;
Passengers me = new Passengers(myFullName.toUpperCase(), myStreetAddress.toUpperCase(), myCity.toUpperCase(), myState.toUpperCase(), myWeight, mySeat.toUpperCase());
Passengers[] passengers = new Passengers[16];
passengers[0] = me;
int count = 1;
Scanner scanner = new Scanner(new File(filePath));
while(scanner.hasNextLine() && count < 16) {
String tempLine = scanner.nextLine().toUpperCase().trim();
String line = tempLine;
if(tempLine.endsWith("/")) {
line = tempLine.substring(0, tempLine.length());
}
String[] lineData = line.split("/");
if(lineData.length == 5) {
String[] cityState = lineData[3].split(",");
String fullName = lineData[0].trim() + " " + lineData[1].trim();
String streetAddr = lineData[2].trim();
String city = cityState[0].trim();
String state = cityState[1].trim();
double weight = Double.parseDouble(lineData[4]);
String seat = getSeatNumber(passengers, weight);
Passengers passenger = new Passengers(fullName, streetAddr, city, state, weight, seat);
passengers[count] = passenger;
count++;
}
}
for(Passengers passenger : passengers) {
if(null != passenger) {
System.out.println(passenger);
}
}
scanner.close();
}
private static String getSeatNumber(Passengers[] passengers, double weight) {
String seatTrailingChar = "A";
if(leftWtSum > rightWtSum) {
seatTrailingChar = "B";
rightWtSum += weight;
}else {
leftWtSum += weight;
}
int leftSeatMax = 0;
int rightSeatMax = 0;
for (int i = 0; i < passengers.length; i++) {
if(null != passengers[i]) {
int maxSeatNum = Integer.parseInt(passengers[i].getSeat().substring(0, 1));
String seatNumChar = passengers[i].getSeat().substring(1);
if(seatNumChar.equalsIgnoreCase("A") && leftSeatMax < maxSeatNum) {
leftSeatMax = maxSeatNum;
}
if(seatNumChar.equalsIgnoreCase("B") && rightSeatMax < maxSeatNum) {
rightSeatMax = maxSeatNum;
}
}
}
if(seatTrailingChar.equalsIgnoreCase("A") && leftSeatMax < 8){
return ++leftSeatMax + seatTrailingChar;
}
if(seatTrailingChar.equalsIgnoreCase("B") && rightSeatMax < 8){
return ++rightSeatMax + seatTrailingChar;
}
return null;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------
//[Passengers.java]
public class Passengers {
private String fullName;
private String streetAddress;
private String city;
private String State;
private double weight;
private String seat;
public Passengers(String fullName, String streetAddress, String city, String state, double weight, String seat) {
this.fullName = fullName;
this.streetAddress = streetAddress;
this.city = city;
State = state;
this.weight = weight;
this.seat = seat;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
@Override
public String toString() {
return fullName + ", " + city + ", " + State + ", " + seat;
}
}
//*************************************************************************************************************************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.