Computer Science APEA 15-100, Summer 2009
Homework 8
Due: Tue 14-Jul-2009 at 8:59am (email copy only)
(no late submissions accepted).
Read these instructions first!
public static void testAbcd() {
System.out.print("Testing abcd... ");
int n = abcd();
assert((n >= 1000) && (n < 10000));
assert(pow(n/1000,(n/100)%10)*pow((n/10)%10,n%10) == n);
System.out.println("Passed all tests!");
}
Helper Method: pow
In writing the "abcd" method, you will see that you will have to raise
an integer value to an integer power. Now, the Math class has a pow method,
but it raises a double value to a double power, and returns a double. We
could convert that into an int by rounding it and then truncating, but
that's a nuisance, especially if we have to do it more than once (which in
fact you must for the abcd problem). Instead, it would be handy to write
our own method, pow, that takes two integers and returns the integer value
of raising the first integer to the second integer. Because we are writing
this pow method specifically to help us with the abcd method, we say the pow
method is a
helper method. Using helper methods is a form of
decomposition,
breaking a problem up into smaller, more easily manageable problems. Here
is the complete description for the helper method pow that you must write to
solve the abcd problem:
Write the following method:
public static int
pow(int a, int b)
This method takes two integers, a and b, and returns the integer value ab.
If b is less than 0, the method just returns 0. As usual, do not worry
about overflow. Here is a test method for you:
public static void testPow() {
System.out.print("Testing pow... ");
assert(pow(2, -1) == 0);
assert(pow(2, 0) == 1);
assert(pow(2, 1) == 2);
assert(pow(2, 5) == 32);
assert(pow(-2, 5) == -32);
assert(pow(-2, 6) == 64);
System.out.println("Passed all tests!");
}
Note: When writing pow, you must use a loop, and you
may not use any Math methods. In the loop, keep multiplying by "a", and
keep doing this until you have multiplied "b" times.
public static void testBinaryToDecimal() {
System.out.print("Testing binaryToDecimal... ");
assert(binaryToDecimal("0") == 0);
assert(binaryToDecimal("1") == 1);
assert(binaryToDecimal("10") == 2);
assert(binaryToDecimal("11") == 3);
assert(binaryToDecimal("01001") == 9);
assert(binaryToDecimal("11001") == 25);
assert(binaryToDecimal("This is not a binary number!") == -1);
assert(binaryToDecimal("") == -1);
assert(binaryToDecimal(null) == -1);
System.out.println("Passed all tests!");
}
Hint #1: Not sure about counting in binary? Try this page:
http://en.wikipedia.org/wiki/Binary_numeral_system#Counting_in_binary
Or for more fun, try this page:
http://en.wikipedia.org/wiki/Finger_binary
Hint #2: In base 10 (decimal, what you are used to), say you have a number (say, 32) and you wish to add a digit (say, 4) to its right end (hoping to get 324). To do this, multiply the number by 10 (320 * 10 = 320) and add the new digit (320 + 4 = 324 -- hurray!). The same approach works in binary, only instead of multiplying by 10 (the base of our decimal system), we multiply by 2 (the base of the binary system). So, if we have the binary number 110 (the number 6 in decimal), and we wish to add a 1 to the right, we multiply 110 times 2 (which is written as 10 in binary) to get 1100 in binary (or 12 in decimal, and indeed 6*2 = 12) and then add 1 to get 1101 in binary, which is 13 in decimal. Why is this a hint? Because this is the simple way to solve this problem. According to this algorithm, you can solve this problem by starting your answer at zero, and iterating over the string from left-to-right, where for each character (representing a digit) you double your current answer and then add the next digit to it. That's it.
Hint #3: For this and other problems in this assignment, you may need to determine the length of a string. This is done with the length method, as the following code demonstrates:
class MyCode {
public static void main(String[] args) {
String s = "Carpe diem";
int n = s.length();
System.out.println(n); // prints 10
}
}
(larger
image with details)
(larger
image with details) public static void testWordsearchContains() {
System.out.print("Testing wordsearchContains... ");
// These tests cover this wordsearch:
// t a c w
// n o o c
// d o g x
assert(wordsearchContains("tacw|nooc|dogx", "cat"));
assert(wordsearchContains("tacw|nooc|dogx", "cod"));
assert(wordsearchContains("tacw|nooc|dogx", "coon"));
assert(wordsearchContains("tacw|nooc|dogx", "dog"));
assert(wordsearchContains("tacw|nooc|dogx", "ox"));
assert(!wordsearchContains("tacw|nooc|dogx", "caw"));
assert(!wordsearchContains("tacw|nooc|dogx", "cow"));
assert(!wordsearchContains("tacw|nooc|dogx", "con"));
assert(!wordsearchContains("tacw|nooc|dogx", "dogs"));
assert(!wordsearchContains("tacw|nooc|dogx", "ax"));
System.out.println("Passed all tests!");
}
Note: This approach of representing a 2-dimensional board in a 1-dimensional string is not a very good idea. Soon, we will learn about other ways we can more naturally represent this data, such as in a 2-dimensional array.
Carpe diem!