APCS: Magpie Project, Activity 5


3 points

Activity 5 is on page 13 in the Student Guide. Do a File - Save As on Magpie4.java, creating Magpie5.java.

Read page 13 of the Student Guide.

The only change in this final activity is that, in getRandomResponse(), the possible responses are no longer stored in an extensive IF statement. Instead, they are stored in an array of Strings. A random number is generated and the method returns the String that occupies that slot in the array. Modify your getRandomResponse method accordingly. You can use the 6 random responses that you already have.

A concise way of setting up this array is like this:
private String[ ] randomResponses = {"Do you really think so?", "Interesting, tell me more.", "Hmmm."};
(This shows just the first 3 responses.)

Resources:
MagpieRunner5.java
Student Guide

 

Links:
Magpie Project
AP Computer Science