CS 206 - Introduction to Data Structures

Lab 2

Unix and Visual Studio Code

Tuesday, Feb 16

Depending on the state of your device and how much installing you need to do, this lab could take quite a while.

Using Visual Studio Code to write Java programs

Installations

If you are working on your own machine -- and you probably are given that you are supposed to be in quarantine -- the first step is probably to install Visual Studio Code on your machine. This https://code.visualstudio.com/download should do it. Follow the directions there.
  1. On your PC Only. Install Java.
    Go to the website https://adoptopenjdk.net/ Click on "download latest release JDK 11.0.10+9". Once it is downloaded, open the file. Do the installer thing.
  2. On Macs. Open a terminal. Enter java -version. If you are told that the java version is less than 11, follow the PC instructions of the previous step. (The default version of Java that comes with Macs is 8.) For instance, my machine is a little out of date, I have "openjdk version 11.0.8". This is OK. If you get a response like "openjdk version 1.8.0_242", then you need to upgrade.

Woring with Visial Studio code (VSC)

  1. Start VSC
    1. On the machines in the CS lab -- Applications Menu / Programming / Code - OSS
    2. On you own machine -- it depends. I assume you can figure it out.
  2. On the machines in the lab do this every time... Check to confirm that the correct extension packs are installed
    1. Java Extension Pack
    2. PrintCode
    On your own machine, do install these two extensions
    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. During the installation of the Java Extension Pack, if Java is not installed on your machine you will prompted to install it. Go back to the instructions above for installing Java and do so.
  4. In the window that appears (it should say "Welcome") tap on "Open folder". From the file menu, select "open folder" (Windows) or "open workspace" (mac).
    1. Navigate to the cs206 directory you made in lab 1.
    2. tap on "new folder" (in lower left (Mac) or upper left (Windows))
    3. Name you new folder "Lab2"
    4. Make sure the folder ou just created is highlighted then Tap on "open".
  5. You can also create the lab2 folder using the command line as in lab1. In that case you would just navigate to the lab1 folder and click on "open".
  6. The left column of the VSC window should now say "Explorer" at the top. Just below that should be an entry for Lab2.
  7. Create a new file "HelloWorld.java"
    1. Tap on the document with a '+' icon to the right of "Lab2". When you hover over this icon it should say "New File".
    2. type "HelloWorld.java"
  8. Tap in the HelloWorld.java tab that should have just appeared.
  9. Write a complete java implementation of a "hello world" program.
  10. 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.)
      You will only get the behavior is the Java Extension Pack is properly installed.

    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) {
      			EchoCount ec = new EchoCount();
      			ec.echo("EchoCount.java");
      	    }
      
      	    public EchoCount()
      	    {
      	    }
      
      	    public void echo(String filename)
      	    {
      	        try (BufferedReader br = new BufferedReader(new FileReader(filename));) {
      	            int lineCount=0;
      	            while (br.ready()) {
      	                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 may 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 .." is the one to use. So select "import"
      4. The squiggly line should go away.
      (Red squggles indicate problems that you must fix for the program to run. Yellow squiggles 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 each line as it is read, and the name of the file being read in addition to printing the number of lines in the file.
    7. Be sure you understand this program, you will use 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 a complete java class that wraps this method (following the pattern of EchoCount)

    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
    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

    The modified version of EchoCount.java.

    Send email to gtowell206@cs.brynmawr.edu