CS 109 --> 113

Lab 1, 2

Getting Started using Java and Unix

In this lab you will:
  1. Confirm lab access
  2. Confirm your ability to log into the lab computers
  3. Set up your account
  4. Create a Java program in Visual Studio Code (VSC)
  5. Compile and run the program from the command line
  6. Compile and run the program from within VSC

Lab Access

Check that your ID card opens the door into the lab. If it does not, let me know.

Logging In

Find an available computer in the lab. Follow along with me to log in to your Unix account. This process is also described here.
If you cannot log in, let me know.

Terminals

Open a terminal. Follow along with me to log in to your Unix account. This process is also described here.

Directory setup and some Unix

Suppose your user name is Xena. Then your "home directory" is /home/xena. You can see this using the Unix pwd command as follows:
    xena@loin ~$> pwd
    /home/xena
Here the xena@loin ~$> is the Unix "prompt" which shows that you are logged in as "xena" on a machine named "loin". (Your machine name will differ.) The response to the pwd command is /home/xena which is your home directory. pwd is short for "present working directory".

Now make a new subdirectory for all of your work in this course. Call this directory "CS113". (I usually use capital letters to start directory names. This is not required, it is just convenient.) To do so, use the Unix mkdir command as shown

    xena@loin ~$> mkdir CS113
Suggestion, never put spaces into either directory or file names. They are a pain in Unix and in Java.

You can see this is successful by getting a listing of the contents of the current directory using the Unix ls command as follows.
    xena@loin:~$ ls
CS113	 Documents  local  mpub		 Music	Pictures  public_html  tmp
Desktop  Downloads  mail   mpublic_html  Node	Public	  Templates    Videos
ls is short for "list". What you see will be slightly different, but you should certainly see CS113

Now go into the CS113 directory and make a new directory there to hold things you will do today. Then switch into the directory you just made

        xena@loin:~$ cd CS113
        xena@loin:~/CS113$ mkdir Lab01
        xena@loin:~/CS113$ cd Lab01
        xena@loin:~/CS113/Lab01$ ls
        xena@loin:~/CS113/Lab01$ pwd
        /home/xena/CS113/Lab01
        xena@loin:~/CS113$ cd
        xena@loin:~/CS113/Lab01$ pwd
        /home/xena/
        xena@loin:~/CS113$ cd CS113/Lab01
        xena@loin:~/CS113/Lab01$ pwd
        /home/xena/CS113/Lab01
    
cd is short for "change directory". It is almost always a good idea to use ls after cd to look at the files in the place you just moved to. In this case there are none, which is as expected since you just created the directory.

For each homework and lab, you should do something like the above to create a new clean place for the work you are about to do. That is, create a HW01 directory, a LAB02 directory, etc.

Now lets do something using Visual Studio Code (VSC).

Leave the terminal open, we will return to it

A Program in VSC

Start VSC. You can do this either from the terminal by entering code or from the dots gird in the lower left (as you did for starting a terminal).

Within VSC, select File Menu / "Open Folder"; then navigate to the Lab01 directory you just created and open that folder. Hint, always select the exact folder you want, not one of its parents.

On the left, click on the extensions button (3 squares together and one slightly apart (usually at the bottom)) and confirm in the list of installed extensions that "Extension Pack for Java" is installed.

On the left, click of the top icon -- a document on top of a document.

Put the mouse over "CS113". Just to the right you should get a set of icons. Click on the leftmost -- a document with a plus in the lower left.

Just below a box should appear with a blinking cursor waiting for you to type. Enter "HelloWorld.java" then hit return

Now on the right, start entering the hello world program given below

        public class HelloWorld {
            public static void main(String[] args) {
                System.out.println("Hello World");
            }
        }
    

Compile and run

Go back to the terminal. Enter the following:
xena@loin:~/CS113/Lab01$ ls
HelloWorld.java
xena@loin:~/CS113/Lab01$ javac HelloWorld.java
xena@loin:~/CS113/Lab01$ ls
HelloWorld.class  HelloWorld.java
xena@loin:~/CS113/Lab01$ java HelloWorld
Hello World
Note that the program you wrote in VSC shows up in the terminal. The we compile the file using the javac command. If successful, this creates a new file "HelloWorld.class". Finally, we run the program using the java command.

Back in VSC

VSC is an IDE (integrated development environment). It would not be particularly integrated if you had to go to a separate terminal to run you program. There are three alternatives.
  1. Open a terminal inside VSC and do the javac/java thing there. To open a terminal in VSC, select "Menu Terminal / New Terminal". This terminal has the same behavior as the terminal you opened earlier.
  2. Just above the "main" function are the words "Run" and "Debug" (for me they are light grey). Click on "run" or "debug". This can fail sometimes when the compile and run in the terminal works.
  3. Up near the top of the VSC window there is an arrow pointing right. Click on it to compile and run your program. This can fail sometimes when the compile and run in the terminal works.

Once your Hello World program works, modify it to print "hello world" at least 5 times.

Unix Recap

