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!"
You have been hired by the makers of the robot to help create a new and improved version with an internal clock. Your focus will be to improve its conversational skills by having it report on the time and 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!"
A good software development practice is to start by writing the smallest amount of code that you can test, test it, then continue by adding small, incremental changes and testing all along the way. (This is sometimes known as Agile Development, Iterative, Incremental Development, or "always have working code.") This lab is designed to step you through incremental development of this project.
The first new piece of functionality for the robot is basically to print out a timestamp.
main
method to something appropriate (examples:
TalkingRobotApp, TRApp, etc, where the "App" part of the name indicates
that this is the class that has a main
method, representing
the whole application).
Add a simple if
statement that adds "AM" or "PM" to the
timestamp, depending on the hour reported by the clock.
(Which of the clock's time methods would be most
useful for this?)
Run appropriate tests. The output so far should consist of a welcome message and a timestamp for a random time. For example,
Welcome to TR1, the Talking Robot program. Timestamp: 11:45 AMor
Welcome to TR1, the Talking Robot program. Timestamp: 02:23 PM
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 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 phrase. 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 PM Thought for the day: May the Force be with you!
Design: Before starting to implement this aspect of 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:
Modify your program to print a single phrase after the timestamp. For now, you can choose any of the possible phrases you identified above. The format of your output should match the format specified in the Specifications section of this page.
Modify your program to test which time block is represented by the time returned by the clock. Staying with the principles of iterative, incremental development, choose one phrase to print for each time block. Make sure your output still matches 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. Due to the nature of pseudo-random numbers (the kind generated by a computer's random number generator, you only want to have one random number generator, even if you are eventually going to ask it for many random numbers. Thus, you should create the generator once, before you start checking time blocks.
Random generator = new Random(); // Do this just once, before if statements. int randNum = generator.nextInt(10); // Do this wherever you need a new random number.(Remember to include an
import java.util.Random
statement 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?
YourName_TR_Lab
, zip it,
and submit the complete project to
Kit as a single zip file.
When you complete the lab, you can start work on Programming Project #2 (due at the beginning of Lab 3).