Do this...
Create a test-case class named
Experiment05Test
.
It is sometimes useful to be able to combine relational
expressions. For example, suppose we wish to find out if the value
of i
is within the range 1 to 100.
Mathematically, you can write
1 <= i <= 100
However, Java misinterprets it because it can only handle one comparison at a time; Java's data type then get in the way..
Do this...
Create a test method in Experiment05Test
named
testLogicalExpressions()
, and add these
statements:
int i; i = 5; assertTrue("bad, uncompilable expression", 1 <= i <= 100);
Compile your program.
Look at the compiler's errors (in Eclipse).
Question #03.05.01 Write down the
exact error message that the compiler gives you.
To be answered in Exercise
Questions/lab03.txt
.
So what went wrong? Well, the Java compiler evaluates from left to right, as if you had written this code:
int i = 5; boolean b = (1 <= i); assertTrue(b <= 100);
The compiler is confused how to compare a boolean
(i.e., true
) with an int
(i.e.,
100
).
Do this...
Use a //
comment to comment out the bad assert
statement.
We have to break up the whole comparison into separate
comparisons. In English, we read this mathematical expression like
so: "Is 1 less than or equal to i
, and is
i
less than or equal to 100?" Verbally, we break it
into two separate expressions!
We can code up those two expressions separately:
1 <= i
and
i <= 100
These expression now need to be joined together somehow. "Joining them" sounds like an action; perhaps one of Java's logical operators might help:
Operator | Meaning |
---|---|
&& |
AND operator evaluates to true if
and only if both of its operands are
true . |
|| |
OR operator evaluates to true if
and only if either of its operands are
true . |
! |
NOT operator takes one operand and
evaluates to true if and only if its operand is
false . |
Based on our English expression of the original mathematical comparison, it appears we want to join the two subexpression with an AND operator:
1 <= i && i <= 100
You'll often see anxious programmers adding extra (but technically unnecessary) parentheses:
(1 <= i) && (i <= 100)
This is for readability.
Play around with this version of the expression:
Do this...
Add these statements to the test method:
i = 5; assertTrue("i between 1 and 100", 1 <= i && i <= 100);
(This code assumes that you've kept the declaration of
i
from the beginning of this experiment.) Compile and run the test-case class for a green
bar.
You should question, though, whether this really works or not.
You should try some more values for i
:
Do this...
Copy and paste those last two lines of code several times; each
time change the value of i
to test the comparison as
fully as you can. Here are some cases to consider:
i
that's too small.i
that's too big.i
nicely in the range (just like 5).i
right on the border of the range.Add at least five more assert statements to test the comparison.
For each time you copy-and-paste the two lines of code, you'll
have to change the 5
and perhaps change the
assertTrue
to be an assertFalse
. Be sure
to change each assertion's comment to match the new assertion.
AND operator, logical operator, NOT operator, OR operator