CS 145 Lab 2 – Math and String
Welcome to lab 2!
If you have checkpoints from the last 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.
You must work with a partner that you have not worked with before.
Our goal today is acquaint ourselves with methods of the
Person A types. Create an account on Practice-It! and complete the following problems:
- BJP3 Self-Check 1.6: legalIdentifiers
- BJP3 Self-Check 1.14: TestOfKnowledge
- BJP3 Self-Check 2.2: expressions1
- BJP3 Self-Check 2.9: Oops2-errors
Person B types. Open your Eclipse workspace and create a package named
Complete three 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
mainmethod that prompts the user to enter the name of a file containing spaces. Using methods of the
Stringclass, substitute underscores (
_) for any spaces and print the result.
- While on vacation, you go for a run around a perfectly circular lake. A camera at the center of the lake tracks you as you run, and upon exiting, a kiosk dispenses a ticket listing the the lake’s radius and the number of degrees (not radians) you ran around it, but it does not tell you how far you ran. In class
Circlethon, write a
mainmethod that prompts you for the radius and number of degrees, and does tell you how far you ran. In other words, compute the arc length, which is radians times radius. Use this Desmos sketch to check your work.
- You enroll at wizard school and the sorting hat wants to assign you a username. It asks you for your first name and last name, 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 “Miles” and “Driven”, the username is “mdriven”. In class
Usernamer, write a
mainmethod that accomplishes this task.
- In class
Clamper, write a
mainmethod 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.maxto solve this. Do not use
ifstatements, 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.