CS 109/113

Lab 2

Arrays and yet more Unix

In this lab you will:
  1. Learn four more Unix commands
  2. Write two Java programs using for or whileloops and do stuff with arrays

Unix

Often you are in a terminal window and have just entered a directory, and having used the ls command to see its contents, you wonder "what is in that file?" You could, of course, open it in VSC, but that is a pain since you are already in the terminal. Unix to the rescue. There are two commands you can use.
CommandExplanation
catPrints the entire file to the screen. This is mostly useful for short files
lessPrints the file to the screen, one "page" at a time where are page is the amount of stuff that will fit onto your terminal window.
To advance to the next page, hit the spacebar.
To go back a page, hit 'b'.
To quit, hit 'q'

ssh

The ssh command (available on Ma, Unix and Windows) allows you to work on your laptop exactly as it you were working within a terminal window on a lab computer. This can be confusing as when you you are working in ssh, you do not effect your laptop, but rather your account on the department UNIX server. You are using the keyboard and screen of your laptop, that is all.

To start an ssh session, open a terminal on your laptop

        ssh UNIX_NAME@goldengate.cs.brynmawr.edu
    
where UNIX_NAME is your UNIX account name. When prompted, enter your password.

Unlike every other Unix command you have used, this one does not complete quickly, rather it effectively opens a terminal on the machine goldengate. So you end up with a terminal onto the UNIX servers that is running inside a terminal that is running on your laptop!

Once you have the ssh terminal running, set up -- on the UNIX servers -- for todays lab.

        cd 
        cd CS113 
        mkdir Lab2
    

If you have not done so already, open and log into a UNIX machine in the lab and open a terminal. Verify that the Lab2 directory exists.

Finally, close your ssh session (on your laptop) by entering exit.

Using ssh (and scp as described next) means that, in the future, you do not have to set foot in the lab when you want to submit an assignment (if you are doing your work on a laptop). Everything that you would do on a lab computer can be done from within an ssh session.

scp

scp is a Unix command that is closely related to ssh and cp. Like cp it is used to copy files. Like ssh, it is used to do things between two machines.

For instance, suppose you want to copy files for an assignment from you computer to the Unix computers -- e.g., so you can submit them. Futher suppose you are working on homework 50. Finally, suppose that you have done everything on your laptop. The first thing you need to do is to get the Unix side ready to receive your work. So use ssh to open a Unix terminal. Then create a HW50 directory
    cd
    cd CS113 
    mkdir HW50
this assumes you have a CS113 directory (you might have named it CS109). Once you have made the HW50 directory in Unix, open a second terminal on your own computer and in that terminal use "cd" to navigate to the directory containing your work for the assignment. Assuming you use the same directory structure on your own computer and in the lab, this process can be accomplished with the following commands
    cd
    cd CS113
    cd HW50
Then use the scp command to copy each of the files you want to submit from your computer to the lab. For example you should have a file named Readme (or readme, or README):
    scp Readme UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW5/Readme
scp is case sensitive, so you need to be sure to type the name correctly. As always, when you read "UNIX_NAME" put in your UNIX user name. repeat the scp command for each file in the directory on your laptop. Also, with each scp command you will need to enter your UNIX password.

Using scp to get files to your laptop

Frequently on assignments I will provide files ... for instance
    /home/gtowell/Public/CS113/HW5/cc.txt
this is on Unix. If yo are working on Unix, you can copy it to your local directory as
    cp /home/gtowell/Public/CS113/HW5/cc.txt cc.txt
likewise you can copy the file to you laptop using scp using:
    scp UNIX_NAME@goldengate.cs.brynmawr.edu:/home/gtowell/Public/CS113/HW5/cc.txt cc.txt
if you get messages about "cannot connect" from goldengate, try replacing "goldengate" with "comet".

Java

Arrays, part 1

Write a program called Verifier that goes through each command line input and verifies that that input is an integer in the range 0--100. If the input is not an integer, the program may die. If the input is an integer, but not in the right range, the program should print an error message -- e.g.
    -42 is not in the range 0 .. 100
The program should go through all command line inputs, be there 2 or 200. If all inputs are integers in the correct range, the program should print
    The input is good

Arrays, part 2

Write a program called Reverse that takes integers from the command line. First, create an integer array called forward that has the same length as the number of command line inputs. Next convert the command line inputs into integers, storing the result in "forward".

Next, create an array of doubles of the same size as "forward", but named "reversed". Then fill "reversed" with the contents of "forward" times 2.5 but in reverse order. Do this using a loop. So at the end of this step you should have two arrays, possibly named forward and reverse. If your command line input looked like
    java Reverse 1 10 7 9 30
then your arrays should be
    forward = [1,10,7,9,30] 
    reversed = [75.0, 22.5, 17.5, 25.0, 2.5] 
Hint to do the reversal using a loop. The location of something at location N in the forward array will be at location (reversed.length - 1 - N) in the reversed array.

Finally, print the contents of reversed and forward using a "for" loop so the zeroth entry in "forward" and "reversed" appear on the same line, followed by the first, ...

java Reverse 2 4 8 12 5
0: 2 5.0
1: 4 10.0
3: 8 20.0
4: 12 30.0
5: 5 12.5

What to hand in

Use your phone to take a picture of VSC screen(s) with your java programs. Send the picture to gtowell@brynmawr.edu