# teaching machines

## CS 145: Lab 2 – Math and String

Welcome to lab 2!

If you have checkpoints from the last lab to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.

Work with a partner that you have not worked with before.

Our goal today is acquaint ourselves with methods of the Math and String classes.

### Checkpoint 1

Person A types. Make a lab02 package in your Eclipse project.

Complete two of the following problems:

• In class Circler, prompt the user for four numbers: an x-coordinate for point A, a y-coordinate for point A, an x-coordinate for point B, and a y-coordinate for point B. Print the parametric equation of the circle whose perimeter passes through these two points. The equation should follow this pattern:
(x - originX)^2 + (y - originY)^2 = radius^2
For example, if point A is (0, 0) and point B is (1, 0), print the following equation:
(x - 0.5)^2 + (y - 0)^2 = 0.5^2
• In class Clamper, write a main method that asks the user for a single number. Clamp the number to the range [lo, hi] and print the result. That is, if the number is less than lo, print lo. If it is greater than hi, print hi. Otherwise, the number is already within the range and can be printed as is. Use Math.min and Math.max to solve this. Do not use if statements, even if you’re familiar with them. Do not prompt the user for lo and hi. You, the developer, should define them directly in the code according to your whimsy.
• In class Range, prompt the user to enter a low number and a high number, both integers. Generate a random integer in that range, inclusively. Use only Math.random, not the Random class. Test your code with a small range, like 20 through 21, and make sure you see all numbers in this range.

### Checkpoint 2

Person B types.

Complete two of the following problems:

• When you publish a file on the web for people to access, it’s better that the files name not contain any spaces. Otherwise, the spaces get turned into funny codes like %20. (20 is the value 32 in base 16, and 32 is the integer value of the space character in the ASCII and Unicode standards.) In class Cleanup, write a main method that prompts the user to enter the name of a file containing spaces. Using methods of the String class, substitute underscores (_) for any spaces and print the result.
• You enroll at wizarding school and the sorting hat wants to assign you a username. It asks you for your name (first and last), retrieves it with only a single Scanner method call, and then magically prints out your username, which is the first letter of your first name followed by your complete last name, all lowercase. For example, if the user enters “Robin Steele”, the username is “rsteele”. In class Usernamer, write a main method that accomplishes this task.
• In class Ticker, prompt the user to enter two team names and two integer scores. Print them using a single call to printf (or print and String.format) such that the team names are left-aligned to fill 15 columns and the scores are right-aligned to fill 4 columns, with a | character separating the columns. For example:
Heffalumps     |  17
Woozles        |   9
See this reference on the format string protocol.