This lab will give you a chance to work with some key features of input and output in Java programming. These topics are covered in Section 1.5 of your text.
To complete this project, follow the steps listed below:
Before moving on, you should take the time to look over the source code for RandomSeq.java. Hopefully you will have no trouble seeing what this program should do when you run it, but ask your lab instructor about anything that might seem unclear.
Important reminder: for our purposes it will not be sufficient merely to compile individual source files. Unfortunately, this is all that happens if you right-click on a source file and select the Compile File option. Instead you want to use the Build or Clean and Build option. This compiles all the source files in your project, and it creates the dist subfolder containing the distribution JAR file named Lab05.jar. As usual, we will be using this JAR file to run individual project files from the terminal window.
java -cp dist/Lab05.jar RandomSeq 10
You should now be staring at a list of 10 random real values in the terminal window.
which creates a new textfile named randomseq.txt and makes it the standard output for this invocation of RandomSeq. If you now use ls to list the files in your Lab05 folder, you should see randomseq.txt as well as the usual NetBeans subfolders. You can use either the cat or more command to list the contents of randomseq.txt.
java -cp dist/Lab05.jar RandomSeq 10 > randomseq.txt
Of course you could choose other names for the output file. You can also supply a complete or relative pathname (not just a base name) to create the file in some other folder. For instance, try
and then see if you can find the new file randomseq2.txt.
java -cp dist/Lab05.jar RandomSeq 10 > ../randomseq2.txt
First you need to download these libraries, either from the Booksite or directly from the links above, and add them to the default package for your project. While you are at it, you will also want to download another library class named StdDraw.java, which is intended to help you write programs for drawing graphics.
java -cp dist/Lab05.jar RandomSeq 10
Hopefully you now see the same sort of display in the terminal window that the original version produced...
This statement prints a single random value on a line, just like the println() statement in RandomSeq.java. But it prints the value using a format specifier string that specifies the total number of character spaces used to display it (7 in this case), as well as the number of digits shown after the decimal point (5).
Try out this feature now, by replacing the println() statement in RandomSeq by the printf() version as written above. Rebuild the project and run RandomSeq to see the formatted values. We will use the new version for the rest of this lab.
Note: As of Java 1.5 there is a standard System.out.printf() statement, which is quite similar to the textbook StdOut version.
Now return to the terminal window, and enter the command
Note that nothing seems to happen at first. That is because this program is waiting for you to provide the standard input stream it is looking for. This will happen only when you start entering data from the keyboard. In this case, you want to supply a sequence of real numbers, separated from each other by whitespace (blanks, tabs, newlines -- but no commas or other punctuation marks). For keyboard input on Unix/Linux systems, you will "end" this stream whenever you type ctrl-D. For Microsoft Windows systems, you end the standard input stream by typing ctrl-Z, followed by the ENTER key.
java -cp dist/Lab05.jar Average
So... enter, say, 5 values, not all on the same line, and then type ctrl-D. What happens at that point?
The command to redirect the input is as follows:
So enter this command, and the Average program should immediately read all the numbers in randomseq.txt and write its results to standard output (which in this case is the screen again).
java -cp dist/Lab05.jar Average < randomseq.txt
The first part of this invokes the cat command, which in this case will list the contents of randomseq.txt to its standard output. This is follwed by a vertical bar (|), which represents the pipe that sends this output as standard input to a sort command. As used here, sort treats its standard input as a sequence of lines (character strings) which it sorts (using standard lexicographic order) and then passes on to its own standard output.
cat randomseq.txt | sort
So enter this command, and see if the results help to clarify all the words you just tried to read...
As a somewhat more long-winded example, try your hand at using the pipe mechanism to have RandomSeq generate 1000 numbers, which Average reads and processes. Let the standard output of Average be the screen, so that when you run this command all you will see is something like the following:
Number = 1000 Average = 0.498597009999994
Now use the textbook StdDraw class (which you should have downloaded along with StdOut and StdIn) to write a complete new program named DrawRandomSeq, which reads the output of RandomSeq2 and produces a graphic display similar to the one shown below:
This display shows N random values (1000 in this example), with each value plotted vs. its index. Thus the x-scale of the plot should be from 0 to N. You can decide for yourself what sort of y-scale works best here.
Note that the horizontal blue line shown above is just the average of all the random values. For this part you probably want to compute the average directly within DrawRandomSeq.
When your project is complete and working correctly, demonstrate it to your lab instructor. Then, before you exit NetBeans, clean your Lab05 project. Finally, before you logout, switch back to your terminal and set your default directory back to csci/273.002/labs. Then create a JAR file of your Lab05 project folder, using the command
jar cf Lab05Project.jar Lab05Please leave both your Lab05 project folder and Lab05Project.jar in your csci/273.002/labs directory for the remainder of the semester.