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 can read a 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!"
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 2 - 4 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. For example, "Let's go eat lunch!" should not be one of the possible phrases at 6 in the morning.
The full output of your program should consist of a welcome message, a timestamp, and your randomly chosen message. For example,
Welcome to TR1, the Talking Robot program. Timestamp: 11:45 AM What's up, Doc? Ready for lunch?or
Welcome to TR1, the Talking Robot program. Timestamp: 02:23 AM Thought for the day: May the Force be with you!
Design: 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. You might write these out as a table.
|Time Block||2 - 4 Possible Phrases for Time Block|
Implementation: A good software development practice is to start by writing the smallest amount of code that you can test, test it, then continuing by adding small, incremental changes and testing all along the way. (This is sometimes known as Iterative, Incremental Development or "always have working code.")
Alternative: If you completed the Timestamp Mini-Lab, make a copy of the entire project directory and give it an appropriate name (examples: TalkingRobot, TR, etc.), then open the new project in BlueJ and rename the class that contains the
mainmethod to something appropriate (examples: TalkingRobotApp, TRApp, etc).
Note: In Java, the source code for a class is always in a file with the name of the class and a
Note: If you completed the Timestamp Mini-Lab, you have already done this step. Go straight to the Add AM/PM to your Timestamp step.
Download Clock.java to your project. Look at
the class documentation for the Clock class. Focus on the
getMM() methods. Which would be most useful for
creating a timestamp where hours range from 1 - 12?
In your main class, construct a clock object using techniques you have learned in earlier labs and store it in a variable of the right type. Compile and test your program. Do you expect the program's behavior to be any different? Why or why not? Does the actual behavior match your expectation? Then print a simple timestamp showing the hours and minutes. Compile and test your program again. Does the actual behavior match your expectations?
To test that your timestamp works for a variety of times of day, you
would have to wait for hours between tests. Instead, change your
program to ask the clock to report random times, rather than the actual
current time. Temporarily report the result of
on a separate line, so that you can see that you are getting both
morning and afternoon/evening times. Test enough times that you see
Add a simple
if statement that adds "AM" or "PM" to the
timestamp, depending on the hour. Run appropriate tests.
Modify your program to test which time block is represented by the time returned by the clock. (Which of the clock's time methods would be most useful for this?) Staying with the principles of iterative, incremental development, choose one phrase to print for each time block. Adjust your output to match the format specified in the Specifications section of this page.
Note: You do not need to get the time repeatedly for each time block test. Just get the time once, put the value in a variable, then test that variable against the various time block possibilities.
Run the program enough times to test each time block.
Stop and Think: How many tests do you expect will be needed? How many were actually needed?
Modify your program so that for one of your time blocks you randomly print one of the several (2 - 4) possible phrases you identified in your design.
Remember from earlier labs that you can introduce random behavior into a program by generating a random number and then using that number to determine the program's behavior. To get a random number, construct a random number generator and then ask it for a number:
(Remember to include an
Random generator = new Random();
int randNum = generator.nextInt(10);
import java.util.Randomstatement at the top of your class.)
What do you need to do to test this modification?
Stop and Think: How would the structure of the program be different if there were a 1/3 chance the robot says "What's up, Doc?" regardless of the current time?
.javafile for your class (the human-readable version), not the
.classfile (machine-readable version produced when you compile a class).]
When you complete the lab, you can start work on Programming Project #2 (due at the beginning of Lab 3).