NotesWeek6.Sept 27-Oct 1

NotesWeek6.Sept 27-Oct 1 - CS 180 Problem Solving and...

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: CS 180 Problem Solving and Object Oriented Programming Fall 2010 hMp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: Notes for Week 6: September 27Oct 1, 2010 Aditya Mathur 9/27 1. Feedback for Week 5 2. Review 3. Loops (again) Department of Computer Science Purdue University West LafayeMe, IN, USA Readings and Exercises for Week 6 Readings: Chapter 3: 3.3, 4.1, 4.2, 4.3, 4.4 Exercises: 4.1, 4.2, 4.3, 4.5 9/27/2010 CS 180. Fall 2010. Week 6 2 Special help session Sunday October 3: 57pm LWSN B158 9/27/2010 CS 180. Fall 2010. Week 6 3 Announcements 1. Project 2 now available. Due: Friday Oct 8, 2010 2. Project 1 not submiMed? Can s^ll submit it if you wish to be graded. 3. Problems with turnin? Contact your recita^on TA, not the lab TA 9/27/2010 CS 180. Fall 2010. Week 6 4 Announcements 4. If you wish to par^cipate in the programming compe^^on send mail to [email protected] with the following informa^on: Your name [leader] and a list of team members with their email addresses; team size must be 3 or 4. A brief descrip^on of what applica^on does your team plan to develop. Your app will be for a smart phone or a robot 9/27/2010 CS 180. Fall 2010. Week 6 5 Examina^on 1 1. Wednesday Sept 29. 810pm. EE 129 2. Two parts: Part A and Part B 3. Part A: 15 mul^ple choice ques^ons: essen^ally a review of weeks 1 through 5. CLOSED BOOK. 4. Part B: two programming ques^ons; one problem requires ifelse statement; no loops. OPEN BOOK. 9/27/2010 CS 180. Fall 2010. Week 6 6 Feedback for Week 5 9/27/2010 CS 180. Fall 2010. Week 6 7 Q1. The lab exercises were useful (Use scale of 110: 10 most useful, 1 not useful at all) (a) 810 (b) 47 (c) 13 (d) Missed Week 5 lab 9/27/2010 CS 180. Fall 2010. Week 6 8 Q2. The recita^on exercises were useful (Use scale of 110: 10 most useful, 1 not useful at all) (a) 810 (b) 47 (c) 13 (d) Missed 5 recita^on 9/27/2010 CS 180. Fall 2010. Week 6 9 Q3. The recita^on instructor was helpful. (Use scale of 110: 10 most helpful, 1 not helpful at all) (a) 810 (b) 47 (c) 13 (d) Missed week 5 recita^on 9/27/2010 CS 180. Fall 2010. Week 6 10 Q4. I understand condi^ons. (a) Yes (b) No 9/27/2010 CS 180. Fall 2010. Week 6 11 Q5. I understand ifelse statement. (a) Yes (b) No 9/27/2010 CS 180. Fall 2010. Week 6 12 Q6. I understand while loop. (a) Yes (b) No 9/27/2010 CS 180. Fall 2010. Week 6 13 Q7. So far I am liking the course (10 liking a lot, 1 not liking at all). (a) 810 (b) 47 (c) 13 9/27/2010 CS 180. Fall 2010. Week 6 14 Loops: More! 9/27/2010 CS 180. Fall 2010. Week 6 15 Loops: Why use them? Useful to do "same" thing over and over either a fixed or a variable number of ^mes. 9/27/2010 CS 180. Fall 2010. Week 6 16 while loop: Basic structure ini^alize; while (condi^on){ loop body; } finalize; 1. The loop body is guarded by the condi^on. 2. Before each itera^on, the condi^on is evaluated. If it is true then the loop body is executed exactly once else the loop is said to terminate. 3. Number of itera^ons depends on the condi^on. 4. A loop that never terminates is also known as an "infinite loop" 9/27/2010 CS 180. Fall 2010. Week 6 17 for loop: Fixed number of itera^ons ini^alize; for( int i=0; i<N; i++){ loop body; } finalize; 1. The loop body is executed N ^mes. 2. Variable i is set to 0. It is incremented by 1 aqer each itera^on. 3. Number of itera^ons is N unless something dras^c happens in the loop body. 9/27/2010 CS 180. Fall 2010. Week 6 18 for loop: Example: Sum the first 10 integers int sum=0; for( int i=1; i<=10; i++){ sum=sum+i; } System.out.println(sum); 9/27/2010 CS 180. Fall 2010. Week 6 19 for statement: general format init term iter for( init; term; iter){ loop body; } sample for( int i=1; i<=10; i++){ sum=sum+i; } init: Evaluated once at the start of the loop term: termina^on condi^on, checked at the start if each itera^on iter: evaluated at the end of each itera^on 9/27/2010 CS 180. Fall 2010. Week 6 20 Problem: Statement Con^nuously monitor the posi^on of two ground vehicles. The posi^on of each vehicle is available as an (x,y) coordinate. If the vehicles are less than distance d apart then display a warning signal. Stop monitoring when both vehicles have stopped. 9/27/2010 CS 180. Fall 2010. Week 6 21 Problem: Understanding How do we get the posi^on: Assume that both vehicles provide this informa^on with respect to an origin. How oqen should we get the posi^on? Assume every 1 second. What is d? Assume that d is given. How do we know that a vehicle has stopped? Assume that the vehicle has stopped when its two consecu^ve posi^ons do not change. 9/27/2010 CS 180. Fall 2010. Week 6 22 Algorithm Get minimum allowable distance d; moving=true; // Both vehicles moving pos1=get posi^on of vehicle 1; pos2=get posi^on of vehicle 2; while (moving){ compute distance dist; if(dist<=d){ Display warning; } 9/27/2010 if((pos1prevPos1)==0){ stop1=true; } if((pos2prevPos2)==0){ stop2=true; } if(stop1&&stop2){ moving=false; } } // end of loop prevPos1=pos1; prevPos2=pos2; pos1=get posi^on of vehicle 1; pos2=get posi^on of vehicle 2; CS 180. Fall 2010. Week 6 23 Algorithm (Graphical) Start Yes Ini^alize Stop monitoring? No too close? No Update vehicle posi^ons No Both stopped? Set end flag What should be ini^alized? Yes Display Warning Yes Wait for 1 second 9/27/2010 End CS 180. Fall 2010. Week 6 24 Algorithm [Textual] do forever { get pulse rate; if the pulse rate is between 60 and 100 then Display "Normal"; else if the pulse rate is<60 then Display "Low"; else Display "High"; Wait for 5 seconds; } 9/27/2010 CS 180. Fall 2010. Week 6 25 Problem We are given monthly rainfall data for a region. Write a program to compute the average yearly rainfall in that region. Understand the problem: 1. In what form is the data available? Integer? Float? Input via console? 2. How much data? 9/27/2010 CS 180. Fall 2010. Week 6 26 Java program for pulse rate monitoring and display. Live demo. 9/27/2010 CS 180. Fall 2010. Week 6 27 Quiz: 9/27/2010 9/27/2010 CS 180. Fall 2010. Week 6 28 Q1. int sum=0; Random r=new Random (); int num=r.nextInt(10); while (sum<100 || num!=0){ sum=sum+num; } The loop will terminate when (a) num is 0 (b) sum is equal to or greater than 100 and num is 0 (c) Sum is less than 10 9/27/2010 CS 180. Fall 2010. Week 6 29 Q2. int sum=0, num=0; while (num<3){ num=num+1; sum=sum+num; } The value of sum upon loop termina^on is (a) 3 (b) 6 (c) 2 (d) 1 9/27/2010 CS 180. Fall 2010. Week 6 30 Q3. int sum=0, num=0; while (num<3){ sum=sum+num; num=num+1; } The value of sum upon loop termina^on is (a) 3 (b) 6 (c) 2 (d) 1 9/27/2010 CS 180. Fall 2010. Week 6 31 Q4. int sum=0, num=1; while (num<4){ if(num%2==0){ sum=sum+num; } num=num+1; } The value of sum upon loop termina^on is (a) 3 (b) 6 (c) 2 (d) 1 9/27/2010 CS 180. Fall 2010. Week 6 32 Q5. int sum=0; for (int i=3; i>0; i){ sum=sum+i; } The value of sum upon loop termina^on is (a) 3 (b) 6 (c) 2 (d) 1 9/27/2010 CS 180. Fall 2010. Week 6 33 End of Quiz: 9/27/2010 9/27/2010 CS 180. Fall 2010. Week 6 34 Java program for vehicle collision detec^on. Live demo. 9/27/2010 CS 180. Fall 2010. Week 6 35 Week 6: September 27Oct 1, 2010 Hope you enjoyed this week! Ques^ons? Contact your recita^on instructor. Make full use of our office hours. 9/27/2010 CS 180. Fall 2010. Week 6 36 ...
View Full Document

This note was uploaded on 02/05/2012 for the course CS 180 taught by Professor Staff during the Fall '08 term at Purdue.

Ask a homework question - tutors are online