Decisions

Classes in Java and their representation in UML.

Background

CodingBat is a free site of live problems to build skill in Java and/or Python. It was created by Nick Parlante, who is Computer Science lecturer at Stanford. The name “CodingBat” was based on the Greek word “bat” meaning “this domain was available” (or so they say).

Objectives

  • Practice writing small methods with conditionals and decisions.
  • Explore combined conditionals as substitutes for decision statements.
  • Observe the range of test cases needed to test decision statements.

Key Terms

unit test
A single example of running a small program unit, often a method.
expected value
The correct value a program should return for a unit test (aka test case).
actual value
The value a program actually returns for a unit test.
simplified code or boolean expression
A version of a piece of code that is less complicated or shorter than the original.

Part 0: Account Setup

  1. Go to codingbat.com and create an account (click the link on the upper right).
  2. Please use your JMU email when setting up your account.
  3. After logging in, click the “prefs” link (upper right) and “share to” your instructor’s email (duanzx@jmu.edu, stewarmc@jmu.edu) so you will get credit.
  4. Also make sure your name is entered as shown: Last, First

Part 1: Warm Up

As you begin to work CodingBat’s logic problems, you might find the CodingBat Boolean Logic page or their videos helpful:

  1. Java If Boolean Example Solution Code 1
  2. Java If Boolean Example Solution Code 2

Work through the following problems (all from Warmup-1 ). As you work through the problems notice that all of these have solutions available to you. Spend about 5 minutes trying each problem, before you look at the solution. When you are trying the problem and get the results, look at the test cases displayed by codingbat. Identify the expected and actual values. Notice that you may pass several tests, but not all of them. Unlike straight line code without decisions, conditionals and decision statements take a variety of test cases to make sure they work.

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

Part 2: String methods

Many of the problems on codingbat require you to use String methods that you may not yet have encountered. Check out especially:

  1. charAt
  2. equals
  3. length
  4. substring

Part 3: Warm-up with strings

Work through some of the Warmup-1 problems that require String methods:

  1. notString
  2. mixStart

Part 4: Problem Solving

  1. Solve the following problems (all from Logic-1 problems):
    1. squirrelPlay
    2. caughtSpeeding
    3. withoutDoubles
    4. shareDigit

  2. Solve the following problems ( from Logic-2 problems):
    1. roundSum
    2. blackjack