Lab 1: Getting Started with UNIX (Linux) and Java

Objectives. The objectives of this lab are:

Part I: UNIX Tutorial

During this semester all of Java programs will be developed on UNIX operating system. Thus it is important that you get comfortable with the operating system as early as possible. When you come to the lab you may login to any available workstation using your login and the password, which are the same as the login/password for your Trinity mail account.

Part II: Developing Java Programs on UNIX

We will be using Sun's JDK (Java Development Kit) to develop Java programs. The JDK is a collection of command-line programs, i.e., you have to type in commands to edit, compile, and execute your programs.

Creating a Working Directory. Use the file browser (the house icon) to create a directory (folder) in your home directory for this course (e.g., cpsc215). Once you create the cpsc215 folder, right click on its icon and use the Properties menu to set the protections on this directory so that only you (the owner) can view and modify the content of that folder. In other words, set the protections so that group and Others cannot view your cpsc215 folder. Once you have set the protections on your cpsc215 folder, create a directory within cpsc215 for today's lab (lab1).

Editing your program. Open the KWrite editor by clicking on its icon in the task bar. This editor is fairly intuitive and simple to use.

Changing your Working Directory. To compile this program within the Terminal window, you must first change your working directory to lab1. When you first start up the Terminal program, you will be in your home directory (e.g., /home/ram). To change into the lab1 directory, you could use the following command:

   cd cpsc215/lab1
The command is cd, which is short for change directory. The argument to the command is the relative path name of the directory you want to change to---in this case, cpsc215/lab1. Rather than using a relative path name, it is always possible to use the absolute path name of the directory. Here are some examples:
  cd /home/ram/cpsc215/lab1      -- Change to the lab1 directory
  cd /home/ram/                  -- Change to my home directory
  cd                             -- With no argument you change to my home directory
  cd ..                          -- The .. argument changes to the parent directory

Compiling your program. Assuming you are in the same working directory that the Hello.java file is in, the following command is used to compile the program:

   $ javac Hello.java

Running your program. Once your program is successfully compiled, the following command is used to run the program:

   $ java Hello

Correcting Errors. Replace the word System with system (of course, you need to edit your program using KWrite editor). Compile your program. What happened? As you observed, the JDK will print the error messages on the Terminal. Now correct the error, recompile and run your program. Note that everytime you modify your program, you need to save it before you compile it.

Things to Try on Your Own

Part III: Chinese Remainder Theorem

Problem

The greatest common divisor, or GCD, of two positive numbers n and m is the largest number j, such that n and m are both multiples of j. Euclid proposed a simple algorithm for computing GCD(n,m) where n > m, which is based on the a concept known as the Chinese Remainder Theorem. The basic idea is to repeatedly compute n mod m letting the remainder be the new value of m and letting m be the new value of n until the remainder is 0. Here's an example calculation where N = 80844 and M = 25320. Their GCD is the last non-zero number in the sequence of remainders, i.e., 12.

IterationNMN % M
080844253204884
1253204884900
24884900384
3900384132
4384132120
513212012
6120120

Details

Complete the following program by writing and testing an implementation of the gcd() method. As given here, the program will compile and run but will always return 0 as the GCD. Copy and paste the code into a file named GCD.java. Save it in your lab1 directory. Then write the code for the gcd() method.
import java.io.*;    // Java I/O classes

public class GCD {                               

    public GCD() {}  // Default constructor

    /**
     * gcd() computes the greatest common divisor of n, m where n > m.
     */
    public int gcd(int n, int m) {  
        return 0;
    }

    public static void main(String argv[]) throws IOException
    {
        BufferedReader input = new BufferedReader	
	      (new InputStreamReader(System.in));
         String inputString;

         int n, m;  // Two integer values

         System.out.print("Input the greater value, n:  ");  // Prompt
         inputString = input.readLine();                     // Read a String
         n = Integer.parseInt(inputString);     // Convert the String to an int
         System.out.println("You input: " + n); // Echo what the user input

         System.out.print("Input the smaller value, m:  ");  // Prompt
         inputString = input.readLine();                     // Read a String
         m = Integer.parseInt(inputString);     // Convert the String to an int
         System.out.println("You input: " + m); // Echo what the user input

         GCD theGCD = new GCD();       // Construct a GCD object
         int gcd = theGCD.gcd(n, m);   // Compute the GCD of n and m
         System.out.println("The GCD of " + n + " and " + m + " = " + gcd);
    } // main()
} // GCD

Hand In

You're done. Great work!