University of North Carolina at Asheville

CSCI 273.002: Mathematical Algorithms


Lab 02

Data Types, Values, Operators, and Expressions


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

Introduction

In this lab you will again use a terminal window and the NetBeans IDE to build and test simple console applications that use the built-in data types in the Java programming language. In case you don't remember everything you learned in Lab 01 about terminal windows and NetBeans, you may frequently want to refer to that lab as you work through this project. You can also get some 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. 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.

    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

    
        cd csci/273.002/labs
        
    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.
  2. So now that you are in the right default directory, launch the NetBeans IDE. Note: you can do this either by entering the terminal command
    
        netbeans &
        
    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...

  3. Now switch from the Projects view to the Files view. Again, first open your Lab02 project folder, then look for a folder named src. This is where the Java source files in your "default package" actually reside. It is also the folder into which you now want to download a few textbook demos.

    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.

  4. Now attempt to Build your complete Lab02 project. This not only compiles all the files in your project, but it also creates a JAR file named Lab02.jar and stores in in a subfolder named dist. After building, you should use the Files view to locate this JAR file.

    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:

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

    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.

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

    Write a program that takes two int values a and b from the command line and prints a random integer between a and b.

    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.

  6. Finally, add another new Java main class to the default package, named SumOfDice. Write statements within the main() method of this class that solve Problem 1.2.20 in your text. For your convenience, the problem statement is as follows:

    Write a program that prints the sum of two random integers between 1 and 6 (such as you might get when rolling dice).

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



What To Submit

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 Lab02
Recall 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.jar
and 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.


Last revised 21 January 2010, 3:35 pm