CS 148 Lab 3 – Math and String
Welcome to lab 3!
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 to acquaint ourselves with methods of the Math
and String
classes.
Checkpoint 1
Person A types. Make a labs.lab03
package in your IntelliJ project.
Complete three 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
Useprintf
instead of a messy concatenation. - In class
Clamper
, write amain
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 the number lo. If it is greater than hi, print the number hi. Otherwise, the number is already within the range and can be printed as is. UseMath.min
andMath.max
to solve this. Do not useif
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 onlyMath.random
, not theRandom
class. Test your code with a small range, like 20 through 21, and make sure you see all numbers in this range. - 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 classUsernamer
, write amain
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 toprintf
(orprint
andString.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.
Checkpoint 2
Person B types.
Write a class Crossworder
that gets two words as input and prints one vertically and one horizontally so that they intersect. For example:
vertical: coffee horizontal: suffering c o suffering f e e
This problem is very open-ended. Solve it in a way that makes sense to you.