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
-
If you don’t already have a CodingBat account, register for one by completing and submitting the form at CodingBat Prefs: Create Account.
-
After you have successfully created and logged in with the new account, browse to the CodingBat Prefs: Account Settings page.
-
In the Teacher Share field, enter
nbennett10@cnm.edu
(copy & paste is the recommended method, to avoid potential misspellings). -
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.)
-
This problem requires you to evaluate and return a
boolean
expression. Hint: If you find yourself returning atrue
orfalse
, conditioned on aboolean
expression beingtrue
orfalse
, consider simply returning thatboolean
expression; if you are returningfalse
if the expression istrue
, andtrue
if the expression isfalse
, consider simply returning the logical negation of the expression. -
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 numericvalue
(not needed in this case, of course), you would invokeMath.sqrt(value)
and take the returned result. Can you find a method in theMath
class to help? -
This is another one requiring a mathematical computation and a conditional operation—very similar elements to those of the previous problem.
-
In Java, instances of the
String
class are immutable—that is, once created, we can’t change the contents of aString
. However, there are methods ofString
that construct new instances derived from a given instance. Hint: Look for a method that constructs and returns a newString
from a portion of an existingString
. -
Consider look for a method in the
String
class that will let you get a singlechar
from a specified position in aString
, then concatenating the extractedchar
, the originalString
, and the extractedchar
again. -
Another
String
problem. Hint: TheString
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. -
This problem involves evaluating a complex
boolean
condition. If possible, try to compose a single expression that computes theboolean
value you need, and return the value of that expression. -
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 2String
instances for equality, don’t use==
; instead, use theequals
method:string1.equsls(string2)
returnstrue
ifstring1
andstring2
contain identical contents. -
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.
-
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 ofchar
values returned by theString.toCharArray()
method—but there may be an even easier approach, based on answering this question: If you construct a newString
based on another, but with all thee
characters removed, can you compare the lengths of the 2String
instances to find out how manye
s were removed? -
To solve this problem, consider a local
String
variable that you can repeatedly concatenate with single characters, to build theString
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:
-
Declare a local variable (as the first line in the method body) of the same type as the return type of the method.
-
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 theboolean
primitive type,false
is a reasonably safe initial value. For numeric primitive types—byte
,short
,char
,int
,long
,float
, anddouble
—the value0
is usually chosen.) -
Add, as the last line of the method body, a
return
statement to return the variable you declared in step 1. -
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.