CS 40 Fall 2007 Homework 2

1. In lecture 4, the evil step sister ate either cookie or cookie2. The code in the if and else branches looked almost identical. Your task is to provide a method eat so that the code can simply be

if evilStepSister.distance(cookie) < evilStepSister.distance(cookie2)
   evilStepSister.eat(cookie)
else
   evilStepSister.eat(cookie2)

Your eat method should be able to eat any object. The evil step sister should move close to the object, move the object to its mouth, and make it disappear (by setting its opacity). Submit a world in which the evil step sister chooses the closests of two food items (need not be cookies) and then eats the closest one, using your eat method.

Grading criteria:

a) Do you have an eat method?

b) Is it a method of the evil step sister (and not the world?)

c) Does it have a parameter?

d) Is the eat method called from my_first_method?

20 points total

2. Implement the following scene:

A creature of your choice asks: "What is your name?" Then the program user gets to type in a response. Next, the creature asks: "What would you like me to do for you?" Again, the program user gets to type in a response. Now the creature thinks "(insert request)???" and then says "I am sorry, (Insert name), but I am afraid I can't do that."

Here is a sample dialog:

Creature: What is your name?

User: Dave

Creature: What would you like me to do for you?

User: Open the pod bay doors

Creature (thinks): Open the pod bay doors???

Creature: I am sorry, Dave, but I am afraid I can't do that.

As you know from the FirstEncounter, the say method can be used to have the creature say something. To get a response from the user, you use a function (not a method) from the World class: Ask user for a string. Store the result in a variable. (The point of this exercise is to work with variables.)

For the thinking step, you need to join together two strings, the user's request and "???". This is achieved with another function from the World class: a joined with b.

In the final step, you need to join together three strings "I am sorry, ", the user's name, and ", but I am afraid I can't do that.". Use a joined with b twice.

Grading criteria

a) Does the program get input from the user?

b) Is the input stored in a variable?

c) Does the thinking string have the form "(insert request)???", with the "???" joined at the end?

d) Does the creature say "I am sorry, (Insert name), but I am afraid I can't do that."?

20 points total

Put two attachments, eat.a2w and dialog.a2w into the eCampus assignment.