1COMP 202 – Introduction to Computing 1COMP 202-Week 41COMP 202 – Week 4Conditional ProgrammingLearning 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 flowCOMP 202 – Introduction to Computing 1COMP 202-Week 42Last 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-Scannerkb = new Scanner(System.in);int x = kb.nextInt()double area, radius;area = Math.PI* Math.pow(radius, 2);COMP 202 – Introduction to Computing 1COMP 202-Week 43Programming with conditions•Do some common thingsIf some specific condition is trueDo something specialDo some other common things•Example (Weather forecaster) –Input temperatureInput humiditycalculate feels_like_temperatureif (feels_like_temperatureis more than 40)generate warningPrintout temperature, humidityand feels_like_temperature
has intentionally blurred sections.
Sign up to view the full version.
2COMP 202 – Introduction to Computing 1COMP 202-Week 44More 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 1COMP 202-Week 45Computing SalarySolution• 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_bonusCOMP 202 – Introduction to Computing 1COMP 202-Week 46Programming with conditions•If (some condition is true)–Do thisOtherwise –Do something else• Example-If (train travel time is lower than car travel time)print (“Better travel by car”)Otherwiseprint(“Better travel by train”)