Flow of Control Chapter 3 Part 3 The Switch Statement
Agenda Hw 03 comments Review of Ch03 - Parts 1 & 2 Conditional operator I/O of boolean values The switch statement Random numbers Methods with arguments - Chapter 03: Branching Slide # 2
The Conditional Operator 1/2 Used instead of writing a simple if statement if (n1 > n2) else max = n1; max = n2; can be written as max = (n1 > n2)? n1 : n2; Needs three operands; the condition, and two actions for true and false respectively The? and : together are call the conditional operator or ternary operator Chapter 03: Branching - Slide # 3
The Conditional Operator 2/2 The conditional operator is useful with print and println statements Example: System.out.println("You worked " + hours + ((hours > 1)? hours" : hour")); Chapter 03: Branching - Slide # 4
Input and Output of Boolean Values For output, just write the Boolean variable name For input, use the nextboolean()method and save the result into the Boolean variable Example boolean booleanvar = false; System.out.println(booleanVar); System.out.println("Enter a boolean value:"); Scanner keyboard = new Scanner(System.in); booleanvar = keyboard.nextboolean(); System.out.println("You entered " + booleanvar); Chapter 03: Branching - Slide # 5
The switch Statement 1/8 The switch statement is a multiway branch that makes a decision based on an integral (integer or character) expression Java 7 allows String expressions as well Begins with the keyword switch followed by an integral expression in parentheses and called the controlling expression A list of cases follows, enclosed in braces Chapter 03: Branching - Slide # 6
The switch Statement 2/8 Each case consists of the keyword case followed by A constant called the case label A colon (:) A list of statements An optional break statement The list is searched for a case label matching the controlling expression The action associated with a matching case label is executed If you do not add the break statement, control will continue to the next case If no match is found in all case entries, the case labeled default is executed Chapter 03: Branching - Slide # 7
The switch Statement 3/8 The default case is optional, but recommended, even if it simply prints a message Repeated case labels are not allowed Syntax: switch (Controlling_Expression) { case Case_Label: Statement(s); break; case Case_Label: default: } Chapter 03: Branching - Slide # 8
switch Statement Example Chapter 03: Branching - Slide # 9
The switch Statement 4/8 The action for each case typically ends with the word break The optional break statement prevents the consideration of other cases The controlling expression can be anything that evaluates to an integral type Chapter 03: Branching - Slide # 10
The switch Statement 5/8 You can omit the break statement to simplify the statements Example: Checking for upper- and lower-case responses that are the same Chapter 03: Branching - Slide # 11
The switch Statement 6/8 More examples from: http://math.hws.edu/javanotes/c3/s6.html Chapter 03: Branching - Slide # 12
The switch Statement 7/8 More examples from: http://math.hws.edu/javanotes/c3/s6.html Chapter 03: Branching - Slide # 13
The switch Statement 8/8 More examples from: http://math.hws.edu/javanotes/c3/s6.html Using strings Chapter 03: Branching - Slide # 14
Switch Statement Videos thenewboston: Java Programming Tutorial - 12 - Switch Statement (6:46) EJ Media: Java Tutorial for Beginners - 8 - Switch statements (6:12) BrandonioProductions: Learning Java: Part 22: The Switch Statement (17:08) - Chapter 03: Branching Slide # 15
Intro Methods with Arguments You can pass as many arguments to a java method You can return nothing (void) only one result from a method of any data type Videos Learneroo Java Methods and Parameters (2:03) BrandonioProductions Learning Java: Part 5: Custom methods with return statements and parameters (7:49) thenewboston Java Programming Tutorial - 15 - Use Methods with Parameters (6:40) - Chapter 03: Branching Slide # 16
Random class Random Numbers -1/3 Must import Random library Must create an object and apply the methods on that object More flexible Generates different data types Generates uniformly distributed random numbers Math.random() Method Returns a double value in the range 0.0 up to, but not including 1.0 0.0 <= x < 1.0 Needs more work to translate into the desired range You can use multiplication to expand the range Addition to shift the range Casting to convert to integer No need to create an object Random class vs. Math.random() method Stack Overflow - Chapter 03: Branching Slide # 17
Random Numbers -2/3 Examples: generate a double between 0.0 and 10.0 double randdouble = Math.random() * 10; generate a integer between 0 and 10 int randint = Math.round(Math.random() * 10); int randint = (int) (Math.random() * 10); int randint = Random.nextInt(10); Generate a range from 40.0 to 90.0 double randdouble = Math.random() * 50 + 40; Generate a range from 50 to 99 int randint = (int) ( Math.random() * 50) + 50; General format: Return a random number between a and a+b, excluding a+b double randdouble = a + Math.random() * b - Chapter 03: Branching Slide # 18
Some videos: Adam Gaweda: Random Numbers -3/3 Using Java's Math.random() Method (5:12) The Java Hub - Free Tutorials Java for the Absolute Beginner - #14 - Random Numbers (7:08) Dice uses loops & Random class TheNewBoston: Java Programming Tutorial - 26 - Random Number Generator (5:14) Dice uses loops & Random class BrandonioProductions: Learning Java: Part 8: Generating Random Numbers (3:55) OnliveGamer Java Tutorial 8 - Random Number Generator (7:56) TheKaleb32 How to Generate Random Numbers in Java (10:35) - Chapter 03: Branching Slide # 19
Summary You have learned about Conditional operator The type boolean I/O of boolean values Java switch statement Random class Chapter 03: Branching - Slide # 20