University of North Carolina at Asheville

CSCI 273.002: Mathematical Algorithms


Lab 04

Arrays


[Introduction] [Lab Project] [What To Submit]

Introduction

In this lab you will use the NetBeans IDE and a terminal window to build and test simple console applications that create and use arrays in Java. In case you still need some help with NetBeans and terminal windows, do not hesitate to consult with your lab instructor. You may also want to review parts of your previous labs as you work through this project. And remember that you can get general help on Linux terminal commands from our Basic Linux Commands summary page.


Lab Project

  1. Login to your Computer Science Linux account and open a terminal window. Use the terminal command
    
        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.

  2. Now go to the Booksite and download the java source files Sample.java, CouponCollector.java, PrimeSieve.java, and SelfAvoidingWalk.java from Section 1.4 in your text. Be sure that all these files end up in your default package, which corresponds to the project subfolder Lab04/src/ under the Files view of your project.

    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.

  3. Now attempt to Build your complete Lab04 project. Remember that this step compiles all the files in your project and creates a JAR file named Lab04.jar in the project subfolder named dist. After building, you should use the Files view to verify that this JAR file was indeed created.

    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.

  4. Now add a new Java main class to the default package, named Deal. Write statements within the main() method of this class that solve Problem 1.4.10 in your text. For your convenience, the problem statement is as follows:

    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.

  5. Now add another new Java main class to the default package, named MyPrimeSieve. In the main() method of this class, start by copying all the lines from the Booksite demo PrimeSieve that you downloaded above. Then modify this program so that instead of just printing the number of primes less than or equal to N, it prints all the primes themselves. Try to find a way to print ten values on each line before going to the next line.

    Checkpoint 3: Demonstrate your program to your lab instructor, using the terminal window.

  6. Now add yet another new Java main class to the default package, named MySelfAvoidingWalk. In the main() method of this class, start by copying all the lines from the Booksite demo SelfAvoidingWalk that you downloaded above. Then modify this program to solve Exercise 1.4.18 in your text:

    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.

  7. Your final task in this lab is intended merely to give you a look ahead at the sort of programs you will be able to write as of next week. First, download the two source files StdDraw.java and ShowOneWalk.java into the default package. Clean and Build your project, then return to the terminal window and attempt to run ShowOneWalk using the following command:
    
        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.



What To Submit

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.


Last revised 4 February 2009, 10:30 am