Due on: March 13
For this lab you do not have to submit anything for the "Unix exercises" part. These exercises introduce you to more Unix tools that might help you in doing your assignments. You do need to submit the Java programs at the end of the lab.
UNIX Exercise: The "diff" utility
The "diff" utility is used to compare two files for differences and identify the lines that need to be changed to make the two files identical. You can use "diff" with either individual files or whole directories.
Using emacs, create two files (named list.1 and list.2) that contain the following short lists:
ant ant bee bow cat catTo compare the two files for differences, at the command tool prompt enter:
diff list.1 list.2The output should look like this:
2c2 < bee --- > bowThis tells you there is a difference in line 2 of the files: the first file (<) contains "bee" while the second file (>) contains "bow". The "2c2" means that to make the files the same, you would have to change line 2 in the first file to be that of line 2 in the second file. "diff" shows these two lines -- the three hyphens (---) separate the contents of the two files.
Now, using the text editor (emacs), change file list.2 so that it looks like the following:
ant bow cat fredUse the "diff" command again as given above. You should see the following output:
2c2 < bee --- > bow 3a4 > fredThe first four lines are the same as before. The last two lines tell you that an extra line is in the second file (the "3a4" means you would have to add the fourth line in the second file into the first file, after its third line) The second file contains a fourth line "fred" that does not appear in the first file. The other diff "command" is d for delete -- it would show you the lines you need to remove from the first file to make it the same as the second file.
The syntax of "diff" is:
diff [-cefhn] [-C <num>] [-D <string>] [-bitw] <file_1> <file_2>Use the "man" command to find out what all the options above mean (i.e. enter "man diff" at the command line prompt).
NOTE: The output of "diff",
which isn't always easy to read, indicates text that must be added, changed,
or deleted to make the two files identical. It can also be used to generate
instructions (called a script) that you can use to perform the modifications
required.
UNIX Exercise: The "grep" utility
The "grep" utility command is a Unix command that lets you find a phrase in a file or group of files. If "grep" finds the phrase, it prints out the line or lines that it found it in. The first argument to "grep" is the phrase you are searching for, in double-quotes, and the second (and third, fourth,...) arguments is the file (or files) that you want "grep" to search in. For example, do the command:
grep "cat" list.2This should print out one line with the word "cat" on it. That's because the line that "cat" was in in the file "list.2" was simply just the word "cat". Now do:
grep -n "cat" list.2This should print out "3:cat". What the "-n" does is tell grep to print out the line number as well as the line itself. Grep can be used to search more than one file at a time. When it does this, it also prints out the file name that the phrase was found in. A shorthand way of specifying the two list files that you have from the first exercise (list.1 and list.2) is "list.*", which means "all files that begin with "list." -- the "*" means "anything here", so "list.*" means "list.(anything)"
Do the command:
grep "cat" list.*It should print both file names (list.1 and list.2), and the word "cat" following them, because they both contain a line with "cat" in it. Now do:
grep -n "cat" list.*This prints out both the name and the line number of where it finds "cat".
Questions: (continue doing the script to capture your output)
UNIX Exercise: Housekeeping All the users on our system are given a limited amount of disk space in the UNIX account. Since the disk space is limited, you should use it wisely. Wise use of the disk space requires that you periodically inspect all your files and delete the ones you no longer need.
The command "quota -v" will tell you how much disk space you are allowed. The command "du" will tell you how much you are using.
Unwanted/unneeded files can be one or more of the following:
Starting Java
Note: Supporting material for this lab is in Chapter 2 of the Java textbook.
1. Use emacs to enter the following lines of Java code into a file called "Hello.java". Watch your punctuation!
class Hello
{
public static void main(String[] arguments)
{
System.out.println("Hello World!");
}
}
Now, compile the program by typing
the following at the command prompt:
javac Hello.javaYou can now run the program by typing
java HelloYou should see the message "Hello World!" appear on the screen. Congratulations! You have just written your first Java program!
2. Use emacs to
create a copy of the Name_Tag program on page 53 of the text. Compile
and run the program with your name as the command line argument.
System.out.println("Hello World");
This routine prints the text string given as an argument, and then does
a "carriage return", or in other words, gets ready to print whatever comes
next on the following line. There is a corresponding procedure that prints
the text, but does not start a new line afterward, but will print more
on the same line. It is
System.out.print("Hello World");
So, two calls of System.out.print will print on the same
line, and a new line is only started when System.out.println
is finally called. The statements
System.out.print("Hello ");
System.out.print("this is a ");
System.out.println("single line of output");
Will result in the output
Hello this is a single line of outputAnd then anything after these statements will start on a new line (because the last statement is a call to System.out.println). Notice the spaces at the ends of the first two strings that are printed.
These print routines can also print other values, like integers and real numbers. The statement
System.out.println(5*4+6);will print out
26And these forms of printing can be combined to print things like:
System.out.print("The result of this expression is: ");
System.out.println(5*4+6);
which results in the output
The result of this expression is: 26
class printargs
{
public static void main(String Args[])
{
System.out.println("The first argument is " + Args[0]);
System.out.println("The second argument is " + Args[1]);
}
}
Where the Args variable can be thought of as a "list of strings",
and the syntax Args[0] can be thought of like the ML List.nth
function (it returns the 0th element, where element 0 is the first element).
This program, if compiled and then run like
java printargs hello goodbyewould produce the output
The first argument is hello The second argument is goodbyeIf it was run like
java printargs 2345 65.4It would print
The first argument is 2345 The second argument is 65.4But it is important to know that those numbers are just strings that look like numbers! To use them in an expression, what we need to do is to actually convert them from strings into numbers. Java will automatically convert integers to reals (and vice versa), but it won't automatically convert a string to either type of number. We can do this in the following way using built-in functions provided by Java:
class printargs
{
public static void main(String Args[])
{
int num1; // this declares an integer variable
double num2; // this declares a real variable
num1 = Integer.valueOf(Args[0]).intValue(); // this converts to int
num2 = Double.valueOf(Args[1]).doubleValue(); // this converts to double
System.out.print("The first argument plus 3 is ");
System.out.println( num1 + 3);
System.out.print("The second argument plus 3.4 is ");
System.out.println( num2 + 3.4);
}
}
This program, when run with the same arguments as above, will print
The first argument plus 3 is 2348 The second argument plus 3.4 is 68.8Note that the System.out.print and the System.out.println procedures can take integer and real values, and print them out, as well as strings. But each call can only have one argument, of one of these types.
3. Write a java program in
a file called lab6a.java that reads one integer value and prints out the
value multiplied times 5. Print some phrase along with the value such as
"Your answer is: ". The class name in your program should be lab6a,
so that when you compile lab6a.java, you get a lab6a.class file.
4. Write a java program in a file
called lab6b.java that reads one integer and one real value, and prints
out the sum of the two numbers, and then the product of the two numbers.
The values should have phrases printing out with them, like in the previous
problem. Again, the class name should be lab6b.
5. Write a java program in a file called lab6c.java that prints out the square of an integer argument, using the function below:
static int square ( int x)
{
return x * x;
}