Unit 3 Assignment
String & Math
AP Computer Science 2021-22 / Dr. Kessner
Reading
-
Look at the online docs for the
String
class (i.e. look at the intro, and skim down the list of functions to see what’s available, find the functions we use in class). -
Look at the online docs for the
Math
class
Coding Bat practice
Do these exercises on Coding Bat Warmup 1.
- nearHundred
- missingChar
- backAround
- startHi
- hasTeen
- mixStart
- close10
- monkeyTrouble
- loneTeen
- notString
Note: There is nothing to turn in for this part.
Code
-
Choose two of the above Coding Bat exercises involving the String class, and implement fully with at least 3 unit tests each.
-
Greetings. Write a function
greetings()
that takes a single Stringname
and returns returns a greeting using the given name. Be sure to include unit tests.Sample output:
greetings("Dr. Kessner") -> "Hello, Dr. Kessner, how are you?" greetings("Ascii Cat") -> "Hello, Ascii Cat, how are you?" greetings("Sydneys") -> "Hello, Sydneys, how are you?"
-
Attention. Write a function
attention()
that takes a singleString
as input and returnstrue
if the string starts with “Hey, you!”. Be sure to include unit tests.Sample output:
attention("Hello, my name is Inigo Montoya.") -> false attention("Excuse me, Dr. Kessner?") -> false attention("Hey you! Give me your code!" -> true
-
Coin flip. Write a function that flips a coin randomly, returning a String, either “Heads” or “Tails”. Functions involving randomness are a little tricky to write unit tests for. So you should just have your
main()
function print the results from 10 or 20 coin flips to try out your function. -
Die rolling Write a function that returns the result of rolling a single 6-sided die. In other words, when you call the function, it should randomly return 1, 2, 3, 4, 5, or 6.
Note: The Unit 3 Quiz will consist of writing functions involving Strings, like #2 and #3 above, including complete function declarations.