Dana Vrajitoru
C151 Multi-User Operating Systems

Lab 5

Due date: Monday, March 1, 2021.

Ex. 1 : javac

In this lab you will learn to compile a small program with the javac compiler and use file input and output redirection.

In your folder c151, create a folder called week5 and then change your working directory to it.

a. Emacs Settings

Before we start, edit the file .emacs in your home directory. If you already have such a file, go to the end of it and add the content below. If the file doesn't exist yet, create and store the lines below in it:

; use spaces instead of tabs
(setq-default indent-tabs-mode nil)
; use 4 spaces in the indentation
(setq default-tab-width 4)
(setq tab-width 4)

These lines add settings in Emacs that tell it to use spaces instead of tabs for indentation of code, and also to use an indentation of 4 spaces. These are currently important features when we edit code.

b. Compiling and executing

Copy the following Java source file:
/home/dvrajito/public_html/teach/c151/SumArray.java
to your c151/week5/ directory. Go to that directory.

Alternatively you can download it from:
SumArray.java
and then transfer it to your Linux account by sftp, or use wget. Or you can simply copy the text from the browser to a buffer in Emacs and save it locally.

This program inputs a number of elements from the user, stores them in an array, computes their sum, and outputs the result.

Read the code to familiarize yourself with it.

Before using the debugger, let us compile the program with javac and run it.

Compile the program with the command

javac SumArray.java

Check that the file SumArray.class was created in the folder, then run the program without the debugger with the command

java SumArray

We can run a program by redirecting to it an input from a file. For this, edit a file called input.txt. In this file, write the number 5 at the top, and then 5 more numbers after that, one per line. Then run the command

java SumArray < input.txt

You should see the output of the program as if you had entered the content of the file when prompted by the program.

We can also redirect the output of the program to another file. For that, run the command as

java SumArray < input.txt > output.txt

The program will now run without any visible output. However, if you look in the file output.txt, you should see the output of the program stored there.

c. Multiple Files and Packages

If you look at the file SumArray.java, it contains a line at the top specifying the package sumar that is commented out. Edit the file and uncomment this line.

To be able to compile the file with this package, create a folder called sumar, then move the file SumArray.java inside it. Then compile it with the new command

javac sumar/SumArray.java

If you look in the folder sumar, there should now be a two files there, the .java file you moved there, and a .class file.

To run the program again but from the package folder, use the command

java sumar.SumArray

This will run the program the same way as before. Make sure that it still works. You can also run it with the file input.txt.

Now, let's create another Java file to contain the main. Edit a file called Main.java containing an empty class Main, and move the main function from the class SumArray into it. Make sure the main is deleted from the class SumArray. Try compiling the two files. You will get some error messages, because the functions from the class SumArray are not defined in the class Main.

To fix this issue, we need to use the class name whenever we call these functions. Add SumArray. in front of all the function calls in the main, and recompile it. This time it should work.

Run the program with the command

java sumar.Main

Make sure everything still works fine. This is it for this lab.

Upload to Canvas: The file lab5.txt, containing the log of your terminal, and the files SumArray.java, Main.java, input.txt and output.txt that you created.