Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Computation as an Computation as an Expressive MediumExpressive MediumLab 1: Loops, Animation, Lab 1: Loops, Animation, and Simple User Interactionand Simple User InteractionMicah HorvatMicah Horvat*Slides blatantly stolen from Annie Lausier, which were blatantly stolen *Slides blatantly stolen from Annie Lausier, which were blatantly stolen from Jason Alderman, which were blatantly stolen from Mayhew Seavey. from Jason Alderman, which were blatantly stolen from Mayhew Seavey. The cycle continues.The cycle continues.Lab HoursLab HoursAny suggestions?Any suggestions?I’ll send out an e-mail by the end I’ll send out an e-mail by the end of the day with with lab hours of the day with with lab hours And always feel free to e-mail And always feel free to e-mail me if you have questions: me if you have questions: [email protected]@mail.gatech.eduhttp://www.lcc.gatech.edu/~mazalek/courses/fall07/lcc6310/What have you gone What have you gone over?over?Window size and coordinate systemWindow size and coordinate systemCommenting codeCommenting codeVariablesVariablesDrawing primitivesDrawing primitivespointpointlinelinerectrectellipseellipsetriangletriangleprint() and println()print() and println()if(boolean){ do something }else{ do if(boolean){ do something }else{ do something} logicsomething} logic(0,0)xyWhat will we go over?What will we go over?Courseware!Courseware!Variables: A RecapVariables: A RecapDrawing primitives (more info)Drawing primitives (more info)Java Control Flow: LoopingJava Control Flow: LoopingThe Processing “draw()” looping The Processing “draw()” looping function for animationfunction for animationSimple mouse interactionSimple mouse interactionVariablesVariablesVariables are placeholders for Variables are placeholders for your data.your data.int i; // declares a new variable ii = 1; // assigns the value of 1 to the new variableint j = i + 1; // declares a new variable j, and assigns //it to i + 1, which is 2.VariablesVariablesTwo main types: Primitives and ObjectsTwo main types: Primitives and ObjectsPrimitives:Primitives:BooleanBoolean, , CharChar, Byte, Short, , Byte, Short, IntInt, Long, , Long, FloatFloat, Double, DoubleObjects:Objects:Everything else. Everything else. Constructed with primitives as a base. For Constructed with primitives as a base. For example, a “String” object is a series of example, a “String” object is a series of Char variables.Char variables.More info on drawing More info on drawing primitivesprimitivesZooooom on over to the Zooooom on over to the reference pages…reference pages…Java Control FlowJava Control FlowIf-Then-Else: Conditional LogicIf-Then-Else: Conditional Logicint i = 5;if(i > 10) {drop_it_like_its_hot();}else if(i > 3) {park_it_like_its_hot();}else {pop_it_like_its_hot();}Java Control Flow: Java Control Flow: LoopingLoopingWant to draw ten lines, evenly-Want to draw ten lines, evenly-spaced apart?spaced apart?line(10, 10, 10, 200);line(20, 10, 20, 200);line(30, 10, 30, 200);...This can get old, fast. Also would be tedious if you wanted to change all of the lines to start 5 pixels higher, instead.Java Control Flow: Java Control Flow: LoopingLoopingWant to draw ten lines, evenly-Want to draw ten lines, evenly-spaced apart?spaced apart?line(10, 10, 10, 200);line(20, 10, 20, 200);line(30, 10, 30, 200);...This can get old, fast. Also would be tedious if you wanted to change all of the lines to start 5 pixels higher, instead.What if we could run this same line of code multiple times, only changing these two numbers?Java Control Flow: Java Control Flow: While LoopsWhile LoopsWhile loops can make your dreams come true.While loops can make your dreams come true.line(10, 10, 10, 200);line(20, 10, 20, 200);line(30, 10, 30, 200);...int i = 10;while(i <= 100) { line(i, 10, i, 200); i = i + 10;}A while loop will run the code inside the brackets repeatedly until the condition in the parentheses is false.Java Control Flow: Java Control Flow: While LoopsWhile LoopsBe careful with loops. Make sure the Be careful with loops. Make sure the condition will eventually return condition will eventually return false, or else it will go forever.false, or else it will go forever.while(true) { ...}int i = 10;while(i > 0) { line(i, 10, i, 200); i = i + 10;}Both of these loops are valid, will compile, and run infinitely. Your dreams will soon become nightmares!Java Control Flow: For Java Control Flow: For LoopsLoopsFor Loops are just like While loops, but a For Loops are just like While loops, but a bit more compact for simple incrementing, bit more compact for simple incrementing, like in our last example.like in our last example.These two loops are functionally the same:These two loops are functionally the same:int i = 10;while(i <= 100) { line(i, 10, i, 200); i += 10;}for(int i = 10; i <= 100; i += 10) { line(i, 10, i, 200);}(i += 10 is equivalent to i = i + 10)Java Control Flow: Nested Java Control Flow: Nested LoopsLoopsNesting of loops refers to putting one Nesting of loops refers to putting one loop inside the brackets of another.loop inside the brackets of another.for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); }}What does this mean? It means that the inside loop will run all the way through, then i will increment, and the inside loop will run again.Java Control Flow: Nested Java Control Flow: Nested LoopsLoopsStill confused? Let’s look at Still confused? Let’s look at this thing closer.this thing closer.for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); }}ijSets i to 10. That’s all for now.Java Control Flow: Nested Java Control Flow: Nested LoopsLoopsStill confused? Let’s look at Still confused? Let’s look at this thing closer.this thing closer.for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); }}ijSets j to 10. That’s all for now.Java Control Flow: Nested Java Control Flow: Nested LoopsLoopsStill confused? Let’s look at Still confused? Let’s look at this thing
View Full Document