week-04-handout

# week-04-handout - COMP 202 Week 4 Conditional Programming...

This preview shows pages 1–5. Sign up to view the full content.

1 COMP 202 – Introduction to Computing 1 COMP 202- Week 4 1 COMP 202 – Week 4 Conditional Programming Learning Outcomes : • Using conditions in program to make decisions • Compose complex conditions using logical operators • Different Java syntaxes for conditional programming • User interface and the Input-Process-Output flow COMP 202 – Introduction to Computing 1 COMP 202- Week 4 2 Last week review Class and Object, Library Classes How to create object of a specific Class and how to use its member methods and variables • Example - Scanner kb = new Scanner ( System . in ); int x = kb.nextInt() double area, radius; area = Math . PI * Math . pow (radius, 2); COMP 202 – Introduction to Computing 1 COMP 202- Week 4 3 Programming with conditions Do some common things If some specific condition is true Do something special Do some other common things Example (Weather forecaster) – Input temperature Input humidity calculate feels_like_temperature if ( feels_like_temperature is more than 40) generate warning Printout temperature , humidity and feels_like_temperature

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 COMP 202 – Introduction to Computing 1 COMP 202- Week 4 4 More example (computing salary) Given the number of hours you worked and the hourly rate as input, compute your weekly salary. Condition is that, if you have worked more than 40 hours, then you will get 50% additional salary for the hours in excess of 40. Figure out the steps to solve this problem COMP 202 – Introduction to Computing 1 COMP 202- Week 4 5 Computing Salary Solution • Input total_hours, hourly_rate • If (worked more than 40 hours) additional_hours Å total_hours – 40 regular_salary Å total_hours * hourly_rate overtime_bonus Å additional_hours * 0.5 total_salary Å regular_salary + overtime_bonus COMP 202 – Introduction to Computing 1 COMP 202- Week 4 6 Programming with conditions If (some condition is true) Do this Otherwise Do something else • Example- If (train travel time is lower than car travel time) print (“Better travel by car”) Otherwise print(“Better travel by train”)