6 Replies Latest reply: Jun 24, 2007 12:00 PM by 807605 RSS

    About file read and output (complex question)

    807605
      Basically, I finished most part of the assignment. The thing is that I can not get the
      "load " method of part c). How to read the file that is created and create the new Flight object? I can finish the whole assignment in a few hours, and I just want to see if anyone can provide some hints. Also, if you find some mistakes in my codes, please tell me although I am confident that there should be no mistakes. The information and my codes are shown below:



      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 �Kennedy International Airport�, whose code is �JFK�, and whose location
      is �New York, NY�). Assume that the name, location, and code of an airport cannot

      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 �Joe�, last name is �Smith�, 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 �Examples 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.

      // This is the Airport Class. 
      // The Airport class represents an airport. 
      public class 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 method for Assignment 5. 
        public static Airport buildFromString (String s )
        { 
          // Here I make some String Objects to use. 
          String Airport_Name;
          String Airport_Code;
          String Airport_Location;
          
          // I use the 'substring' and 'indexOf' methods to get the correct information. 
          Airport_Name = s.substring(0, s.indexOf( "(", 0 ) - 1); // this gives the airport name.
          
          Airport_Code = s.substring(s.indexOf("(", 0 ) + 1, s.indexOf(")", 0 )); // this gives the airport code.
          
          Airport_Location = s.substring(s.indexOf(":", 0) + 2, s.length() ); // this gives the airport location.
          
          // Here I call the constructor method and pass the required data. 
          // A new object is created. 
          Airport newAirport = new Airport (Airport_Name, Airport_Code, Airport_Location );
          
          // return the object. 
          return  newAirport;
          
        }
        
        
      
      }
      
      
      
      // This is the Passenger class.
      // This class represents a passenger.
      
      public class 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.
        private int 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.
        
        public int 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 method for Assignment 5.
        public static Passenger buildFromString (String p)
        {
          // Here I make some objects and a variable to use later. 
          String FirstName;
          String LastName;
          int Age;
          
          FirstName = p.substring(6, p.indexOf (" ", 6) ); // this gives the first name.
          
          LastName = p.substring(p.indexOf (" ", 6) + 1, p.indexOf ("\n", 0) ); // this gives the last name.
          
          // Here I did something special to convert a String into a integer. 
          Age =Integer.parseInt ( p.substring(p.indexOf (":", 5) + 2, p.length() ) ); // this gives the age. 
          
          // Here I call the constructor and make a new object. 
          Passenger newPassenger = new Passenger (FirstName, LastName, Age);
          
          // Return the object. 
          return newPassenger; 
         
        }
        
        
        
      }
      
      
      
      
      
      
      
      
      // This is the Flight class.
      import java.io.*;
      public class 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.
         // This method has been modified so that it allows users to add unlimited passengers. 
         public void addPassenger(Passenger p) 
         {  
            if (this.numPassengers () >= passengers.length ) 
            {
              Passenger [] newArray = new Passenger [passengers.length * 2];
              for (int i = 0; i < passengers.length; i++)
              { newArray  = passengers [i]; } 
      passengers = newArray;
      }

      for (int i = 0; i < passengers.length; i++)
      {
      if (passengers[i] == null)
      {
      passengers[i] = p; // Add the passenger to the array.
      break;
      }
      }
      }

      // This is the numPassengers method.
      public int 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.

      public void setOrigin (Airport newOrigin)
      { origin = newOrigin; } // Set the Origin.

      public void 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 getPassenger method.
      public Passenger getPassenger ( int n )
      {
      Passenger result;
      if (this.numPassengers() < (n + 1) )
      { result = null; }

      else
      { result = this.passengers[n]; }

      return result;

      }




      // This is the save method
      public boolean save (String s)
      {
      boolean result = false;

      try
      {
      PrintWriter out = new PrintWriter(s);

      out.println (this.getFlightNumber() );
      if(this.getOrigin() == null )
      { out.println("Not Set"); }
      else
      { out.println(this.getOrigin() ); }

      if(this.getDestination() == null )
      { out.println ("Not Set"); }
      else
      { out.println(this.getDestination() ); }

      for(int i = 0; i < passengers.length; i++ )
      {
      if(passengers [i] != null)
      { out.println (passengers [i] ); }
      else
      { break; }

      }

      out.close();

      result = true;

      }

      catch (IOException ex)
      { result = false; }

      finally
      { return result; }

      }




      //




      }









































        • 1. Re: About file read and output (complex question)
          807605
          learn to play nice yet?

          http://forum.java.sun.com/thread.jspa?threadID=5187365&messageID=9732246#9732246

          Let me look at your code....
          Do you have a test class where you test these three classe above?

          Message was edited by:
          petes1234
          • 2. Re: About file read and output (complex question)
            807605
            To petes1234

            Ha Ha Ha, hey punk, you know what, your words make me laugh, really baby!
            I don't care who you are , but I am sure that your parents never teach you how to be a person, how to behave the way you should. Look at your reply, don't you feel that you are so naive? Really, you have many stuff to learn. The first thing is how to be a person . By the way, I will not use "Ace 1 " to log in any more, so I won't see any reply, and I think you won't reply anything valuable.
            Seriously, I can finish all my assignment myself, it is just a matter of time. Believe or not, it is up to you .
            Sun is a great
            cooperation , and I love this forum. But the existence of you pollute this forum....
            • 3. Re: About file read and output (complex question)
              800282
              To petes1234

              Ha Ha Ha, hey punk, you know what, your words
              make me laugh, really baby!
              don't care who you are , but I am sure that your
              parents never teach you how to be a person, how to
              behave the way you should. Look at your reply,
              don't you feel that you are so naive?
              ...
              Sun is a great
              ooperation , and I love this forum. But the
              existence of you pollute this forum....
              Yeah, that's a great way to get help.
              • 4. Re: About file read and output (complex question)
                807605
                To petes1234

                Ha Ha Ha, hey punk, you know what, your words
                make me laugh, really baby!
                don't care who you are , but I am sure that your
                parents never teach you how to be a person, how to
                behave the way you should. Look at your reply,
                don't you feel that you are so naive? Really, you
                have many stuff to learn. The first thing is how
                to be a person . By the way, I will not use "Ace 1
                " to log in any more, so I won't see any reply, and
                I think you won't reply anything valuable.
                eriously, I can finish all my assignment myself, it
                is just a matter of time. Believe or not, it is up to
                you .
                Sun is a great
                ooperation , and I love this forum. But the
                existence of you pollute this forum....
                Great, just f'n great. While you were typing this, I was typing this:
                On a first read of Flight:

                1) get rid of some extraneous comments. many such as "// Here I use 'this' again." don't add much and only clutter your code. of course ignore this if they are required by your teacher.

                2) In addPassenger, System.arraycopy may be a simpler way to copy Passengers from one array to the other.

                3) Seems simpler to me to hold numPassengers as a private field of the class rather than a method.

                4) Your teacher is kind of forcing you to re-writing the ArrayList class -- a variable sized array. You may get some good ideas by reading the code for this class.

                5) Your save method has me lost. Finally a spot where more verbose comments would be useful, but all you have is "// This is the save method". And why the return result in the finally clause? That just smells odd. In all likelihood, you will use the finally block to close any open files, but my guess is not much more.
                Something like:
                    public boolean save(String s)
                    {
                        boolean result = false;
                        PrintWriter out = null;
                        try
                        {
                            out = new PrintWriter(s);
                            out.println(this.getFlightNumber());
                            if (this.getOrigin() == null)
                            {
                                out.println("Not Set");
                            }
                            else
                            {
                                out.println(this.getOrigin());
                            }
                
                            if (this.getDestination() == null)
                            {
                                out.println("Not Set");
                            }
                            else
                            {
                                out.println(this.getDestination());
                            }
                
                            for (int i = 0; i < passengers.length; i++)
                            {
                                if (passengers[i] != null)
                                {
                                    out.println(passengers);
                }
                else
                {
                break;
                }

                }
                result = true;
                }
                catch (IOException ex)
                {
                result = false;
                }

                finally
                {
                if (out != null)
                {
                out.close();
                }
                }
                return result;

                }


                But no more help. Get lost. Stay lost.
                • 5. Re: About file read and output (complex question)
                  796440
                  To petes1234

                  Ha Ha Ha, hey punk, you know what, your words
                  make me laugh, really baby!
                  don't care who you are , but I am sure that your
                  parents never teach you how to be a person, how to
                  behave the way you should. Look at your reply,
                  don't you feel that you are so naive? Really, you
                  have many stuff to learn. The first thing is how
                  to be a person . By the way, I will not use "Ace 1
                  " to log in any more, so I won't see any reply, and
                  I think you won't reply anything valuable.
                  eriously, I can finish all my assignment myself, it
                  is just a matter of time. Believe or not, it is up to
                  you .
                  Sun is a great
                  ooperation , and I love this forum. But the
                  existence of you pollute this forum....
                  Oh, goody. Another rectum for the killfile.
                  • 6. Re: About file read and output (complex question)
                    807605
                    well Ace, I may not be the smartest or most helpful poster here, but it now appears that you've lost the support of two posters who happen to be gold-star and platinum-star posters, folks who know their chit.

                    Try not shooting yourself in the foot when you post under the new identity. I wish you lots of luck.