15-110 FALL 2009 [CORTINA]

LAB 9

In this lab, you will work with a simple array of objects. Also, you will experiment with composing, compiling and running code using the Terminal application. DO NOT USE ECLIPSE FOR THIS LAB.

EXERCISES

Click HERE to download a project named Lab9 that you will complete for this lab. EXTRACT THIS ZIP FILE AND LEAVE THE FOLDER ON YOUR DESKTOP.

This project contains the following classes:

  1. Start the Terminal application (look under the menus for this application on the Linux machines). Once you have it started, navigate to the Java code by using the cd command:

    cd Desktop/Lab9

    The cd command stands for "change directory". Before Microsoft Windows and MacOS came along, files were stored in directories, not folders. They're the same thing, but the analogy changed when windowing environments became popular.

    Open the Plane.java file for editing using the simple pico text editor:

    pico Plane.java

    The pico editor allows you to type text and move the cursor with the arrow keys. The commands to save your file, cut and paste text, etc. are shown at the bottom of the screen. Example: to save your file and exit, use the key combination Control-X (shown on the screen as ^X). Experiment with some of the other control commands.

    In the editor, complete the Plane class by adding a toString method that returns a string that contains the plane's destination and flight number. Save the file and exit from pico using control-X. Save the file under the same name. After you exit pico, test to see if the Java code in this file compiles correctly:

    javac Plane.java

    javac is the Java compiler. If your class compiles correctly, you will see no errors and will get the terminal window prompt again. If you see errors, go back into pico and edit the file to correct the error(s). (Each syntax error will contain a line number. Use control-C in pico to find that line.) If you re-edit the file, you must compile it again.

    To compile all of your Java files at once, type

    javac *.java

    *.java matches any file name that ends in .java. Do this to compile the entire program now.

    If you get no syntax errors, you are ready to run the program:

    java Airport

    The java command starts the main method in the name of the class that you supply. In this case, our main method is in the Airport class. You should not type .java after the class name. (NOTE: If you get into an infinite loop, use Control-C to kill the program run.) If you don't get the correct output, go back into pico and make changes to your code. Then save and exit and compile and run again.

    SHORTCUT: To edit and compile/run, open two Terminal windows. Use one to edit and one to compile/run.

  2. Open the Airport.java file using pico:

    You will see that this class has a main method starts by declaring an array named runway that will hold 10 (references to) planes. Ten new planes are created that are stored into the array. Then the main method calls a helper method that prints out all of the planes in the array using your toString method that you wrote for each plane.

    Add code in main that shifts all of the planes one position toward the beginning of the array, inserting the first plane into the last cell of the array (at index 9). Then call the helper method to print out your array of planes again to see if you solved the problem correctly. Save and exit out of pico, and compile your program:

    javac *.java

    Again, if you have any syntax errors, use pico to make changes.

    Once you get no syntax errors, you are ready to run the program:

    java Airport

  3. Once you get this code to work correctly, use pico to add more code to the main method of Airport.java that shifts the planes in the array one position away from the beginning of the array, moving the last plane into the first array position (index 0). Remember to call the helper method to print out the array of planes again. Compile and run the revised program to see if it works correctly. If not, go back to the editor and make changes, compile and run again until it works correctly.

  4. Use pico to edit Airport.java to add more code to sort the planes in the array in increasing order based on flight number. To do this, write code that looks at all of the planes to find the minimum flight number and swap this plane with the plane at index 0. Repeat this again, looking for the plane with the minimum flight number starting from index 1 and swap it with the plane at index 1. Repeat this again, starting from index 2, etc. Eventually, all of the planes will be sorted from increasing order based on flight number. (You should be able to do this with a pair of nested loops.) Remember to call the helper method to print out the array after you sort the planes. Edit, compile and run your final program until it works correctly.

If you want to use a more powerful editor in the terminal window, try emacs. (Use a search engine to find out more about how to use emacs.) The emacs editor is used by many professional computer scientists for editing in a UNIX environment.

HANDIN

At the end of lab, create a zip file of your program and submit it to the handin server: http://handin.intro.cs.cmu.edu/v1