Computer Science 15-100 (Lecture 18), Spring 2009
Homework 4
Due: Thu 12-Feb-2009 at 11:59pm (email copy) and at Friday's
class/recitation
(identical physical copy)
(no late submissions accepted).
Read the instructions from hw3 first (changing
"hw4" for "hw3" where appropriate).
-
This week, we are not
giving you a file to start from. But you must create your own Hw4.java
file and place all your solutions in that one file. Name everything
very carefully, so your file name and your method names exactly match
the spec! The autograder is very unforgiving!
-
For every method you
write, you are expected to also write a corresponding test method. If
your method is named foo(), your test method should be named testFoo().
You will be graded on the quality of your test cases: on the one hand,
you should include all the boundary cases you can think of; on the other
hand, you should not include gratuitously long lists of test cases.
Keep it succinct yet complete.
-
Wherever appropriate, you
are expected to write well-chosen helper methods. You will be graded
on the quality of your helper methods. You will not be graded
on test methods for your helper methods, but these are highly recommended.
- Prime Methods
- nthBalancedPrime
- nthEmirpsPrime
- nthHappyPrime
- nthSelfPrimeInBase10
- String Methods
- reverse
- isPalindrome
- isRotation
- simpleCalculator
- Kinds of Loops
-
Bonus/Optional:
Painting Dominoes
- Prime Methods
Write each of the following methods, along with appropriate test methods
and helper methods. For definitions of these terms, see the excellent
List of Prime
Number on Wikipedia. In particular, this can help you in defining
your test methods.
Note for these nthFoo problems, assume the parameter is a zero-based
integer. So, for example, nthPrime(0) would return 2, and nthPrime(1)
would return 3. Return -1 for any negative parameters (so nthPrime(-3)
would return -1). Use this same pattern for all the nthFoo problems.
Also note your nthFoo methods must compute the answers.
You may not hardcode the answers based on the lists on that Wikipedia
page (even though you may, of course, hardcode your test methods
based on those lists!).
Also note: there are many other flavors of primes in that list that you
should be comfortable implementing, say on a future quiz or test...
- nthBalancedPrime
- nthEmirpsPrime
- nthHappyPrime
Hint: when checking whether or not a number is happy, infinite
loops always contain 4's, and non-infinite loops always end in 1's, so
you can keep going until you either reach a 4 or a 1.
- nthSelfPrimeInBase10
- String Methods
Write each of the following methods, along with appropriate test methods
and helper methods.
Remember that you may only use the charAt and length methods from the String
class (so, for example, the equals method from the String class is
off-limits here).
- reverse
This method takes a string and returns the same string in reverse
(so reverse("abc") returns "cba"). The reverse of the empty string
is the empty string, and the reverse of null should be null.
- isPalindrome
This method takes a string and returns true if the string is a
palindrome (the same forwards as backwards) and false otherwise.
Note that the empty string is a palindrome (if a degenerate one), and
the null string is not a palindrome. Your test should be
case-insensitive, so "aA" is a palindrome, and it should skip
spaces and punctuation, so "Madam, I'm Adam" is also a palindrome.
- isRotation
This method takes two strings and returns true if one is a rotation
of the other and false otherwise. For example, "abcde" is a
rotation of "cdeab". Every non-null string (including the empty
string) is a rotation of itself. The null string is not a rotation
of any string (not even another null string).. This method is
case-sensitive and treats spaces and punctuation the same as other
characters (so "abcde" is not a rotation of "cd eab").
- simpleCalculator
This method takes a string containing a simple arithmetic problem
containing two possibly-negative integers separated by an operator (one
of +, -, *, /, or %), and returns the integer result of the computation.
For example, simpleCalculator("-14/3") would return the integer value -4
(so it does integer division). Do not worry about overflow or
division by zero (it is acceptable to throw an exception in that case).
- Kinds of Loops
In the file Hw4KindsOfLoops.java, choose any one of the preceding
problems and solve (or "re-solve") it several times: once using only
"for" loops, once using only "while" loops, once using only "do-while"
loops, and once using only infinite loops and break/continue (do not use
break or continue in any of the other solutions). Also, include a
brief paragraph explaining the advantages of each loop construct in general,
and then indicate which of the four approaches is best for this particular
problem (whichever one you chose) and why so.
-
Bonus/Optional:
paintingDominoes
In the file Hw4PaintingDominoes.java, paint a set of 28 dominoes
matching the following figure as closely as possible (while only painting
rectangles and ovals):
![](images/dominoes.png)
Note: The same restrictions from above apply. In particular, you
may not use drawLine, but instead must paint the white lines using fillRect
in some way.
Note: You must use loops appropriately here, and you also should use
well-chosen helper methods (say, for painting the dots)
Note: On close inspection, you may notice that the corners of the
dominoes are rounded slightly. You are not responsible for this
effect.
Carpe diem!