University of North Carolina at Asheville

CSCI 273.002: Mathematical Algorithms

Lab 03

Conditionals and Loops

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


In this lab you will use the NetBeans IDE and a terminal window to build and test simple console applications that use control flow statements in the Java programming language. 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 Lab 01 and Lab 02 as you work through this project. And remember that you can get 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. Helpful reminder: One way to do this is to right-click on any blank region of the Desktop, and select the Open terminal option of the popup menu. Immediately change your default directory (folder) in the terminal window to csci/273.002/labs by entering the command
        cd csci/273.002/labs

  2. Now 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, select the File/New Project... option in the toplevel menu bar. In the list boxes labeled Categories: and Projects: appearing in the first dialog, select Java and Java Application respectively. In the second dialog, enter Lab03 as the Project Name. Also make sure that the Project Location (the folder that will contain your Lab03 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. Remember also 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 Lab03 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.

  3. Use your browser to access the Booksite, and download the java source files,,,, and from Section 1.3 in your text. Be sure that all these files end up in your default package, which corresponds to the project subfolder Lab03/src/ under the Files view of your project.

    Caution: 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. You want to click the first 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.

  4. Now attempt to Build your complete Lab03 project. Remember that this step not only compiles all the files in your project, but it also creates a JAR file named Lab03.jar in a subfolder named dist. After building, you should use the Files view to verify that this JAR file was indeed created.

    Remember that Lab03.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 Lab03
        java -cp dist/Lab03.jar PowersOfTwo 20 
        java -cp dist/Lab03.jar Harmonic 100
        java -cp dist/Lab03.jar Sqrt 2.0
        java -cp dist/Lab03.jar Binary 255
        java -cp dist/Lab03.jar Factors 6
    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/Lab03

    Once you have verified that all the programs are running properly, make more detailed tests of the following selected programs. For each input, try to understand the program output.

        java -cp dist/Lab03.jar PowersOfTwo 30
        java -cp dist/Lab03.jar PowersOfTwo 31
        java -cp dist/Lab03.jar PowersOfTwo 32

    java -cp dist/Lab03.jar Binary 1 java -cp dist/Lab03.jar Binary 2 java -cp dist/Lab03.jar Binary 7 java -cp dist/Lab03.jar Binary 15 java -cp dist/Lab03.jar Binary 2147483647 java -cp dist/Lab03.jar Binary 2147483648

    java -cp dist/Lab03.jar Factors 26 java -cp dist/Lab03.jar Factors 52 java -cp dist/Lab03.jar Factors 32767 java -cp dist/Lab03.jar Factors 2147483647

    java -cp dist/Lab03.jar Sqrt 2 java -cp dist/Lab03.jar Sqrt -2

    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 explain your previous results, and also to experiment with additional command-line arguments for some of them.

  5. Now add a new Java main class to the default package, named MySqrt. In the main() method of this class, start out by copying all the statements in the Booksite demo Sqrt that you previously added to this project. Then modify the program so that if the command-line argument is a negative number, its only output is the message "ERROR: negative argument for sqrt()". Of course, for nonnegative numbers it should behave just like Sqrt.

    Checkpoint 2: Demonstrate your program to your lab instructor, using the terminal window to supply the argument.

  6. Now add another new Java main class to the default package, named Cbrt. Write statements within the main() method of this class that print the cube root of the command-line argument, using a strategy similar to that of the Booksite program Sqrt.

    Note: the algorithm used in Sqrt is actually a special case of Newton's method for finding roots of a function f(x), namely solutions of the equation f(x) = 0. In general, Newton's method is an iterative scheme, in which an initial guess at a root, say x0, is repeatedly updated using the formula

    xi+1 = xi - fi / (df/dx)i

    where fi = f(xi) and (df/dx)i is just the derivative of f evaluated at xi. Note that for Sqrt, the equation is just f(x) = x2 - C = 0, where the constant C is the input argument. Similarly, for Cbrt the equation is f(x) = x3 - C = 0. So hopefully from this much information, and the source code for Sqrt, you should be able to figure out the precise iteration scheme to use for Cbrt... but if necessary, you can get some guidance at this point from your lab instructor.

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

  7. Finally, add yet another new Java main class to the default package, and name this one AvgRandom. Write statements within the main() method of this class that solve Problem 1.3.9 in your text. For your convenience, the problem statement is as follows:

    Write a program that takes an integer N as a command-line argument, uses Math.random() to print N uniform random values between 0 and 1, and then prints their average value.

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

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 Lab03 project. Then reset the default directory in your terminal window to be csci/273.002/labs again. Note: if your default directory is still set to csci/273.002/labs/Lab03, the easiest command to move one level "upstairs" is just

    cd ..
Now create a JAR file of your complete Lab03 project development folder, using the command
jar cf Lab03Project.jar Lab03
Recall that this JAR file differs from the Lab03.jar file that NetBeans creates in the dist project folder when you Build a project. Lab03.jar is a distribution JAR file, suitable only for running Java programs, not writing or compiling them. Lab03Project.jar contains your entire project development folder, including all your sources and NetBeans project management files.

For future reference, remember that you can upload a NetBeans project development JAR file to another system running NetBeans, extract its contents via a command similar to

    jar xf Lab03Project.jar
and use NetBeans to Open your project and continue working on it.

Please leave both your Lab03 project folder and Lab03Project.jar in your csci/273.002/labs directory for the remainder of the semester.

Last revised 5 February 2009, 9:55 am