The goal for the lab is to understand arrays and use them to improve both the code and the functionality of the Conversation system. 

  1. New partners, see Announcements. Outside of lab, add to your partner evaluation form and send it to me.

  2. Decide whose code to work from and fix any problems. E.g. Make sure all the required functions are there and that your "last spoken to" variable is a Ruler.

  3. Add a new ruler to the system. As you do this make a note of each individual change to the code. Save these notes so you can submit them when you complete the lab. Call these your Before Notes. Be sure to send these to your partner, as well as the code you complete.

  4. Declare an array called rulerList at the beginning of the Conversation class. This array will hold all the rulers. Compile and test.

  5. Initialize rulerList appropriately. Compile and test.

  6. As you create the rulers, store them into your rulerList array. Compile and test.

  7. Replace the code in identifyRulers() with a loop. Note that the code got shorter. Compile and test.

  8. Remember the numRulers variable we've been keeping around? Use it to initialize rulerList and to govern the loop in (7). Compile and test.

  9. Replace the code in reportRulers() with a loop. Compile and test.

  10. The next few steps will introduce some randomness into who's speaking with whom in our system. Paradoxically, we start by making it less random.

  11. Replace the code in converse with a loop that repeats 10 times and just has the same ruler saying hello to the same other ruler each time. Compile and test. Congratulations! You now have very boring output!

  12. Introduce a speakerIndex variable into the converse routine. Set it equal to a random integer between 0 and one less than the number of rulers in your system. (Remember all the things you have to do, including an import statement, to use a random number generator. Just copy this from one of our other examples.) Use the array to have this random speaker do the talking. If you've done everything right, something like rulerList[speakerIndex].sayHelloTo(lincoln) should do the trick. Do you see how this works? If speakerIndex is randomly set to 3 then Ruler number 3 will speak. A different ruler could speak on each repetition of the loop. You now have somewhat less boring output.

  13. Introduce a local listenerIndex variable into the converse routine and use it appropriately. We now have randomly chosen speakers talking to randomly chosen listeners. It's more like a real conversation! Unfortunately sometimes people end up talking to themselves....

  14. You are making good progress, keep going.

  15. Make a plan for preventing rulers from talking to themselves. Show the plan to me. The plan could be in English or it could be in a mixture of English and Java.

  16. Once your plan has been approved, implement, compile, and test.

  17. Take a look at the variables of type Ruler. Now that we have rulerList we don't need all these individual Ruler variables like lincoln and joan any more. Change the code so that you no longer use these individual variables and then remove them.

  18. Add another ruler, once again keeping track of the changes you have to make. If there are more than two such changes (modify or add only two statements), revise the code until there are only two. Call these your After Notes.

  19. You have probably only changed the Conversation class, but please e-mail all three to me and to your partner so that we have the complete picture. In the body of the message, paste your Before and After notes on the steps required to add a new ruler to the system. As always please include CSCI 151, time and date.