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.
// Hello.java - this program prints a hello message.
// your name
// CPSC 215
// January 15, 2002
public class Hello
{
public static void main(String argv[])
{
System.out.println("Hello CPSC 215!");
}
}
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/lab1The 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.
| Iteration | N | M | N % M |
|---|---|---|---|
| 0 | 80844 | 25320 | 4884 |
| 1 | 25320 | 4884 | 900 |
| 2 | 4884 | 900 | 384 |
| 3 | 900 | 384 | 132 |
| 4 | 384 | 132 | 120 |
| 5 | 132 | 120 | 12 |
| 6 | 120 | 12 | 0 |
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