CS 145 Lab 2 – Scanner and String
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:
- 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.
- 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:
- 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.
- Retrieve a date from the user, something like “April 14, 1865”. Read the whole line of input, but extract and print only the year. Any date may be entered—do not assume the year starts at a particular index.
- You’ve got the dimensions of the images your camera takes into a string of the form
WIDTHxHEIGHT
, whereWIDTH
andHEIGHT
are actual numbers. How many megapixels is your camera? Look atScanner.useDelimiter
to make the task a little easier.