Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 6: September 27-Oct 1, 2010 Aditya Mathur Department of Computer Science.

Similar presentations


Presentation on theme: "CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 6: September 27-Oct 1, 2010 Aditya Mathur Department of Computer Science."— Presentation transcript:

1 CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 6: September 27-Oct 1, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ 1.Feedback for Week 5 2.Review 3.Loops (again) 4.Exam 1: Review 5.Data input from files 6.Data output to file 7.Demo 9/27 This Week: 9/29

2 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/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 62

3 Special help session Sunday October 3: 5-7pm LWSN B158 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 63

4 Announcements 1.Project 2 now available. Due: Friday Oct 8, 2010 1.Project 1 not submitted? Can still submit it if you wish to be graded. 1.Problems with turnin? Contact your recitation TA, not the lab TA 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 64

5 Announcements 4.If you wish to participate in the programming competition send mail to ndcao@purdue.edu with the following information: Your name [leader] and a list of team members with their email addresses; team size must be 3 or 4. A brief description of what application does your team plan to develop. Your app will be for a smart phone or a robot 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 65

6 Examination 1 1.Wednesday Sept 29. 8-10pm. EE 129 1.Two parts: Part A and Part B 2.Part A: 15 multiple choice questions: essentially a review of weeks 1 through 5. CLOSED BOOK. 3.Part B: two programming questions; one problem requires if-else statement; no loops. OPEN BOOK. 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 66

7 Feedback for Week 5 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 67

8 Q1. The lab exercises were useful (Use scale of 1-10: 10 most useful, 1 not useful at all) (a)8-10 (b)4-7 (c)1-3 (d)Missed Week 5 lab 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 68

9 Q2. The recitation exercises were useful (Use scale of 1-10: 10 most useful, 1 not useful at all) (a)8-10 (b)4-7 (c)1-3 (d)Missed 5 recitation 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 69

10 Q3. The recitation instructor was helpful. (Use scale of 1-10: 10 most helpful, 1 not helpful at all) (a)8-10 (b)4-7 (c)1-3 (d)Missed week 5 recitation 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 610

11 Q4. I understand conditions. (a)Yes (b)No 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 611

12 Q5. I understand if-else statement. (a)Yes (b)No 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 612

13 Q6. I understand while loop. (a)Yes (b)No 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 613

14 Q7. So far I am liking the course (10 liking a lot, 1 not liking at all). (a)8-10 (b)4-7 (c)1-3 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 614

15 Loops: More! 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 615

16 Loops: Why use them? 9/30/2010 Useful to do “same” thing over and over either a fixed or a variable number of times. ©Aditya Mathur. CS 180. Fall 2010. Week 616

17 while loop: Basic structure 9/30/2010 initialize; while (condition){ loop body; } finalize; 1.The loop body is guarded by the condition. 2.Before each iteration, the condition is evaluated. If it is true then the loop body is executed exactly once else the loop is said to terminate. 3.Number of iterations depends on the condition. 4.A loop that never terminates is also known as an “infinite loop” ©Aditya Mathur. CS 180. Fall 2010. Week 617

18 for loop: Fixed number of iterations 9/30/2010 initialize; for( int i=0; i<N; i++){ loop body; } finalize; 1.The loop body is executed N times. 2.Variable i is set to 0. It is incremented by 1 after each iteration. 3.Number of iterations is N unless something drastic happens in the loop body. ©Aditya Mathur. CS 180. Fall 2010. Week 618

19 for loop: Example: Sum the first 10 integers 9/30/2010 int sum=0; for( int i=1; i<=10; i++){ sum=sum+i; } System.out.println(sum); ©Aditya Mathur. CS 180. Fall 2010. Week 619

20 for statement: general format 9/30/2010 for( init; term; iter){ loop body; } init: Evaluated once at the start of the loop term: termination condition, checked at the start if each iteration iter: evaluated at the end of each iteration for( int i=1; i<=10; i++){ sum=sum+i; } sample init term iter ©Aditya Mathur. CS 180. Fall 2010. Week 620

21 Problem: Statement 9/30/2010 Continuously monitor the position of two ground vehicles. The position 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. ©Aditya Mathur. CS 180. Fall 2010. Week 621

22 Problem: Understanding 9/30/2010 How do we get the position: Assume that both vehicles provide this information with respect to an origin. How often should we get the position? 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 consecutive positions do not change. ©Aditya Mathur. CS 180. Fall 2010. Week 622

