This lab should be done individually. It is okay to get help from the TAs and/or the instructor if you get stuck, but you should try to do it on your own first.
Tip: You should get into the habit of making backup copies of your work.
Some of the hot toys lately have been robots. Consider one particular robot that can walk and talk. Its "conversation" consists of uttering one of several phrases chosen at random, such as "Hey, how's it going?" or "You look marvelous!"
Suppose the robot also has a clock, and can be used as an alarm clock. You have been hired by the makers of the robot to help create a new and improved version. Your focus will be to improve its conversational skills by making its choice of phrases depend on the time of day. For example, if it's before 10 am, it might say "Good morning!", "This is way too early for me!", or "Hey, how's it going?" If it's close to lunch time, it might say, "I'm hungry. Let's go eat lunch.", "I'm never going to finish my homework before class!", or "You look marvelous!"
Specifications: You should break the day into at least four time blocks. Time blocks are defined by hours of the day: an hour is an integer between 0 and 23, where 0 represents the hour between midnight and 1 AM, 12 represents the hour between noon and 1 PM, 23 represents the hour between 11 PM and 12 midnight, and so on. For each time block, there should be at least three different phrases that the robot might randomly choose among. At least one or two of those should be time-specific (like "Let's go eat lunch!"). Others can be more generic. Generic phrases can be used in multiple time blocks if you want. For example, you could decide to include "Hey, how's it going?" as a possible phrase in every time block if you want. On the other hand, time-specific phrases should not be among the possibilities in inappropriate time blocks. You will have to write your randomly-chosen phrase to the console, since you don't have an actual robot to program.
Design: For this program, you will use 3 classes:
an application class containing the main method, a class
representing robots, and a class representing a clock. The
main method will construct a robot and ask it to utter
a time-appropriate random phrase. The robot will ask the clock for
the current hour of the day in order to decide on possible phrases to
use. The class diagram below shows the three classes and shows that
the RobotLabApp application class uses an object (or more than one
objects) of the Lab2TalkingRobot class, and that robot
objects use the Clock class. (You may use the same class
names as the diagram, although you do not need to.)
Before starting to implement your talking robot, take a few minutes to plan which time blocks you are going to use and identify at least one phrase for each time block.
Implementation: One common process for developing software is to develop it in small increments, testing the program at every step to ensure that you have a working program all along the way.
Stop and Think: Read the source code for the
mainmethod; given what you see there, does the behavior you saw when you ran the program make sense to you? Read the source code for the robot and clock classes. Notice that the robot constructor constructs a clock object. Are any clock methods called yet? Does the robot class have any methods beyond its constructor? Does the clock'sgetHourmethod return the hour of the current time of day? (You should be able to answer the last question just from the documentation comments for the class.)Note: The
Clockclass claims to be a "stub" class. A stub class is a very simplified version of a class used for testing purposes, or as a temporary measure when the real class hasn't been developed yet. It must have all the public methods of the class, but they often do not do anything, or do as little as possible to get the rest of the program to compile and run.
main method to construct a robot,
recompile, and test. (Should the program behavior be any different
when you test this time, compared to when you tested last time?)
main method to call the new
method, recompile, and test. (You could have tested after you added
the method to the robot class but before you called it from the
main method. Would you have learned anything from that
test?)
main method appears.
Clock class
to see how to construct and use a random number generator.
(Note: The Random class is a standard Java class, but
to use it in a class you must remember to include an import
statement like the one at the top of the Clock class.)
Modify your program so
that for one of your time blocks you randomly print one of several
possible phrases. What do you need to do to test this modification?
|
Optional Enhancement: Using a random number to represent the current time makes testing
easier, because otherwise you would have to test it at various times during
the day. You could improve the
To get the current hour of the day, you would need to use a statement such as:
If you have time at the end of lab you may experiment with the |
main
method, your modified robot class, and
the Clock class
if you modified it to use the Calendar class.
When you complete the lab, you can start work on Programming Project #2 (due at the beginning of Lab 3).