if ( !(speed > 2000 && memory > 512) )
System.out.println("Reject this computer");
else
System.out.println("Acceptable computer");
Say that you are considering a computer with 2200 MHz speed and 750 Meg of memory. This computer is not rejected. Evaluation proceeds like this:
! ( speed > 2000 && memory > 512 ) ------+------ -------+--- | | ! ( T && T ) ---------+----------- | ! ( T ) !T F
A truth table is another way to analyze the expression. The first two columns are filled with the possible truth values of the relational expressions. The remaining cells show how these truth values are combined.
speed > 2000 | memory > 512 | speed > 2000 && memory > 512 | ! (speed > 2000 && memory > 512) |
---|---|---|---|
F | F | ||
F | T | ||
T | F | ||
T | T |
If speed
is 2200 and memory
is 750 the
the fourth row of the table is selected.
The last column shows that this computer is not rejected.
All computers are rejected except for those that meet both requirements,
corresponding to the last row of the table.
Does the following program fragment do the same thing as the original fragment?
boolean reject = !(speed > 2000 && memory > 512); if ( reject ) System.out.println("Reject this computer"); else System.out.println("Acceptable computer");