MCS273 Introduction to C++ Programming (January 2013)

Teach a child about an analog clock


Overview

Your goal in this lab is to teach a child to use an analog clock. The idea is to display the time in digital form, and ask the child to click on where the hour hand should point to, and then to where the minute hand should point.

You may do this lab individually or in pairs.

Tasks (3 points total)

  1. Staying in the same project as the last lab, copy your your better-clock.cpp program from last lab to teaching-clock.cpp as a starting point for this lab. Change it so that it draws the face and tick-marks, but doesn't draw the clock hands. If you didn't finish the clock-ticks in lab, just draw the clock face without the tick marks. It's too late to be checked off for that portion of last lab.

  2. Somewhere in the window (perhaps beneath the clock) display the current time in digital form in normal (non-military) format with am or pm. For example, 1:32pm

    We haven't taught you a way to combine an integer and a string. Include the procedure digitToString before your main():

    #include <string>
    #include <cassert>
    
    string digitToString (int digit) {
       assert (digit >= 0 && digit <= 9);
       static string dummy = "X";
       dummy[0] = '0' + digit;
       return dummy;
    }
    
    You should now be able to type:
       string five = digitToString (0) + digitToString(5);
       cout << five << endl;
    
    Hint: You'll want to use n%10 and n/10 to extract individual digits.

  3. Be sure to test your code so far by using both a morning and afternoon time. The best way to do this is in the debugger, since it allows you to change the values of variables while running a program, without changing the program! This technique is a good one for debugging code, since you don't have to actually change the code to test it. Were you to change the code to (temporarily) hardwire a time, you might forget to fix it again later.

  4. Check-off (1 point): Demonstrate that you've modified the appropriate values in the debugger in order to test the am/pm part of your code, and that it results in the correct value.

  5. Display a message in the window, asking the user to click on where the hour hand should point. If the user clicks outside the clock circle, or too close to the center of the clock, output an appropriate message and exit.

  6. Draw the hour hand pointing in the direction the user clicked. The hour hand should be of fixed length, not the length suggested by the mouse click.

    For this part, you need to learn how to use the inverse of the tan in C++. There are sever ways of doing this, two of which I explaine here, and both of which you should try:

    • In a unix shell (such as Terminal) you can search for commands using the man command. There are two functions which might come in handy, which I found by the man command in Mac OS X. I typed:

        man -k atan
      
      to get a list of all manual pages which mention the atan function. There are two that are relevant, atan and atan2. Before embarking on the program, be sure to figure out which of the two is more useful. Inspect the individual manual pages by typing:
        man atan
        man atan2
      

    • More easily, visit a web site like http://www.cplusplus.com/ and use their search engine.

  7. Repeat for the minute hand.

  8. If the hands are pretty close to where they should be, congratulate the user.

  9. Check-off (2 points): Once done, be sure to get checked off. As always, we'll check that you have clean code.

  10. Additional challenge: If you have the time and inclination, modify your program so that it doesn't just exit if the user clicks outside the clock or two near the center. Instead, give them some message telling them how they were in error and to try again.

    This will require you to look ahead at chapter 4 of our textbook, which concerns loops, in particular while loops.