In this lab you will implement a subroutine in the "Pick-A-Student" applet. The purpose of this applet is ... to pick a student! You have already seen Professor Forbes use something like this in class when he wants to pick a student to answer a question. What the applet does is display pictures of all the CPS1 students on the screen in succession; when it stops, the last picture displayed is the student picked. If we want to pick two students, we can run the applet twice - it won't pick the same student both times. We can "pick" the entire class by just running the applet one time for every student in CPS1.
Create a lab8 subdirectory in your cps001 directory. Copy the PickAStudent.java code and its html file PickAStudent.html to your ~/cps001/lab8 directory. You also need to copy the file PICS.jar. The files are located here:
/afs/acpub.duke.edu/project/web/cps001/code/
Open PickAStudent.java in XEmacs and compile - without making any changes to the code just yet!
View the PickAStudent applet using appletviewer (if you have problems, try /usr/bin/appletviewer instead). The applet will have a Run button at the top of the window and a message at the bottom saying Images Loaded.
Click the Run button. You should see pictures of all your classmates (and yourself, if you completed your Prelab properly!) appear in the applet window. Run the applet a few more times. Notice that each time the applet is run, the students are always in the same order - alphabetical by filename (the order listed in the CPS1 student pictures directory). Also, each time you run the applet the array size decreases by one, and the last picture displayed is different for each run. If we use the applet "as-is" to pick a student, it's pretty easy to see who always gets picked first! And second, and third, and so on. This applet would be much more interesting if we couldn't predict which student it will pick. In this lab you will complete a subroutine to pick the students in random order.
In the PickAStudent.java file, look for the Shuffle subroutine:
PickAStudent.java contains code to load the CPS1 pictures (in the order listed in the CPS1 student pictures directory) and store them in an array named myPictures. The variable size stores the number of elements in the array. There is a significant amount of code involved in loading the pictures, displaying them, and such, but you do not need to even look at it.
The shuffle subroutine rearranges the order of the pictures so that your own picture comes up first and the rest are picked by the applet (which corresponds to the last picture element in the array) at random each time the applet is Run. The first time the applet is Run all the rest of the students are equally likely to be picked. The value of size is equal to the number of students in the class (or, the number of students who verified we had their pictures!). The second time the applet is Run, all students but one (the student who was picked the first time) are equally likely to be picked, and size=size-1. And so on.
Note: The size of the array at each run is maintained outside the Shuffle subroutine - you should not change its value. All you need to do is shuffle the elements in the array, regardless of how many there are.
Currently, Shuffle doesn't do anything to change the order of the
elements of myPictures, it just increments the value of an integer
The subroutine RandBetween may be used to generate an index
randIndex between k and size-1. You have already written code to swap array elements in your Prelab.
Test your algorithm thoroughly before you submit! Submit the files
PickAStudent.java and PickAStudent.html as follows:
Replace # with your section number. DON'T FORGET TO INCLUDE YOUR NAME AND
LAB SECTION AT THE TOP OF ALL OF YOUR FILES!
Hints/Suggestions
Submitting
submit_cps001 lab8/sec# PickAStudent.java PickAStudent.html
Jeff Forbes
Last modified: Tue Oct 29 22:08:26 2002