3.10. 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. Run the program several times with different values of x to test both if and else branches.

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 generate a random number and print if it is evenly divisible by 2. However, the code has an error and always prints “Your number is even”, even if the random number is odd. Fix the code so that it only prints the message when the number is even.

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.

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.

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.

The following code should print if x is less than 0, equal to 0, or greater than 0. Finish it to work correctly.

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.

Finish the code below to print you can go out if you don’t have homework and you have done the dishes.

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.

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.

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.

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

You have attempted of activities on this page