week-04-handout

week-04-handout - 1 COMP 202 –Introduction to Computing 1...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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”) 3 COMP 202 –Introduction to Computing 1 COMP 202 - Week 4 7 Java specific syntax The if Statement The if-else Statement COMP 202 –Introduction to Computing 1 COMP 202 - Week 4 8 The if Statement • The if statement has the following syntax: if ( condition ) statement ; if is a Java reserved word The condition must be a boolean expression . It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped. COMP 202 –Introduction to Computing 1 COMP 202 - Week 4 9 Control flow of an if statement condition evaluated false statement true 4 COMP 202 –Introduction to Computing 1 COMP 202 - Week 4 10 Example: Salary.java import java.util.Scanner; public class Salary { public static void main (String args) { final double RATE = 8.25; // regular pay rate double regular_salary = 0.0, bonus = 0.0;double regular_salary = 0....
View Full Document

This note was uploaded on 06/28/2008 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 23

week-04-handout - 1 COMP 202 –Introduction to Computing 1...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online