CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 5: September 20-24, 2010 Aditya Mathur Department of Computer Science Purdue.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

CS107 Introduction to Computer Science Lecture 2.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Logic & program control part 3: Compound selection structures.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 180 Problem Solving and Object Oriented Programming Spring 2011 February 16, 2011 Aditya Mathur Department of Computer Science Purdue University West.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 5: September 19-23, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 13: Nov 14-18, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 10: Oct 24-28, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 8: Oct 10-14, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 9: Oct 17-21, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for the Final Lecture Dec 7, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 6: September 26-30, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update:
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
CS 101E – Exam 2 Review Spring 2007 Michele Co. Announcements Review Session Tonight, 7/7:30 p.m., OLS 009 Will be announced via In-class Exam Wednesday.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
What does a computer program look like: a general overview.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 11: Nov 1-5, 2010 Aditya Mathur Department of Computer Science Purdue University.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue.
Digesting a big problem ONE BYTE AT A TIME. Quiz YOU WILL WORK WITH YOUR PARTNER (LAB PARTNER NOT FULL TEAM). FOR SOLOISTS OR THOSE WHOSE PARTNER IS NOT.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 10: Oct 25-29, 2010 Aditya Mathur Department of Computer Science Purdue.
A High Flying Overview CS139 – Fall 2006 How far we have come.
int [] scores = new int [10];
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CISC105 – General Computer Science Class 4 – 06/14/2006.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Problem of the Day  Why are manhole covers round?
A High Flying Overview CS139 – Fall 2010 How far we have come.
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.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Repetition Statements
CE221 – Data Structures & Algorithms Lab Introduction
User input We’ve seen how to use the standard output buffer
Outline Altering flow of control Boolean expressions
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Just Enough Java 17-May-19.
Loops and Iteration CS 21a: Introduction to Computing I
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 5: September 20-24, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA 1.Feedback for Week 4 2.Quiz 3.Review 4.Straight line vs. “wiggly” programs 5.if-then-else 6.Conditions 7.Loops 9/20 9/22 This Week:

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©Aditya Mathur. CS 180. Fall Week 52

Special help session Sunday September 26: 2-4pm LWSN B158 9/22/2010©Aditya Mathur. CS 180. Fall Week 53

Announcements 1.Project 1 due on Friday September If you wish to participate in the programming competition send mail to with the following information: Your name [leader] and a list of team members with their 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/22/2010©Aditya Mathur. CS 180. Fall Week 54

Announcements 3.If you cannot see me during my regular office hours then send me and we can arrange to meet at another mutually convenient time. 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©Aditya Mathur. CS 180. Fall Week 55

Feedback for Week 4 9/22/2010©Aditya Mathur. CS 180. Fall Week 56

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 4 lab 9/22/2010©Aditya Mathur. CS 180. Fall Week 57

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 4 recitation 9/22/2010©Aditya Mathur. CS 180. Fall Week 58

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 4 recitation 9/22/2010©Aditya Mathur. CS 180. Fall Week 59

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©Aditya Mathur. CS 180. Fall Week 510

Q5. I understand the meaning of overflow in an arithmetic expression. (a)Yes (b)No 9/22/2010©Aditya Mathur. CS 180. Fall Week 511

Q6. I understand what is type mismatch. (a)Yes (b)No 9/22/2010©Aditya Mathur. CS 180. Fall Week 512

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/22/2010©Aditya Mathur. CS 180. Fall Week 513

Quiz: 9/20/2010 9/22/2010©Aditya Mathur. CS 180. Fall Week 514

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©Aditya Mathur. CS 180. Fall Week 515

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©Aditya Mathur. CS 180. Fall Week 516

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©Aditya Mathur. CS 180. Fall Week 517

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©Aditya Mathur. CS 180. Fall Week 518

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©Aditya Mathur. CS 180. Fall Week 519

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 exception will be generated 9/22/2010©Aditya Mathur. CS 180. Fall Week 520

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 exception will be generated 9/22/2010©Aditya Mathur. CS 180. Fall Week 521

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©Aditya Mathur. CS 180. Fall Week 522

End of Quiz: 9/20/2010 Any project related questions? 9/22/2010©Aditya Mathur. CS 180. Fall Week 523

Review: First four weeks 9/22/2010©Aditya Mathur. CS 180. Fall Week 524

