gnuplot
command (click here
for a more detailed tutorial).
1) For each function you want to plot on your graph, create a file. Each line of these files should contain two numbers, the x value and the y value, separated by white space. Let's say these are called theoretical-1.dat, experimental-1.dat, theoretical-2.dat, and experimental-2.dat. Each file might look like this:
10 2.5 20 3.1 100 12 500 55.2 1000 152
2) Make a file of gnuplot commands. Say it's called 4-plots.gp (the gnuplot file must have the .gp file extension). Note that two of the three "set terminal"/"set output" commands shown below are commented out (# is comment):
# To view in X: set terminal pbm monochrome small set output "graph.pbm" # To print on a PostScript printer: #set terminal postscript #set output "graph.ps" # To view on an ordinary terminal: #set terminal dumb 79 35 #set output "graph.txt" set title "Two Theory and Two Experiment Plots" set xlabel "Input size" set ylabel "Time (sec)" set data style linespoints plot "theoretical-1.dat" title "Theoretical 1", \ "theoretical-2.dat" title "Theoretical 2", \ "experimental-1.dat" title "Experimental 1", \ "experimental-2.dat" title "Experimental 2"
3) If you want a file that is a picture of the graph containing the four plots, type the following at the shell prompt:
$ gnuplot 4-plots.gp
- If you used "set terminal dumb 79 35", this will be an ASCII picture of the graph. The graph's file name should end in ".txt". It will be 79 characters wide and 35 lines long. Such a picture is for testing purposes only. Do not turn it in for this assignment; it's too low-quality. You can look at it in your favorite text editor or with
$ more graph.txt
or print it with
$ lpr -Pprintername graph.txt
- If you used "set terminal postscript", the output will be a PostScript file that can be printed on a laser printer. The graph's file name should end in ".ps". You can print it on a laser printer with
$ lpr -Pprintername graph.ps
or you can use ghostview to view the file:
$ ghostview graph.ps
- If you used "set terminal pbm monochrome small", the output will be a PBM file that can be displayed on a workstation or X terminal. The graph's file name should end in ".pbm". You should create .pbm files for Homework 4. You can view them with
$ xv graph.pbm
Random Numbers
Random
class in the java.util
package
provides pseudo-random numbers.Math
has a method random
, which
is just a wrapper for instantiating Random
one time, and then
supplying values.
double d = Math.random(); // 0.0 ... 1.0
Random
are:
public Random(); // no args constructor
public Random(long seed);
// using the same seed gives the same sequence of numbers
public int nextInt(); // returns a random int
public long nextLong(); // returns a random long
public double nextDouble(); // returns a value in range 0.0 to 1.0
public synchronized void setSeed(long seed);
// resets the seed value
public synchronized double nextGaussian();
// a Gaussian distribution has mean 0.0, standard deviation 1.0
nextGaussian
provides numbers symmetrically distributed
around zero, and successively unlikelier to be picked the further away
from zero you get.
Random r = new Random();
int myturn = 1 + Math.abs(r.nextInt()) % 6;
double mypercent = r.nextDouble() * 100.0;
Random
with or without a seed.setSeed
method can be used to reset the seed value
at any time.Executing a Program from a Java Application
java.lang.Runtime
does this.exec
method in the Runtime
object with your
command as an argument. This call creates a Process
object. Give
the full path name to the executable, and make sure the executable really
exists on the system.Process
(which will be coming to
you as an input stream) to an input stream reader in your program.BufferedReader
on it and read a line at a time, as in the
code below.
import java.io.*;
public class exwho {
public static void main(String args[]) {
try {
Runtime rt = Runtime.getRuntime(); // step 1
Process prcs = rt.exec("/bin/who"); // step 2
InputStreamReader isr = // step 3
new InputStreamReader( prcs.getInputStream() );
BufferedReader br = new BufferedReader( isr ); // step 4
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
}
catch (IOException ioe) { System.out.println(ioe); }
}
}
System
called
Runtime
which does a couple of runtime-related things.Process
that allows you
to get in, out, err streams, and to destroy, or to wait for a process./bin/who
is a Unix program that lists the users
on a system.
$ javac exwho.java
$ java exwho
als84 pts/25 Apr 2 21:30 (98AAE98E.ipt.aol.com)
avd4 pts/5 Apr 2 22:39 (cpw.math.columbia.edu)
hh245 pts/6 Apr 2 21:41 (butler213a-06)
vss8 pts/0 Apr 2 22:31 (handogod.schap.rhno.columbia.edu)
Runtime rt = Runtime.getRuntime();
Process prcs = rt.exec("gnuplot plot-random.gp");
plot-random.gp
(also note that this code has to catch an IOException).
[CS3139 Home]