23 Algorithm 9/30/2010 Get minimum allowable distance d; moving=true; // Both vehicles moving pos1=get position of vehicle 1; pos2=get position of vehicle 2; while (moving){ compute distance dist; if(dist<=d){ Display warning; } prevPos1=pos1; prevPos2=pos2; pos1=get position of vehicle 1; pos2=get position of vehicle 2; if((pos1-prevPos1)==0){ stop1=true; } if((pos2-prevPos2)==0){ stop2=true; } if(stop1&&stop2){ moving=false; } } // end of loop ©Aditya Mathur. CS 180. Fall 2010. Week 623

24 Algorithm (Graphical) 9/30/2010 Start Stop monitoring? Yes No too close? Display Warning Both stopped? Set end flag YesNo Update vehicle positions No Yes End Wait for 1 second Initialize What should be initialized? ©Aditya Mathur. CS 180. Fall 2010. Week 624

25 Algorithm [Textual] 9/30/2010 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; } ©Aditya Mathur. CS 180. Fall 2010. Week 625

26 Problem 9/30/2010 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? ©Aditya Mathur. CS 180. Fall 2010. Week 626

27 9/30/2010 Java program for pulse rate monitoring and display. Live demo. ©Aditya Mathur. CS 180. Fall 2010. Week 627

28 Quiz: 9/27/2010 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 628

29 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/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 629

30 Q2. int sum=0, num=0; while (num<3){ num=num+1; sum=sum+num; } The value of sum upon loop termination is 9/30/2010 (a)3 (b)6 (c)2 (d)1 ©Aditya Mathur. CS 180. Fall 2010. Week 630

31 Q3. int sum=0, num=0; while (num<3){ sum=sum+num; num=num+1; } The value of sum upon loop termination is 9/30/2010 (a)3 (b)6 (c)2 (d)1 ©Aditya Mathur. CS 180. Fall 2010. Week 631

32 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 termination is (a)3 (b)6 (c)2 (d)1 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 632

33 Q5. int sum=0; for (int i=3; i>0; i--){ sum=sum+i; } The value of sum upon loop termination is (a)3 (b)6 (c)2 (d)1 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 633

34 End of Quiz: 9/27/2010 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 634

35 9/30/2010 Java program for vehicle collision detection. Live demo. ©Aditya Mathur. CS 180. Fall 2010. Week 635

36 9/30/2010 Exam 1: TODAY! 8-10pm. EE 129 ©Aditya Mathur. CS 180. Fall 2010. Week 636

37 9/30/2010 Nature of the exam Part A: 15 multiple choice questions. Complete and turn in Part A and get Part B. Part B: Two programming problems One straight line program and one program requires if-else; no loops, no concurrency. ©Aditya Mathur. CS 180. Fall 2010. Week 637

38 9/30/2010 Review: Problem solving Understand the requirements Divide a problem into simpler problems Write algorithm to solve simple problems Combine algorithms Incrementally map algorithm to a program—write some code, test it then add more code, test it, and then add more code, and so on until you are done. ©Aditya Mathur. CS 180. Fall 2010. Week 638

39 9/30/2010 Review: Types Primitive types byte, short, int, long, float, double, boolean, char Memory requirements for each primitive type Class Template for creating objects Has a constructor and other methods At least one main() method ©Aditya Mathur. CS 180. Fall 2010. Week 639

40 9/30/2010 Review: Variables and objects Variable Must have a type May change its value during program execution Objects and variables: difference Object created from a class Variable declared to be of primitive type Names and keywords Object Created from a class Does work through its methods ©Aditya Mathur. CS 180. Fall 2010. Week 640

41 9/30/2010 Review: Input/Output Data input from console Use Scanner class Use methods such as nextInt(), nextDouble(), etc Data output to console Use System.out.println(…) For formatted format strings and then print using System.out.println(…) Alternatively, use System.out.format(….) ©Aditya Mathur. CS 180. Fall 2010. Week 641

42 9/30/2010 Review: Handling numbers Overflow and underflow Divide by zero exception Infinity, -Infinity and NaN Accuracy in number representation Integers Floating point numbers ©Aditya Mathur. CS 180. Fall 2010. Week 642

43 9/30/2010 Review: Handling strings Use the String class Concatenate strings using + Compare strings using equals() ©Aditya Mathur. CS 180. Fall 2010. Week 643

44 9/30/2010 Review: Operators Arithmetic operators + - / % ++ -- Infinity, -Infinity and NaN Relational operators >= != == Floating point numbers Conditional operators && || ©Aditya Mathur. CS 180. Fall 2010. Week 644

45 9/30/2010 Review: Declarations and statements Declarations Used for declaring a class, or a variable Assignments Used for assigning a value to a variable or an object Object creation Used for construct an object from a class using new ©Aditya Mathur. CS 180. Fall 2010. Week 645

