As we’re coming up to speed, we’ll use problems from CodingBat to familiarize ourselves with Java syntax & key standard library classes.

Create an account

  1. If you don’t already have a CodingBat account, register for one by completing and submitting the form at CodingBat Prefs: Create Account.

  2. After you have successfully created and logged in with the new account, browse to the CodingBat Prefs: Account Settings page.

  3. In the Teacher Share field, enter nbennett10@cnm.edu (copy & paste is the recommended method, to avoid potential misspellings).

  4. Click the Share button.

Warmup problems

Of the problems listed in the Warmup-1 section, please attempt those listed in the first column. Feel free to jump around, and don’t worry if you get stuck. (Remember that all of these problems have a Show Solution button, so you can see how the author of the problem solved it. You may want to try that button with a few of the problems, and then see if you can get through others of the same type without using it.)

  1. sleepIn

    This problem requires you to evaluate and return a boolean expression. Hint: If you find yourself returning a true or false, conditioned on a boolean expression being true or false, consider simply returning that boolean expression; if you are returning false if the expression is true, and true if the expression is false, consider simply returning the logical negation of the expression.

  2. diff21

    Solving this correctly requires computing an absolute difference between 2 numbers, doubling it under a specific condition, and returning a result. Remember: Many mathematical operations are provided by methods of the Math class. For example, if you want to compute the square root of a numeric value (not needed in this case, of course), you would invoke Math.sqrt(value) and take the returned result. Can you find a method in the Math class to help?

  3. nearHundred

    This is another one requiring a mathematical computation and a conditional operation—very similar elements to those of the previous problem.

  4. missingChar

    In Java, instances of the String class are immutable—that is, once created, we can’t change the contents of a String. However, there are methods of String that construct new instances derived from a given instance. Hint: Look for a method that constructs and returns a new String from a portion of an existing String.

  5. backAround

    Consider look for a method in the String class that will let you get a single char from a specified position in a String, then concatenating the extracted char, the original String, and the extracted char again.

  6. startHi

    Another String problem. Hint: The String class has a large number of specialized methods; you may well find that one of them does almost exactly what you need. Read the documentation carefully.

  7. hasTeen

    This problem involves evaluating a complex boolean condition. If possible, try to compose a single expression that computes the boolean value you need, and return the value of that expression.

  8. mixStart

    This problem is written as if you need to check the start of a String for a particular substring; however, it is really asking for a smaller substring, starting with the character at position 1 (rather than position 0). What method can construct such a substring? Hint: To compare 2 String instances for equality, don’t use ==; instead, use the equals method: string1.equsls(string2) returns true if string1 and string2 contain identical contents.

  9. close10

    Another problem involving testing absolute differences. Don’t hesitate to declare local variables to hold intermediate values you need to arrive at the final answer.

  10. stringE

    There are many ways to tackle a problem like this. You might iterate over the contents of the String—or even better, over the array of char values returned by the String.toCharArray() method—but there may be an even easier approach, based on answering this question: If you construct a new String based on another, but with all the e characters removed, can you compare the lengths of the 2 String instances to find out how many es were removed?

  11. everyNth

    To solve this problem, consider a local String variable that you can repeatedly concatenate with single characters, to build the String you need to return.

Tips

  • When in doubt, if you need to write a method that returns a result, begin the implementation with the following steps:

    1. Declare a local variable (as the first line in the method body) of the same type as the return type of the method.

    2. In the same statement in which you declare the variable, assign it an initial value. (If the return type is a class—including String—rather than a primitive, null is a reasonable initial value. For the boolean primitive type, false is a reasonably safe initial value. For numeric primitive types—byte, short, char, int, long, float, and double—the value 0 is usually chosen.)

    3. Add, as the last line of the method body, a return statement to return the variable you declared in step 1.

    4. Between the 2 lines of code you created in the above steps, write the code to compute and assign the correct value to the variable declared in step 1.

    With experience, you’ll be able to simplify the above steps in some cases—but it’s never a bad idea to start out with them.

  • In CodingBat, the test cases aren’t displayed until you submit your code (using the Go button). However, it can be very useful to see these test cases while you’re writing the code, to clarify your understanding of how the method is expected to behave. To do this, simply submit your code before it’s done; it will probably fail most of the test cases, but the test cases will be displayed. Note that this trick only works if your code actually compiles; if it doesn’t, the output from submitting your code will be a compilation error message (which you can use to help you fix the code), but none of the test cases will be displayed. (As a rule, if you follow the steps listed above, your code won’t compile until after step 3 is done.)

  • Important: Please remember to log in to CodingBat before writing and submitting code; otherwise, your code won’t be saved, and will disappear when you navigate away from the page of any given problem.