I need help !!!!!!

Hi,

I have a big question here. It has 4 parts. I already finished two parts, and I get stuck in the third part because I don't know how to set up the two constructors and

write the methods. Below is the whole question and my codes to the first two parts

For this question, you will be writing 4 classes. You are required to put each class in its own

file, and so you should submit 4 Java files. The first 3 classes are worth 3 marks each, and the

driver class, AirlineTester, is worth 1 mark.

You will be implementing a basic model for an Airline, including the classes for Airports,

Flights, and Passengers. Finally, you will implement a driver that tests the other classes to ensure

that they are working correctly. Each class is described in detail below. Unless otherwise

specified, make all instance variables private, and all of the specified methods should be

made public. You may also create helper methods if you like, but all methods other than

those specified must be made private.

(a) The Airport class represents and airport. Every airport has a name (such as 揔ennedy

International Airport?, an airport code (such as 揓FK?, and a location (such as 揘ew

York, NY?. Implement a constructor that lets you set these three properties when the

object is created. For each of these instance variables, implement a getter, but since

none of these variables are likely to change, do not implement setter methods for any

of them.

Write a toString method that returns the name, code, and location of the airport in a format

such as:

Kennedy International Airport (JFK): New York, NY

(b) the Passenger class represents a passenger. Every passenger has a first name, last

name, and age. Write a constructor that takes all three of these values and sets them

appropriately, and write getters for all three variables. Since none of these values are

likely to change, do not write setters for these variables, but write a toString method that

returns the information in a nice format, such as:

Name: Joe Smith

Age: 35

3

(c) The Flight class represents a flight between two airports. Every flight has a flight

number, two Airport objects, one for the origin and one for the destination of the

flight, as well as up to 5 passengers. Since we are just starting to learn about Arrays, you

may use an Array to keep track of the passengers, or you may use 5 instance variables

(though in the next assignment, you will be working with these same classes, and you

will be required to use Arrays, so you might want to try using them for this assignment).

Write two constructors, one which takes just the flight number, and one that takes the

flight number and the two airports. Write getters for all of the variables except for the list

of passengers. Write setters for the origin and destination airports, and write a method

called addPassenger that takes a Passenger object and returns a boolean value indicating

whether the passenger was added to the flight. The method should try to add

the passenger to the list of passengers, and return true if successful, or false if there are

already 5 passengers on the flight.

Also write a method called numPassengers that returns the number of passengers on

the flight.

Write a method called toString that prints out the flight information. If either the origin or

destination airport has not been set, output the value 揘ot Set?in its place. An example

of the output of toString is as follows:

Flight Number: 1234

From: Kennedy International Airport (JFK): New York, NY

To: Not Set

2 Passenger(s):

Name: Joe Smith

Age: 35

Name: Jane Smith

Age 33

(d) The AirlineTester class is a driver class, and contains just a main method. The main

method must create 6 passenger objects, 3 airport objects, and 2 flight objects. Give all

of the objects different values, so they are all unique.

Make sure that you test all of the methods (including all getters and setters, and constructors)

of each class at least once. Also, make sure that you test that the Flight toString

method will print out 揘ot Set?if an airport is not set for either the source or destination.

Show that the Flight addPassenger method will properly return false if the flight is

already full. The point of this question is to prove to us that everything works as it should,

so think about how best to do this.

// This is the Airport Class.

// The Airport class represents an airport.

publicclass Airport

{

private String Name;

private String Code;

private String Location;

// This is the constructor. It has three parameters.

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;}

public String getCode()

{return Code;}

public String getLocation()

{return 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;

}

}

// This is the Passenger class.

// This class represents a passenger.

publicclass Passenger

{

private String firstName;

private String lastName;

private String age;

// This is the constructor.

public Passenger(String first_Name, String last_Name, String 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;}

public String getLastName ()

{return lastName;}

public String getAge ()

{return age;}

// This is the toString method that returns the information.

public String toString ()

{

String Name ="Name:" +" " + firstName +" " + lastName;

String Age ="Age:" +" " + age;

String result = Name +"\n" + Age;

return result;

}

}

[8804 byte] By [Ace1a] at [2007-11-27 7:30:16]
# 1

What is the problem in setting up the two constructors in the Flight Class... It should be like this:

public Flight (int flightNo) {

...... // Do your initialization here

}

public Flight(int flightNo, Airport origin, Airport dest) {

...... // Do your initialization here

}

And what is the problem in implementing the methods?

diptaPBa at 2007-7-12 19:10:27 > top of Java-index,Java Essentials,Java Programming...
# 2

> For this question, you will be writing 4 classes. You are required to put

> each class in its own

> file, and so you should submit 4 Java files. The first 3 classes are

> worth 3 marks each, and the

> driver class, AirlineTester, is worth 1 mark.

so you got 6 marks already =)

c:

// Flight.java

public class Flight {

String flightNumber;

Airport origin,

destination;

Passenger[] passengers = new Passenger[5];

public Flight(String flightNumber) {

this.flightNumber = flightNumber;

}

public Flight(String flightNumber, Airport origin, Airport destination) {

this.flightNumber = flightNumber;

this.origin = origin;

this.destination = destination;

}

public boolean addPassenger(Passenger p) {

boolean result = false;

for (i = 0; i < passengers.length && result; i++) {

if (passengers[i] == null) {

passengers[i] = p;

result = true;

break;

}

}

}

public int numPassengers() {

int result = 0;

for (int i = 0; i < passengers.length; i++) {

if (passengers[i] == null) {

result = i;

break;

}

}

return result;

}

// getter & setter, to String ...

}

d:

public class AirLineTester {

public static void main(String[] args) {

// ...

}

}

j_shadinataa at 2007-7-12 19:10:27 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi j_shadinata ,

Thanks for your help. I just have some small questions about the code you posted. First, in the addPassenger method, you set the boolean result to be false, and in the for loop you set the value of result must be true so that the body of the for loop will be executed. Since the initial value of result is false, the body of the for loop will never be executed. So I think "for (i = 0; i < passengers.length && result; i++)"should be "for (i = 0; i < passengers.length && ! result; i++)" . Also, you forget to define i .

Another thing is thatinstead of writing "if (passengers == null)" , can I write

"if (passengers == "")" ? (Not using null) According to the requirement of the question, do I need to write getters for Airport origin and destination ? If so, how ? Also, I don't quite understand that why there are two constructors. ( what is the first constructor " public Flight(String flightNumber) " used for ? ) I mean if I create Flight objects, when should I use the first one and when the secondone ?

I am a beginner of Java. Thanks for helping .

Ace1a at 2007-7-12 19:10:27 > top of Java-index,Java Essentials,Java Programming...
# 4
Next time please use a meaningful subject line. Yours conveys zero information.
jverda at 2007-7-12 19:10:27 > top of Java-index,Java Essentials,Java Programming...
# 5
I suggest you look up the null literal and understand it first before you try to remove it from your program
r035198xa at 2007-7-12 19:10:27 > top of Java-index,Java Essentials,Java Programming...