PART A: AIRLINE SEATING [10 MARKS] Millions of commercial airline flights cross
ID: 3585923 • Letter: P
Question
PART A: AIRLINE SEATING [10 MARKS] Millions of commercial airline flights cross the world's cities every year, and each one of these flights must solve what seems like a simple problem: how do you arrange where each passenger on one of those flights sits? In this assignment, we will solve a variation on that problem, where we will observe one restriction on the seats: people who book a flight together will be f possible-seated together Your program will process data from an array of strings (call this the "bookings" array). The first array element will contain a number indicating how many seats the aircraft has. The remaining elements will contain information about groups of passengers who have booked seats on the flight. The first element of this grouping will be a number that indicates how many people are in the group. The remaining elements identify the individual passengers in that group by their last and first name For example, the array might contain the following 10 strings: 8 Nobbly, Greg Nobbly, Jo-Anne Lee, Sook Lukas, Stephie Lukas, Cambridge Lukas, Ogden which means there are 8 seats on the flight, the first group of passengers has 2 people (with their names below), the second group has 1 person, and the third group has 3 people Normally this data would come from input, such as lines from a text file, not from an array. That's the next assignment The seating chart for the flight will be stored in an array of strings (call this the "seats" array), whose size is equal to the number of seats on the flight. Each position in the array corresponds to a seat on the flight, the first element in the array is seat 1, the second element is seat 2, etc. Initially all the seats are empty Passengers will be seated by placing their name in this array Your program will seat passengers as follows It will create a "seats" array of the appropriate size (given in the first position of the "bookings" array) · It will process the remaining items in the "bookings, array or strings For each group or passengers t will attempt to seatExplanation / Answer
Ques1)
I have written all the functions and their definations that you may require, just add function to input data as given in your assignment. Each function has its purpose written above it in comment. You just have to use the function seatPicker(). the rest are called automatically by it. Also write the code for main function accordingly.
import java.util.Random;
import java.util.ArrayList;
public class MyClass {
// String array to store the seats
String[] seats;
// integer to store the no of seats
int totalSeat;
private Random randomGenerator;
// intialize class by sending the total no of seats
MyClass(int seat){
totalSeat = seat;
seats = new String[seat];
}
// Function checks if the seat at passed index is empty
boolean checkIfEmpty(int seat){
if (seats[seat] == null || seats[seat] == "")
return true;
else
return false;
}
// function to pick a seat here in the argument pass string array with following info
// [no. of people in batch, name1, name2, name3......]
// Example [3, John, Adam, Anna]
void seatPicker(String[] data){
ArrayList<Integer> segment = new ArrayList<Integer>();
int batchNo = Integer.parseInt(data[0]);
segment = segmentChecker(batchNo);
if(segment.isEmpty())
{
for(int i= 1; i<=batchNo; i++){
randomPlacement(data[i]);
}
}
else
{
int index = randomGenerator.nextInt(segment.size());
placement(segment.get(index),data);
}
}
// Function place the passed person randomly
void randomPlacement(String data){
int index = randomGenerator.nextInt(totalSeat);
if(checkIfEmpty(index)){
seats[index] = data;
}
else{
randomPlacement(data);
}
}
// Function to place the passed batch in an empty space
void placement(int index, String[] data){
int batchNo = Integer.parseInt(data[0]);
for(int i=1; i<=batchNo; i++, index++){
seats[index] = data[i];
}
}
// FUnction to check which segments are empty for a given batch, it returns an arraylist of starting indexes of all available batches
ArrayList<Integer> segmentChecker(int size){
int x = 0, i = 0;
ArrayList<Integer> segment = new ArrayList<Integer>();
boolean isSegment = false;
while(i< totalSeat){
while(!checkIfEmpty(i)){
++i;
}
for(i=x; i<x+size; i++){
if(checkIfEmpty(i)){
isSegment = true;
}
else{
isSegment = false;
break;
}
}
if(isSegment)
segment.add(x);
x = i;
}
return segment;
}
// Displays data accordingly
void display{
for(int i =0; i< totalSeat; i++){
System.out.println("Seat "+i+1+": "+seats[i]);
}
}
public static void main(String args[]) {
// formulate the input and pass them to appropriate method
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.