CS 180 Problem Solving and Object Oriented Programming Spring 2011
February 16, 2011
Aditya MathurDepartment of Computer SciencePurdue UniversityWest Lafayette, IN, USA
http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/
1. Quiz2. Review3. Straight line vs. “wiggly” programs4. if-then-else5. Conditions6. Loops
Today:
©Aditya Mathur. CS 180. Spring 2011 2
Readings and Exercises
Readings:Chapter 3: 3.1, 3.2, 3.3, 4.1-4.4
Exercises: 3.1, 3.2, 3.3, 3.4, 4.2, (4.5 or 4.6)
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 4
Q1. If double tax=Math.pow(3,2);then the value of tax is
(a) 9 (integer)
(b) 9.0 (double)
(c) 3 (integer)
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 5
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
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 6
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
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 7
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
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 8
Q5. In the statement double tax= computeTax(salary, taxRate); method computeTax
(a) has one parameter
(b) has two parameters
(c) has no parameters
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 9
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
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 10
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
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 11
Q8. Suppose r is a random number between 0 and 1
[inclusive]. 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
2/16/2011
©Aditya Mathur. CS 180. Spring 2011 14
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?2/16/2011
©Aditya Mathur. CS 180. Spring 2011 16
Straight line program: Single path of execution
2/16/2011
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
.
.
Single fixed path
©Aditya Mathur. CS 180. Spring 2011 17
Program: Multiple paths of execution
2/16/2011
Start
Execute a statement
Execute statements
Execute a statement
End
.
.
condition
Execute statements
true false
true pathfalse path
©Aditya Mathur. CS 180. Spring 2011 18
Example: Obama Tax Plan [No longer valid]: The problem
2/16/2011
Given the net income, compute tax to be paid.
©Aditya Mathur. CS 180. Spring 2011 19
Example: Obama Tax Plan: Algorithm (Graphical)
2/16/2011
Start
Get net income
Use tax rate=0.30
Compute tax
End
..
income<250000
Use tax rate=0.20
true false
true path false path
©Aditya Mathur. CS 180. Spring 2011 20
Example: Obama Tax Plan: Algorithm (Textual)
2/16/2011
Get net income.
if net income < 250000 thentax rate=0.2;
else tax rate=0.3;
Tax to be paid=tax rate*net income;
©Aditya Mathur. CS 180. Spring 2011 21
Example: Traffic Light: The problem
2/16/2011
Given the current state of a traffic light, find its new state when it is time to change.
©Aditya Mathur. CS 180. Spring 2011 22
Example: Traffic Light: Algorithm (Textual)
2/16/2011
Get current state of the traffic light.
if current state is Red thenset next state to Green;
else if current state is Green
thenset next state to
Orange;else
Set next state to Red;
©Aditya Mathur. CS 180. Spring 2011 23
Example: Traffic light: Algorithm (Graphical)
2/16/2011
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?
true false
©Aditya Mathur. CS 180. Spring 2011 24
Example: Patient Care: The problem
2/16/2011
Administer medicine to a patient if it is time to do so.
©Aditya Mathur. CS 180. Spring 2011 25
Example: Patient Care: Algorithm (Textual)
2/16/2011
Get current time.
if current time>= time to give medicine thengive medicine to the patient
else do something else;
Get time when the patient is to be given medicine.
©Aditya Mathur. CS 180. Spring 2011 26
Example: Patient care: Algorithm [Graphical]
2/16/2011
Start
Get current time of the day
Do something else
End
Time to administer medicine?
Give medicine
true false
true pathfalse path
©Aditya Mathur. CS 180. Spring 2011 27
Example: DJ: The problem
2/16/2011
Play music given the genre of user’s choice (R&B or classical).
©Aditya Mathur. CS 180. Spring 2011 28
Example: DJ: Algorithm (Textual)
2/16/2011
Get genre requested.
if requested genre is R&B thenplay 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. Spring 2011 29
Example: DJ: Algorithm (Graphical)
2/16/2011
Start
Get request
End
Play Mozart
true false
Classical
R&B?
Play Byoncè
Apologize
How many execution paths?
true false
©Aditya Mathur. CS 180. Spring 2011 30
Conditional execution in Java: Obama tax Plan
2/16/2011
Get net income.
if net income < 250000 thentax 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 < 250000) {
taxRate=0.2;}else {
taxRate=0.3;}
taxPaid=taxRate*netIncome;
©Aditya Mathur. CS 180. Spring 2011 31
Conditional execution in Java: Traffic Light
2/16/2011
Get current state of the traffic light.
if current state is Red thenset next state to Green;
else if current State is Green
thenset 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. Spring 2011 33
Conditions: Simple
2/16/2011
An expression that evaluates to true or false.
int x, y, z ; boolean b;
trueFalsex<yx>y+zMath.pow(x,3)>=yz*z-x==yx+3<=y!b
Examples:
©Aditya Mathur. CS 180. Spring 2011 34
Conditions: Compound
2/16/2011
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. Spring 2011 35
Conditions: Dangerous! But survived
2/16/2011
int x, y, p;
if (x=y){p=1;
}else {p=2;
}
if (x==y){p=1;
}else {p=2;
}
Desired Actual
Compiler error! Not a problem, the program will not execute.
©Aditya Mathur. CS 180. Spring 2011 36
Conditions: Dangerous! May not survive
2/16/2011
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;
}
Desired Actual
No compiler error! Program will execute.What is the value of p? As expected? Then what is the problem?
©Aditya Mathur. CS 180. Spring 2011 38
Q1. Value of x after executing the following ?
(a) 2
(b) 1
(c) 3
2/16/2011
if (true) {x=1;
}else{x=2;
}
©Aditya Mathur. CS 180. Spring 2011 39
Q2. Value of z after executing the following?
(a) 11
(b) 7
(c) 16
2/16/2011
int x=10, y=15, z=7;if (x+1==y || z>=0) {
z=x+1;}else{
z=y+1;};
©Aditya Mathur. CS 180. Spring 2011 40
Q3. Value of z after executing the following?
(a) 11
(b) 7
(c) 16
2/16/2011
int x=10, y=15, z=7;if (x+1==y && z>=0) {
z=x+1;}else{
z=y+1;};
©Aditya Mathur. CS 180. Spring 2011 41
Q4. Value of z after executing the following?
(a) 11
(b) 16
(c) None of the above
2/16/2011
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. Spring 2011 42
Q5. Value of x after executing the following?
(a) 11
(b) 16
(c) None of the above
2/16/2011
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. Spring 2011 43
Q6. Value of x after executing the following?
(a) 19
(b) 20
(c) None of the above
2/16/2011
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. Spring 2011 45
Problem
2/16/2011
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. Spring 2011 46
Algorithm (Graphical)
2/16/2011
Start
if (c)
Display “Low”
Yes No
pR<60
pR in range
Display “Normal”
Display “High”
true false
Get pulse rate (pR)true
false
End
Range: 60-100
What should be c?
Wait for 5 seconds”
©Aditya Mathur. CS 180. Spring 2011 47
Algorithm [Textual]
2/16/2011
do forever {get pulse rate;if the pulse rate is between 60 and 100 then
Display “Normal”;else
if the pulse rate is<60 thenDisplay “Low”;
elseDisplay “High”;
Wait for 5 seconds;}
©Aditya Mathur. CS 180. Spring 2011 48
Problem
2/16/2011
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. Spring 2011 49
Algorithm [Textual]
2/16/2011
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. Spring 2011 50
Algorithm (Graphical)
2/16/2011
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
falseAny problem here?
©Aditya Mathur. CS 180. Spring 2011 51
Loops in Java
2/16/2011
Start
condition
statements
Initialization
true
statements
false
End
initialization statements;
while (condition){statements;
}
statements;
©Aditya Mathur. CS 180. Spring 2011 532/16/2011
Java program for pulse rate monitoring and display. Live demo.
Top Related