relational operators relational operators are used to compare two numeric values and create a...
TRANSCRIPT
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.
Relational Operators
Operator Expression Interpretation> x > y if x > y then true< x < y if x < y then true>= x >= y if x >= y then true<= x <= y if x <= y then true
== x == y if x == y then true!= x != y if x != y then true
Equality Operators
Relational Operators
4 > 6
7 + 3 / 2 == 5 + 5 / 2
Assume a = 4 and x = 9, a <= b
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).
Logical Operators
• && (logical And):
Expression1 Expression2 Expression1 && Expression2
True True True
True False False
False True False
False False False
Logical Operators
• II (logical Or):
Expression1 Expression2 Expression1 || Expression2
True True True
True False True
False True True
False False False
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
Logical Operators
• ^ (logical Exclusive Or):
Expression1 Expression2 Expression1 ^ Expression2
True True False
True False True
False True True
False False False
Logical Operators
• ! (logical Not):
Expression1 ! Expression1
True False
False True
Logical Operators
• (4 >= 7) && (3 + 4 == 7)
• (4 >= 7) && (3 + 4 == 7) || (4 < 7)
• (4 >= 7) && (3 + 4 == 7) || (4 < 7) && true
Control Structures
• Control structures are used to organize actions (statements).
• Examples:– Sequence Structure– Selection Structure– Repetition Structure
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);
Selection Structures
• Selection Structures allow you to write code that will select and execute specific code statements instead of other code statements.
If Selection Structure
• The basic structure of an if statement in Java is:if (boolean_expression) {
statement;
}
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.
If Selection Structure
If (hungry == true) {
outputBox.printLine(“find some food”);
}
outputBox.printLine(“find some food”);
hungry == true
False
True
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 <60 = F, 60 – 69 = D, 70 - 79 = C, 80 - 89 = B, and >90 = A.
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.
If/else Selection Structure
• The basic structure of an if/else statement in Java is:if (boolean_expression) }
statement 1;
} else {
statement 2;
}
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 TrueoutputBox.printLine(“get more work done);
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;
}
}
If/else Selection Structure
• It may also be written as:if (boolean_expression_1) {
statement1;
} else if (boolean_expression_2) {
statement2;
} else {
statement3;
}
If/else Selection Structure
• Let’s look at our grade program again and rewrite it using the if/else structure.
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 { }
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;
}
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
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”);
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)
The Conditional Operator
If (grade > 70) {
outputBox.printLine (“C”);
} else {
outputBox.printLine(“F”);
}
outputBox.printLine(grade > 70 ? “C” : “F”);
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) {
outputBox.printLine(“true”);
} else {
outputBox.printLine(“false”);
}
The Conditional OperatorWrite 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”);
}
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.
Switch Selection Structureswitch (switch_expr) {
case item_1:
statement1;
statement2; …
break;
case item_2:
statement1; …
break;
default:
statement 1; …
break;
}
Switch Selection Structureswitch ( grade ) {
case 'A':
++aCount;
break;
case 'B':
++bCount;
break;
...
default:
outputBox.printLine( "Incorrect grade. Enter new grade." );
break;
}
++aCountCase A’
False
Truebreak
++bCountCase B’
False
True break
...
outputBox.printLine ...default
False
Truebreak
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.
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.
Switch Selection Structure
• What if you have multiple values you want to test for and have them execute the same “case”?
Switch Selection Structureswitch (switch_expr) {
case item_1: item_2:
statement1;
statement2; …
break;
case item_3:
statement1; …
break;
default:
statement 1; …
break;
}
Switch Selection Structure
• Write a program to read in letter grades and calculate how many of each letter are entered.
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);
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.
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.
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)
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);
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);