Lab 8: Pick-A-Student

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.

Getting Started

  1. 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/
  2. Open PickAStudent.java in XEmacs and compile - without making any changes to the code just yet!

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

The Shuffle Subroutine

In the PickAStudent.java file, look for the Shuffle subroutine:

private void shuffle() { /**********************************/ /* PUT YOURSELF IN THE FIRST SPOT */ /**********************************/ int j=0; while(j< size - 1) { /* SEARCH the myPictures array for your picture!*/ if (/*fill in condition for finding your picture*/) { /* SWAP your picture with the first element in myPictures */ } j=j+1; } /************************************/ /* AND ALL THE REST... */ /************************************/ int randIndex; int k = 1; /* THINK ABOUT IT: WHY IS "k" being set to "1"? */ while (k < size - 1) { /* ************************************ */ /* ADD YOUR CODE HERE! */ /* ************************************ */ /* FILL IN: SET randIndex = ??? */ /* FILL IN: SWAP elements of myPictures */ /* array */ /* ************************************ */ k = k+1; } }

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 k (initially 0) at each iteration of the while loop. Using the instructions and algorithm from Prelab, you need to add code to Shuffle so that it shuffles the elements of the myPictures array.

Hints/Suggestions

Submitting

Test your algorithm thoroughly before you submit! Submit the files PickAStudent.java and PickAStudent.html as follows:

submit_cps001 lab8/sec# PickAStudent.java PickAStudent.html

Replace # with your section number. DON'T FORGET TO INCLUDE YOUR NAME AND LAB SECTION AT THE TOP OF ALL OF YOUR FILES!



Jeff Forbes
Last modified: Tue Oct 29 22:08:26 2002