Part II The Switch Statement
The switch Statement Provides alternative to if-else chain For cases that compare value of integer expression to specific value Reserved words: switch case default break
The switch Statement (continued) Syntax: switch (expression) { // start of compound statement case value-1: <------------terminated with a colon statement1; statement2; break; case value-2: <-------------terminated with a colon statementm; statementn; default: <-------------------terminated with a colon statementaa; statementbb; } // end of switch and compound statement
The switch Statement (continued) Once entry point has been located : All further case evaluations are ignored Execution continues through end of compound statement Unless break statement is encountered break statement Causes immediate exit from switch statement
import javax.swing.*; public class SelectDiskMaker { public static void main(String[] args) String s1, outMessage; int code; s1 =JOptionPane.showInputDialog("Enter a number:"); code = Integer.parseInt(s1); switch (code)
import javax.swing.*; public class SelectDiskMaker { public static void main(String[] args) String s1, outMessage; int code; s1 =JOptionPane.showInputDialog("Enter a number:"); code = Integer.parseInt(s1); switch (code) { case 1: outMessage = "3M Corporation"; break; case 2: outMessage = "Maxell Corporation"; case 3: outMessage = "Sony Corporation"; case 4: outMessage = "Verbatim Corporation"; default: outMessage = "An invalid code was entered"; } // end of switch
JOptionPane.showMessageDialog(null, outMessage, "Program 4.6", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } // end of main() } // end of class
Program Design and Development: Introduction to UML Explicit design Should always be undertaken before coding begins Referred to as program modeling Unified Modeling Language (UML) Program modeling language with its own set of rules and notations Not part of Java language
Program Design and Development: Introduction to UML (continued) Must understand and specify: What objects in system are What can happen to objects When it can happen Each item addressed by number of individual and separate views and diagrams
Class and Object Diagrams Class diagrams Used to describe classes and their relationships Object diagrams Describe specific objects and relationships Attribute Characteristic that each object in class must have Attribute type Primitive Or class type
Class and Object Diagrams (continued) Visibility Defines where attribute can be seen Whether attribute can be used in other classes or is restricted to class defining it Operations Transformations that can be applied to attributes Ultimately coded as Java methods
Class and Object Diagrams (continued) Figure 5.9: A class and object representation
Relationships (continued) Figure 5.17: A multilevel aggregation
Relationships (continued) Figure 5.18: A generalization relationship
Common Programming Errors Assuming if-else statement selecting incorrect choice when problem is values being tested Using nested if statements without including braces to clearly indicate desired structure Inadvertently using assignment operator = in place of relational operator == when comparing Boolean data Forgetting to use break statement to close off case within switch statement
Summary Relational expressions if-else statements Referred to as conditions Used to compare operands if-else statements Used to select between two alternative statements Based on value of relational or logical expression Can contain other if-else statements
Summary (continued) Compound statement switch statement Number of individual statements enclosed within brace pair { and } switch statement Multiway selection statement
End of Chapter 5 Be ready to review chapter questions for Thursday Submit topic for presentation work on today’s lab