Lab: Talking Robot

Using If-Else and Random Numbers


 

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!"

Improved Choice of Phrases based on Time of Day:

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. For each time block, there should be at least three different phrases that the robot might ramdomly 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.

To begin, download the Talking Robot program. The code you add should go into the main method of the Robot.java file in the Code folder. Currently, the clock on the robot is malfunctioning, so when you push the button to get the current time of the day and a message, you will get a random time. Use the Random class to get a random number between 0 and 23 to represent what hour of the day it is. Print a message telling the user the "current" time.

Next, modify your program by printing a simple phrase that depends on the "current" time block. For example, if one of your time blocks is 3am - 10am, you might want to just print "Good morning" when the current time falls in that block. Once you have that code written to your satisfaction, replace the single phrase with a block of code that prints one of several phrases depending on a random number.

Be sure to edit the comments in the Robot.java file as appropriate, including the class documentation comments at the top of the file. The class documentation comments should describe the purpose and behavior of your main class from a user's perspective. Focus on what the program does, rather than how it does it. Include your name and the date as well as the names of anyone from whom you received help. Providing proper documentation is an important step towards writing well-structured and reusable programs.

Note: 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 program to actually make use of the current time by using the Calendar class. To get a Calendar object whose fields are initialized with the current date and time, you would need a statement such as:

Calendar rightNow = Calendar.getInstance();
To get the current hour of the day, you would need to use a statement such as:
int timeOfDay = rightNow.get(rightNow.HOUR_OF_DAY);
This returns 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.)