Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using the application AirportCode.java. This application includes a long extended-if construct that matches an airport code to the name of its city. Change this long
Using the application AirportCode.java. This application includes a long extended-if construct that matches an airport code to the name of its city.
Change this long if-statement to utilize parallel arrays. Create an array of code strings and a parallel array of city strings. When the button is clicked, match the input to the list of codes and write out the corresponding city name. Essentially, make the application do what it does now with fewer lines of code.
AirportCode.java.
// This application returns the name of an airport given // its code import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AirportCode extends JFrame { // Class wide component declarations private JTextField inputText; private JLabel inputLabel; private JButton theButton; // set up GUI public AirportCode() { // Set up container Container frameContainer = getContentPane(); frameContainer.setLayout( new FlowLayout() ); // Construct textfield1 and its label inputLabel = new JLabel("Airport Code"); frameContainer.add( inputLabel ); inputText = new JTextField( 5 ); frameContainer.add( inputText ); // Set up button and associate with event handler theButton = new JButton("Get Name"); frameContainer.add( theButton ); ButtonHandler handler = new ButtonHandler(); theButton.addActionListener( handler ); // Set application window attributes setTitle( "Airport Code Identifier" ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setSize( 200, 150 ); setVisible( true ); } // end constructor TextFieldTest // Private inner class for event handling private class ButtonHandler implements ActionListener { // Process button event public void actionPerformed( ActionEvent event ) { if ( event.getSource() == theButton ) { // Get text from text field String airportCode = inputText.getText(); String airportName = new String(); if (airportCode.equals("MBS")) airportName = "Saginaw"; else if (airportCode.equals("GRR")) airportName = "Grand Rapids"; else if (airportCode.equals("LAN")) airportName = "Lansing"; else if (airportCode.equals("DTW")) airportName = "Detroit"; else if (airportCode.equals("FNT")) airportName = "Flint"; else if (airportCode.equals("TVC")) airportName = "Traverse City"; else airportName = "Unknown"; String outString = "Airport is: " + airportName; JOptionPane.showMessageDialog( null, outString ); } } // end method actionPerformed } // Main method - to launch application public static void main( String args[] ) { AirportCode application = new AirportCode(); } } // end class TextFieldTest
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started