Lab 6: More Unix and Starting Java

Handed out: March 7

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           cat
To compare the two files for differences, at the command tool prompt enter:
diff  list.1  list.2
The output should look like this:
2c2
< bee
---
> bow
This 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
fred
Use the "diff" command again as given above.  You should see the following output:
2c2 
< bee 
--- 
> bow 
3a4 
> fred
The 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.2
This 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.2
This 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)

  1. Do 'grep -n "square" *.sml'. What files do you find "square" in?
  2. Do 'grep -n "bee" list.*'. What files do you find "bee" in?

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:

  1. The "core" file.
  2. The backup files generated by the editors (with the % sign at the end or  .BAK or .backup extension.
  3. The files which were useful at some earlier time but no longer are.
Look at all your files using the "ls" command.

Starting Java

Note: Supporting material for this lab is in Chapter 2 of the Java textbook.


Problems

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: You can now run the program by typing You 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.
 


More on printing text

In class, we saw the built-in procedure that is used to print a line: 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 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 Will result in the output And 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

will print out And these forms of printing can be combined to print things like: which results in the output

Reading in values from the command line

In class we saw that a command line argument is a string that can be used in the program, for example: 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 would produce the output If it was run like It would print But 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: This program, when run with the same arguments as above, will print Note 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.


More Problems

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;
            }