OOJ Lecture 12
OOJ Lecture 12
Covers
Nested ifelse statements Multiway branching statements
12/1
12/2
Example
Voting example
Rule: You can vote in Australia if you are an Australian citizen and 18 years of age or older. Write a program that asks the user whether they are a citizen. If they are not, display You are not eligible to vote . Otherwise, ask for their age, and display You are too young to vote or You are eligible to vote as the case may be.
12/4
Example
Algorithm
Get users citizenship IF NOT Australian citizen THEN Display You are not eligible to vote ELSE Get users age IF younger than 18 THEN Display You are too young to vote ELSE Display You are eligible to vote ENDIF ENDIF
12/5
public static void main(String[ ] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Are you a citizen of Australia? "); String citizenString = keyboard.nextLine(); char citizen = citizenString.charAt(0); if (citizen != 'y') { System.out.println("You are not eligible to vote"); } else { System.out.print("How old are you? "); int age = keyboard.nextInt( ); if (age < 18) { System.out.println("You are too young to vote"); } else { System.out.println("You are eligible to vote"); } } }
12/6
Example
Program output
Are you a citizen of Australia? yes How old are you? 31 You are eligible to vote Are you a citizen of Australia? no You are not eligible to vote Are you a citizen of Australia? yes How old are you? 16 You are too young to vote 12/7
12/8
When an ifelse structure is created to decide between more than two mutually exclusive paths, it is termed a multiway branching statement
12/9
Exercise
Problem
Write Java code that outputs pass if the integer variable mark is at least 50, that outputs special exam if mark is less than 50 and the variable special is true, and outputs fail if mark is less than 50 and special is false
12/10
Exercise
Solution
if (mark >= 50) { System.out.println("pass"); } else { if (special == true) System.out.println("special exam"); else System.out.println("fail"); }
12/11
Exercise
Minor improvements
implicit == operator and left-aligned format
if (mark >= 50) { System.out.println("pass"); } else if (special) { System.out.println("special exam"); } else { System.out.println("fail"); }
12/12
Class exercise
int extra = 2; if (extra < 0) { System.out.println("small"); } else if (extra == 0) { System.out.println("medium"); } else { System.out.println("large"); }
12/13
12/14
Class exercise
int x = 2; System.out.println("Start"); if (x <= 3) if (x != 0) System.out.println("Hello from the second if"); else System.out.println("Hello from the else"); System.out.println("End"); 12/15
Class exercise
int x = 4; System.out.println("Start"); if (x <= 3) if (x != 0) System.out.println("Hello from the second if"); else System.out.println("Hello from the else"); System.out.println("End"); 12/16
Class exercise
int x = 4; System.out.println("Start"); if (x <= 3) if (x != 0) System.out.println("Hello from the second if"); else System.out.println("Hello from the else"); System.out.println("End"); 12/17
Matching elses
Each else is matched with the nearest unmatched if It can become confusing to work out which else matches which if, especially when some if statements do not have an else part Always bracket your if statements for clarity and readability
12/18
12/19
Class exercise
Problem
Write a multiway ifelse statement that classifies the value of an integer variable n into either less than 0, between 0 and 100 inclusive, and greater than 100 Print sensible messages
12/20
Solution
12/21
Example
The integer variable month contains a value representing a month of the year: 1 = January, 2 = February, , 12 = December Write a multiway ifelse statement to display the name of the month based on the content of the variable month If month does not contain an integer value between 1 and 12, an error message should be displayed instead
12/22
Example
import java.util.*; public class MonthName { public static void main(String[ ] args) { Scanner keyboard = new Scanner(System.in); int month; System.out.println("Enter the month [1-12]"); month = keyboard.nextInt( ); if (month == 1) { System.out.println("January"); } else if (month == 2) { System.out.println("February"); } else if (month == 3) { System.out.println("March"); }
12/23
Example
else if (month == 4) { System.out.println("April"); } else if (month == 5) { System.out.println("May"); } else if (month == 6) { System.out.println("June"); } else if (month == 7) { System.out.println("July"); } else if (month == 8) { System.out.println("August"); }
12/24
Example
else if (month == 9) { System.out.println("September"); } else if (month == 10) { System.out.println("October"); } else if (month == 11) { System.out.println("November"); } else if (month == 12) { System.out.println("December"); } else { System.out.println("Not a valid month"); } } }
12/25
Class exercise
Write a Java code segment that outputs to screen the message Red Dwarf, White Star or Blue Moon depending on whether the value of the String object hue is red, white or blue respectively. If hue has any other value, output Black Hole.
12/26
Solution
12/27
Class exercise
Write a Java code segment that checks if an integer variable key is odd or even.
If the variable is even and divisible by 4, output to screen the message even again, otherwise output only even. If the variable is odd and negative, output to screen the message negatively odd, otherwise output positively odd.
12/28
Solution
12/29
Next lecture
Blocks and scope Branching statements
12/30