Download presentation
Presentation is loading. Please wait.
Published byGary McKenzie Modified over 9 years ago
1
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship between the two operators. –Relational operators are evaluated after all arithmetic operators.
2
Relational Operators
3
4 > 6 7 + 3 / 2 == 5 + 5 / 2 Assume a = 4 and x = 9, a <= b
4
Logical Operators Logical Operators are used to compare boolean values and create a boolean result. –Logical operators are usually used to compare the results of relational operators. –Logical operators are evaluated after all relational operators. –Logical operators are && (logical And), & (boolean And), || (logical Or), | (boolean Or), ^ (boolean Exclusive Or), and ! (logical Not).
5
Logical Operators && (logical And): Expression1 Expression2 Expression1 && Expression2 True True True True False False False True False False False False
6
Logical Operators II (logical Or): Expression1 Expression2 Expression1 || Expression2 True True True True False True False True True False False False
7
Logical Operators && vs. & and || vs. | –Both && and || will only evaluate the expressions until it knows a result while the & and | operators will evaluate all the expressions before they return the result. –Gender == 1 || age >= 65 –Gender == 1 | age >= 65
8
Logical Operators ^ (logical Exclusive Or): Expression1 Expression2 Expression1 ^ Expression2 True True False True False True False True True False False False
9
Logical Operators ! (logical Not): Expression1 ! Expression1 True False False True
10
Logical Operators (4 >= 7) && (3 + 4 == 7) (4 >= 7) && (3 + 4 == 7) || (4 < 7) (4 >= 7) && (3 + 4 == 7) || (4 < 7) && true
11
Control Structures Control structures are used to organize actions (statements). Examples: –Sequence Structure –Selection Structure –Repetition Structure
12
Sequence Structures public static void main(String[] args) { MainWindow mainWindow = new MainWindow; OutputBox outputBox = new OutputBox(mainWindow); outputBox.printLine( “Welcome to”); outputBox.printLine( "Java Programming!”); } outputBox.printLine(“ Welcome to”); outputBox.printLine( “Java Programming!”); MainWindow mainWindow = new MainWindow(); OutputBox outputBox = new OutputBox( mainwindow);
13
Selection Structures Selection Structures allow you to write code that will select and execute specific code statements instead of other code statements.
14
If Selection Structure The basic structure of an if statement in Java is: if (boolean_expression) { statement; }
15
If Selection Structure If the boolean_expression is true then the statement is executed. If the boolean_expression is false then the statement is skipped and the next statement in the sequence is executed.
16
If Selection Structure If (hungry == true) { outputBox.printLine(“find some food”); } outputBox.printLine (“find some food”); hungry == true False True
17
If Selection Structure Let’s create the statements for the following problem: We want to categorize grades such that we will print out the corresponding letter grade. Assume 90 = A.
18
If/else Selection Structure The if/else statement allows you to write code that executes one statement if the boolean_expression is true and different statement if the boolean_expression is false.
19
If/else Selection Structure The basic structure of an if/else statement in Java is: if (boolean_expression) } statement 1; } else { statement 2; }
20
If/else Selection Structure If (hungry == true) { outputBox.printLine(“find some food”); } else { outputBox.printLine(“get more work done”); } outputBox.printLine(“ find some food”); Hungry == true False True outputBox.printLine( “get more work done);
21
If/else Selection Structure The statement inside an if/else structure may be another if/else statement. In Java it looks like: if (boolean_expression_1) { statement1; } else { if (boolean_expression_2) { statement2; } else { statement 3; }
22
If/else Selection Structure It may also be written as: if (boolean_expression_1) { statement1; } else if (boolean_expression_2) { statement2; } else { statement3; }
23
If/else Selection Structure Let’s look at our grade program again and rewrite it using the if/else structure.
24
If/else Selection Structure The if/else and if statements we have worked with so far execute only one statement. We can also execute multiple statements using compound statements. –Compound statements are actually multiple statements enclosed in brackets { }
25
If/else Selection Structures The format for an if/else statement which includes a compound statement in Java is: if (boolean_expression) { statement1; statement2; } else { statement3; statement4; statement5; statement6; }
26
Nesting If Statements Nesting if statements makes your program more powerful because it can handle many different situations. –Nesting occurs when one or more if structures are inside of another if statement. –The else statement is always associated with the previous if statement unless { } are used to change the associativity. Dangling else
27
Nested If Statements What is printed when the following is evaluated: if (y == 8) if (x == 5) outputBox.printLine(“1”); else outputBox.printLine(“2”); outputBox.printLine(“3”); outputBox.printLine(“4”);
28
The Conditional Operator The conditional operator is basically a short cut to the if/else structure. –It is called a ternary operator because it has three arguments that form the conditional expression. (boolean_expression? true_operation : false_operation)
29
The Conditional Operator If (grade > 70) { outputBox.printLine (“C”); } else { outputBox.printLine(“F”); } outputBox.printLine(grade > 70 ? “C” : “F”);
30
The Conditional Operator Write the conditional operator for the following if/else statement. boolean a = true, b = false, c = true; if ((a && c) && c || b) { out putBox.printLine(“true”); } else { out putBox.printLine(“false”); }
31
The Conditional Operator Write the conditional operator for the following if/else statement. If (grade >= 90) { outputBox.printLine(“The grade is an A”); } else if (grade >= 80) { outputBox.printLine(“The grade is an B”); } else if (grade >= 70) { outputBox.printLine(“The grade is an C”); } else { outputBox.printLine(“The grade is an F”); }
32
Switch Selection Structure The Switch selection structure is basically short hand for multiple if/else statements. It allows you to perform statements for a specific value of a variable when there are multiple possibilities. –The switch expression must be an integer or char result.
33
Switch Selection Structure switch (switch_expr) { case item_1: statement1; statement2; … break; case item_2: statement1; … break; default: statement 1; … break; }
34
Switch Selection Structure switch ( grade ) { case 'A': ++aCount; break; case 'B': ++bCount; break;... default: outputBox.printLine( "Incorrect grade. Enter new grade." ); break; } ++aCount Case A’ False True break ++bCount Case B’ False True break... outputBox. printLine... default False True break
35
Switch Selection Structure The switch should always use the break statement for each case or the structure will not work properly. Your switch statements should always have a default case for completeness purposes. Anything you can represent with a switch you can represent as a nested if/else statement.
36
Switch Selection Structure Write a program to determine the cost of products sold in the last week at a mail order house. They have five products whose retail prices are: product 1 - $2.98; product 2 - $4.50; Product 3 - $9.98; Product 4 - $4.49; Product 5 - $6.87 The program should ask the user to enter the product number and the quantity sold for one day. The program should use a switch statement to determine the total retail price for each product.
37
Switch Selection Structure What if you have multiple values you want to test for and have them execute the same “case”?
38
Switch Selection Structure switch (switch_expr) { case item_1: item_2: statement1; statement2; … break; case item_3: statement1; … break; default: statement 1; … break; }
39
Switch Selection Structure Write a program to read in letter grades and calculate how many of each letter are entered.
40
ListBox Class A ListBox allows a program to present multiple alternative inputs to the user in a well-defined manner. –A ListBox must be associated with a MainWindow. MainWindow mainWindow = new MainWindow(); ListBox listBox = new ListBox(mainWindow);
41
ListBox Class When a ListBox is created it has nothing in it. The program must add items to the list. listBox.addItem(“Item One”); –As many items as needed can be added to the list.
42
ListBox Class To retrieve the user’s input use the getSelectedIndex() method. int selection = listBox.getSelectedIndex(); –The first item in the list has an index of zero (0). –If the user makes no selection, then the result is ListBox.NO_SELECTION. –If the user cancels the input, then the result is ListBox.CANCEL.
43
Color Class Java represents colors using individual red, green, and blue components combined to create a color. –Each component is a value between 0 and 255. Black = (0,0,0) White = (255, 255, 255)
44
Color Class The color class also provides some predefined colors. (see page 276) –To use the predefined colors you reference them by their name: Color.magenta –To change the color of an object: objectName.setColor(Color.pink);
45
DrawingBoard Class The DrawingBoard class can be used to draw shapes. –To draw a line: objectName.drawLine( x1, y1, x2, y2); –To draw a circle: objectName.drawCircle(centerX, centerY, radius); –To draw a rectangle: objectName.drawRectangle(x, y, width, height);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.