CS 151 - Introduction to Data Structures

Lab 1

Java, reading CSV files

UNIX

Find a lab machine: log in using the credentials you should have received. (See the LabComputers document).
  1. Make a directory for holding all of your working this semester.
    1. Open a terminal window: On mac, Applications/Utilities/Terminal; On Windows: run powershell; on Unix Applications Menu / Programming / Terminal
    2. Make sure you are in your home directory (more on this next week).
      cd ~ 
    3. create a new directory
      mkdir CS151
If you are planning on programming on your own laptop repeat these directions on your own machine.

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.
  • 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
    3. Tabnine AI Code Completion
    On your own machine, do install at least the first 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.
  • 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.
  • In the window that appears (it should say "Welcome" or "Getting Started") tap on "Open ... ". From the file menu, select "open ..." (do not use "open workspace" theat does something else entirely).
    1. Within the popup that just appeared
    2. Navigate to the CS151 directory you just made.
    3. tap on "new folder" (in lower left (Mac) or upper left (Windows))
    4. Name you new folder "Lab1"
    5. Make sure the folder you just created is highlighted then Tap on "open".
  • The left column of the VSC window should now say "Explorer" at the top. Just below that should be an entry for Lab1.
  • 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"
  • Tap in the HelloWorld.java tab that should have just appeared.
  • Write a complete java implementation of a "hello world" program.
  • 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 if 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). USE A LOOP.
    2. Run your program

    Get a csv file

    1. Within VSC, in the "terminal" menu, select "new terminal". This will open a terminal identical to the one you opened above.
    2. If you are using a lab machine:
      		cp /home/gtowell/Public/151/Lab1/code.csv code.csv
      	
      if you are working on your own laptop
      		scp YOURUNIXNAME@165.106.10.154:/home/gtowell/Public/151/Lab1/code.csv code.csv
      	
      Above, substitute you actual UNIX login name for YOURUNIXNAME you will be prompted for your UNIX password.

    Write a program to read and parse the contents of a CSV 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 "ReadCSV.java"
    2. Tap in the ReadCSV.java tab (you should already be on that tab).
    3. Enter the following program (You may use cut/paste):
      	public class ReadCSV
      	{
      		public static void main(String[] args) {
      			ReadCSV ec = new ReadCSV();
      			System.out.println(ec.csvCollection("us.csv"));
      		}
      	
      		public ReadCSV() {
      		}
      	
      		public ArrayList csvCollection(String filename) {
      			ArrayList rslt = new ArrayList<>();
      			try (BufferedReader br = new BufferedReader(new FileReader(filename));) {
      				while (br.ready()) {
      					String l = br.readLine();
      					String[] brokenLine = l.split(",", 25);
      					rslt.add(brokenLine);
      				}
      				System.out.println("Lines all read ");
      			} catch (FileNotFoundException fnf) {
      				System.err.println("Could not open the file" + fnf);
      			} catch (IOException ioe) {
      				System.err.println("Reading problem" + ioe);
      			}
      			return rslt;
      		}
      		
      		public void echoCSV(String filename)
      		{
      			try (BufferedReader br = new BufferedReader(new FileReader(filename));) {
      				int lineCount = 0;
      				while (br.ready()) {
      					String l = br.readLine();
      					lineCount++;
      					String[] brokenLine = l.split(",", 25);
      					System.out.println(lineCount + "    ");
      					for (int i = 0; i < brokenLine.length; i++) {
      						System.out.print(" "+ i + ":" + brokenLine[i]);
      					}
      				}
      				System.out.println("Lines all read ");
      			} 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 sqiggles 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.
    7. Add comments to this program to illustrate your thorough understanding of what this program does and how it does it. Use a lot more line-by-line comments than you normal. (Normally, I use very few line-by-line comments.) You will use the CSV file reading method illustrated here in Homework 1 and many other homeworks through the semester. So be sure you are comfortable with this code.
    8. In addition, add the following functionality

    What to Hand In

    The modified version of ReadCSV.java.

    Send email to gtowell151@cs.brynmawr.edu. You can cut / paste you code into the email, attach your code, take a photo of your screen. Almost anything so that I can see you work. (Labs will be like that.)