ppt - AD Book Enterprises

Download Report

Transcript ppt - AD Book Enterprises

7/10: Comparison Operators
• Comparison.java
• Using the if structure
• Equality operators
• Relational operators
import
statement
Comparison.java
import javax.swing.JOptionPane;
class
header
method
header public class Comparison {
public static void main( String args[] )
declaring {
variables:
String firstNumber, secondNumber, result;
Strings
int number1, number2;
& ints
initializing
firstNumber &
secondNumber firstNumber = JOptionPane.showInputDialog( "Enter first int.:" );
secondNumber = JOptionPane.showInputDialog( "Enter 2nd int:" );
initializing
number1 &
number2
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
initializing
result
result = "";
Comparison.java (pt. 2)
if ( number1 == number2 )
result = number1 + " == " + number2;
if ( number1 != number2 )
result = number1 + " != " + number2;
“If number1 is equal to
number2, then set
result to be number1
plus a double equal sign
plus number2.”
if ( number1 < number2 )
result = result + "\n" + number1 + " < " + number2;
if ( number1 > number2 )
result = result + "\n" + number1 + " > " + number2;
if ( number1 <= number2 )
result = result + "\n" + number1 + " <= " + number2;
if ( number1 >= number2 )
result = result + "\n" + number1 + " >= " + number2;
The if structure
Use the if structure to have statements execute
ONLY under certain conditions.
if ( condition )
rest of statement ;
Note that the if ( condition ) is not a statement by
itself ( just like English ).
The spacing and line structure is good practice.
Comparison.java (pt. 2)
if ( number1 == number2 )
result = number1 + " == " + number2;
if ( number1 != number2 )
result = number1 + " != " + number2;
“If number1 is less than
number2, set result to be
itself plus a new line escape
sequence plus number1
plus a ‘less than’ sign plus
number2.”
if ( number1 < number2 )
result = result + "\n" + number1 + " < " + number2;
if ( number1 > number2 )
result = result + "\n" + number1 + " > " + number2;
if ( number1 <= number2 )
result = result + "\n" + number1 + " <= " + number2;
if ( number1 >= number2 )
result = result + "\n" + number1 + " >= " + number2;
Equality & Relational Operators
• To compare values.
• Equality operators: Equal to, not equal to
• Relational operators: Greater than, less than
Equality Operators
English
phrase
Algebraic
Equality
Operator
Java
equality
operator
Example of
Java condition
x is equal
to y
=
==
x == y
x is not
equal to y
=
!=
x != y
Relational Operators
English
phrase
Algebraic
Relational
Operator
Java
Example of
relational Java condition
operator
x is greater
than y
>
>
x > y
x is less
than y
<
<
x < y
x is greater
or equal to
y
x is less
than or
equal to y
>
>=
x >= y
<
<=
x <= y
Comparison.java (pt. 3)
JOptionPane.showMessageDialog( null, result, "Comparison
Results", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
result, whatever it is after
executing the if statements,
is put in the main body of
the MessageDialog box.
Program of the Day
• Problem 2.17 (pg. 73): write a program
• Work in pairs
• Use Comparison.java as a help/template in
creating the program