In this lab you used the following Unix commands. All of these commands also work in Terminal windows on Macs. Most of them work in Command windows (or powershell) on Windows. More next week.
CommandDescription
cd When used with no arguments (as here), return to the home directory
cd xxxxSwitch from the current directory to a subdirectory named xxxx.
cd .. (This is literally "cd" followed by a space then 2 periods.) Switch from the current directory to the parent of the current directory.
cd xxx/yyyswitch from the current directory to the subdirectory yyy which is contained in the the directory xxx. xxx must be a subdirectory of the current directory
lsLIst the contents of the current directory
pwdShow the full path of the current directory
javac Xxx.javaCompile the java program Xxx.java (Xxx.java must be in the current directory
javac XxxRun the java program Xxx. The file Xxx.class must be in the current directory. Do not include ".class" -- that is "java Xxx.class" will not work.

Unix -- Part 2

Log in and open a terminal as in lab 1. This process is also described here.

Recall that in the last lab you used the following Unix commands

CommandDescription
cd When use with no arguments, return to the home directory
cd xxxxSwitch from the current directory to a subdirectory named xxxx.
cd xxx/yyyswitch from the current directory to the subdirectory yyy which is contained in the the directory xxx. xxx must be a subdirectory of the current directory
lsLIst the contents of the current directory
pwdShow the full path of the current directory
javac Xxx.javaCompile the java program Xxx.java (Xxx.java must be in the current directory
javac XxxRun the java program Xxx. The file Xxx.class must be in the current directory. Do not include ".class" -- that is "java Xxx.class" will not work.
Using the commands above, go into your CS113 directory and create a new directory for the lab; the instruction below assume you named this directory Lab02.

Copy

Use the Unix copy command to copy the HelloWorld program from your Lab01 folder to your Lab02 folder. The command is (I use "UNIX$" as the command prompt -- yours will differ" )
    UNIX$ cp Lab01/HelloWorld.java Lab02/HelloWord2.java
The command says to copy the file HelloWord.java which is in the Lab01 folder into the Lab02 folder with the name HelloWorld2.java.

Now change your directory to Lab02 and compile the file. If everything went correctly, you should have gotten the following error message

    HelloWorld2.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
            public class HelloWorld {
                   ^
    1 error
The problem, as the error message indicates is that the file name and class name do not match. So make them match using the copy command -- as follows
    cp HelloWorld2.java HelloWorld.java
Here, we are copying within a directory so we do not need a directory name as above.

Now compile HelloWorld.java. It should be fine.

Unix rm and mv

The copy in the last section is kind of ugly as it leaves behind the useless HelloWorld2.java. Two options:
  1. delete the file HelloWorld2.java
  2. Do copy in the first place, move (rename) it.
To remove (delete) the File
rm HelloWorld.java
Here rm is short for remove. Be careful with remove, it is permanent. (You may be able to recover a file you removed, but it involves talking to the sysadmin and this only works on files that have been on the system for a while (days).)

Usually the better option than copy followed by remove is to simply rename the file. For instance

    mv HelloWord2.java HelloWorld.java
mv is short for "move". This command is much bigger than simple renaming, which you might guess from the name "move". cp, rm and mv can all be destructive. rm is obvious. cp and mv do not warn you that you are copying a file onto another file of the same name. If you do that, the original version of the file is lost.

scp

The final Unix command for today

"scp" acts much like "cp" EXCEPT it can copy files from your labtop to Unix, or the converse.

To use scp open a terminal window on your laptop. Use cd to navigate to a directory with the file you want to upload to Unix. Then scp it to a directoy on the Unix system. For instance, suppose on Unix you have a directory named Lab02 in a CS113 directory which is in your home directory. On your laptop you have the same directory structure. Further suppose that on your laptop you have a file named Readme that you want to get to Unix. Given this setup, so the following:

    // open a terminal on your laptop
    $ cd
    $ cd CS113/Lab02 //
    $ ls
    // A listing of files one of the files is Readme
    $ scp Readme UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/Labo2/Readme 

Replace "UNIX_NAME" with your Unix login. The scp command will ask you for your unix password. Enter it. If all goes well, you will see something like:
    Readme                                                  100%  162     5.6KB/s   00:00
which shows that 100% of the file Readme was copied in 00:00 minutes.

Java

Leap Year

Using VS code, open the Lab02 directory. It should have the HelloWord.java file from above, but not much more. Then, create a new file in VS Code named LeapYear.java copy/paste this code into it:
public class LeapYear {
   public static void main(String[] args) {
       int year = Integer.parseInt(args[0]);
   }
}
Then, modify the program so that it displays “true” or “false” depending on whether the input year is a leap year. A year is a leap year if it is divisible by 4 (e.g. 2020), unless it is divisible by 100 in which case it is not (e.g. 1900, 2100), unless it is divisible by 400 (e.g. 2000), in which case it is!

Therefore a year is a leap year if: (it is divisible by 400) OR (it is divisible by 4 AND NOT divisible by 100)

Keep in mind that you can use the modulo operator "%" when determining whether one number is divisible by another.

Your program should produce the correct output for each of the following cases:
java LeapYear 2020
true
 
java LeapYear 2021
false
 
java LeapYear 1600
true
 
java LeapYear 1900
false

Valid Triangle

Next, create a new file in VS Code named ValidTriangle.java and copy/paste this code into it:
public class ValidTriangle {
   public static void main(String[] args) {
       int s1 = Integer.parseInt(args[0]);
       int s2 = Integer.parseInt(args[1]);
       int s3 = Integer.parseInt(args[2]);
   }  
}

Modify the program so that it displays "true" or "false" depending on whether the three numbers represent valid lengths of the sides of a triangle.

For three numbers to represent the valid lengths of the sides of a triangle, each number must be less than the sum of the other two.

Here are some cases

java ValidTriangle 7 10 8
true
 
java ValidTriangle 40 12 15
false
Finally, modify your ValidTriangle program so that it also displays "false" if any of the three values are 0 or negative.

What to hand in

Use your phone to take a picture of the VSC screen on a unix machine with one of your java programs in the background and yourself in the foreground. Send the picture to gtowell@brynmawr.edu