University of North Carolina at Asheville

CSCI 273: Mathematical Programming


Lab 08

Libraries and Clients

[Introduction] [Instructions] [What To Submit]


Introduction

This lab will give you a chance to work with several examples of library classes in Java. Some of your tasks will involve downloading and running applications that use methods defined in one or more library classes available on the Booksite for your text. Such programs are known as clients of the libraries. You will also have a chance to write a new version of one of the Booksite client applications. And you will even write a small Java library of your own, containing some simple but useful mathematical functions.

You can find useful background information on libraries and clients in Section 2.2 of your text.

Instructions:

To complete this project, follow the steps listed below:

  1. Login to your home directory and launch a terminal window. Use the cd command to change your default directory to csci/273/labs. Keep this window open for future reference.
  2. Launch the NetBeans IDE and raise the New Project dialog. Define the project to be a Java Application named Lab08. Make sure that the project folder will be created within your csci/273/labs folder. As in your previous labs, you should also uncheck the Create Main Class checkbox before you click Finish.
  3. You just completed what is the usual first step in these labs, which is to create a single NetBeans project to hold all the classes you will work with during the session. But this lab is now going to take an unusual step: before proceeding further with Lab08, you need to create another NetBeans project named Booksite1. In contrast to the procedures used in your earlier labs, this lab will use a completely separate project to build all the library classes that you will need to download from the Booksite for Lab08. So immediately raise the New Project dialog again. This time define the project to be a Java Class Library, rather than a Java Application. Enter the project name Booksite1, and again make sure that the project folder will be created within your csci/273/labs folder.
  4. Now download the textbook standard IO library classes StdIn.java, StdOut.java, StdDraw.java, StdAudio.java, StdRandom.java, StdStats.java, StdArrayIO.java, Gaussian.java, and Picture.java into the default package for project Booksite1. You will also need to locate the file Gaussian.java among the Chapter 2 demos and include it as well. As in your earlier labs, you will not need to modify any of these classes in any way.

  5. Now Build your Booksite1 project. Then verify that you now have a dist subfolder containing a distribution JAR file Booksite1.jar. If not, consult immediately with your lab instructor before proceeding.

    Otherwise, you now need to put a copy of Booksite1.jar into your csci/273/labs folder. Assuming that the default directory in your terminal window is still set to csci/273/labs, you can do this by entering the following command:

    
        cp Booksite1/dist/Booksite1.jar .
        
    Note: be sure not to leave out the dot (.) at the end of the line... and please type at least one space before the dot...
  6. Now you can put the Booksite1 project aside and return to Lab08. You will be using Lab08 to download and develop clients of several of the library classes that you compiled under the project Booksite1. But your next task is to make all those library classes available to Lab08.

    In your earlier labs you did this using a brute-force approach: you downloaded all the sources (.java files) for the library classes you needed, placing them directly into the default directory for your project. This time you will use a more elegant (and more efficient) method: you will add the JAR file Booksite1.jar to the classpath for your Lab08 project. This will automatically make all the Booksite classes that you downloaded into Booksite1.jar available to any client program you write in Lab08. Of course Booksite1.jar contains only the bytecode (.class) files, not their sources, but the bytecode is all your client programs will need.

    A good way to add any JAR file (or folder) to a project classpath in the NetBeans IDE is to select the Projects view, right-click on the Lab08 project name, and select the Properties option in the popup menu. This should raise a property sheet dialog that initially appears as shown below:

    In the left-hand panel labeled Categories, select Libraries. The initial dialog should then be replaced by one that looks like this:

    In the right-hand panel of the resulting dialog, select Add JAR/Folder. Clicking this should raise the subdialog

    Find and select Booksite1.jar, then click Open to add it to your project classpath.

  7. Now return to the Booksite. Find the file IFS.java, which is listed in your text as Program 2.2.3 (see page 233) and download it into the default package for Lab08. While you are at it, do the same for the demo Bernoulli.java, Program 2.2.6, on page 242).

    Open IFS.java in the NetBeans editor window. If you immediately see lots of red lines, that probably means that somehow you did not actually manage to add Booksite1.jar to your classpath in the last step. It might also be possible that you omitted one or more of the required files in Booksite1. In any event, if you cannot resolve the problem(s) by yourself, consult with your lab instructor before you attempt to go further.

    As soon as the NetBeans editor seems to be happy with IFS.java, open Bernoulli.java in the editor window and make sure it is also free of red lines. Then attempt to Build your Lab08 project. This step should cause you no problems, since you are just compiling unmodified Booksite source files. But if anything seems to go wrong at this stage, it is again time to alert your lab instructor.

  8. IFS is a client program that draws some strange and wondrous patterns based on a randomized sequence of linear transformations known as Iterated Function Systems. You can find a brief survey of this topic in your text (see pp. 231-235). To do its thing, IFS requires some data about the transformations from its standard input stream. So before you can run the client program IFS, you will also need to download some sample textfiles of the type it expects.

    To keep all these files in one convenient place, you should now create a new subfolder of Lab08 named, say, IFSData. Then download the files sierpinski.txt, barnsley.txt, and coral.txt into IFSData. Note that these examples were all downloaded from the Booksite and stored locally for your convenience. Incidentally, with a little detective work you should be able to download several more IFS data files directly from the Booksite.

  9. Now return to your terminal window, and enter the command cd Lab08 to set your default directory to be the Lab08 project folder. You are now ready to run IFS using a terminal command, but this time the command will be a bit more long-winded. For one thing, you will have to include Booksite1.jar as well as the project distribution JAR file Lab08.jar in the runtime classpath. You will also need to redirect the standard input stream to one of the IFS data files you downloaded above.

    So hopefully you now understand why the command you should now enter is

    
        java -cp dist/Lab08.jar:../Booksite1.jar IFS 10 < IFSData/sierpinski.txt
        
    So try to type all this in, and stand back...

    Not very impressive? Essentially just a blank window? Well, if you look closely enough, you just might be able to see a few barely discernable points scattered around.

    OK, so close this window and try increasing the value 10 in this command to 100. Repeat using 1000, then 10000, and maybe even 100000. By that time you should have a much better idea of what is going on. And you certainly should now take time to check out the other examples cited above.

    The purpose of this exercise is certainly not to make you an expert on Iterated Function Systems, but it does illustrate how much you can benefit from well-designed libraries. Look in particular at the source code for IFS.java, and you will have to admit that it is very short. Part of that is due to the fact that it is data-driven, meaning that it uses a very general format that allows the same code to process a wide variety of transformations. But the use of library methods to handle various subtasks is an even more important factor in reducing the size of client programs. If client programs had to include explicit code to do all the work by themselves, they would be much longer.

  10. Bernoulli is a client program discussed on pages 241-242 of your text. This application repeatedly conducts a simulated experiment for a specified number of trials T. Each complete experiment (one trial) consists of tossing a fair coin N times in a row. The quantity measured in each experiment is the number of heads that occur out of the total of N tosses. This is a random variable that conforms to a binomial distribution, which for increasing N approaches the Gaussian distribution you encountered in your previous lab.

    The primary reason for including this program here is to introduce you to some useful array-plotting methods in the Booksite library StdStats.

    Try running Bernoulli using the following command:

    
        java -cp dist/Lab08.jar:../Booksite1.jar Bernoulli 50 10000
        
    While the plot window is still open, you should look over the source code for Bernoulli in the NetBeans editor and try to understand just how the plot was generated. While you are at it, note again how little code it takes to do this sort of thing if suitable library classes are available to you.
  11. OK, so now it is your turn to write (or at least modify) a client program. Create a new Empty Java File named MyBernoulli in the default package for Lab08. Start to fill it in by copying all the contents of the Booksite demo Bernoulli. Then modify the code as requested in Exercise 2.2.10 on page 249 of your text (the statement of this problem is also available on the Booksite). Basically you need to make an animated plot of the histogram as it grows, which you can accomplish simply by redrawing the plot after each trial. You are also requested to accept an additional command-line argument, specifying the probability P that the coin actually turns up heads each time. Note that the original Bernoulli demo simply assumed that the experiments used a fair coin, with P = 0.5.
  12. As your final task in this lab, write your own Java library class of hyperbolic functions, namely sinh(x), cosh(x), and so forth. This is really just Exercise 2.2.2 in your text (see page 248). The best way to do this is to create yet another new NetBeans project, which you might name Hyperbolics. As with Booksite1, this should be a Java Class Library rather than a Java Application.

    As soon as you write this class and manage to compile it, show your source code to your instructor.

    Extra Credit, up to 20 extra points: Add a separate Java main class named PlotHyperbolics to your Lab08 project. Using your Hyperbolics library, have PlotHyperbolics produce a combined plot of sinh(x) and cosh(x). The results should at least vaguely resemble the image shown below:


What To Submit

When your project is complete and working correctly, demonstrate it to your lab instructor. Then, before you exit NetBeans, clean your Lab08 project. Finally, before you logout, switch back to your terminal and set your default directory back to csci/273/labs. Then create a JAR file of your Lab08 project folder, using the command

jar cf Lab08Project.jar Lab08
Please leave both your Lab08 project folder and Lab08Project.jar in your csci/273/labs directory for the remainder of the semester.