Modify a text file: $/final_sample.pde
$/final_sample.pde
//// Sample code and class definitions for final exam. //// File name: final_sample.pde int arrayname[]= new int[20]; color bg= color(255,191,191); // Background color. String lastClick; int avg; int countA; Ball newball= new Ball(); Button a=new Button("A",100,20, 40, 40, 0, color(255,223,223) ); //++++ ADD MORE BUTTONS HERE. void setup() { //// Initialize. size(640,480); //++++ ADD YOUR CODE HERE. } void draw() { //// Draw each frame: buttons at top, values at bottom + bar chart. background(bg); fill(0,0,255); text( "Your Name", 10, 480 ); fill(0); text("Mean average: ", 500,10); text(avg, 600,10); //// Display array, showArray(arrayname, arrayname.length); //// Buttons. showButtons(); //// Show ball, if visible. if (newball.visible) { newball.move(); newball.show(); if (newball.isOver(mouseX,mouseY)) { newball.vanish(); } } // DEBUGGING: if (newball.visible) { text(newball.x, 600,170); text(newball.y, 600,180); } } void showButtons() { //// Show the buttons. a.show(); //++++ ADD YOUR CODE HERE. } void mousePressed() { //// Check which button was clicked /* When a button is "clicked", perform the task described below: A: Change the background color, alternating between light-green and light-blue. B: Find the largest and smallest values in the array, and swap them. C: Replace each array element with random value (0-100) and recalculate the mean average. D: Change the array (and recalculate average): Double all values that are less than the mean average. Subtract 50 from all values that are greater than the mean average E: Change the array (and recalculate average): Add ten to all odd values in the array; Divide all odd values by three. F: Create a new ball (30 pixels diameter, randomly colored) at a random height on the left side. Ball moves diagonally (2 down, 3 right), "bouncing" off all edges of the window. Ball vanishes if it hits (or comes within a 50-pixel radius of) mouse position. */ if (a.isOver(mouseX,mouseY)) { wasClicked(a); countA++; //++++ ADD YOUR CODE HERE. } //++++ ADD YOUR CODE HERE. } void wasClicked( Button x ) { //// Record which button was clicked. lastClick= x.name; x.count++; } void fillArray( int a[], int m ) { //// Fill the array with random values (0-100); //++++ ADD YOUR CODE HERE. } void showArray( int a[], int m) { //// Show the array //++++ ADD YOUR CODE HERE. } class Button { //// A button, displayed on screen, and able to detect when mouse is OVER. int x= 10; // Position and size of this button. int y= 10; int w= 100; int h= 50; color c; // Color of this button. int type=1; // Button type (1=rectangle) String name; // Text on this button. int count=0; //// CONSTRUCTOR(S) //// Button( String s, int xset, int yset) { //Button(); //// Accept x & y coordinates; use default for the others. x= xset; y= yset; c=color(255,127,127); name=s; } Button( String s, int xset, int yset, int wset, int hset, int tset, color cset) { //Button(); //// Accept x & y coordinates; use default for the others. x= xset; y= yset; w= wset; h= hset; c= cset; type= tset; name=s; } //// METHODS() //// void show() { //// Show the button. if (isOver(mouseX,mouseY)) { //++++ ADD YOUR CODE HERE. } if (type<1) { ellipseMode(CORNER); ellipse(x,y,w,h); }else{ rectMode(CORNER); rect(x,y,w,h); } // Name on button. fill(0); text( name, x+10, y+h/2 ); // Count if (count>0) text(count, x+20, y+h/2+10 ); } boolean isOver( int xx, int yy ) { //// Return true if xx,yy is over this button! if (xx < x) return false; else if (xx > x+w) return false; else if (yy < y) return false; else if (yy > y+h) return false; return true; } } class Ball { //// A ball, displayed on screen if x>0. int x,y,r=10; // Position and size of this ball. int dx=3; int dy=2; color c= color(255,0,0);; boolean visible=false; //// METHODS() //// void create() { //// Create a ball. visible= true; //++++ ADD YOUR CODE HERE ++++ } void vanish() { //// Make the ball vanish. visible= false; } void show() { //// Show the ball, only if it is visible. if (visible) { //++++ ADD YOUR CODE HERE ++++ } } void move() { //// Move the ball. Bounce off walls. //++++ ADD YOUR CODE HERE ++++ } boolean isOver( int xx, int yy ) { //// Return true if xx,yy is over this button! //++++ ADD YOUR CODE HERE ++++ return false; } }