Evaluate Expressions
|
Grab a Piece of Paper! |
Evaluate each expression.
Verify your answer by entering the code in the Java Playground.
Identify any places where short-circuiting occurs.
|
Use the variable declarations to evaluate the boolean expressions that use the relational operators and logic operators.
Learn more:
int a = 6, b = 3, c = 9, d = 2, e = 8;
double x = 7.5, y = 2.5, z = 3.0, w = 0.0;
boolean p = true, q = false;
((a + b) > c) && (d != b)
((c - a) <= b) || (e % d == 0)
!((a * d) < c) && (x >= y)
(b + d == a) || (c / b > d)
((e - b) > (a * d)) && p
((int)(x / y) == a / b) || q
((c % a) <= d) && (w == 0.0)
(a + e > c) || (b * d < a)
((a - b) >= d) && (!q)
((z + w) < x) && ((a % d) == 0)
You can use the Java Playground to check your answers.
Write Code Snippets
Practice 1: Determine if eligible for a reward
Write a code segment to determine if a student is eligible to receive a reward. Students are eligible if they have an average above an 89 and an attendance rating greater than or equal to 80.
- Determine example test values for
average
and attendance
so that you are testing all cases.
- Determine the expected value of
isEligible
for each of your test cases.
- Complete the code segment below.
- Test your code segment with each test case. If it doesn't yield your expected outcome, make necessary changes to your code.
Practice 2: Determine if you can purchase an item
Write a code segment to determine whether or not an item can be purchased. The value of canPurchase
is true
if the itemPrice
is less than or equal to maxPrice
or the item is on sale.
- Determine example test values for
itemPrice
, price
and onSale
so that you are testing all cases.
- Determine the expected value of
canPurchase
for each of your test cases.
- Complete the code segment below.
- Test your code segment with each test case. If it doesn't yield your expected outcome, make necessary changes to your code.
Practice 3: Determine if a movie is recommended or not
Write a code segment to determine whether or not a movie should be recommended. Movies are not recommended if it has a rating less than or equal to 5 and there are at least 9 reviews.
- Determine example test values for
rating
and reviews
so that you are testing all cases.
- Determine the expected value of
isRecommended
for each of your test cases.
- Complete the code segment below.
- Test your code segment with each test case. If it doesn't yield your expected outcome, make necessary changes to your code.
Truth Tables
|
Grab a Piece of Paper! |
Determine the possible outcomes for each expression by creating a truth table.
Verify your answer by entering the code in the Java Playground.
|
In these expressions, A
and B
are boolean
variables, x
, y
are int
variables.
(A && B)
!(A || B)
(A && !B) || (!A && B)
(A || B) && !C
(x < y) && !(x >= y)
You can use the Java Playground to check your answers.