Skip to content

Lab 2: CodingBat Logic

CodingBat.com is a free site of live problems to build skill in Java and/or Python. CodingBat was created by Nick Parlante, who is Computer Science lecturer at Stanford.

Learning Objectives

After completing this lab, you should be able to:

  • Write short methods that include conditional and logic expressions.
  • Apply combined conditionals as substitutes for decision statements.
  • Describe the range of test cases needed to test decision statements.

Note

Each of these problems require you to write a short method. The method definition (starting with the keyword public) is provided.

None of these problems use System.out or System.in. Instead, each method must return the result based on the parameter values.

Account Setup

Your lab will be graded directly in CodingBat. Please do the following to set up your account:

  1. Open the Create Account page in a new tab (so you can keep reading these instructions).

  2. Enter your JMU email address and a password you can remember for the CodingBat site.

  3. After logging in, open Account Settings and enter your instructor's email address under Teacher Share (see table below).

  4. In the Memo textbox, enter your name as "Last name, First name" (Ex: Watson, Emma).

Instructor Email Address
Dr. Duan duanzx@jmu.edu
Dr. Mayfield mayfiecs@jmu.edu
Dr. Shrestha shrestcx@jmu.edu
Dr. Wang wangid@jmu.edu
Dr. Weikle weikleda@jmu.edu

Warm-Up Problems

Solve the following problems from the Warmup-1 category. All of these problems have solutions available to you. Spend about five minutes trying each problem before you look at the solution.

When you submit your code, look at the test cases displayed by CodingBat. Identify the expected and actual values. Notice that you might pass several tests, but not all of them. Unlike sequential code, conditionals and decision statements require many test cases to make sure they work.

  1. sleepIn
  2. monkeyTrouble
  3. sumDouble
  4. diff21
  5. parrotTrouble
  6. makes10
  7. nearHundred
  8. posNeg

Solutions

Video explanations of the following Warmup-1 problems are available:

Logic Problems

Solve the following problems from the Logic-1 category.

  1. squirrelPlay
  2. caughtSpeeding
  3. withoutDoubles
  4. shareDigit

Solve the following problems from the Logic-2 category.

  1. roundSum
  2. blackjack

Note

As you work on these problems, you might find CodingBat's help page useful:

If you finish the lab early (and have extra time), try solving other problems in Logic-1 and Logic-2.