Coding Exercises: Strings and Math
1. Greetings.
Write a function greetings()
that takes a single
String name
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("(>^.^<)") -> "Hello, (>^.^<), how are you?"
greetings("Sydneys") -> "Hello, Sydneys, how are you?"
2. Attention.
Write a function attention()
that takes a single
String
as input and returns true
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
3. 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 check the output of your function.
4. 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. No unit tests for this, but print out the results of calling your function multiple times to show that it is behaving properly.