## CSci 2101 Problem set 3: Java methods and stacks

### 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)

• Do not use any Stack methods other than `pop`, `push`, `peek`, and `empty` for this problem.
• Use `new` to create new stacks.
• Make sure to test your program thoroughly. Submit all your test data and a brief description of results. If you change anything in your program, make sure to retest it on all previous test cases to make sure that you are still getting the same results.
• Your main should contain the initial test data, test calls to the method described below, and any printing needed at the end to make sure that the program works as expected.

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.

#### 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.
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.