Lab 3: Genius Bar
1 Introduction(s)
You’ll work in labs in pairs. Find someone to work with for this first lab and introduce yourself.
Make sure at least one of you have a laptop to work on for this lab.
The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. We’ll have you switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.
2 Purpose
In this lab, you’ll practice writing functions and building simple world programs using the design recipe for systematic computational problem solving.
3 Making progress
Lab 2: A World of Fun introduced you to the world of big-bang programs. However, fundamentally, the program you wrote was just a fancier version of animate programs you had already seen: the program did not respond to any interactions from the user.
In this lab, you will write a very simple interactive program that responds to user actions.
You will make a program that shows a progress bar that graphically displays the percentage of some task being complete (e.g. downloading a file, installing some software, starting DrRacket).
The first version of the program should assume progress is made at a uniform rate of %1 per tick of time. You will need to use the 2htdp/image and 2htdp/universe libraries to complete this lab.
(require 2htdp/image) (require 2htdp/universe)
The main function for this program will look like this:
;; main : Integer -> Integer ;; Launch progress bar at given percentage level (between 0 and 100) (define (main x) (big-bang x [on-tick progress-forward] [to-draw progress-draw]))
Your job is to design progress-forward and progress-draw.
We will add functionality to handle user input shortly. For the time being, the program should display a rectangular scene with a red rectangle aligned on the left side. The width of the rectangle should correspond to the percentage of progress that has been made. When 0% progress has been made, the rectangle should be 0 px wide. When 100% progress has been made, the rectangle should be as wide as the scene.
Lab problem 1: Problem analysis and data definition
Consider the description above. Express how you wish to represent information as data.
Lab problem 2: Signatures, purpose statements
Consider the use of progress-forward and progress-draw. Write down a signature, a statement of purpose, and a function header for both functions. The purpose statement should answer the question: what does the function compute?
Lab problem 3: Examples
Illustrate the use of progress-forward and progress-draw with examples and their expected outcome.
Lab problem 4: Inventory
Take an inventory of what each function has to compute with.
Lab problem 5: Code
Guided by your example, signature, and purpose statement, complete the definitions of progress-forward and progress-draw.
Lab problem 6: Test
Using check-expect, turn your earlier examples in to tests and confirm your functions behave as expected. Revise your program if things do not work as intended.
4 Wait a minute...
Now consider adding the ability for the user to pause and unpause the progress by pressing the space bar.
Here’s an idea for how to represent a paused progress bar: use negative numbers to mean progress is paused. So for example, the interpretation of -25 is that 25% progress has been made, but progress is paused.
To handle key events, we need to add another clause to our big-bang and will need to design another function.
;; main : Integer -> Integer ;; Launch progress bar at given percentage level (between 0 and 100) ;; Allow user to pause and unpause with press of a key (define (main x) (big-bang x [on-tick progress-forward] [to-draw progress-draw] [on-key progress-pause]))
Lab problem 7: Revise
Revisit problems 1-6 with this revised description of the program.
Can you think of anything deficient about this representation? In particular, is this representation adequate to represent all possible states of a progress bar, or is there information that is unrepresentable?
5 Bonus
If you’ve made it through the rest of the lab and are looking for something more to do, consider the question posed at the end of the previous section. Read up on structures and re-design the data representation of progress bars to use a compound structure to represent the state of a progress bar with two pieces of data: the percentage complete and a boolean indicating whether paused.
Lab problem 8: Revise (again)
Revisit problems 1-6 with this revised data representation.