teaching machines

CS 145 Lab 2 – Scanner and String

September 14, 2014 by . Filed under cs145, fall 2014, 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! Where possible, please work with someone that you did not work with last week. The exchange of new ideas and perspectives is not an opportunity you want to rob yourself of.

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.

Checkpoints 1 and 2

Create a lab2 package in your workspace.

The following problems employ Scanner and methods of the String class. Solve two or more. Switch persons at the keyboard between problems.

  1. Prompt the user for a filename. Retrieve the filename. 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.
  3. Prompt the user for her first name. Retrieve the name. Prompt for her last name—let’s assume she has one. Print her username, which is the the first letter of her first name tacked on to her last name. Ensure the username is all lowercase, even if the input was not.
  4. Retrieve a date from the user, something like “April 14, 1865”. Read the whole line of input, but extract and print only the year.

Quiz

There’s a quiz in the second half of lab today. It will start on the hour and be available for 50 minutes. You can access it through Desire2Learn (D2L). There’s a link on the UWEC website. Go to the D2L page for your lab section and click on Quizzes.

Quizzes are taken individually. You may use Eclipse, a calculator, off-line notes, and your textbook. Other software, like web browsers, is strictly off-limits.