KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SwitchLoad


1 /* SwitchLoad.java */
2
3
4 /** Tests the switch statement in various ways; also has try/catch. */
5
6 public class SwitchLoad implements org.quilt.cl.RunTest {
7     public static final int MAGICNUMBER = 0xCAFEBABE;
8     public static final long MAGICLONG = 0x00000000CAFEBABE;
9     public static final short MAGICSHORT = (short) 0xCAFE;
10
11     public static final float PI_FLOAT = (float) 3.1415;
12     public static final double PI_DOUBLE = (double) 3.1415;
13     public static final int NORMALNUMBER = 188;
14     public static final int WEIRDNUMBER = -1;
15
16     private double twoPie;
17
18     public boolean switchStatement( int stmt ) {
19         
20         switch( stmt ) {
21         case WEIRDNUMBER:
22             throw new RuntimeException JavaDoc("Weird Switch");
23         case NORMALNUMBER:
24             return false;
25         case MAGICNUMBER:
26             return true;
27         default:
28             throw new RuntimeException JavaDoc("Default Switch");
29         }
30     }
31     
32     public boolean doMagicSwitch() throws RuntimeException JavaDoc {
33         return switchStatement( MAGICNUMBER );
34         //return true;
35
}
36
37     public boolean doNormalSwitch( ) throws RuntimeException JavaDoc {
38         return switchStatement( NORMALNUMBER );
39         //return false;
40
}
41
42     public boolean doWeirdSwitch( ) throws RuntimeException JavaDoc {
43         return switchStatement( WEIRDNUMBER );
44         //throw new RuntimeException("Weird Switch");
45
}
46
47     public boolean doDefaultSwitch( ) throws RuntimeException JavaDoc {
48         return switchStatement( 10 );
49     }
50         
51     public int runTest(int x) {
52         boolean weirdSwitchOK = true;
53         boolean defaultSwitchOK = true;
54
55         if (!doMagicSwitch())
56             throw new RuntimeException JavaDoc("Magic Switch failed.");
57         if (doNormalSwitch())
58             throw new RuntimeException JavaDoc("Normal Switch failed.");
59         try {
60             doWeirdSwitch();
61             weirdSwitchOK = false;
62         } catch (Exception JavaDoc e) {
63             weirdSwitchOK = true;
64         }
65
66         if (!weirdSwitchOK)
67             throw new RuntimeException JavaDoc("Weird Switch failed.");
68
69         boolean isBad = false;
70         try {
71             doDefaultSwitch();
72             defaultSwitchOK = false;
73         } catch (Exception JavaDoc e) {
74             defaultSwitchOK = true;
75         }
76
77         if (!defaultSwitchOK)
78             throw new RuntimeException JavaDoc("Default Switch Failed");
79
80         return 42;
81     }
82 }
83
Popular Tags