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.
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:
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.
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
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.
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