NotesWeek5.Sept 20-24 - CS 180 Problem Solving and Object...

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 5: September 2024, 2010 Aditya Mathur 9/20 1. Feedback for Week 4 Department of Computer Science Purdue University West LafayeMe, IN, USA 2. 3. 4. 5. 9/22 6. 7. Quiz Review Straight line vs. "wiggly" programs ifthenelse CondiYons Loops Readings and Exercises for Week 5 Readings: Chapter 3: 3.1, 3.2, 3.3 [Exclude switch statement] Exercises: 3.1, 3.2, 3.3, 3.4 9/22/2010 CS 180. Fall 2010. Week 5 2 Special help session Sunday September 26: 24pm LWSN B158 9/22/2010 CS 180. Fall 2010. Week 5 3 Announcements 1. Project 1 due on Friday September 24. 2. If you wish to parYcipate in the programming compeYYon send mail to ndcao@purdue.edu with the following informaYon: Your name [leader] and a list of team members with their email addresses; team size must be 3 or 4. A brief descripYon of what applicaYon does your team plan to develop. Your app will be for a smart phone or a robot 9/22/2010 CS 180. Fall 2010. Week 5 4 Announcements 3. If you cannot see me during my regular office hours then send me email and we can arrange to meet at another mutually convenient Yme. 4. Work hard! Read Chapters 1, 2, and 3 of the textbook. Solve as many exercises at the end of each chapter. 9/22/2010 CS 180. Fall 2010. Week 5 5 Feedback for Week 4 9/22/2010 CS 180. Fall 2010. Week 5 6 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 4 lab 9/22/2010 CS 180. Fall 2010. Week 5 7 Q2. The recitaYon exercises were useful (Use scale of 110: 10 most useful, 1 not useful at all) (a) 810 (b) 47 (c) 13 (d) Missed 4 recitaYon 9/22/2010 CS 180. Fall 2010. Week 5 8 Q3. The recitaYon instructor was helpful. (Use scale of 110: 10 most helpful, 1 not helpful at all) (a) 810 (b) 47 (c) 13 (d) Missed week 4 recitaYon 9/22/2010 CS 180. Fall 2010. Week 5 9 Q4. I am confident that I will be able to complete and turnin project 1 prior to the September 24 deadline. (a) Yes, I am confident. (b) No, I am not confident. 9/22/2010 CS 180. Fall 2010. Week 5 10 Q5. I understand the meaning of overflow in an arithmeYc expression. (a) Yes (b) No 9/22/2010 CS 180. Fall 2010. Week 5 11 Q6. I understand what is type mismatch. (a) Yes (b) No 9/22/2010 CS 180. Fall 2010. Week 5 12 Q7. So far I am liking the course (10 liking a lot, 1 not liking at all). (a) 810 (b) 47 (c) 13 9/22/2010 CS 180. Fall 2010. Week 5 13 Quiz: 9/20/2010 9/22/2010 CS 180. Fall 2010. Week 5 14 Q1. If double tax=Math.pow(3,2); then the value of tax is (a) 9 (integer) (b) 9.0 (double) (c) 3 (integer) 9/22/2010 CS 180. Fall 2010. Week 5 15 Q2. In the statement Random gen=new Random(); gen denotes (a) a variable of type Random (b) an object of type Random (c) a random number 9/22/2010 CS 180. Fall 2010. Week 5 16 Q3. In the statement Color c= myRose.getColor(); getColor (a) denotes an object (b) is the name of a method (c) is the name of a variable 9/22/2010 CS 180. Fall 2010. Week 5 17 Q4. In the statement Color c= myRose.getColor(); myRose (a) denotes an object (b) is the name of a method (c) is the name of a variable 9/22/2010 CS 180. Fall 2010. Week 5 18 Q5. In the statement double tax= computeTax(salary, taxRate); method computeTax (a) has one parameter (b) has two parameters (c) has no parameters 9/22/2010 CS 180. Fall 2010. Week 5 19 Q6. Suppose z=0 and the following is executed int x=y/z; then which of the following is true? (a) Value of x will be infinity (b) Value of x will be 0 (c) A divide by zero excepYon will be generated 9/22/2010 CS 180. Fall 2010. Week 5 20 Q7. Suppose z=0, and y and z are of type double, and the following is executed double x=y/z; then which of the following is true? (a) Value of x will be infinity (b) Value of x will be 0 (c) A divide by zero excepYon will be generated 9/22/2010 CS 180. Fall 2010. Week 5 21 Q8. Suppose r is a random number between 0 and 1. Which formula is correct to scale r to a number between 5 and 12? (a) r +5 (b) r *5+12 (c) r *7+5 (d) r *5+7 9/22/2010 CS 180. Fall 2010. Week 5 22 End of Quiz: 9/20/2010 Any project related quesYons? 9/22/2010 CS 180. Fall 2010. Week 5 23 Review: First four weeks 9/22/2010 CS 180. Fall 2010. Week 5 24 1. 2. 3. 4. 5. 6. 7. 8. 9. What is a class and how is it defined? What is an object? How to generate an object from a class? What is a primiYve type? What is a declaraYon? What is a variable? What is an assignment statement? What is overflow and when does it occur? When do we get the special numbers "infinity" and "NaN"? 10.How to read from console? 11.How to write onto console? 12.What is algorithmic thinking? 13.Where lies the challenge in programming? 9/22/2010 CS 180. Fall 2010. Week 5 25 CondiYonal execuYon 9/22/2010 CS 180. Fall 2010. Week 5 26 Straight line program: Single path of execuYon A program that has exactly one path that is traversed when the program is executed. Start Execute a statement Execute a statement . . Execute a statement End 9/22/2010 CS 180. Fall 2010. Week 5 27 Single fixed path Program: MulYple paths of execuYon Start Execute a statement true path true condiYon false false path Execute statements Execute statements Execute a statement . . End 9/22/2010 CS 180. Fall 2010. Week 5 28 Example: Obama Tax Plan: The problem Given the net income, compute tax to be paid. 9/22/2010 CS 180. Fall 2010. Week 5 29 Example: Obama Tax Plan: Algorithm (Graphical) Start Get net income true path true income<250000 false false path Use tax rate=0.20 Use tax rate=0.30 Compute tax . . End 9/22/2010 CS 180. Fall 2010. Week 5 30 Example: Obama Tax Plan: Algorithm (Textual) Get net income. if net income < 250000 then tax rate=0.2; else tax rate=0.3; Tax to be paid=tax rate*net income; 9/22/2010 CS 180. Fall 2010. Week 5 31 Example: Traffic Light: The problem Given the current state of a traffic light, find its new state when it is Yme to change. 9/22/2010 CS 180. Fall 2010. Week 5 32 Example: Traffic Light: Algorithm (Textual) Get current state of the traffic light. if current state is Red then set next state to Green; else if current state is Green then set next state to Orange; else Set next state to Red; 9/22/2010 CS 180. Fall 2010. Week 5 33 Example: Traffic light: Algorithm (Graphical) Start How many execuYon paths? Get current state of the traffic light true state is Red? Next state=Green false true state is Green? false Next state=Orange Next state=Red End 9/22/2010 CS 180. Fall 2010. Week 5 34 Example: PaYent Care: The problem Administer medicine to a paYent if it is Yme to do so. 9/22/2010 CS 180. Fall 2010. Week 5 35 Example: PaYent Care: Algorithm (Textual) Get current Yme. Get Yme when the paYent is to be given medicine. if current Yme>= Yme to give medicine then give medicine to the paYent else do something else; 9/22/2010 CS 180. Fall 2010. Week 5 36 Example: PaYent care: Algorithm [Graphical] Start Get current Yme of the day true path true Time to administer medicine? false false path Give medicine Do something else End 9/22/2010 CS 180. Fall 2010. Week 5 37 Example: DJ: The problem Play music given the genre of user's choice (R&B or classical). 9/22/2010 CS 180. Fall 2010. Week 5 38 Example: DJ: Algorithm (Textual) Get genre requested. if requested genre is R&B then play Byonc's "Put a ring on it" else if requested genre is classical then play Mozart's "Eine Kleine Nacht Music" else Say "sorry I do not have the right music for you." 9/22/2010 CS 180. Fall 2010. Week 5 39 Example: DJ: Algorithm (Graphical) Start Get request true Play Byonc R&B? false true Play Mozart Classical false Apologize How many execuYon paths? End 9/22/2010 CS 180. Fall 2010. Week 5 40 CondiYonal execuYon in Java: Obama tax Plan Get net income. double netIncome=source.getDouble(); double taxRate, taxPaid; if (netIncome < 250000) { taxRate=0.2; } else { taxRate=0.3; } taxPaid=taxRate*netIncome; if net income < 250000 then tax rate=0.2; else tax rate=0.3; Tax to be paid= tax rate*net income; 9/22/2010 CS 180. Fall 2010. Week 5 41 CondiYonal execuYon in Java: Traffic Light Get current state of the traffic light. if current state is Red then set next state to Green; else if current State is Green then set next state to Orange; else Set next state to Red; String lightStatus=source.next(); if (lightStatus.equals("Red")) { lightStatus="Green"; } else { if (lightStatus.equals("Green")) { lightStatus="Orange"; }else{ lightStatus="Red"; } } 42 9/22/2010 CS 180. Fall 2010. Week 5 CondiYonal ExecuYon in Java: Live demo 9/22/2010 CS 180. Fall 2010. Week 5 43 Three quesYons from Monday's class 9/22/2010 CS 180. Fall 2010. Week 5 44 Class ordering in a file Q1: Can we place two classes in any order in a file? A: (a) If the two classes are disjoint, and public, then they must be placed in separate files. Hence order does not maMer. (b) If one class is inside another (inner class) then too the order does not maMer. The outermost class will be saved in a file. 9/22/2010 CS 180. Fall 2010. Week 5 45 Example: Class order public class OrderTest{ public staYc void main(String args){ new AnotherOrderTest(); } } class AnotherOrderTest{ Note: this is not public } If AnotherOrderTest is made public then it must be in its own file. 9/22/2010 CS 180. Fall 2010. Week 5 46 AudioClip: Constructor Q2: Does AudioClip have a constructor? A: No, it does not. AudioClip is an interface. We will discuss interfaces later in this course. 9/22/2010 CS 180. Fall 2010. Week 5 47 Loop() Q3: Is loop() executed in a separate thread? A: Yes. Check it out by removing the stop() method from the example we discussed on Monday. You will note that the main() method has terminated but the sound conYnues to play! 9/22/2010 CS 180. Fall 2010. Week 5 48 CondiYons 9/22/2010 CS 180. Fall 2010. Week 5 49 CondiYons: Simple An expression that evaluates to true or false. Examples: int x, y, z ; boolean b; true False x<y x>y+z Math.pow(x,3)>=y z*zx==y x+3<=y !b 9/22/2010 CS 180. Fall 2010. Week 5 50 CondiYons: Compound An expression that contains two or more simple condiYons and evaluates to true or false. Examples: int x, y, z; boolean b; String s1, s2; x<y &&y==z (x>y+z) || (s1.equals(s2)) Math.pow(x,3)>=y && !(s1.equals(s2)) (z*(zx))!=y (x+3)<=y && b !b || y==0 9/22/2010 CS 180. Fall 2010. Week 5 51 CondiYons: Dangerous! But survived int x, y, p; Desired if (x==y){ p=1; }else { p=2; } Actual if (x=y){ p=1; }else { p=2; } Compiler error! Not a problem, the program will not execute. 9/22/2010 CS 180. Fall 2010. Week 5 52 CondiYons: Dangerous! May not survive int x, y, p; boolean door1Open=true, door2Open=false; Desired if (door1Open==door2Open){ p=1; }else { p=2; } Actual if (door1Open=door2Open){ p=1; }else { p=2; } No compiler error! Program will execute. What is the value of p? As expected? Then what is the problem? 9/22/2010 CS 180. Fall 2010. Week 5 53 Quiz: 9/22/2010 9/22/2010 CS 180. Fall 2010. Week 5 54 Q1. Value of x a|er execuYng the following ? if (true) { x=1; }else{ x=2; } (a) 2 (b) 1 (c) 3 9/22/2010 CS 180. Fall 2010. Week 5 55 Q2. Value of z a|er execuYng the following? int x=10, y=15, z=7; if (x+1==y || z>=0) { z=x+1; }else{ z=y+1; }; (a) 11 (b) 7 (c) 16 9/22/2010 CS 180. Fall 2010. Week 5 56 Q3. Value of z a|er execuYng the following? int x=10, y=15, z=7; if (x+1==y && z>=0) { z=x+1; }else{ z=y+1; }; (a) 11 (b) 7 (c) 16 9/22/2010 CS 180. Fall 2010. Week 5 57 Q4. Value of z a|er execuYng the following? int x=10, y=15; int z; if ((x+1==y) && y==15) { int z=x+1; }else{ int z=y+1; }; (a) 11 (b) 16 (c) None of the above 9/22/2010 CS 180. Fall 2010. Week 5 58 Q5. Value of x a|er execuYng the following? int x=10, y=15; if ((x+1==y) && y==15) { int z=x+1; }else{ int z=y+1; }; x=z; (a) 11 (b) 16 (c) None of the above 9/22/2010 CS 180. Fall 2010. Week 5 59 Q6. Value of x a|er execuYng the following? int x=10, y=15; if ((x+1==y) && y==15) { int z=x+1; }else{ int z=y+1; }; int z=9; (a) 19 x=x+z; (b) 20 (c) None of the above 9/22/2010 CS 180. Fall 2010. Week 5 60 Loops 9/22/2010 CS 180. Fall 2010. Week 5 61 Problem Monitor the pulse rate of a paYent every 5 seconds. If the pulse rate is between 60 and 100 then display "Normal". If the rate is below 60, then display "Low". If the rate is above 100 then display "High". 9/22/2010 CS 180. Fall 2010. Week 5 62 Algorithm (Graphical) Start false if (c) Range: 60100 true Get pulse rate (pR) Yes Display "Normal" pR in range No true Display "Low" What should be c? pR<60 false Display "High" Wait for 5 seconds" 9/22/2010 End CS 180. Fall 2010. Week 5 63 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/22/2010 CS 180. Fall 2010. Week 5 64 Problem We are given yearly rainfall data for a region. Write a program to compute the average 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/22/2010 CS 180. Fall 2010. Week 5 65 Algorithm [Textual] IniYalize total rainfall to 0; IniYalize count to 0; Get next rainfall data item; let us denote it by r. while (r>=0){ Add r to total rainfall; Add 1 to count; Get next rainfall data item; } average rainfall=r/count; // Is there a problem here? 9/22/2010 CS 180. Fall 2010. Week 5 66 Algorithm (Graphical) Start IniYalize total rainfall to 0; IniYalize count to 0; Get rainfall data item; denote it by r. false Any problem here? r>=0 true average=r/count; Display average End Add r to total rainfall; Add 1 to count; Get rainfall data item CS 180. Fall 2010. Week 5 9/22/2010 67 Loops in Java Start IniYalizaYon false iniYalizaYon statements; while (condiYon){ statements; } condiYon true statements statements End CS 180. Fall 2010. Week 5 9/22/2010 statements; 68 Java program for pulse rate monitoring and display. Live demo. 9/22/2010 CS 180. Fall 2010. Week 5 69 End of Quiz: 9/22/2010 Any project related quesYons? 9/22/2010 CS 180. Fall 2010. Week 5 70 Week 5: September 2024, 2010 Hope you enjoyed this week! QuesYons? Contact your recitaYon instructor. Make full use of our office hours. 9/22/2010 CS 180. Fall 2010. Week 5 71 ...
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 University-West Lafayette.

Ask a homework question - tutors are online