CS 206 - Introduction to Data Structures

Lab 1

Unix and Visual Studio Code

Thursday, Jan 23

Part 1 Using Visual Studio Code to write Java programs

As I work on the big screens in the lab echo my actions ...
  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 confirms that the correct extension packs are installed
    1. Java Extension Pack
    2. PrintCode
  3. In the window that appears (it should say "Welcome") tap on "Add Workspace folder" under "Start". (You will likely never need to do this step again.)
    1. In the list in on the left side of the popup, tap on "Home"
    2. In the upper right tap on the folder icon
    3. In the small popup, enter "cs206"
    4. Tap on "create"
    5. Tap on "Add"
  4. Right click on "cs206" and select "Add Folder to Workspace". In the popup, tap on the folder with star icon in the upper right. In the little popup that appears name the folder "Lab1" then tap on "create". Finally, tap on "add"
  5. Right click on "Lab1" and select "New File". Name the file "Hello World.java"
  6. Tap on the HelloWorld.java tab
  7. Write a complete java implementation of a "hello world" program.
  8. 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"

Part 2: 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.
To open a terminal window: Applications Menu / System Tools / MATE Term 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/Public206 What did this command do? 
What is the difference between this and cd /home/gtowell/Public206 followed by ls  
cd /home/gtowell/206 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. Other places, not so much.

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. Ctrl-click (left click) on "Lab1" and select "new File". Name the file "EchoCount.java"
  2. Tap on 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/Public206/lab01/descent.txt"));) {
    	            int lineCount=0;
    	            while (null!=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.
  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.

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 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"
When you are complete with all of this, turn in the first page of this lab with unix answers filled in and you name on the top. At the bottom of the page write the number of lines in your EchoCount.java file as reported by the program.