Selection Coding Practice (2.1-2.6)

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java2 class periods10 exercises34 graded tests

Files

README.mdDownload

Selection Coding Practice (2.1-2.6)

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

Exercise 1

The code should print x is greater than 0. However, the code has errors. Fix the code so that it compiles and runs correctly.

Open the 01-ifpractice1 folder and edit Test1.java. Press Run to try it; Run Tests checks it (1 test).

Exercise 2

The 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.

Open the 02-ifpractice2 folder and edit Test1.java. Press Run to try it; Run Tests checks it (1 test).

Exercise 3

The 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.

Open the 03-ifpractice3 folder and edit Test1.java. Press Run to try it; Run Tests checks it (1 test).

Exercise 4

The 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.

Open the 04-ifpractice4 folder and edit Test1.java. Press Run to try it; Run Tests checks it (1 test).

Exercise 5

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

Open the 05-ifpractice5 folder and edit Test1.java. Press Run to try it; Run Tests checks it (3 tests).

Exercise 6

Finish the code 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.

Open the 06-ifpractice6 folder and edit Test1.java. Press Run to try it; Run Tests checks it (4 tests). Its tests check your code, not what it prints: “print statement You can go out”, “print statement You can't go out”, “or”, “Code has been changed”.

Exercise 7

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

Open the 07-ifpractice7 folder and edit Test1.java. Press Run to try it; Run Tests checks it (4 tests). Its tests check your code, not what it prints: “print statement You can go out”, “and”, “not”, “Code has been changed”.

Exercise 8

Finish the code so that it prints You have a fever if your temperature is above 100 and otherwise prints You don't have a fever.

Open the 08-ifpractice8 folder and edit Test1.java. Press Run to try it; Run Tests checks it (4 tests). Its tests check your code, not what it prints: “print statement You have a fever”, “print statement You don't have a fever”, “if statement for temp greater than 100”, “Code has been changed”.

Exercise 9

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.

Open the 09-ifpractice9 folder and edit Test1.java. Press Run to try it; Run Tests checks it (5 tests).

Exercise 10

Finish the code 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.

Open the 10-ifpractice10 folder and edit Test1.java. Press Run to try it; Run Tests checks it (10 tests). Its tests check your code, not what it prints: “Code has been changed”, “print statement - A”, “print statement - B”, “print statement - C”, “print statement - D”, “print statement - E”, “if you scored 92 or higher”, “else if you scored 82 or higher”, “else if you scored 72 or higher”, “else if you scored 62 or higher”.

More practice

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

For teachers

Selection Coding Practice (2.1-2.6)

Students read CSAwesome2's “Selection Coding Practice (2.1-2.6)” in CS Room and work through its 10 Java coding exercises.

  • Time: 2 class periods
  • Tests: 34 in 10 graded exercises, set up for you when you import the lesson

Goals

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–10, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-ifpractice1Test1.javaAutograded1
02-ifpractice2Test1.javaAutograded1
03-ifpractice3Test1.javaAutograded1
04-ifpractice4Test1.javaAutograded1
05-ifpractice5Test1.javaAutograded3
06-ifpractice6Test1.javaChecks code text only4
07-ifpractice7Test1.javaChecks code text only4
08-ifpractice8Test1.javaChecks code text only4
09-ifpractice9Test1.javaAutograded5
10-ifpractice10Test1.javaChecks code text only10

Teachers can download a reference solution after signing in.