|
|
Notes: (i) Includes further examples of use of expressions, casts, reading single characters from input stream and "if-else". (ii) Where we read from the input stream this is handled somewhat clumsily, it would be better to read in the input as a single string and then use methods from the StringTokernizer class to obtain the components; however we will not be covering this class in this sequence of WWW pages. (iii) The problem example also makes use of the divide by zero error check method described on the previous WWW page.
1. OVERVIEW |
An "if-else" statement supports
selection from only two alternatives; we can of course nest such statements or
use constructs such as "if ... else if ... else", but it is usually more
succinct to use a switch statement (also sometimes referred to as a
case statement). Switch statement allows selection from
many alternatives where each alternative is linked to a predicate,
referred to as a selector, which when evaluated to true causes an associated
program statement (or statements) to be executed.
2. JAVA SWITCH STATEMENT |
In Java the general format of a switch (case) statement is as follows: switch SELECTOR { case < SELECTOR_VALUE_1 > : < STATEMENTS_1 > break ... case < SELECTOR_VALUE_N > : < STATEMENTS_N > break default: < DEFAULT_STATEMENTS > } Selections may be made according to:
Selectors must be of a discrete type (such as an integer or a character). An example Java class containing a switch statement is given in Table 1. |
Where N is of type integer, the code in Table 1 states that:
Note the use of the break statement in the code. Note also that it is consider good practise to always include a default alternative to act as a "catch all". |
// SWITCH EXAMPLE APPLICATION // Frans Coenen // The University of Liverpool, UK // Wednesday 24 March 1999 // Revised Monday 25 July 2005 import java.util.*; class SwitchExampleApp { // ---------------- FIELDS ------------------ // Create Scanner class instance private static Scanner input = new Scanner(System.in); // --------------- METHODS ------------------ /** MAIN METHOD: */ public static void main(String[] args) { // input System.out.print("Input an integer "); int number = input.nextInt(); // Process number using a case statement switch (number) { case 0: System.out.println("Number is 0"); break; case 1: System.out.println("Number is 1"); break; case 2: case 3: case 4: System.out.println("Number is 2, 3 or 4"); break; default: System.out.println("Number is less than 0 or greater than 4"); } } } |
Table 1: Switch example program
Where < OPERAND > is an integer of some kind and < OPERATOR > is one of the operators `+', `-', `*' or `/' (integer division). Thus given the expression: 63*35 The program should calculate the value of the expression and display the result. Note: remember to include a divide by zero test.
x = sqrt(2147483647) = 46340.95000105 Thus, rounding down (casting) to the nearest integer, the maximum value for x is 46340. We can check this: 46340*46340=2147395600 which is less than the 2147483647 prescribed maximum for the int range, and 46341*46341=2147488281 which is greater than the 2147483647 maximum for the int range and will thus result in an integer overflow error in the context of the proposed simple calculator. The minimum value for x is then -46340 (-46340*46340=-2147395600 which is greater than the -2147483648 prescribed minimum for the int range). We will therefore insist that the input values for the operands are within the range of -46340..46340 inclusive. From Figure 2 we require two class, Calculator and CalculatorApp. The Calculatorclass definition contains fields for not only the operands and operator, but also two constants to hold the maximum and minimum permitted values for the operands. Note 1: In actual fact the result of 46341x46341 will be -2147479015 because the "sign bit" will get set causing the result to be interpreted as a negative number. 3.3.1 Calculator Class
A set of Nassi-Shneiderman charts for the above is given in Figure 3. ![]() Figure 3: Nassi-Shneiderman charts for Calculator class 3.3.1 CalculatorApp Class
A Nassi-Shneiderman chart for the above is given below Figure 4. Note how the case statement is included in the chart. ![]() Figure 4: Nassi-Shneiderman chart for CalculatorApp class
![]() Figure 6: Activity diagram for calculator problem (switch statement) 3.4. Implementation3.4.1 Calculator class
Table 2: Calculator class implementation 3.4.2 CalculatorApp class
Table 3: Calculator application class implementation 3.5 Testing1. Path Testing: We should test all paths through the system (path testing). As there are a lot of these (see Figure 5) it is a good idea to subdivide the testing to address individual methods where different paths exists, makeCalculation, division and inputCheck. For our purposes we will consider the inputCheck method in isolation.
3. Arithmetic Testing We will combine the arithmetic testing with the additional path testing we must under take. The makeCalculation method has five paths. Four for the recognised operators and 1 where the operator is not recognised. This suggests at least five test cases. Combining this with requirements for arithmetic testing the sequence of test cases presented below is suggested.
4. Data validation testing: This should also be done. Given the large amount of testing that is to be undertaken a test program is appropriate. A suitable program is given in table 4.
Table 4: Calculator application test harness | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4. FURTHER EXAMPLE ILLUSTRATING THE USE OF THE JAVA SWITCH STATEMENT. |
Created and maintained by Frans Coenen. Last updated 10 February 2015