CSci 2101 Problem set 3: Java methods and stacks

Due Wednesday, February 8th at 11:59pm (by e-mail)

42 points

Problem 1 (8 points + up to 6 points of extra credit)

Write and test a method isPalindrome that takes a string and returns true if the string is a palindrome (i.e. it reads the same forward and backward) and false otherwise. Some examples of palindromes are "civic" and "tenet", and "tot". Note that "Bob" is not a palindrome since lower-case characters are different from upper-case. Neither are strings with spaces and additional charcaters, such as "bird rib" (because of the space).

For 3 points of extra credit modify the program to ignore spaces, commas, and periods so that "bird rib" is considered a palindrome.

For another 3 points of extra credit make the program recognize that upper-case letters match the corresponding lower-case letters without listing all 26 letters of the alphabet. Hint: check out methods in the String class or the Character class (or you can do it even without using predefined methods). In this case (and assuming you have implemented the first extra credit part) "A Santa at Nasa" would be a palindrome.

Problem 2 (8 points)

Write and test a method randomAverage that takes three integers: lower bound of random numbers, upper bound of a random numbers (non-inclusive), and the number of the numbers you'd like generated. For instance, when called as randomAverage(1, 5, 10), the method returns the average of 10 numbers between 1 (inclusive) and 5 (non-inclusive, so 4 is the highest value).

The method must return the average of these numbers as a double. Recall that when dividing two integers in Java you get the result as an integer even if you are assigning it to a double variable, for instance:


int num1 = 5;
int num2 = 2;
double result = num1/num2;
produces 2. You need to convert one of the operands to a double before you divide to get the desired result:

int num1 = 5;
int num2 = 2;
double result = ((double) num1)/num2;
Note the placement of parentheses in the above example!

The method must return 0 if the number of random numbers is less than 1 or if the upper bound is lower than the lower bound. Make sure to test for these cases.

What happens to the average if you keep the bounds the same and keep increasing the number of random numbers? Document your observation in a comment.

Problem 3: copying a stack (9 points)

Write a method that takes a stack of Integers and creates and returns a copy of this stack, i.e. a stack that has the exact same elements in the same order. The given stack must be unchanged when the method is finished (make sure to print it out at the end to check this condition). Please include your test data when submitting the program.
Hint: think of additional storage that you need for this task.

Problem 4: Checking if two stacks are equal (10 points)

Recall that you may use only pop, push, peek, and empty methods when working with stacks.

Write and test a method that takes two stack of Integers and returns true when the two stacks contain the same elements in the same order and false otherwise. The two stacks must be in the original order when the method finishes (regardless of whether the stacks were the same or not). You might want to write a recursive method for this problem, but you don't have to.
Important: the program must work without a run-time error (for instance, when one of the stacks is empty and the other one is not, it should return false and not stop with an error). Carefully handle all cases when one stack has fewer elements than the other one.
Please submit all of your test data.

Problem 5 (7 points)

Write and test a method that takes a stack of integers (as Integer type) and returns the smallest element in the stack. The stack must be the same after the method as it was before (print it in main after the call to make sure). Test your method carefully and submit all your test data.

How to submit

Submit your Java file(s) to me by e-mail. The subject must be Problem Set N, where N is the problem set number. If working in a group, CC your group partner.


CSci 2101 course web site.