switch Statement
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.
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!
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.
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.
Top Down Design
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.
Top-Down Design (cont’d) Start JCreator. Open the file “Lab11.java”. Lab11.java is in your Lab11 folder.
Top-Down Design (cont’d) The “big problem” is defined in the main method.
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 }
Top-Down Design (cont’d) We need four instance fields to solve the problem.
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 }
Top-Down Design (cont’d) input(), process() and output( ) are the smaller parts of the problem.
Top-Down Design (cont’d) input( )
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(); }
Top-Down Design (cont’d) output( )
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)); }
Top-Down Design (cont’d) process( )
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; }
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]: 14.6 + 27.9 14.6 + 27.9 = 42.5
Questions?
Java Begin Lab 11 Object Oriented Programming The material in this chapter is not tested on the AP CS exams.
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 }
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 }
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(); }
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."); }
“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."); }