Student+Randomizer

code format="java" /**** * Title: Student Randomizer * * Author: Lyle Kozloff * * Description: Displays exciting colors and random names until you click - then * displays a single, static name on an ominous screen. Names are not repeated until * the class list has been exhausted once. * * Assumptions: MAXLENGTH will be greater than the number of lines in your file. *             FILENAME will be the correct path and filename. *             DISPSIZE will be the size you want displayed, less than 500 not reccommended. *             FONTNAME will be the file name of a font you've pre-created. You may need to change it! ****/

final int MAXLENGTH = 50; final String FILENAME = "grade7.txt"; final int DISPSIZE = 600; final String FONTNAME = "CharterBT-Roman-48.vlw";

PFont f; BufferedReader reader;

String line = ""; //trick our while condition String[] students = new String[MAXLENGTH]; //can't have more than MAXLENGTH students at a time int loopCounter = 0; int count = 0;

//set up drawing area, load fonts, read file and create randomized array void setup { size(DISPSIZE, DISPSIZE);

f = loadFont(FONTNAME);

//This is how you read from a file. reader = createReader(FILENAME); while(line!=null){ try { line = reader.readLine; //read a single line } catch (IOException e) { e.printStackTrace; line = null; //if this happens, it's the end of the file. }     if (line == null) { // Stop reading because of an error or file is empty break; }   students[loopCounter] = line; //put the name of a student in the next position in the array loopCounter++; }

//randomize the array students = randomizeArray(students, loopCounter); } //what to do while we wait void draw { background(random(200,255),random(200,255),random(200,255)); textFont(f,24); fill(0); text(students[int(random(students.length))],random(DISPSIZE),random(DISPSIZE)); textFont(f,48); textAlign(CENTER); text("Click and HOLD!", DISPSIZE/2, DISPSIZE/2); }

//What to do if the mouse is held down void mousePressed{ noLoop; //stop looping on draw;

// make sure that we aren't going out of bounds if(count>=students.length){ //if we are going out of bounds, re-randomized the array students = randomizeArray(students, students.length); count = 0; //reset the position }

// display the next name on the randomized list background(128,128,128); textAlign(CENTER); text(students[count], DISPSIZE/2, DISPSIZE/2);

count++; //increment to next student on the randomized list }

//What to do if the mouse is released void mouseReleased{ loop; //go back to draw; }

//returns a randomized subset of kids of length arrlength private String[] randomizeArray(String[] kids, int arrlength){

String buffer; String[] randomizedArray = new String[arrlength]; int position = 0; int randPos = 0;

while(position<arrlength){ randPos = int(random(arrlength)); buffer = kids[randPos]; kids[randPos] = null; //don't want that kid again!

if(buffer!=null){ randomizedArray[position] = buffer; //println(randomizedArray[position]); position++;

} }  return randomizedArray; } code