Lab: Gaining Experience with Conditions and Decisions
Instructions:
Answer the following questions one at a time. After answering each question,
check your answer (by clicking on the check-mark icon if it is available)
before proceeding to the next question.
Getting Ready:
Before going any further, you should:
-
Download the following files:
to an appropriate directory/folder. (In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above.)
1. Basic Syntax:
This portion of the lab will help you determine whether you understand
the syntax of
if
statements.
(
Note: Some variable declarations may be omitted for simplicity.
The necessary declarations should be obvious.)
-
Consider the following fragment:
BasicExamples.java
(Fragment: 1)
a = 0;
if (a > 0) JMUConsole.println("Positive");
else JMUConsole.println("Non-Positive");
-
What would be printed by the fragment above if it were compiled
and executed properly? (Note: Don't yet create a program that
includes this fragment, trace the code yourself.)
-
Why?
The boolean
expression a > 0
(involving the
binary relational operator >
)
evaluates to false
because a
contains the
value 0
(which is not greater than 0).
-
Re-write the fragment above putting each call to
JMUConsole.println()
on its own line (without
using a {
or }
).
BasicExamples-Solution.java
(Fragment: 1a)
a = 0;
if (a > 0)
JMUConsole.println("Positive");
else
JMUConsole.println("Non-Positive");
-
Re-write the fragment above putting each call to
JMUConsole.println()
in its own block (i.e., inside of
a {
and }
).
BasicExamples-Solution.java
(Fragment: 1b)
a = 0;
if (a > 0)
{
JMUConsole.println("Positive");
}
else
{
JMUConsole.println("Non-Positive");
}
-
Consider the following fragment:
BasicExamples.java
(Fragment: 2A)
a = 0;
if (a > 0) JMUConsole.println("Positive");
else if (a == 0) JMUConsole.println("Zero");
else JMUConsole.println("Negative");
-
What would be printed by the fragment above if it were compiled
and executed properly? (Note: Don't yet create a program that
includes this fragment, trace the code yourself.)
-
The body of the first
else
clause contains a single
statement. What is it? (Note: Be careful and write it on a single line
so that there is no ambiguity.)
if (a == 0) JMUConsole.println("Zero"); else JMUConsole.println("Negative");
-
Why does this fragment generate the output it does??
The boolean
expression a > 0
evalutes
to false
so control enters the body of
the else
clause, which is another if
statement. The boolean
expression in this
if
statement is a == 0
which evaluates
to true
. So, control enters the body of the if
clause
and not the body of the else
clause.
-
Re-write the fragment above putting each call to
JMUConsole.println()
on its own line (without
using a {
or }
).
BasicExamples-Solution.java
(Fragment: 2a)
a = 0;
if (a > 0)
JMUConsole.println("Positive");
else if (a == 0)
JMUConsole.println("Zero");
else
JMUConsole.println("Negative");
-
Re-write the fragment above putting each call to
JMUConsole.println()
in its own block (i.e., inside of
a {
and }
).
BasicExamples-Solution.java
(Fragment: 2b)
a = 0;
if (a > 0)
{
JMUConsole.println("Positive");
}
else if (a == 0)
{
JMUConsole.println("Zero");
}
else
{
JMUConsole.println("Negative");
}
-
Consider the following fragment:
BasicExamples.java
(Fragment: 2B)
a = 0;
if
{
(a > 0) JMUConsole.println("Positive");
}
else
{
if (a == 0) JMUConsole.println("Zero");
JMUConsole.println("Negative");
}
-
What would be printed by the fragment above if it were compiled
and executed properly? (Note: Don't yet create a program that
includes this fragment, trace the code yourself.)
-
Why?
Since a
contains the value 0
,
the boolean
expression a > 0
evaluates
to false
and control enters the body of
the else
clausew. There are two statements in the
else
clause. In the if
statement, the
boolean
expression a == 0
evaluates
to true
so the body of the if
clause is entered
and Zero
is printed. Then the next statement is executed and
Negative
is printed.
-
Consider the following fragment:
BasicExamples.java
(Fragment: 3A)
a = 1;
if (a > 4) ;
JMUConsole.println("A");
JMUConsole.println("B");
-
What would be printed by the fragment above if it were compiled
and executed properly? (Note: Don't yet create a program that
includes this fragment, trace the code yourself.)
-
Create a small application in Java that contains the fragment above.
Note: Copy and paste the fragment so that you don't make any
mistakes.
-
Compile and execute the application.
-
What was printed?
-
Why?
The statement to be executed when the condition evaluates to
true
(i.e., the body of the if
clause) is empty. The other two statements are not part of the
if
statement (despite the fact that the first statement is
indented and appears to be the body of the if
clause).
-
Consider the following fragment:
BasicExamples.java
(Fragment: 3B)
a = 1;
if (a > 4) ;
{
JMUConsole.println("A");
}
JMUConsole.println("B");
-
What would be printed by the fragment above if it were compiled
and executed properly? (Note: Don't yet create a program that
includes this fragment, trace the code yourself.)
-
Why?
For the same reason - the brackets don't change anything. The
body of the if
clause is still empty.
-
Consider the following fragment:
BasicExamples.java
(Fragment: 4)
a = 5;
if (a > 4)
JMUConsole.println("C");
else ;
JMUConsole.println("D");
-
What would be printed by the fragment above if it were compiled
and executed properly?
-
Create a small application in Java that contains the fragment above.
(Copy and past the fragment so that you don't make any mistakes.)
-
Compile and execute the application.
-
What was printed?
-
Why?
This time, the statement to be executed when the condition evaluates
to false
(i.e., the body of the else
clause)
is empty. Since the condition evaluate to
true
a "C" is printed. Since the other statement is not
part of the
if
or else
, it is executed and a "D" is printed.
2. Basic Logic and Rules of Inference:
This portion of the lab will help you understand some basic
logical operations and some rules of inference.
(
Note: Some variable declarations may be omitted for simplicity.
The necessary declarations should be obvious.)
-
Consider the following fragments:
LogicExamples.java
(Fragment: 1)
// Fragment a
if (price > 100) {
tax = 5;
} else {
tax = 0;
}
// Fragment b
if (price <= 100) {
tax = 0;
} else {
tax = 5;
}
-
Are these two fragments equivalent (i.e., will they always
produce the same results)? If not, explain.
Yes
-
Consider the following fragments:
LogicExamples.java
(Fragment: 2)
// Fragment c
tax = 0;
if (price >= 200) {
tax = 10;
}
// Fragment d
tax = 10;
if ( !(price >= 200) ) {
tax = 0;
}
// Fragment e
if (price >= 200) {
tax = 10;
} else {
tax = 0;
}
-
Are these three fragments equivalent (i.e., will they always
produce the same results)? If not, explain.
Yes
-
How many operators are in the
boolean
expression in
the if
statement of Fragment d? What are they, what kinds of
operations are they, and what are their operands?
There are two. >=
is a relational operator that has
two numeric operands, price
and 200
. !
is a unary logical operator, its operand is (price >= 200)
.
-
Re-write the
boolean
experssion in Fragment d
in such a way that it does not use the !
operator.
You may use a different relational operator, but you must not
change the operands or their order.
price < 200
-
Re-write Fragment d so that it uses this
boolean
expression and results in tax
containing the same
value after it is executed.
tax = 10;
if (price < 200)
{
tax = 0;
}
-
Consider the following fragments:
LogicExamples.java
(Fragment: 3)
// Fragment f
tax = 0;
if (price >= 500) {
tax = 50;
}
// Fragment g
if (price < 500) {
tax = 0;
}
tax = 50;
-
Are these two fragments equivalent (i.e., will they always
produce the same results)? If not, explain.
No.
Fragment f will result in tax
containing
the value 50
when price
is greate than
or equal to 500 and 0
otherwise.
Fragment g will always result in tax
containing
the value 50
.
-
Consider the following fragment:
LogicExamples.java
(Fragment: xor)
boolean movie1, movie2;
if (movie1 ^ movie2) {
JMUConsole.println("OK");
}
-
Re-write the fragment above in Java using only the logical
operators
&&
, ||
,
and !
.
LogicExamples-Solution.java
(Fragment: xor)
boolean movie1, movie2;
// movie1 or movie2 and not both movie1 and movie2
if ((movie1 || movie2) && (!(movie1 && movie2))) {
JMUConsole.println("OK");
}
3. Nesting:
This portion of the lab will help you understand
nested
if
statements.
(
Note: Some variable declarations may be omitted for simplicity.
The necessary declarations should be obvious.)
-
Consider the following fragment:
NestingExamples.java
(Fragment: dink)
numberOfIncomes = 2;
numberOfKids = 0;
if (numberOfIncomes == 2) {
if (numberOfKids == 0) {
JMUConsole.println("DINK");
} else {
JMUConsole.println("Not a DINK");
}
}
-
What would be printed by the fragment above if it were compiled
and executed properly?
-
Re-write the fragment above without using nested
if
statements.
NestingExamples-Solution.java
(Fragment: dink)
numberOfIncomes = 0;
numberOfKids = 0;
if ((numberOfIncomes == 2) && (numberOfKids == 0)) {
JMUConsole.println("DINK");
} else {
JMUConsole.println("Not a DINK");
}
-
Consider the following fragment:
NestingExamples.java
(Fragment: category)
value = 5;
group = 1;
if (value < 100) {
if (group < 0) {
category = 1;
} else {
category = 2;
}
} else {
category = 3;
}
JMUConsole.println(category);
-
What would be printed by the fragment above if it were compiled
and executed properly?
-
Re-write the fragment above without using nested
if
statements.
NestingExamples-Solution.java
(Fragment: category)
value = 5;
group = 1;
category = 3;
if ((value < 100) && (group < 0)) {
category = 1;
}
if ((value < 100) && (group >= 0)) {
category = 2;
}
JMUConsole.println(category);