cd csci/273.002/labs
to enter your csci/273.002/labs directory.
Then launch the NetBeans IDE. Recall that you can do this either
by entering the terminal command
netbeans &
or by selecting the Applications/Programming/NetBeans IDE
6.5 option in the toplevel window bar of your desktop.
Once NetBeans is ready, create a new project named Lab04 following the same steps you followed in your previous labs. Important reminders: make sure that the Project Location (the folder that will contain your Lab04 project folder) is set to csci/273.002/labs. Also, remember to uncheck the Create Main Class checkbox before you click Finish.
Now go to the NetBeans explorer window. Make sure that the Projects view is selected, open your Lab04 project, then open Source Packages, and look for <Default Package>. Remember that if you want to create a new Java class in the default package, you can right-click on <Default Package> and select the type of class you want (Java class, Java main class, Empty Java file), and start editing.
Important reminder: remember that the Booksite listings come in two versions. The first version you see displays the source listing in the form of an HTML file, which includes visual features intended to improve human readability. If you click the first link at the top of the HTML listing, you will see the plain-text file. In all cases, you should download only plain-text versions into your NetBeans projects.
Note that the most efficient way to download complete Booksite demos is to right-click on the plain-text link to raise a popup dialog and select the Save Link As... option. Just remember that you want to save the file in the Lab04/src subfolder to make it part of the default package.
Remember that Lab04.jar contains only the bytecode (.class) files for the project classes, not the project source files. But as you saw in your previous labs, you can use a terminal window to run any of the classes in your project from this JAR file. So return to your terminal window at this point, and make an initial test of each Booksite program in your project by entering the following commands:
cd Lab04
java -cp dist/Lab04.jar Sample 5 20
java -cp dist/Lab04.jar CouponCollector 1000
java -cp dist/Lab04.jar PrimeSieve 100
java -cp dist/Lab04.jar SelfAvoidingWalk 20 1000
Note: the first of these commands assumes that your default
directory was previously set to csci/273.002/labs. If you are
not sure about this, replace the first command by
cd ~/csci/273.002/labs/Lab04
At this point you should pause to study the comments at the top of each of these demo programs, to make sure that you understand just what each program does. Then look again at the output from each one and convince yourself you know what is going on...
Once you have properly mediated on all these demos, make more detailed tests of the following subset. As you change the input arguments, try to note and understand any corresponding changes or trends in the program output.
java -cp dist/Lab04.jar Sample 5 10
java -cp dist/Lab04.jar Sample 10 10
java -cp dist/Lab04.jar Sample 20 10
java -cp dist/Lab04.jar CouponCollector 10
java -cp dist/Lab04.jar CouponCollector 100
java -cp dist/Lab04.jar CouponCollector 1000
java -cp dist/Lab04.jar CouponCollector 10000
java -cp dist/Lab04.jar CouponCollector 100000
java -cp dist/Lab04.jar CouponCollector 1000000
java -cp dist/Lab04.jar PrimeSieve 10
java -cp dist/Lab04.jar PrimeSieve 100
java -cp dist/Lab04.jar PrimeSieve 1000
java -cp dist/Lab04.jar PrimeSieve 10000
java -cp dist/Lab04.jar SelfAvoidingWalk 21 1000
java -cp dist/Lab04.jar SelfAvoidingWalk 21 10000
java -cp dist/Lab04.jar SelfAvoidingWalk 31 10000
java -cp dist/Lab04.jar SelfAvoidingWalk 41 10000
java -cp dist/Lab04.jar SelfAvoidingWalk 50 10000
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 questions about the programs themselves as well as your runtime results. You should also expect requests to experiment with additional command-line arguments for some of them.
Write a program Deal that takes a command-line argument N and prints N poker hands (five cards each) from a shuffled deck, separated by blank lines.
Note: your text provides some code snippets that should be useful to you here (see pp. 91-93). For your convenience, they are listed below (in slightly revised form):
String [] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
String [] rank = {
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace" };
String [] deck = new String [52];
for (int i = 0; i < 13; i++)
for (int j = 0; j < 4; j++)
deck [4*i + j] = rank[i] + " of " + suit[j];
int numCards = deck.length;
for (int i = 0; i < numCards; i++) {
int r = i + (int) (Math.random() * (numCards - i));
String t = deck[i];
deck[i] = deck[r];
deck[r] = t;
}
Meditate a bit on these statements, then copy whatever you find
useful into
your main() method. Note that you can select and copy
lines like this directly from your browser and paste them
into NetBeans source files. Of course you will have to write
some lines of your own, but these snippets should help a lot...
Checkpoint 2: Demonstrate your program to your lab instructor, using the terminal window to supply the argument.
Checkpoint 3: Demonstrate your program to your lab instructor, using the terminal window.
Modify SelfAvoidingWalk (Program 1.4.4) to calculate and print the average length of the paths as well as the dead-end probability. Keep separate the average lengths of escape paths and dead-end paths.
Checkpoint 4: Demonstrate your working program to your lab instructor, using the terminal window.
java -cp dist/Lab04.jar ShowOneWalk 21
If all goes well, you should now be staring at a new window that might look something like this:
This is nothing more than a visual demo related to SelfAvoidingWalk. In particular, it displays a different single random path through the N-by-N grid each time you run it. The one shown above is obviously an example of a dead-end path.
Note that the actual demo, namely ShowOneWalk.java, uses a "standard library" class StdDraw.java available from the Booksite. But ShowOneWalk itself is a fine example of the concise visual programs that you will soon know how to write.
That's it, nothing for you to modify here... so you can proceed directly to
Checkpoint 5: Run ShowOneWalk for your lab instructor, using the terminal command listed above.
After your instructor has checked out the various stages of your project, you are almost finished with the lab. But before you exit NetBeans, Clean your Lab04 project. Then reset the default directory in your terminal window to be csci/273.002/labs again. Reminder: if your default directory is still set to csci/273.002/labs/Lab04, the simplest command to move one level "upstairs" is just
cd ..
Now create a JAR file of your complete Lab04 project development
folder, using the command
jar cf Lab04Project.jar Lab04
Remember that this JAR file differs from the distribution JAR
fileLab04.jar that
NetBeans creates in the dist project folder when you Build
a project. Lab04Project.jar contains your entire project
development folder, including all your sources and NetBeans project
management files.
Please leave both your Lab04 project folder and Lab04Project.jar in your csci/273.002/labs directory for the remainder of the semester.