11 Two modes

11 Two modes - Two modes Two modes Often, programs will...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Two modes Two modes Often, programs will have two "modes" or "states" The combination of an if and a if and a boolean can switch between the two 1 Two modes Two modes mode = !mode this line of code "flips" mode back and forth between true and false. 2 A program using a boolean and A program using a the mouse to switch "modes" boolean mode = false; void setup(){}//empty void draw() { background(0); background(0); if(mode == true) if(mode { ellipse(50,50,50,50); ellipse(50,50,50,50); } else else { rect(25,25,50,50); rect(25,25,50,50); } } void mouseClicked() { mode = !mode; mode } 3 Now let's modify the program so Now let's modify the program so that the ellipse has a random position boolean mode = false; void setup(){}//empty void draw() { background(0); background(0); if(mode == true) if(mode { } ellipse(int(random(100)),int(random(100)),50,50); else else { rect(25,25,50,50); rect(25,25,50,50); } } void mouseClicked() { mode = !mode; mode } 4 noLoop() and redraw() noLoop() Normally, the screen is refreshed at 60hz (60 times a second) Every time the screen all the code in draw() executes If you call noLoop() in setup(), though, draw() only executes once If you want to draw the screen again, call redraw() 5 The fixed version with The fixed version with noLoop() and redraw() boolean mode = false; void setup() { noLoop(); noLoop(); } void draw() { background(0); background(0); if(mode == true) if(mode { ellipse(int(random(100)),int(random(100)),50,50); ellipse(int(random(100)),int(random(100)),50,50); } else else { rect(25,25,50,50); rect(25,25,50,50); } } void mouseClicked() { mode = !mode; mode redraw(); redraw(); 6 } charAt() charAt() Lets you get at the individual characters of a String What would the following code display? String x = "mystery"; println(x.charAt(2)); 7 the length() of a String the A String can be thought of like an Array The length of the String is the number of characters in the String What would be displayed by this program? String one = "dog"; String two = "elephant"; String three = ""; println(one.length()); println(two.length()); println(three.length()); 8 the length() of a String the String one = "dog"; String two = "elephant"; String three = ""; println(one.length()); println(two.length()); println(three.length()); 9 boolean functions boolean A boolean function returns true or false Let's say you work at the admissions office for UC Santa Cruz where the average freshman admission has a GPA of 3.7 and an SAT of 1750* Your asked to write a function that will return true if a freshman has scores that are both better than average or false if they don't http://www.universityofcalifornia.edu/admissions/undergrad_adm/selecting/camp_profiles/camp_profiles_ucsc.html 10 boolean functions boolean void setup() { println(getsIn(4.0,1700)); println( println(getsIn(3.0,1400)); println( println(getsIn(3.5,2100)); println( println(getsIn(3.8,1800)); println( } boolean getsIn(float gpa, int sat) int { } ??? ??? 11 boolean functions boolean void setup() { println(getsIn(4.0,1700)); println( println(getsIn(3.0,1400)); println( println(getsIn(3.5,2100)); println( println(getsIn(3.8,1800)); println( } boolean getsIn(float gpa, int sat) int { } ??? ??? 12 boolean functions boolean void setup() { println(getsIn(4.0,1700)); println( println(getsIn(3.0,1400)); println( println(getsIn(3.5,2100)); println( println(getsIn(3.8,1800)); println( } boolean getsIn(float gpa, int sat) int { } if(gpa > 3.7 && sat > 1750) if(gpa ??? ??? 13 boolean functions boolean void setup() { println(getsIn(4.0,1700)); println( println(getsIn(3.0,1400)); println( println(getsIn(3.5,2100)); println( println(getsIn(3.8,1800)); println( } boolean getsIn(float gpa, int sat) int { if(gpa > 3.7 && sat > 1750) if(gpa { return true; return } else else ??? ??? } 14 boolean functions boolean setup() void { println(getsIn(4.0,1700)); println( println(getsIn(3.0,1400)); println( println(getsIn(3.5,2100)); println( println(getsIn(3.8,1800)); println( } boolean getsIn(float gpa, int sat) int { } if(gpa > 3.7 && sat > 1750) if(gpa { return true; return } else else { return false; return } 15 void setup() { if(isMatch("elephant")==true) if(isMatch("elephant")==true) { println("Match 1"); println("Match } if(isMatch("alligator")==true) if(isMatch("alligator")==true) { println("Match 2"); println("Match } if(isMatch("ox")==true) if(isMatch("ox")==true) { println("Match 3"); println("Match } if(isMatch("pup fish")==true) if(isMatch("pup { println("Match 4"); println("Match } } boolean isMatch(String sWord) { if(sWord.length()>=3) if(sWord.length()>=3) { return sWord.charAt(0) == sWord.charAt(2); return } else else { return false; return } } Find the output Find the output 16 void setup() { if(mystery("antelope", 'e')==true) if(mystery("antelope", { println("One"); println("One"); } else else { println("Two"); println("Two"); } if(mystery("aardvark", 'a')==true) if(mystery("aardvark", { println("Three"); println("Three"); } else else { println("Four"); println("Four"); } What is the output What is the output of this program? } boolean mystery(String word, char letter) { int sum = 0; int for(int i = 0; i < word.length(); i++) for(int { if(word.charAt(i) == letter) if(word.charAt(i) { sum++; sum++; } } return sum > 2; return } 17 ...
View Full Document

This note was uploaded on 01/13/2012 for the course CS 12 taught by Professor Ma during the Spring '08 term at UC Davis.

Ask a homework question - tutors are online