CS 110: Introduction to Programming in Java

Kalamazoo College

Lab: Talking Robot

Using If-Else and Random Numbers


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

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 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.

To begin, drag the folder titled "TalkingRobot" from \\Dragon\cls_compsci\cs110 to your M: Drive or the Temp folder. The code you add should go into the main method of the Robot.java file, which is part of the Talking Robot project.

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 RandNumGenerator 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.

Remember as always, if you have placed any files in the Temp folder, it is your responsibility under the Kalamazoo College honor code to copy your files onto a USB drive, a floppy, or your own machine, and then remove them from the Temp folder.

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.

If you have time at the end of lab you may experiment with the Calendar class if you like, but be sure that your program is working with the random number before you turn it in.  For example, you could comment out the random number generation while experimenting with the Calendar class and then, before turning it in, change the commenting so that the code using the Calendar class is commented out instead.

When you are done, hand in Robot.java with your modified main method.


When you complete the lab, you can start work on Programming Project #2 (due at the beginning of Lab 3).