teaching machines

CS 145 Lab 2 – Scanner and String

September 19, 2015 by . Filed under cs145, fall 2015, labs.

First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab. No credit will be awarded past these 15 minutes.

Don’t forget to work in pairs! Please work with someone that you did not work with last week.

Objective

As we write code to operate on data, things get complex. We keep the mess down by tucking away the complexity into classes and methods. In this lab, we’ll investigate two very useful classes that the kind folks behind Java have provided us: Scanner for user input and String for manipulating character sequences.

Checkpoint 1

Person A types. Create a lab2 package in your workspace.

Have the documentation for String and Scanner handy. You can find the Java API web page (I search for “java 8 string” or “java 8 scanner”) or, in the Eclipse editor, put your cursor in the name of the class you’re curious about and hit Shift-F2. Try it!

Solve the following problems in the main methods of separate classes:

  1. Prompt the user for a filename and read it from the keyboard. Replace any spaces in the filename with underscores and print it to the screen.
  2. Prompt the user for a password. Retrieve the password. Prompt the user for the password again. Retrieve the password. Print whether or not the same password was entered.

Checkpoint 2

Person B types. Solve at least two of the following problems: