Upload
dinhdung
View
229
Download
8
Embed Size (px)
Citation preview
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Flow of Control
Chapter 3
Part 3 – The Switch Statement
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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
CS 170 _ Java Programming - M. Malaty - Chapter 03:
Branching Slide # 2
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
The Conditional Operator – 1/2
• Used instead of writing a simple if statementif (n1 > n2)
max = n1;
else
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
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 3
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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"));
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 4
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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
• Exampleboolean 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);
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 5
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 6
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 7
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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:
…
}
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 8
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 9
switch Statement
Example
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 10
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 11
• You can omit the break statement to simplify the
statements
• Example: Checking for upper- and lower-case responses that
are the same
The switch Statement – 5/8
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 12
• More examples from: http://math.hws.edu/javanotes/c3/s6.html
The switch Statement – 6/8
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 13
• More examples from: http://math.hws.edu/javanotes/c3/s6.html
The switch Statement – 7/8
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 14
• More examples from: http://math.hws.edu/javanotes/c3/s6.html▪ Using strings
The switch Statement – 8/8
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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)
CS 170 _ Java Programming - M. Malaty - Chapter 03:
Branching Slide # 15
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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)
CS 170 _ Java Programming - M. Malaty - Chapter 03:
Branching Slide # 16
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Random Numbers -1/3• Random class
▪ 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
CS 170 _ Java Programming - M. Malaty - Chapter 03:
Branching Slide # 17
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
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() * bCS 170 _ Java Programming - M. Malaty - Chapter 03:
Branching Slide # 18
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Random Numbers -3/3
• Some videos:
▪ Adam Gaweda:
• 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)
CS 170 _ Java Programming - M. Malaty - Chapter 03:
Branching Slide # 19
JAVA: An Introduction to Problem Solving & Programming, 7th Ed. By Walter Savitch
ISBN-13: 9780133766264 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Summary
• You have learned about▪ Conditional operator
▪ The type boolean
▪ I/O of boolean values
▪ Java switch statement
▪ Random class
CS 170 _ Java Programming - M. Malaty
Chapter 03: Branching - Slide # 20