CS 206 - Introduction to Data Structures

Lab 1

Unix and Visual Studio Code

Friday, Sep 11

Part 0: Access

Make sure you can get into CS231 (you should have keycard access to the room, and that you can log into a computer in that lab using your CS department UNIX login. If the machine is booted into windows you will need to restart the machine to get it into UNIX. Do this step on Friday If you cannot get into the lab or cannot login using UNIX, then me know as soon as possible (ie before 4pm Friday).

Part 1: UNIX

The Unix operating system consists of three parts: the kernel, the shell and the application programs. The kernel is the heart of the operating system, it allocates processor time and memory, handles file operations and user tasks. The shell acts as an interface between the user and the kernel. The shell is what you are typing to at the prompt after you log in.

Before you can being using UNIX, you need to get onto a unix computer. If you are working with your own machine do the following:

MAC

  1. Open a terminal. (The app is in applications/utils)
  2. Use the ssh command to open a session to CS a CS machine. The command is ssh YOUR_CS_NAME@powerpuff.cs.brynmawr.edu. You will be asked for your CS department password. Enter it.
  3. That is it. You are now at a UNIX prompt on the CS department machine named powerpuff.

Windows 10 PC

  1. Open a power shell. In the search barch in the lower left type "powershell"
  2. Use the ssh command to open a session to CS a CS machine. The command is ssh YOUR_CS_NAME@powerpuff.cs.brynmawr.edu. You will be asked for your CS department password. Enter it.
  3. That is it. You are now at a UNIX prompt on the CS department machine named powerpuff.


If you are working on a lab machine, first be sure it is booted into Linux. Then, then to open a terminal window: Applications Menu / System Tools / MATE Term
Some useful UNIX commands: Execute the above unix commands and answers to the questions to their right
cd What directory are you in? 
What is the contents of this directory?  
cd / What directory are you in? 
What is the contents of this directory?  
cd ~ What directory are you in? 
What is the contents of this directory?  
cd /home/YOU (Replace YOU with your login name) What directory are you in? 
What is the difference between this command and the previous one?  
ls /home/gtowell/Public/206 What did this command do? 
What is the difference between this and cd /home/gtowell/Public/206 followed by ls  
cd /home/gtowell/Private What directory are you in? 
You should have noted that you could not cd into this directory. UNIX has a concept of "permissions". These specify whether you are allow into a directory, and weather yo can read or write a file. Generally you have permission to do anything in the directory /home/YOU and anywhere beneath that. Also, because I have given you permission, you can read the files in /home/gtowell/Public/206. Through the semester I will put files there for you to copy. Other places you will likely not have access to.
Not required but a good idea (and assumed below). Execute the following UNIX commands:
  cd ~
  mkdir cs206

Using Visual Studio Code to write Java programs

  1. Start VSC
    1. Applications Menu / Programming / Code - OSS
    2. or Applications Menu / System Tools / Mate Terminal then in the terminal window enter the command "code &"
  2. Check to confirm that the correct extension packs are installed
    1. Java Extension Pack
    2. PrintCode
    To check, tap on the 'square with the top right quadrant slightly moved' icon on the far left of the screen). Look through the 'enabled' list (just to the right of the icon) for both of these. If they are not installed tap in the search bar (above the enable list) and enter the name. Tap and follow on screen directions to install.
  3. In the window that appears (it should say "Welcome") tap on "Open folder" under "Start".
    1. Navigate to the cs206 directory you just made above.
    2. In the upper right tap on the folder icon
    3. In the small popup, enter "Lab1"
    4. Tap on "create"
    5. Tap on "Add"
    6. Make sure the folder ou just created is highlighted then Tap on "open".
  4. You can also create the lab1 folder using Unix as above. In that case you would just navigate to the lab1 folder and click on "open".
  5. The left column should now say "Explorer" at the top. Just below that should be an entry for Lab1.
  6. Create a new file "HelloWorld.java"
    1. Tap on the document with a '+' icon to the right of "Lab1". When you hover over this icon it should say "New File".
    2. type "HelloWorld.java"
  7. Tap in the HelloWorld.java tab that should have just appeared.
  8. Write a complete java implementation of a "hello world" program.
  9. Run your program (to run do the following).
    1. Tap on the insect on the left edge of the screen
    2. Tap on "Run with Java" (Alternately tap on "run" that should have appeared just above you main method.)

More Java

  1. Modify your hello world program to print out "hello World" and the count (ie "Hello World 1", "Hello World 2") a lot for times. (At least 610). Do not do this using 610 print statements.
  2. Run your program

