Download presentation
Presentation is loading. Please wait.
1
switch Statement
2
Objectives: Learn the syntax for switch statements
Another objective is to discuss team development and illustrate software reusability in the context of a simple but realistic case study, the Craps applet.
3
The switch Statement switch case default break switch (variable) {
case value1: ... break; case value2: default: } Reserved words switch case default break There is a legend in software folklore that a missing break in a program caused the whole telephone grid of New York to go down, costing millions of dollars. You have to be careful about breaks; they are among the many opportunities to make a stupid mistake not caught by the compiler. Attention to detail is key. Don’t forget breaks!
4
The switch Statement (cont’d)
switch (num) { case 1: System.out.println ("Buckle your shoe"); break; case 2: ... } This is simply a special case of a case without a break. Case 1 "falls through" to Case 2.
5
The switch Statement (cont’d)
The same case can have two or more labels. For example: switch (num) { case 1: case 2: System.out.println ("Buckle your shoe"); break; case 3: ... } This is simply a special case of a case without a break. Case 1 "falls through" to Case 2.
6
Top Down Design
7
Top-Down Design (cont’d)
Write a program that reads a simple equation from the keyboard in the form [a + b]. Only four symbols will be used: + - * / Solve the equation and display the results on the console screen.
8
Top-Down Design (cont’d)
Start JCreator. Open the file “Lab11.java”. Lab11.java is in your Lab11 folder.
9
Top-Down Design (cont’d)
The “big problem” is defined in the main method.
10
Top-Down Design (cont’d)
import java.text.DecimalFormat; import java.util.Scanner; public class Lab11 { public static void main(String[ ] args) for (int i = 0; i < 3; i++) Lab11 lab = new Lab11( ); lab.input(); // Enter data from the kybd lab.process(); // Perform the calculation lab.output( ); // Display output }
11
Top-Down Design (cont’d)
We need four instance fields to solve the problem.
12
Top-Down Design (cont’d)
public class Lab11 { private double a; private double b; private char symbol; private double solution; public static void main(String[] args) for (int i = 0; i < 3; i++) Lab11 lab = new Lab11( ); lab.input(); // Enter data from the kybd lab.process(); // Perform the calculation lab.output( ); // Display output }
13
Top-Down Design (cont’d)
input(), process() and output( ) are the smaller parts of the problem.
14
Top-Down Design (cont’d)
input( )
15
Top-Down Design (cont’d)
public void input() { Scanner reader = new Scanner(System.in); System.out.print("Enter an expression in the form [a + b]: "); a= reader.nextDouble(); symbol = reader.next().charAt(0); b= reader.nextDouble(); }
16
Top-Down Design (cont’d)
output( )
17
Top-Down Design (cont’d)
public void output() { DecimalFormat df = new DecimalFormat("#.#"); System.out.println(df.format(a) + " " + symbol + “ " + df.format(b) + " = " + df.format(solution)); }
18
Top-Down Design (cont’d)
process( )
19
Top-Down Design (cont’d)
public void process() { switch (symbol) { case '+': solution = a + b; break; case '*': solution = a * b; case '-': solution = a - b; case '/': solution = a / b; }
20
Top-Down Design (cont’d)
Run The Program: Enter an expression in the form [a + b]: 35 / 6 35 / 6 = 5.8 Enter an expression in the form [a + b]: = 42.5
21
Questions?
22
Java Begin Lab 11 Object Oriented Programming
The material in this chapter is not tested on the AP CS exams.
23
Top-Down Design (cont’d)
import java.util.Scanner; public class Lab11A { public static void main(String[ ] args) for (int i = 0; i < 3; i++) Lab11A lab = new Lab11A( ); lab.input(); // Enter data from the kybd lab.output( ); // Display output }
24
Top-Down Design (cont’d)
import java.util.Scanner; public class Lab11A { private int age; public static void main(String[ ] args) for (int i = 0; i < 3; i++) Lab11A lab = new Lab11A( ); lab.input(); // Enter data from the kybd lab.output( ); // Display output }
25
Top-Down Design (cont’d)
public void input() { Scanner reader = new Scanner(System.in); System.out.print("Enter a person’s age (Between 10 & 25): "); age = reader.nextInt(); }
26
Lab 11A – output() public void output() { switch (age) {
case 13: System.out.println("Teenager!"); break; case 16: System.out.println("Keys, please?"); case 18: System.out.println("Rise to vote, sir!"); case 21: System.out.println("Full adult privileges"); default: System.out.println("Too young to do anything."); }
27
“Full Adult Privileges!”
Lab 11A – output() Cover all ages from 10 to 25. Ages 21 – 25 should all say “Full Adult Privileges!” public void output() { switch (age) { case 13: System.out.println("Teenager!"); break; case 16: System.out.println("Keys, please?"); case 18: System.out.println("Rise to vote, sir!"); case 21: System.out.println("Full adult privileges"); default: System.out.println("Too young to do anything."); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.