4.11. Coding Practice¶
The following code should print x is greater than 0
. However, the code has errors. Fix the code so that it compiles and runs correctly.
Line 6 is missing a final )
. Line 7 is missing a semicolon at the end. Line 9 is missing the starting "
.
This is the answer to the previous question.
The following code should check your guess against the answer and print that it is too low, correct, or too high. However, the code has errors. Fix the code so that it compiles and runs correctly.
Line 7 is missing the starting (
. Line 8 is missing the closing "
. Line 9 should be ==
rather than =
to test for equality. Line 12 should be System.out.println
.
This is the answer to the previous question.
The following code should print “You can go out” if you have done your homework and cleaned your room. However, the code has errors. Fix the code so that it compiles and runs correctly.
Line 5 should be true
not True
. Lines 10 and 8 should be swapped.
This is the answer to the previous question.
The following code should print if x is in the range of 0 to 10 (including 0 and 10). However, the code has errors. Fix the errors so that the code runs as intended.
Line 5 is missing an end ;
. Line 6 should be x >= 0
. Line 8 should be else
instead of otherwise
.
This is the answer to the previous question.
The following code should print if x is less than 0, equal to 0, or greater than 0. Finish it to work correctly.
One way to solve this is to add an else if
and then print out if x is equal to 0 and an else
to print that x is greater than 0 as shown below.
This is the answer to the previous question.
Finish the code below so that it prints You can go out
if you have a ride or if you can walk and otherwise prints You can't go out
. Use a logical or to create a complex conditional.
Add an if
statement and use a logical or (||
) to join the conditions and print the one message. Also add an else
statement and print the other message.
This is the answer to the previous question.
Finish the code below to print you can go out if you don’t have homework and you have done the dishes.
Add a conditional with a negation !
for haveHomework and a logical and to create a complex conditional.
This is the answer to the previous question.
Finish the following code so that it prints You have a fever
if your temperature is above 100 and otherwise prints You don't have a fever
.
Add a conditional and print the first message if the temp is above 100 and otherwise print the other message.
This is the answer to the previous question.
Finish the code to print It is freezing
if the temperature is below 30, It is cold
if it is below 50, It is nice out
if it is below 90, or It is hot
using nested if else statements.
Add a conditional with two else if
statements and a final else
.
This is the answer to the previous question.
Finish the code below to print your grade based on your score. The score is an A if you scored 92 or higher, a B if you scored 82 to 91, a C if you scored 72 to 81, a D if you scored a 62 to 71, or an E.
Add a conditional with three else if
statements and a final else
.
This is the answer to the previous question.
For more practice with conditionals, and especially complex conditionals, go to http://codingbat.com/java/Logic-1 and http://codingbat.com/java/Logic-2
In particular we recommend solving the following problems