In this problem we will use recursion to write a simple Java program that contains a couple of class methods.
It's a good idea to have a labs directory in your account.
You can put all your labs from the semester in this folder. Create a labs directory, and then create a subdirectory
named lab2,
under your labs directory, and make it your working directory, as shown below. You can move your lab1 folder from last week into your labs folder as well.

Then follow these steps:
Recurse.java. emacs Recurse.java & emacs & and then type your
file and save (C-x-s) it as Recurse.javapower() to Recurse.java.
This method should compute x^n, according to the following
recursive definition:
|
x^0 = 1.0 |
for all x |
|
x^n = x * x^(n-1) |
for n>0 |
main() method to your program, and write a
few lines of code to invoke power().
Compile and run your program to make sure it works
correctly. Think about some good test cases to use!
morePower(),
to compute x^n according to the following alternative definition:
|
x^n = x^(n/2) * x^(n/2) |
for even values of n |
|
x^n = x * x^(n/2) * x^(n/2) |
for odd values of n |
morePower() (here is a mere wisp of a start of testing)
DecimalFormat class in the Java API)
If you had to choose between the two implementations above, which one would you prefer? Why? Write your answer in the comments of the method.
When you are done, just for practice with the submit command, submit your Recurse.java
program electronically by executing the following
command, while still in your lab2 directory:
submit cs230 powers *.*
powers,
under your drop directory. The powers directory will contain all the files
in your currently working directory.
LabStringUtilities (in a new file called LabStringUtilities.java), that contains a public class method called isPalindrome().
This method takes a String as an argument, and returns a boolean value
indicating whether the input string is a palindrome or not. As a reminder, a string is a palindrome
if it reads the same from left to right as it reads from right to left.
In the main() method of your class, add code to test the isPalindrome().
For easier testing, the argument to isPalindrome() can come as a command-line argument,
i.e. by accessing the contents of args[0] from within your main() method.
Test your method many times. Is it easier to test this method, with
its command-line argument, or the ones in Problem1?
LabStringUtilities,
that, given a String s, returns a string obtained from s by removing
all vowels. For example, calling the method on "i love cs230" should
return the string "lv cs230". Provide some testing code for this method.
main() method that allow the user to choose from three options:
So then the usage would be something like:
java LabStringUtilities "I like to do somersaults" 2
would result in this output:
lk t d smrslts
DigitPlay, that contains a public class method called numDigits().
This method takes a positive integer as an argument, and counts and returns the number of digits in the input number.
For example, if the method is passed the number 123456 as argument, it should return 6, since there are 6 digits
in the input number.
sumDigits() that, again takes a positive integer as an
argument and returns the sum
of the digits in the input number. For example, if the method is passed the number 123456 as argument, it should
return the integer 21, since 1+2+3+4+5+6 = 21. The definition of this method will be very similar to the
numDigits() one.
Both of the above methods should be defined recursively. As always, test each method as soon as you define it, before you proceed to add more code to your program.
Add one more method to your DigitPlay program, called validateNumber()
that takes a positive integer, and determines if it is a possible AMEX Traveler’s check id number.
(Question: If the sum is indeed divisible by 9, can you tell for sure that the input number is a correct
AMEX Traveler’s check id number?)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1