46 9/30/2010 Review: Conditional execution if (condition){ statements; }else{ statements; } if (condition){ Statements; } ©Aditya Mathur. CS 180. Fall 2010. Week 646

47 9/30/2010 Review: Loops: Variable iteration count while (condition){ statements; } ©Aditya Mathur. CS 180. Fall 2010. Week 647

48 9/30/2010 Review: Loops: Fixed iteration count for (init; cond; iter){ statements; } ©Aditya Mathur. CS 180. Fall 2010. Week 648

49 9/30/2010 Review: Program structure Straight line program; only one path of execution Programs with more than one path of execution Every program has at least one class. Program execution begins in main(). Every class has at least one constructor. Note: If you do not provide a constructor then Java provides a default constructor. ©Aditya Mathur. CS 180. Fall 2010. Week 649

50 9/30/2010 Data I/O using files ©Aditya Mathur. CS 180. Fall 2010. Week 650

51 9/30/2010 What is a file? Why files? A file is a collection of data items that generally, though not always, resides on a disk. Some programs are required to write data to a file. Some programs are required to read data from a file. Sometimes files containing data are created by human beings, while at other times a program might create a data file. Files make it easier to record and reuse data as compared to the user having to type the data each time a program executes. Recall how you tested Project 1? Was it not a pain? ©Aditya Mathur. CS 180. Fall 2010. Week 651

52 9/30/2010 Define a File object or a stream? File cancerData=new File(); Creates an abstract file named cancerData FileOutputStream out=new FileOutputStream(); Creates a data output stream for writing data to a file ©Aditya Mathur. CS 180. Fall 2010. Week 652

53 9/30/2010 Reading from a file File cancerFile=new File(“cancerFile.txt”); Creates a virtual File object named cancerFile with file path as cancer.txt Scanner cancerData=new Scanner(cancerFile); Creates a Scanner object named cancerData that will allow reading from cancerFile. Now we can use the well known methods to read from cancerFile using the cancerData object. ©Aditya Mathur. CS 180. Fall 2010. Week 653

54 9/30/2010 Writing to a file FileOutputStream dataOut=new FileOutputStream(“cancerData.txt”); Creates an output data stream named dataOut to write to file named cancerData.txt. PrintWriter cancerData=new PrintWriter(out); Creates a PrintWriter object named cancerData that will allow writing to cancerData.txt. Now we can use the well known methods (e.g. println() to write data to cancerData.txt using the cancerData object. ©Aditya Mathur. CS 180. Fall 2010. Week 654

55 9/30/2010 Problem: Statement A drug manufacturing company has developed a new drug for treating a certain type of cancer. The company has performed experiments to determine the effectiveness of this drug in preventing cancer cells from multiplying. Effectiveness data obtained from the experiments is available in a file named cancerData. We need to write a program to obtain the average, minimum and maximum effectiveness of the drug. ©Aditya Mathur. CS 180. Fall 2010. Week 655

56 9/30/2010 Problem: Data format Data obtained from the experiments is available in a file named cancerData in the following format Code name of the drug: a string of characters For each experiment: Percent of cells prevented from multiplying ©Aditya Mathur. CS 180. Fall 2010. Week 656

57 9/30/2010 Algorithm: Initialization Open file cancerData; Get name of the drug; maxEff=0; // Maximum effectiveness minEff=101; // Minimum effectiveness effTotal=0; // Sum of all effectiveness data numExp=0; // Number of data items processed so far ©Aditya Mathur. CS 180. Fall 2010. Week 657

58 9/30/2010 Algorithm: Loop while(data remains to be processed){ eff=get effectiveness from the next experiment; numExp=numExp+1; effTotal=effTotal+eff; if(maxEff<eff) maxEff=eff; if(minEff>eff) minEff=eff; } ©Aditya Mathur. CS 180. Fall 2010. Week 658

59 9/30/2010 Algorithm: Post processing Average eff=effTotal/numExp; Display average, maximum, and minimum effectiveness. // End of algorithm ©Aditya Mathur. CS 180. Fall 2010. Week 659

60 9/30/2010 Java programs for file writing and reading. Live demo. ©Aditya Mathur. CS 180. Fall 2010. Week 660

61 Week 6: September 27-Oct 1, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 9/30/2010©Aditya Mathur. CS 180. Fall 2010. Week 661


Download ppt "CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 6: September 27-Oct 1, 2010 Aditya Mathur Department of Computer Science."

Similar presentations


Ads by Google