Write a program to count the number of lines in a file.

  1. Start a new Program in VSC
    1. Return to the document view by clicking on the overlapping rectangles (upper left)
    2. Tap on the 'new file' icon to the right of "Lab1" Name the file "EchoCount.java"
  2. Tap in the EchoCount.java tab (you should already be on that tab).
  3. Enter the following program:
    	public class EchoCount
    	{
    	    public static void main(String[] args) {
    	        new EchoCount().echo();
    	    }
    
    	    public EchoCount()
    	    {
    	    }
    
    	    public void echo()
    	    {
    	        try (BufferedReader br = new BufferedReader(new FileReader("/home/gtowell/Public/206/lab01/descent.txt"));) {
    	            int lineCount=0;
    	            while (true) {
    	                String l=br.readLine();
    	                lineCount++;
    	            }
    	            System.out.println("Lines read " + lineCount);
    	        } catch (FileNotFoundException fnf) {
    	            System.err.println("Could not open the file" + fnf);
    			} catch (IOException ioe)
    	        {
    	            System.err.println("Reading problem" + ioe);
    	        }
    	    }
    	}	
    
  4. After entering all of this, you should see many items with a squiggly red underscore; BufferedReader, FileReader, etc. The underscores indicate problems in your code. At each
    1. Position the cursor over the underscored text.
    2. A popup will appear, on the bottom line of the popup, tap on "Quick fix".
    3. Generally, this will suggest ways of fixing the problem. In these cases, the fix "Import". So select "import"
    4. The squiggly line should go away.
    (Red squggles indicate problems that you must fix for the program to run. Yellow squiggle indicate this it would be better to fix; you program will likely run fine but fixing is good practice.)
  5. Run your program.
    1. Tap on the insect on the left edge of the screen
    2. Tap on "Run with Java"
  6. Extend this program so that it prints the file being read in addition to printing the number of lines in the file.
  7. Be sure you understand this program, you will the file reading method illustrated here for the rest of the semester.

Write a program to get input from the user.

The program asks users for integer input. On receiving input, it adds the new integer to a running total. When the total exceeds 100, the program ends. Here is the core method
      public void doUI() {
        int total=0;
        Scanner ss = new Scanner(System.in);
        while (total < 100) {
            System.out.print("Enter an integer (or Exit to quit)");
            String ll = ss.nextLine();
            if ("Exit".equals(ll))
                break;
            try {
                int ii = Integer.parseInt(ll);
                total += ii;
            } catch (NumberFormatException ee) {
                System.err.println(ll + " is not an integer");
            }
        }
    }
Write your program to use this method.

Once you program works, extend it so that the user is asked for input at most 10 times.

Write a program to calculate the Fibonacci sequence.

If you have time in lab Otherwise do this as a part of Homework 1.
The Fibonacci sequence is a set of numbers n1, n2, n3, ... such that nI = n(I-2)+n(I-1). To get started, n1=1 and n2=1;
  1. Create a new Class in VSC
  2. (If needed) Return to the document view by clicking on the overlapping rectangles (upper left)
  3. Right click on "Lab1" and select "New File". Name the file "Fibonacci.java"
  4. Tap on the Fibonacci.java tab (you should already be there)
  5. Write a java program to compute the Fibonacci sequence and the golden mean (the golden mean is the ration between two consecutive fibonacci numbers i.e., nI/n(I-1)). Your program should be able to calculate the fibonacci sequence to an arbitrary length. For now, 20 will do. Your program should generate a table like:
    		1 1 1.0
    		1 2 2.0
    		2 3 1.5
    		3 5 1.6666666666666667
    		5 8 1.6
    		8 13 1.625
    		13 21 1.6153846153846154
    		21 34 1.619047619047619
    		34 55 1.6176470588235294
    		55 89 1.6181818181818182
    		89 144 1.6179775280898876
    		144 233 1.6180555555555556
    		233 377 1.6180257510729614
    		377 610 1.6180371352785146
    		610 987 1.618032786885246
    		987 1597 1.618034447821682
    		1597 2584 1.6180338134001253
    		2584 4181 1.618034055727554
    		4181 6765 1.6180339631667064
    		6765 10946 1.6180339985218033
    	
    Here is the the core code for computing the fibonacci sequence. You will need to write a more code to make this useful.
    		int n_2=1;
    		int n_1=1;
    		for (int i=1; i<20; i++) {
    			int nI = n_2 + n_1;
    			n_1 = n_2;
    			n_2 = nI;
    		}
    	
    When calculating the golden mean (1.618....) you will want to watch out for an issue with integer versus floating point division
  6. Run your program.
    1. Tap on the insect on the left edge of the screen
    2. Tap on "Run with Java"

What to Hand In

This lab must be completed by 11:59pm Saturday Sept 12. (Completion means send the email described below.)

Send email to gtowell206@cs.brynmawr.edu with the following:

  1. A completed table for the questions in the UNIX section
  2. A copy of the last program you completed from the three above. It would be great if you did them all, but you may run out of time.
The easiest way to do this is probably to use your phone and take pictures of the completed table and your screen showing the a completed program. This is just a suggestion for how you could turn things in. Feel free to be creative.