0

call method from another class

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 (btnExecuteQueryActionPerformed). 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.

this is my Flight Schedule class

package assignment9;

public class FlightSchedule {

    private Flight[] flights;

    /**
     * Creates a new schedule from the specified flights.
     *
     * @param flights the flights in this schedule
     */
    public FlightSchedule(Flight[] flights) {
        this.flights = flights;
    }



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

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

        }
        return f;
    }

    /**
     * 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 flightNum = 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[flightNum] = flights[j];
                flightNum++;
            }

        }

        return flight;

    }

    /**
     * this method takes in location code and returns number of flights of that
     * same location code
     *
     * @param locationcode
     * @return number of flights of that particular location code
     */
    private int countFlightsToDestination(String locationCode) {
        int flight = 0;
        for (int i = 0; i < flights.length; i++) {
            Airport temp = flights[i].getDestanation();
            String a = temp.getLocationCode();
            if (a.equals(locationCode)) {
                flight++;
            }

        }
        return flight;
    }

    /**
     * this method takes in location code and returns an array of flights of
     * that same location code
     *
     * @param locationcode
     * @return array of flights of that particular location code
     */
    public Flight[] getFlightsToDestination(String locationCode) {
        int flightDest = 0;
        int loopFlight = countFlightsToDestination(locationCode);
        Flight[] flight = new Flight[loopFlight];
        for (int j = 0; j < flights.length; j++) {
            Airport temp = flights[j].getDestanation();
            String s = temp.getLocationCode();
            if (s.equals(locationCode)) {
                flight[flightDest] = flights[j];
                flightDest++;
            }

        }

        return flight;

    }

    /**
     * this method takes in region code and returns number of flights of that
     * same region code
     *
     * @param regioncode
     * @return number of flights of that particular region code
     */
    private int countFlightsToRegion(int regionCode) {
        int f = 0;
        for (int i = 0; i < flights.length; i++) {
            Airport temp = flights[i].getDestanation();
            int s = temp.getRegion();
            if (s == regionCode) {
                f++;
            }

        }
        return f;
    }

    /**
     * this method takes in region code and returns an array of flights of that
     * same region code
     *
     * @param regioncode
     * @return array of flights of that particular region code
     */
    public Flight[] getFlightsToRegion(int regionCode) {
        int flightReg = 0;
        int loopFlight = countFlightsToRegion(regionCode);
        Flight[] flight = new Flight[loopFlight];
        for (int j = 0; j < flights.length; j++) {
            Airport temp = flights[j].getDestanation();
            int s = temp.getRegion();
            if (s == regionCode) {
                flight[flightReg] = flights[j];
                flightReg++;
            }

        }

        return flight;

    }

    /**
     * this method takes in pilotID and returns number of flights of that same
     * pilotID
     *
     * @param pilotID
     * @return number of flights of that particular pilot ID
     */
    private int countFlightsByPilot(int pilotID) {
        int a = 0;
        for (int i = 0; i < flights.length; i++) {
            Flight s = flights[i];
            if (s.hasPilot()) {
                Pilot temp = s.getPilot();
                int x = temp.getId();
                if (x == pilotID) {
                    a++;
                }
            }
        }
        return a;
    }

    /**
     * this method takes in pilotID and returns an array of flights of that same
     * pilotID
     *
     * @param pilotID
     * @return array of flights of that particular pilot ID
     */
    public Flight[] getFlightsByPilot(int pilotID) {
        int p = 0;
        int loopFlight = countFlightsByPilot(pilotID);
        Flight[] flight = new Flight[loopFlight];
        for (int i = 0; i < flights.length; i++) {
            Flight x = flights[i];
            if (x.hasPilot()) {
                Pilot temp = flights[i].getPilot();
                int s = temp.getId();
                if (s == pilotID) {
                    flight[p] = flights[i];
                    p++;
                }
            }
        }

        return flight;

}
}

and this is my main code

package assignment3;

import javax.swing.JOptionPane;

public class Assignment3Form extends javax.swing.JFrame {

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

    /**
     * Creates new form Assignment3Form
     */
    public Assignment3Form() {
        initComponents();
        btnLoadData.grabFocus();
        initInputState();

        schedule = null;
        FlightSchedule fs;

    }

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

    }                                               

    private void comboQueryActionPerformed(java.awt.event.ActionEvent evt) {                                           

        String selection = comboQuery.getSelectedItem().toString();
        boolean allSelected = selection.equals("All Flights");
        txtQuery.setEditable(!allSelected);
        txtQuery.setText("");
        if (!allSelected) {
            txtQuery.grabFocus();
        }

    }    
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by ObSys
0

First of all the main class should have "package assignment9;" at the top.

You seem to have invoked the FlightSchedule class in you main class with "schedule" as the identfier and then set this to "null" in your constructor.

To use a method in the FlightSchedule class you need to do "schedule.getFlightsToRegion(*RegionCodeGoesHere*)".

This should invoke the getFlightsToRegion method

Edited by ObSys

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.