Hi ,
I'm almost done with my practice paper I have done with the first part and now I'm working in the second part I came to this question and I stopped and didn't know how to call method from another class here is the question "In the Assignment9Form class, write the code for the "Execute Query" button. If "Flight Number" is selected from the combo box, get the user's entry in the text box, and call the method getFlightsByNumber (from FlightSchedule class) Then call displayFlights, passing in the array of flights.
"
I added more information in case you need it
The excersice is about object-oriented technique of composition
the practice is to build a class representing a schedule of flights departing from UK.
The FlightSchedule class contains an array of Flight objects. It also provides methods for querying the array.
The Flight class contains an Airport object (the flight's destination) as well as two Pilot objects (the pilot and co-pilot for the flight).
Starter Code
The method displayFlights is provided. It will display any array of Flight objects to the output area.
There are three data file pilot flight and airports

Is the FlightSchedule class used as a storage that can be read/written? Do you need have an instance of the class created inside your Assignment9Form?

The reason I asked because the requirement does not say that the method needs to be static. If the data stored inside the class is not static, you would need to call the method from an instance of the class. Simply initiate an instance of the class inside the other class you want, and call it.

//i.e.
class Assignment9Form {
  FlightSchedule fs;
  public Assignment9Form() {
    fs = new FlightSchedule();  // the schedule is created and stored for querying
  }
  ...
  ...
  // just an example of the call and returned value is an array of Flight objects
  public void whatever(String flightNumber) {
    Flight[] flights = fs.getFlightByNumber(flightNumber);
    ...
  }
}

// in FlightSchedule class would contain the method...
public Flight[] getFlightByNumber(String flightNumber) {
  ...  // search and return the value, return an empty array if not found
}

However, if this is a static, it would be...

//i.e.
class Assignment9Form {
  // no need an instance of FlightSchedule
  public Assignment9Form() {
  }
  ...
  ...
  // just an example of the call and returned value is an array of Flight objects
  public void whatever(String flightNumber) {
    Flight[] flights = FlightSchedule.getFlightByNumber(flightNumber);
    ...
  }
}

// in FlightSchedule class would contain the static method...
public static Flight[] getFlightByNumber(String flightNumber) {
  ...  // search and return the value, return an empty array if not found
}

Edited 4 Years Ago by Taywin

 public class Assignment3Form extends javax.swing.JFrame {

    Pilot[] pilots;
    Airport[] airports;
    Flight[] flight;
    private FlightSchedule schedule;

this is what in my flightSchedule class every thing seem to be right
I tried your solution , but it still not working
I have to call getFlightsByNumber from btnExecuteQueryActionPerformed

 private void btnExecuteQueryActionPerformed(java.awt.event.ActionEvent evt) {
 } 





 public FlightSchedule(Flight[] flights) {
        this.flights = flights;
    }

    public Flight[] getFlights() {
        return flights;
    }

    private int countFlightsByNumber(String flightNumber) {
        int flight = 0;
        for (int i = 0; i < flights.length; i++) {
            String temp = flights[i].getFlightNumber();
            if (temp.equals(flightNumber)) {
                flight++;
            }

        }
        return flight;
    }

    /**
     * this method takes in flight number and returns an array of flights of
     * that same flight number
     *
     * @param flightNumber
     * @return array of flights of that particular flight number
     */
    public Flight[] getFlightsByNumber(String flightNumber) {
        int f = 0;
        int loopFlight = countFlightsByNumber(flightNumber);
        Flight[] flight = new Flight[loopFlight];
        for (int j = 0; j < flights.length; j++) {
            String temp = flights[j].getFlightNumber();
            if (temp.equals(flightNumber)) {
                flight[f] = flights[j];
                f++;
            }

        }

        return flight;

Edited 4 Years Ago by sk8ergirl

Hmm... Actually, you may use a dynamic array to create the Flight set, and then may return an array instead. The reason is that you don't know how many flights will match the incoming name (dynamic). Using ArrayList would be easier (from java.util.ArrayList). Somewhat as follows...

ArrayList<Flight> foundFlights = new ArrayList<Flight>();  // create an empty dynamic array
for (Flight f : flights) {  // iterate without using index (no need)
  // now the f is representing a Flight object in the flights array
  if (f.getFlightNumber().equals(flightNumber)) {  // found flight data
    foundFlights.add(f);  // add to dynamic array list
  }
}
return foundFlight.toArray();  // convert from ArrayList to array

Edited 4 Years Ago by Taywin

This article has been dead for over six months. Start a new discussion instead.