Get your original paper written from scratch starting at just $10 per page with a plagiarism report and free revisions included!
Write a program that teaches children how to read a clock. Input the hour and minute. Accept only numbers between 0 and 12 for hour and between 0 and 59 for minute. Print out an appropriate error message for an invalid input value. Draw a clock that looks something like this:
To draw a clock hand, you use the drawLine method of the Graphics class. The endpoints of the line are determined as follows:
The value for constant K determines the length of the clock hand. Make the K larger for the minute hand than for the hour hand. The angle is expressed in radians. The angle min of the minute hand is computed as
where Hour and Minute are input values. The values 6.0 and 30.0 designate the degrees for 1 min and 1 h (i.e., the minute hand moves 6 degrees in 1 min and the hour hand moves 30.0 degrees in 1 h). The factor 180 converts a degree into the radian equivalent.
You can draw the clock on the content pane of a frame window by getting the content pane’s Graphic object as described in the chapter. Here’s some sample code:
Note: Please refer to the Hints, Tips, & Pitfalls box on p. 258 about putting a delay in the program so the drawing appears on the window. Also, please refer to the You Might Want to Know box on p. 259 if you want the drawing to remain on the window (instead of disappearing when the window is minimized, for example).