|
Logic
operators Lab2d
In addition to <, >, <=, >=, ==,
!= and the .equals(
) method, there are more operators for the
booleans.
|
Operator |
Read as
|
Example |
Status |
|
&&
conjunction
|
and |
if (n
> 3 && n < 7) |
if n > 3 and n < 7 are
both true,
then statement is true like when n is 5
|
|
||
disjunction
|
or |
if (a
< 5 || a
>
8) |
if
either a < 5 or
a > 8 is true,
then statement is true, in this example it is false when a is 6 |
|
! negation
|
not |
if(!(a
<= 7) ) |
changes a <= 7 to
opposite truth value
|
They allow us to make
combination statement to look at multiple variables or multiple
situations.
|
|
Using &&, || and !
For each of the
following conditionals determine why they evaluate to true or
false.
- ( 6 > 5
&& 4 = =
2 )
false
- (3 * 4
>=12 && 7 <
9 )
true
- ( 9 + 5 !=
14 || 6 > 8
)
false
- ( 8 - 3
==4 || 0 <
1)
true
- ( !( 4
> 3)
)
false
For these assume x =
3, y = 5 and z = 0
- ( x
< y && z == y -
4)
false
- ( x < y || z == y -
4)
true
- ( ! (z == x )
)
true
- ( y > z || ??? )
true
|
|
Sample program
Look at this sample using
&&. - Figure out what is happening before you run it. In this
example I used only if's.
It would have been better
programmed as nested if elses. The code for nested version is in
the next box. Copy, paste and run. there are two flaws in this code -
can you find them and fix them?
| // boolean operator
&&
import javax.swing.*;
public class Lab2d1 { public
static void main(String[] args)
{ String amount =
JOptionPane.showInputDialog("How much do you earn each
week?"); double a =
Double.parseDouble(amount);
if ( a <=
0) { JOptionPane.showMessageDialog(null,amount
+" ok - we are serious
here!!!"); } if(a
> 0 && a < 50) // in algebra this would look like (
0< a < 50 ) {
JOptionPane.showMessageDialog(null,amount
+" gives you some spending
money"); } if( a
> 50 && a < 100
) { JOptionPane.showMessageDialog(null,amount
+" Hey, that is a pretty good job");
} if( a >
100) { JOptionPane.showMessageDialog(null,amount
+" Wow, do you have time to study?
"); }
System.exit(0); } } |
Surprise! With
nesting you did not need the && part. Let's look
at this program.
|
// boolean operator &&
import javax.swing.*;
public class Lab2d2 { public
static void main(String[] args)
{ String amount =
JOptionPane.showInputDialog("How much do you earn each
week?"); double a =
Double.parseDouble(amount);
if ( a <=
0) { JOptionPane.showMessageDialog(null,amount
+" ok - we are serious
here!!!"); } else if(
a < 50) {
JOptionPane.showMessageDialog(null,amount
+" gives you some spending
money"); } else if(
a < 100
) { JOptionPane.showMessageDialog(null,amount
+" Hey, that is a pretty good job");
} else { JOptionPane.showMessageDialog(null,amount
+" Wow, do you have time to study?
"); }
System.exit(0);
} } |
Notes: Lab2d1 requires 4
comparisons everytime it runs Lab2d2 will have 1, 2, or 3 comparisons -
but never 4 - therefore more streamlined code |
|
&& and || in a sample
In this example you will see another use of &&
(and).
Background material: people can tell if a
number is even or odd by looking at the last digit. The computer can not
look at the last digit so we have to tell the computer some other way to
identify an even number from an odd number.
Hint: Think about what happens when you
divide an integer by 2.
Copy paste and run it
// sample using && and ||
import javax.swing.*;
public class Lab2d3 { public static void
main (String[] args) { String num1 =
JOptionPane.showInputDialog("enter an integer "); int n1 = Integer.parseInt(num1);
String num2 = JOptionPane.showInputDialog("enter another
integer "); int
n2 = Integer.parseInt(num2);
if( n1 % 2 ==0 && n2 % 2
==0) { JOptionPane.showMessageDialog(null,num1+" and
"+num2+" are both even");
} else {
JOptionPane.showMessageDialog(null,num1+" and
"+num2+" are both odd "); } System.exit(0);
} } |
Class
discussion - This program is not completely correct. What
happens when we enter one even and one odd number?
We will fix it together in
class |
|
Using && and || to make a
program better
Open Lab2b2 - Bob and the apples.
We are going to "fix" to an old program.
Think about what would make this program look bad.
Try entering a negative number. To prevent the user from using
negative numbers we are going to add an
if...else Before
the first if add this- if your variables are
different - change to your variables.
Now try the program with numbers like -6 or -4
|
if( n < 0 || a < 0) // n is a negative or a is a
negative
{ //
say something about entering bad data
} else { //
rest of program goes here
|
| |
|
Assignment
Open Lab1c5 and save as Lab2d4 - 50
points We know that triangles can be classified by the
length of sides.
If all 3 sides are the same,
it is an equilateral triangle If 2 of the sides are
the equal( congruent) the triangle is isosceles If all
3 sides are different it is scalene.
This assignment is easy with
nested if .. else but lots of if( ) 's
work if you are careful.
Here are some sample number
for you to check your code
|
|
50 points
Lab2d5
Science Academy's grades are
set up like this 90 - 100 are A's 80 up to 90 are B's 70
up to 80 are C's less than 70 are failing
We are going to average 3 grades. Here is a template to
get you started. There is a trick to get the decimal places for the
average. If you can not figure it out, please ask me.
Use conditionals to describe what letter
grade it gets and make a comment. You may use if's with
&& or nested if elses. See
Lab2d1 and Lab2d2 for
samples.
Warning: be sure to test at
the points where comments change - like at 70, 80, 90, and don't forget
100.
|
// average of 3 grades import javax.swing.*;
public class Lab2d5 {
public static void main(String[] args)
{ String grade1 =
JOptionPane.showInputDialog("Enter your grade 1");
int g1 =
Integer.parseInt(grade1);
System.exit(0);
} } |
My sample runs
|