A long Problem about I/O stream, file access, and Strings .
Hi,
This is my homework. Basically itaskstoadd more functionality to thecodes that were developed in the previous homework.The codes shown here is my answer to the previous homework.Could anyone help me withquestions (b)and ( c ) ?
For these questions, you will be adding more functionality to the classes that were developed
in Assignment 4. You may use your own implementation of the classes, or the model solutions
that have been provided, as the starting point for this assignment. You may also use any of the
String methods provided by Java, since some of them (namely indexOf ) will be very helpful.
If we recall, the Scanner class can be connected to a String instead of System.in, which
should also help you out enormously.
Question 1 (10 points)
For this question, you will be supplementing the classes that were developed in Assignment 4
with additional functionality. The first two parts of this question are worth 2 marks each, the
third part is worth 5 marks, and the last part is worth 1 mark.
(a) For this part, you will be adding a new method to the Airport class that allows us to
create a new Airport object given a String that describes the new airport we would
like to create.
Write a static method called buildFromString that takes a String as a parameter,
and returns a new Airport object. The parameter should be a String of the exact
same format that the current toString method would return. For example, we should be
able to pass the string:
Kennedy International Airport (JFK): New York, NY
to the buildFromString method and we should get back a new Airport object
whose name is 揔ennedy International Airport? whose code is 揓FK? and whose location
is 揘ew York, NY?. Assume that the name, location, and code of an airport cannot
3
contain the characters ? ? or ? and so there will be only one ?character that separates
the name and the location of the airport, and there will be only one set of brackets
?and ?that surround the airport code.
(b) For this part, you will be adding a new method to the Passenger class that allows us to
create a new Passenger object given a String that describes the new passenger we
would like to create.
Write a static method called buildFromString that takes a String as a parameter,
and returns a new Passenger object. The parameter should be a String of the exact
same format that the current toString method would return. For example, we should be
able to pass the string:
Name: Joe Smith
Age: 35
to the buildFromString method and we should get back a new Passenger object
whose first name is 揓oe? last name is 揝mith? and age is 35. Assume that the first name
and last name will be single words, and so you can always split the first and last name by
looking for the single space character that separates them.
(c) For this section, you will be adding additional functionality to the Flight class that lets
us add an unlimited number of passengers to a flight, and also to save and load a flight to
and from a file.
First, modify the Flight class to allow an unlimited number of passengers to be added
to a flight. If you are using multiple member variables to store the passenger list, you
must modify the class to use an Array to hold the passengers. Initially, set the size of
the array to be 5, and then alter the addPassenger method so that it always ensures that
there is enough room in the passenger array to add the new passenger. It should do this by
checking if the array is full, and doubling its size if it is. We implemented a feature like
this in the Dog class that is on WebCT under the 揈xamples from Class?section, and so
you may use that as a guide. Also, since the addPassenger method will always succeed,
change its return type to be void.
Write a method called getPassenger that takes an integer n and returns the nth passenger
in the list of passengers, or null if there are less than n + 1 passengers in the list.
The passengers should be indexed starting with 0, and so calling getPassenger with the
argument 0 should return the first passenger in the list, calling it with 1 should return the
second passenger, and so on.
Write a method called save that takes a String as a parameter, and returns a boolean
value. The parameter should represent a filename, and the method should save the contents
of the flight to a file with that filename. All of the contents, including the origin and
destination airports, as well as entire list of passengers must be saved. It is up to you to
come up withe strategy for what the file should contain, but since you must be able to
restore the Flight object and all of its data from the file, you must ensure that all of the
data gets saved to the file. This method should return true if it is able to save the flight,
or false otherwise.
Write a static method called load that takes a String as the parameter and returns a new
Flight object. The parameter will represent a filename, and this method should read
in the contents of the file with that filename, and should build a new Flight objects
whose contents are exactly what was in the file. You can assume that the file was created
by your save method, and that the contents have not been modified since it was created,
and so you will not need to implement code to check that the contents of the file is in a
valid format. The new Flight object that is created must contain exactly what was in
the file, and so you will need to restore the origin and destination airports, and the entire
list of passengers that were saved in the file. Fortunately, you have created methods in the
Airport and Passenger classes that allow you to build these objects from a String
representation, and so you should keep in mind that most of the hard work has already
been taken care of in those two methods, you just need to use them properly in this one.
(d) Update the driver class AirlineTester so that it tests the functionality of the new
methods you have implemented. Make sure that all of the new methods are tested, and
make sure to demonstrate that adding 6 passengers to a flight will work, to show us that
the Array resizing code works properly. Also make sure to save a flight, and then load the
flight and show us that the flight data is exactly the same after loading it.
// This is the Airport Class.
// The Airport class represents an airport.
publicclass Airport
{
// Since the three variables below are instance variables, I make them private.
private String Name;// Airport Name
private String Code;// Airport Code
private String Location;// Airport Location
// This is the constructor. It has three parameters.
// Constructor helps to make objects.
public Airport (String AirportName, String AirportCode, String AirportLocation)
{
Name = AirportName;
Code = AirportCode;
Location = AirportLocation;
}
// The following three methods are the Getter methods.
public String getName()
{return Name;}// return the airport name
public String getCode()
{return Code;}// return the airport code
public String getLocation()
{return Location;}//return the airport location
// This is the toString method which returns the whole name of the airport and the location.
public String toString ()
{
String s = Name +" " +"(" + Code +")" +" : " + Location;
return s;// Return the result.
}
}
// This is the Passenger class.
// This class represents a passenger.
publicclass Passenger
{
// I make the following variables private because they are instace variables.
private String firstName;// the first name of the passenger.
private String lastName;// the last name of the passenger.
privateint age;// the age of the passenger.
// This is the constructor.
// The constructor helps make the objects.
public Passenger(String first_Name, String last_Name,int p_Age)
{
firstName = first_Name;
lastName = last_Name;
age = p_Age;// The variable name "p_Age" means "passenger's age".
}
// The following three methods are getter methods.
public String getFirstName ()
{return firstName;}// Return the first name of the passenger.
public String getLastName ()
{return lastName;}// Return the last name of the passenger.
publicint getAge ()
{return age;}// Return the age of the passenger.
// This is the toString method that returns the information. (full name and the age)
public String toString ()
{
String Name ="Name:" +" " + firstName +" " + lastName;
String Age ="Age:" +" " + age;
String result = Name +"\n" + Age;
return result;// Return the result.
}
}
// This is the Flight class.
publicclass Flight
{
// I make the following three variables private because they are instance variables.
private String flightNumber;
private Airport origin;
private Airport destination;
// Here I use an array to hold passengers.
private Passenger[] passengers =new Passenger[5];
// This is the first constructor that only takes the flight number.
public Flight(String flightNumber)
{
// Here I use 'this' because there are two 'flightnumber'.
this.flightNumber = flightNumber;
}
// This is the second constructor that takes the flight number and the two airports.
public Flight(String flightNumber, Airport origin, Airport destination)
{
// Here I use 'this' again.
this.flightNumber = flightNumber;
this.origin = origin;
this.destination = destination;
}
// This is the addPassenger method.
// It indicates whether a passenger is added successfully or not.
publicboolean addPassenger(Passenger p)
{
boolean result =false;// Initially make it 'false' for convenience.
for (int i = 0; i < passengers.length; i++)
{
if (passengers[i] ==null)
{
passengers[i] = p;// Add the passenger to the array.
result =true;
break;
}
}
return result;// Return the result.
}
// This is the numPassengers method.
publicint numPassengers()
{
int i = 0;
int result = 0;
for (i = 0; i < passengers.length; i++)
{
if (passengers[i] ==null)
{
result = i;
break;
}
}
if (i == passengers.length)
{ result = i;}
return result;// Return the result.
}
// The following three methods are the getter methods.
public String getFlightNumber()
{return flightNumber;}// Return the flightNumber.
// The Return type is Airport.
public Airport getOrigin()
{return origin;}// Return the origin.
// The Return type is Airport.
public Airport getDestination()
{return destination;}// Return the destination.
// The following two methods are the setter methods.
publicvoid setOrigin (Airport newOrigin)
{ origin = newOrigin;}// Set the Origin.
publicvoid setDestination (Airport newDestination)
{ destination = newDestination;}// Set the Destination.
// This is the toString method.
public String toString()
{
int i = 0;
String result;
result ="Flight Number:" +" " + flightNumber +"\n";
if (origin ==null)// If the origin is 'null', then return 'Not Set'.
{ result = result +"From:" +" " +"Not Set" +"\n";}
else
{ result = result +"From:" +" " + origin.toString() +"\n";}
if (destination ==null)// If the destination is 'null', then return 'Not Set'.
{ result = result +"To:" +" " +"Not Set" +"\n";}
else
{ result = result +"To:" +" " + destination.toString() +"\n";}
result = result + numPassengers() +" " +"Passenger(s):" +"\n" +"\n";
for (i = 0; i < passengers.length; i++)
{
if (passengers [i] ==null )
{break;}// If it is 'null', then quits the for loop.
result = result + passengers [i].toString() +"\n" +"\n";
}
return result;// Return the result.
}
}
// This is the AirlineTester Class.
// This class test the other three classes.
publicclass AirlineTester
{
publicstaticvoid main (String [] agrs)
{
// These are the 6 Passenger objects.
// When these objects are made, the corresponding constructor is tested.
// All the passenger names are fictitious since fictitious names are allowed.
Passenger p1 =new Passenger ("Alex","Bond", 20);
Passenger p2 =new Passenger ("Bob","Bond", 30);
Passenger p3 =new Passenger ("Cart","Bond", 40 );
Passenger p4 =new Passenger ("Daisy","Bond", 50 );
Passenger p5 =new Passenger ("Ellen","Bond", 60);
Passenger p6 =new Passenger ("Frank","Bond", 70 );
// These are the Airport objects.
// When these objects are made, the corresponding constructor is tested.
// All the airport names are fictitious since fictitious names are allowed.
Airport a1 =new Airport ("Alberta International Airport","AIA","Edmonton, ED");
Airport a2 =new Airport ("BC International Airport","BIA","Vancouver, VA" );
Airport a3 =new Airport ("Chicago International Airport","CIA","Chicago, CH" );
// This are the Flight objects.
// When these objects are made, the corresponding constructor is tested.
// All the flight names are fictitious since fictitious names are allowed.
Flight f1 =new Flight ("123");// This tests one constructor in the Flight Class.
Flight f2 =new Flight ("789", a1, a2);// This tests the other constructor.
// The following codes test the three getter methods of the Airport Class.
System.out.println(a1.getName() );// get the airport name.
System.out.println(a1.getCode() );// get the airport code.
System.out.println(a1.getLocation() );// get the airport location.
System.out.println();
System.out.println(a2.getName() );
System.out.println(a2.getCode() );
System.out.println(a2.getLocation() );
System.out.println();
// The following codes test the toString method of the Airport Class.
System.out.println(a1.toString() );// print the full name and location of a1.
System.out.println();
System.out.println(a2.toString() );// print the full name and location of a2.
System.out.println();
System.out.println(a3.toString() );// print the full name and location of a3.
System.out.println();
// The following codes test the three getter methods of the Passenger Class.
System.out.println(p1.getFirstName() );// get the first name.
System.out.println(p1.getLastName() );// get the last name.
System.out.println(p1.getAge() );// get the age.
System.out.println();
System.out.println(p2.getFirstName() );
System.out.println(p2.getLastName() );
System.out.println(p2.getAge() );
System.out.println();
// The following codes test the toString method of the Passenger Class.
System.out.println(p1.toString() );// print the full name and age of p1.
System.out.println();
System.out.println(p2.toString() );// print the full name and age of p2.
System.out.println();
System.out.println(p3.toString() );// print the full name and age of p3.
System.out.println();
// The following codes test the getter and setter methods of the Flight Class.
System.out.println (f2.getFlightNumber() );// get the flight number.
System.out.println ();
System.out.println (f2.getOrigin() );// get the origin.
System.out.println ();
System.out.println (f2.getDestination() );// get the destination.
System.out.println ();
System.out.println(f1.getFlightNumber() );// get the flight number.
System.out.println(f1.getOrigin() );// get the origin.
System.out.println(f1.getDestination() );// get the destination.
System.out.println();
f1.setOrigin(a3);// set the origin.
System.out.println(f1.getOrigin() );// then get the origin.
System.out.println();
f1.setDestination(a2);// set the destination.
System.out.println(f1.getDestination() );// then get the destination.
System.out.println();
f1.setOrigin(a2);
System.out.println(f1.getOrigin() );
System.out.println();
f1.setDestination(a3);
System.out.println(f1.getDestination() );
System.out.println();
// The following codes will test the addPassenger, numPassengers, and toString methods of the Flight Class.
// The maximum number of passengers in a flight is 5.
System.out.println("The original number of passengers in f1: " + f1.numPassengers() );// print the number of passengers.
System.out.println();
System.out.println("Add p1 to f1: " + f1.addPassenger(p1) );// add one passenger. (true if add successfully, otherwise false)
System.out.println("Total number of passenger(s) in f1 so far: " + f1.numPassengers() );// print the number of passengers.
System.out.println();
System.out.println("The current flight information: " +"\n\n" + f1.toString() );// print the flight information.
System.out.println("\n\n");
// The following sets of codes basically test the same thing.
System.out.println("Add p2 to f1: " + f1.addPassenger(p2) );
System.out.println("Total number of passenger(s) in f1 so far: " + f1.numPassengers() );
System.out.println();
System.out.println("The current flight information: " +"\n\n" + f1.toString() );
System.out.println("\n\n");
System.out.println("Add p3 to f1: " + f1.addPassenger(p3) );
System.out.println("Total number of passenger(s) in f1 so far: " + f1.numPassengers() );
System.out.println();
System.out.println("The current flight information: " +"\n\n" + f1.toString() );
System.out.println("\n\n");
System.out.println("Add p4 to f1: " + f1.addPassenger(p4) );
System.out.println("Total number of passenger(s) in f1 so far: " + f1.numPassengers() );
System.out.println();
System.out.println("The current flight information: " +"\n\n" + f1.toString() );
System.out.println("\n\n");
System.out.println("Add p5 to f1: " + f1.addPassenger(p5) );
System.out.println("Total number of passenger(s) in f1 so far: " + f1.numPassengers() );
System.out.println();
System.out.println("The current flight information: " +"\n\n" + f1.toString() );
System.out.println("\n\n");
// this tests the situation where the passenger is not added successfully since the flight is already full.
System.out.println("Add p6 to f1: " + f1.addPassenger(p6) );
System.out.println("Total number of passenger(s) in f1 so far: " + f1.numPassengers() );
System.out.println();
System.out.println("The current flight information: " +"\n\n" + f1.toString() );
System.out.println("\n\n");
// The following codes test the situation where either the origin or the destination is Not Set.
f1.setOrigin(null);// set the origin.
System.out.println("The current flight information: " +"\n" + f1.toString() );// print the flight information.
System.out.println("\n\n");
f1.setOrigin(a2);// set the origin.
f1.setDestination(null);// set the destination.
System.out.println("The current flight information: " +"\n" + f1.toString() );// print the flight information.
System.out.println("\n\n");
}
}

