Week 2 Set b
Due Date Thursday 9/12 at 9:00pm (Week 2)
Purpose To write simple functions & tests relating to numbers, strings, images, and conditionals.
Finger Exercises
You should submit a single .rkt file containing your responses to all graded exercises via the Handin Server. We accept NO email submissions.
You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.
Function signatures/purpose statements are not required for this assignment, but attempting them would be good practice and you may receive useful feedback for future assignments.
Graded Exercises
Exercise 2 Jill is about to start her first year at the Hogwarts School of Witchcraft and Wizardry, and so she wants to convert her muggle currency (Pounds Sterling) to something more useful, Galleons. While shopping in Diagon Alley, she happens upon a currency-exchange service that provides the following chart...
Notice that there is a flat fee charged for service as well as a conversion rate for each Pound -> Galleon. For example, it takes 8 Pounds to get your first Galleon, but then only 13 to get your second.
Write a function, diagon-convert, to convert from Pounds to Galleons. Write four check-expects: two match the examples described above, one that is shown in the graph, but not described, and one that is a value greater than those shown in the graph. Note: one of these must result in a non-integer for credit.
Hint: it can be useful to write out a table of input/output pairs to help understand these types of mathematical relationships (and these can then provide great tests!)
Exercise 3 For any word of at least one character that starts with a letter, let’s say that its "bingo word" is the uppercase version of the first letter, followed by a space, and then followed by the number of characters in the word. For example, the bingo word of "bingo" is "B 5" and the bingo word of "Win" is "W 3".
Write a function, bingo-word, that takes a string as an argument and returns its bingo word. You may assume that the argument is a valid word as described above. Write three check-expects: the two examples above, as well as one of your choosing: be creative, but respectful :)
Hint: The String function documentation is a useful place to look for functions that operate on Strings.
Exercise 4 Design the function leap-year? which determines if a natural number represents a leap year in the Gregorian calendar.
Write four check-expects and make sure that once these tests are successful, no parts of your function are highlighted in halloween colors.
Exercise 5 You’ve been tasked to produce an animation of a red ball of radius 20 that moves from left to right across a 400 by 40 background, hits the right edge of the background, then goes back from right to left, bounces off the left edge and so on, to and fro. The background should be black whenever the ball is moving right and it should be light blue whenever the ball is moving left.
Write a function to-and-fro that consumes the amount of time that has elapsed since the start of the animation, in clock ticks, and produces an image of the ball on the appropriate position and backround. Then use (animate to-and-fro) to run the animation.
The animation should start with:
with the ball moving right:
until it hits the right edge:
and starts moving left:
eventually hitting the left edge:
and starting to move right again:
and so on.
Be sure to make use of constants to simplify your code and to have a single point of control over constant values mentioned in the problem statement.