Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 22 Selection and Iteration if ( Boolean Expression ) true statement ; else false statement ; OperatorMeaning ! Logical not < Less than <= Less.

Similar presentations


Presentation on theme: "Chapter 22 Selection and Iteration if ( Boolean Expression ) true statement ; else false statement ; OperatorMeaning ! Logical not < Less than <= Less."— Presentation transcript:

1 Chapter 22 Selection and Iteration if ( Boolean Expression ) true statement ; else false statement ; OperatorMeaning ! Logical not < Less than <= Less than or equal > Greater than >= Greater than or equal == Equal != Not Equal & Boolean and ^ Boolean xor | Boolean or && Logical and || Logical or The if statement can use relational, logical or Boolean operators.

2 Chapter 22 The Wind Chill Page The Wind Chill page uses if…else statements to determine the user’s frostbite risk.

3 Chapter 22 The Wind Chill Page if ((windChill = -10)) { effect = "Frostbite Risk: Little"; extraEffect = "You might feel unpleasant"; } else if ((windChill = -25)) { effect = "Frostbite Risk: Possible"; extraEffect = "It takes more than”+ “ a minute to freeze flesh."; }... else { effect = "There is no danger of frosbite"; extraEffect = "Enjoy the weather.";} } Portion of the if…else statements

4 Chapter 22 The switch Statement switch ( Ordinal Variable ) { case Ordinal Value : Statement(s) break; optional case Ordinal Value : … case Ordinal Value : Statement(s) break; optional default: Statement(s) optional } The switch statement requires an ordinal variable. The integral, character, and boolean types are ordinal. The floating point and string types are not.

5 Chapter 22 The switch Statement The case statements are used to match values of the variable. All statements after a match are executed until a break statement or the end of the switch statement is encountered. The default statement provides an alternative if a match does not occur.

6 Chapter 22 The Button Class Button Constructor Button name = new Button(); Button name = new Button(String s ); Event Handler ActionListener Methods String name.getLabel(); name.setLabel(String s ); name.addActionListener(this); Event Processor void actionPerformed(ActionEvent e); The Button class uses an ActionListener. Methods get or set the button’s face (label).

7 Chapter 22 The Hurricane Page The Hurricane page uses buttons to represent the hurricane categories. A switch statement prints the hurricane information.

8 Chapter 22 The Hurricane Buttons private Button Category1 = new Button("1"); private Button Category2 = new Button("2"); private Button Category3 = new Button("3"); private Button Category4 = new Button("4"); private Button Category5 = new Button("5"); The hurricane category buttons are created in the Hurricane class declaration.

9 Chapter 22 The Hurricane Buttons public void init() { /*place the buttons in the applet add an actionListener for each button */ add(Category1); Category1.addActionListener(this); add(Category2); Category2.addActionListener(this); add(Category3); Category3.addActionListener(this); add(Category4); Category4.addActionListener(this); add(Category5); Category5.addActionListener(this); } The init() method adds the action listeners.

10 Chapter 22 The Hurricane Buttons public void actionPerformed(ActionEvent e) { //find the depressed button, Create a message string String eventString = e.getActionCommand(); int buttonNum = Integer.parseInt(eventString); //The switch statement creates the message strings } The actionPerformed() method finds the selected button. The getActionCommand() method of the ActionEvent class returns a string containing the selected button’s label.

11 Chapter 22 Iteration Statements Iteration statements let programmers execute a code segment zero or more times. Java contains three iteration statements. These statements are identical to their JavaScript counter parts. The for statement. The do statement. The do…while statement.

12 Chapter 22 The for Statement The Java for statement is identical to its JavaScript counterpart. The structure of the statement is: for ( InitialExpression ; Test Expression ; Change Expression ) Statement;

13 Chapter 22 The for Statement Initial Expression: Provides a starting value for an index variable. Test Expression: Controls the repetition. Change Expression: Changes the value of the index variable to move it closer to loop termination. The for statement executes as long as the Test Expression is true.

14 Chapter 22 The Decimal Conversion Page The Decimal Conversion page uses a for statement to print decimal numbers and their binary, octal, and hexadecimal equivalents.

15 Chapter 22 The Column Width Constants private static final int DECIMAL_COL = 5; private static final int BINARY_COL = 65; private static final int OCTAL_COL = 200; private static final int HEX_COL = 280; Java constants are created by placing static final before the type name.

16 Chapter 22 The paint() Method for (i=start; i<= stop; i++) { g.drawString(i+" ", DECIMAL_COL, yPos); g.drawString(Integer.toOctalString(i), OCTAL_COL, yPos); g.drawString(Integer.toHexString(i), HEX_COL, yPos); g.drawString(Integer.toBinaryString(i), BINARY_COL, yPos); yPos += 16; } The for statement in the paint() method prints the decimal and its binary, octal, and hexadcimal counterparts.

17 Chapter 22 The while Statement The while statement is identical to its JavaScript counterpart. The structure of the statement is: while ( Expression ) Statement; The statement in the loop body is executed as long as the expression is true. Compound statements, denoted with braces {}, can be used.

18 Chapter 22 The do...while Statement The do…while statement is the least frequently used of the iteration statements. Since the expression is at the foot of the loop, the do…while statement executes the statement in the body at least one time. do Statement; while ( Expression ); The statement in the loop body is executed as long as the expression is true. Compound statements, denoted with braces {} can be used.

19 Chapter 22 Arrays Java arrays are used to store multiple values in the same variable. Values are accessed by their position in the array. Positions are numbered from 0 and are encased in brackets, for example, x[0]. All arrays have a length property.

20 Chapter 22 Array Basics private static final String [] animalNames = {"cat", "dog", "bird", "fish", "horse"}; This is a constant array of Strings.

21 Chapter 22 Array Basics private Button[] animals = new Button[animalNames.length]; This code creates an array of Buttons. Each button will require a constructor since the code creates only the array.

22 Chapter 22 Two-Dimensional Arrays private static final String [][] matrixNames = { { "[0][0]", "[0][1]", "[0][2]", "[0][3]", "[0][4]"}, {"[1][0]", "[1][1]", "[1][2]", "[1][3]", "[1][4]"}, {"[2][0]", "[2][1]", "[2][2]", "[2][3]", "[2][4]"}, {"[3][0]", "[3][1]", "[3][2]", "[3][3]", "[3][4]"} }; A two-dimensional array is an array that contains one- dimensional arrays as its components. The code creates a constant 2-D array.

23 Chapter 22 Creating New Methods Access-Specifier Return-Type Method-Name (Parameter-List) { //Body of the method } Java lets us add new methods to clases. The syntax for creating new methods is:

24 Chapter 22 Creating New Methods public void createMatrix() { //create a matrix of randomly generated numbers between 0 and 9 int i, j; for (i = 0; i < rows; i++) for (j = 0; j < columns; j++) matrix[i][j] = (int) (Math.random() *10); } The createMatrix() method generates a 2-d array of random numbers.


Download ppt "Chapter 22 Selection and Iteration if ( Boolean Expression ) true statement ; else false statement ; OperatorMeaning ! Logical not < Less than <= Less."

Similar presentations


Ads by Google