1.What is a class and how is it defined? 2.What is an object? 3.How to generate an object from a class? 4.What is a primitive type? 5.What is a declaration? 6.What is a variable? 7.What is an assignment statement? 8.What is overflow and when does it occur? 9.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©Aditya Mathur. CS 180. Fall Week 525

Conditional execution 9/22/2010©Aditya Mathur. CS 180. Fall Week 526

Straight line program: Single path of execution 9/22/2010 A program that has exactly one path that is traversed when the program is executed. Start Execute a statement End.. Single fixed path ©Aditya Mathur. CS 180. Fall Week 527

Program: Multiple paths of execution 9/22/2010 Start Execute a statement Execute statements Execute a statement End.. condition Execute statements true false true path false path ©Aditya Mathur. CS 180. Fall Week 528

Example: Obama Tax Plan: The problem 9/22/2010 Given the net income, compute tax to be paid. ©Aditya Mathur. CS 180. Fall Week 529

Example: Obama Tax Plan: Algorithm (Graphical) 9/22/2010 Start Get net income Use tax rate=0.30 Compute tax End.. income< Use tax rate=0.20 true false true path false path ©Aditya Mathur. CS 180. Fall Week 530

Example: Obama Tax Plan: Algorithm (Textual) 9/22/2010 Get net income. if net income < then tax rate=0.2; else tax rate=0.3; Tax to be paid=tax rate*net income; ©Aditya Mathur. CS 180. Fall Week 531

Example: Traffic Light: The problem 9/22/2010 Given the current state of a traffic light, find its new state when it is time to change. ©Aditya Mathur. CS 180. Fall Week 532

Example: Traffic Light: Algorithm (Textual) 9/22/2010 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; ©Aditya Mathur. CS 180. Fall Week 533

Example: Traffic light: Algorithm (Graphical) 9/22/2010 Start Get current state of the traffic light End Next state=Orange true false state is Green? state is Red? Next state=Green Next state=Red How many execution paths? truefalse ©Aditya Mathur. CS 180. Fall Week 534

Example: Patient Care: The problem 9/22/2010 Administer medicine to a patient if it is time to do so. ©Aditya Mathur. CS 180. Fall Week 535

Example: Patient Care: Algorithm (Textual) 9/22/2010 Get current time. if current time>= time to give medicine then give medicine to the patient else do something else; Get time when the patient is to be given medicine. ©Aditya Mathur. CS 180. Fall Week 536

Example: Patient care: Algorithm [Graphical] 9/22/2010 Start Get current time of the day Do something else End Time to administer medicine? Give medicine true false true path false path ©Aditya Mathur. CS 180. Fall Week 537

Example: DJ: The problem 9/22/2010 Play music given the genre of user’s choice (R&B or classical). ©Aditya Mathur. CS 180. Fall Week 538

Example: DJ: Algorithm (Textual) 9/22/2010 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.” ©Aditya Mathur. CS 180. Fall Week 539

Example: DJ: Algorithm (Graphical) 9/22/2010 Start Get request End Play Mozart true false Classical R&B? Play Byoncè Apologize How many execution paths? truefalse ©Aditya Mathur. CS 180. Fall Week 540

Conditional execution in Java: Obama tax Plan 9/22/2010 Get net income. if net income < then tax rate=0.2; else tax rate=0.3; Tax to be paid= tax rate*net income; double netIncome=source.getDouble(); double taxRate, taxPaid; if (netIncome < ) { taxRate=0.2; } else { taxRate=0.3; } taxPaid=taxRate*netIncome; ©Aditya Mathur. CS 180. Fall Week 541

Conditional execution in Java: Traffic Light 9/22/2010 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”; } ©Aditya Mathur. CS 180. Fall Week 542

Conditional Execution in Java: Live demo 9/22/2010©Aditya Mathur. CS 180. Fall Week 543

Three questions from Monday’s class 9/22/2010©Aditya Mathur. CS 180. Fall Week 544

Class ordering in a file 9/22/2010 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 matter. (b) If one class is inside another (inner class) then too the order does not matter. The outermost class will be saved in a file. ©Aditya Mathur. CS 180. Fall Week 545

