Looping StructuresThe PlanMotivationSlide 4for LoopSlide 6Slide 7Slide 8while LoopSlide 10Slide 11do-while LoopSlide 13Slide 14Equivalence of LoopsWhen to use which loop?Slide 17Application of Simulated CollisionSlide 19Slide 20Slide 21Practice ProblemsDesign of Video Game PackageSlide 24Basic Principles of DesignWhy Simple?Why functional?Why fast?Why correct?Why easy to modify?Why easy to extend?Capable of ReuseWhy we need MVC?What is MVC?Simplified 1D ModelWhat are our objects?class Particleclass Segment extends Particleinterface Tracker1Dclass VelocityTrackerclass ModelTestMore to come….Looping StructuresThe PlanWhile not everyone understands:1. Motivate loops2. For loops3. While loops4. Do-while loops5. Equivalence6. Application of Simulated Collision7. Practice ProblemsMotivationWhy loop?Sometimes you need to do things again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again...and finally you get tired of typing.MotivationOkay, so that's not all. You also loop in order to:●Group repeatedly executed code for uniformity●Make the number of repetitions easily changeable●Repeat events which the number of exectutions is known only dynamically●Combine with selection statements to make more complex algorithmsfor Loopfor(int i=0; i<10; i++){System.out.println(i);}for Loopfor(int i=0; i<10; i++){System.out.println(i);}InitializationConditionUpdateTrueFalsefor Loopfor(int i=0; i<10; i++){System.out.println(i*0.1);}InitializationConditionUpdateTrueFalseScale factorfor Loopfor(int i=0; i<10; i++){System.out.println(i*0.1+5);}InitializationConditionUpdateTrueFalseScale factorTranslationwhile Loopint i=0;while(i<10){System.out.println(i);i++;}while Loopint i=0;while(i<10){System.out.println(i);i++;}InitializationConditionUpdatetruewhile Loopdouble i=0;while(i<1){System.out.println(i);i+=0.1;}Why mightthis be a problem?do-while Loopint i=0;do{System.out.println(i);i++;}while(i<=10);do-while Loopint i=0;do{System.out.println(i);i++;}while(i<=10);InitializationConditionUpdateTrueFalsedo-while Loopint i=0;do{System.out.println(i);i++;}while(i<=10);Notice this semicolonwas not here in the while loop!int i=0;while(i<10){System.out.println(i);i++;}Equivalence of Loopsfor(int i=0; i<10; i++){System.out.println(i);}When to use which loop?Is it known how many times the loop will execute prior to executing the loop body?YesforNoIs it important for the loop body to always execute at least once?Yesdo-whileNowhileWhen to use which loop?Real answer: Use which ever structure is most convenient, because all loop structures can be represented as any other loop structure.Why are there multiple loop structures then?Simple answer – for the programmer’s convenience.Application of Simulated Collisiondouble velocity=3;double position=1;double timeStep=0.1;//simulate for about 5 secondsdouble time=0;while(time<5){System.out.println("("+t+", "+position+")");time+=timeStep;}Application of Simulated Collisiondouble velocity0=3; double velocity1=-2;double position0=1; double position1=10;double timeStep=0.1;//simulate for about 5 secondsdouble time=0;while(position0!=position1){System.out.println(“p0 is (“+t+", "+position0+")");System.out.println(“p1 is (“+t+", "+position1+")");time+=timeStep;}What’s wrong?Why doesn’t theprogram end?Application of Simulated Collisiondouble velocity0=3; double velocity1=-2;double position0=1; double position1=10;double timeStep=0.1;//simulate for about 5 secondsdouble time=0;while(position0<position1){System.out.println(“p0 is (“+t+", "+position0+")");System.out.println(“p1 is (“+t+", "+position1+")");time+=timeStep;}Problem fixed,right?Application of Simulated Collisiondouble velocity0=-3; double velocity1=2;double position0=10; double position1=1;double timeStep=0.1;//simulate for about 5 secondsdouble time=0;while(position0<position1){System.out.println(“p0 is (“+t+", "+position0+")");System.out.println(“p1 is (“+t+", "+position1+")");time+=timeStep;}What about now?(notice velocity and position change)Practice Problems●Write a loop to print out from 10 to 100 inclusive counting by 10s●Write a loop that starts with an arbitrary double x and divides it by 2 repeatedly until it is less than 1. Output the number of times the loop executed. What is being computed?●Write a loop that sums the first x integers where x is a positive integer. Print out the results.●Write a loop that takes an integer x starting with value 1 and doubles x so long as x is positive. Bonus question: why doesn’t this loop infinitely? Super Bonus question: why does it loop infinitely when x is a double?Design of Video Game PackageAnimationCanvasKeyboardMouseGameLoopJFrameOrAppletTrackerSpriteTrackerSpriteTrackerSpriteThe Plan●Basic principles of design●Motivate why MVC is needed●Model/View/Controller (MVC) overview●Model (today)●View (when we get to Graphical User Interfaces)●Controller (when we get to Event Handling)Basic Principles of DesignWe want our programs to be●simple●functional●fast (to code and to execute)●correct●easy to modify●easy to extend●capable of reuseWhy Simple?●Simple is easy to understand and therefore easier to reuse, modify, and extend.●Simple has less likelihood for program errors and is therefore more likely to be correct.●Simple may be faster than complex, certain to code and perhaps in execution time.Why functional?●It has to work.●How well it works is negotiable considering:–speed to release–cost to release–lifetime of codeWhy fast?●Who wants to wait?●Works better on slower, more out-of-date machines.●Sometimes crucial to application’s purpose–air control–nuclear plant facilities–weather prediction–stock predictionWhy correct?●In some cases, incorrect execution is unacceptable–access control–online sales●In some cases, incorrect execution can be remedied by re-execution–operating system locks up, so just reboot–database goes down, just restart●impacts time codingWhy easy to modify?●Users rarely can tell you exactly what they want●Even if they could tell you, what users want changes●Changes in hardware and software mandate code modification (think Y2K)Why easy
View Full Document