recitation8 - 15-100 RECITATION 8 - FALL 2007 Introduction...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-100 RECITATION 8 - FALL 2007 Introduction In this recitation, you will work with a simple array of objects. Also, you will experiment with composing, compiling and running code using the Terminal application on the Macs. DO NOT USE ECLIPSE FOR THIS RECITATION. Exercises Download the zip file you need for this recitation from the course website. Move this to your workspace. This project contains two classes in the folder of the project. Plane - a class that models a simple plane that has a destination and a flight number. Airport - a class that models an airport runway that holds 10 planes in an array in the order that they will take off. 1. After starting the Terminal application from your dock, navigate to the Java code in your recitation project: cd Desktop/MyWorkspace/Recitation8 Open the Plane.java file for editing: 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.) 2. Open the Airport.java file using pico: pico Airport.java 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 this class: javac Airport.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 The java command starts the main method in the name of the class that you supply. (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 again. SHORTCUT: To compile all of your Java files at once, type javac *.java (*.java matches any file name that ends in .java). 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. Additional Exercises (time permitting) 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 Google 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. When You're Done... Create a zip file of the project folder for this recitation and submit this zip file on Blackboard to the Recitation area. DO NOT ERASE YOUR RECITATION PROJECT FOLDER IN CASE WE NEED TO SEE IT LATER (IF THE SUBMISSION IS DONE INCORRECTLY). ...
View Full Document

This note was uploaded on 01/25/2010 for the course CS 15100 taught by Professor Tom during the Fall '07 term at Carnegie Mellon.

Ask a homework question - tutors are online