Example: Class order 9/22/2010 public class OrderTest{ public static 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. ©Aditya Mathur. CS 180. Fall Week 546

AudioClip: Constructor 9/22/2010 Q2: Does AudioClip have a constructor? A: No, it does not. AudioClip is an interface. We will discuss interfaces later in this course. ©Aditya Mathur. CS 180. Fall Week 547

Loop() 9/22/2010 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 continues to play! ©Aditya Mathur. CS 180. Fall Week 548

Conditions 9/22/2010©Aditya Mathur. CS 180. Fall Week 549

Conditions: Simple 9/22/2010 An expression that evaluates to true or false. int x, y, z ; boolean b; true False x<y x>y+z Math.pow(x,3)>=y z*z-x==y x+3<=y !b Examples : ©Aditya Mathur. CS 180. Fall Week 550

Conditions: Compound 9/22/2010 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*(z-x))!=y (x+3)<=y && b !b || y==0 Examples : An expression that contains two or more simple conditions and evaluates to true or false. ©Aditya Mathur. CS 180. Fall Week 551

Conditions: Dangerous! But survived 9/22/2010 int x, y, p; if (x=y){ p=1; }else { p=2; } if (x==y){ p=1; }else { p=2; } DesiredActual Compiler error! Not a problem, the program will not execute. ©Aditya Mathur. CS 180. Fall Week 552

Conditions: Dangerous! May not survive 9/22/2010 int x, y, p; boolean door1Open=true, door2Open=false; if (door1Open=door2Open){ p=1; }else { p=2; } if (door1Open==door2Open){ p=1; }else { p=2; } DesiredActual No compiler error! Program will execute. What is the value of p? As expected? Then what is the problem? ©Aditya Mathur. CS 180. Fall Week 553

Quiz: 9/22/2010 9/22/2010©Aditya Mathur. CS 180. Fall Week 554

Q1. Value of x after executing the following ? (a) 2 (b) 1 (c) 3 9/22/2010 if (true) { x=1; }else{ x=2; } ©Aditya Mathur. CS 180. Fall Week 555

Q2. Value of z after executing the following? (a) 11 (b) 7 (c) 16 9/22/2010 int x=10, y=15, z=7; if (x+1==y || z>=0) { z=x+1; }else{ z=y+1; }; ©Aditya Mathur. CS 180. Fall Week 556

Q3. Value of z after executing the following? (a) 11 (b) 7 (c) 16 9/22/2010 int x=10, y=15, z=7; if (x+1==y && z>=0) { z=x+1; }else{ z=y+1; }; ©Aditya Mathur. CS 180. Fall Week 557

Q4. Value of z after executing the following? (a) 11 (b) 16 (c) None of the above 9/22/2010 int x=10, y=15; int z; if ((x+1==y) && y==15) { int z=x+1; }else{ int z=y+1; }; ©Aditya Mathur. CS 180. Fall Week 558

Q5. Value of x after executing the following? (a) 11 (b) 16 (c) None of the above 9/22/2010 int x=10, y=15; if ((x+1==y) && y==15) { int z=x+1; }else{ int z=y+1; }; x=z; ©Aditya Mathur. CS 180. Fall Week 559

Q6. Value of x after executing the following? (a) 19 (b) 20 (c) None of the above 9/22/2010 int x=10, y=15; if ((x+1==y) && y==15) { int z=x+1; }else{ int z=y+1; }; int z=9; x=x+z; ©Aditya Mathur. CS 180. Fall Week 560

Loops 9/22/2010©Aditya Mathur. CS 180. Fall Week 561

Problem 9/22/2010 Monitor the pulse rate of a patient 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”. ©Aditya Mathur. CS 180. Fall Week 562

Algorithm (Graphical) 9/22/2010 Start if (c) Display “Low” Yes No pR<60 pR in range Display “Normal” Display “High” truefalse Get pulse rate (pR) true false End Range: What should be c? Wait for 5 seconds” ©Aditya Mathur. CS 180. Fall Week 563

Algorithm [Textual] 9/22/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 Week 564

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

Algorithm [Textual] 9/22/2010 Initialize total rainfall to 0; Initialize 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=total rainfall/count; // Is there a problem here? ©Aditya Mathur. CS 180. Fall Week 566

Algorithm (Graphical) 9/22/2010 Start r>=0 average=total rainfall/count; Initialize total rainfall to 0; Initialize count to 0; true End Get rainfall data item; denote it by r. Add r to total rainfall; Add 1 to count; Get rainfall data item Display average false Any problem here? ©Aditya Mathur. CS 180. Fall Week 567

Loops in Java 9/22/2010 Start condition statements Initialization true statements false End initialization statements; while (condition){ statements; } statements; ©Aditya Mathur. CS 180. Fall Week 568

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

Week 5: September 20-24, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 9/22/2010©Aditya Mathur. CS 180. Fall Week 570