If you carried out the initial instructions in Lab 01 last week, you should already have a directory (folder) in your account named csci/273.002/labs. If so, you should now set this to be your default directory by entering the command
If this command produces error messages, go back immediately to Lab 01 and work through the instructions in Step 1. Get help from your lab instructor as necessary. Do not proceed further in this lab until your default directory is in fact csci/273.002/labs.
or by selecting the Applications/Programming/NetBeans IDE 6.8 option in the toplevel window bar of your desktop.
Once NetBeans is ready, select the File/New Project... option in the toplevel menu bar. As you saw last week in Lab 01, a dialog box will appear as shown below:
Since this is only your second lab using NetBeans, we will cover the next few steps in detail one more time. In your later labs we will omit all these explicit instructions, since they will remain essentially the same from now on.
In the Categories and Projects panes shown above, select Java and Java Application respectively and then click Next. This raises a second dialog with the title New Java Application, in which you are supposed to enter the name and location of your project.
Enter Lab02 as the Project Name. Also, make sure that the Project Location (the folder that will contain your Lab02 project folder) is set to csci/273.002/labs under your account. If it is set to anything else, edit the Project Location textbox now to make sure NetBeans will create your project in your labs folder.
Also for this lab, remember to uncheck the Create Main Class checkbox:
Otherwise, accept the default settings and click Finish.
Now go to the NetBeans explorer window. Make sure that the Projects view is selected, open your Lab02 project, then open Source Packages, and look for <Default Package>. If you want to create a new Java class in the default package, just right-click on <Default Package> and select the type of class you want (Java class, Java main class, Empty Java file), and start editing. But first you should check out the next step...
Use your browser to access the Booksite, and download the java source files Ruler.java, IntOps.java, Quadratic.java, LeapYear.java, and RandomInteger.java from Section 1.2 in your text. Be sure that all these files end up in Lab02/src/.
Note: as you already may have noticed, the Booksite listings come in two versions. The first link shows the source listing as an HTML file, which uses some fancy features intended to improve human readability. You will want to follow a second link at the top of the HTML listing to display a plain-text file. In all cases, you should download only plain-text versions into your NetBeans projects.
Note that Lab02.jar contains only the bytecode (.class) files for the project classes, not the project source files. But as you saw in Lab 01 last week, you can use a terminal window to run any of the classes in your project from this JAR file, using commands such as the following:
Of course you can also use NetBeans to run any of these files. However, it turns out to be more convenient to use the terminal-window approach when you want to try any of the textbook demos that require command-line arguments. In fact it is much more awkward to supply or change these arguments through the NetBeans IDE (even though NetBeans is great for lots of other things, as you will see). But by combining the use of NetBeans (as an editing/debugging tool) with terminal commands to run programs, you have the benefits of using a powerful IDE and a flexible way to provide command-line arguments for your Java programs.
cd ~/csci/273.002/labs/Lab02 java -cp dist/Lab02.jar Ruler java -cp dist/Lab02.jar IntOps 2 3 java -cp dist/Lab02.jar Quadratic -5 6 java -cp dist/Lab02.jar LeapYear 2009 java -cp dist/Lab02.jar RandomInteger 10
Checkpoint 1: Let your lab instructor know you are ready to show that you can run each of these programs from the terminal window. Be prepared for requests to experiment with various command-line arguments for some of them.
Actually, to be a little more precise, have your program generate integers in the range a, a+1, ..., b-2, b-1. That is, the range should include a but not b.
Checkpoint 2: Demonstrate your program to your lab instructor, using the terminal window to supply the values of a and b.
Checkpoint 3: Demonstrate your program to your lab instructor, using the terminal window.
After your instructor has completely checked the various stages of your project, you have only a few housekeeping chores left. First, before you exit NetBeans, clean your Lab02 project. This step removes all the .class files, but leaves all your project sources (.java files) intact. Next, open a terminal window and use the cd command to enter your csci/273.002/labs directory. Then create a JAR file of your Lab02 project development folder, using the command
jar cf Lab02Project.jar Lab02Recall that this JAR file differs from the Lab02.jar file that NetBeans creates in the dist project folder when you Build a project. Lab02.jar is a "distribution" JAR file, suitable only for running Java programs, not writing or compiling them. Lab02Project.jar contains your entire project development folder, including all your sources and NetBeans project management files. You could upload this file to another system running NetBeans, extract its contents via the command
jar xf Lab02Project.jarand use NetBeans to continue working on it.
Please leave both your Lab02 project folder and Lab02Project.jar in your csci/273.002/labs directory for the remainder of the semester.