CSci 2101 Problem set 3: Java stacks, arrays, and array lists

Due Tuesday, February 16th at 11:59pm (by e-mail)

38 points

Problem 1: Checking if two stacks are equal (15 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 2 (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.

Problem 3 (8 points)

Write and test a method that takes an ArrayList of integers and prints (doesn't return) its two smallest elements. Test your method carefully and submit all your test data.

Problem 4 (8 points)

Write and test a method that takes a string and returns an array or an array list of all its substrings, in any order. For instance, if the parameter string is "dog", the returned array (or an array list) will be ["d", "do", "dog", "o", "og", "g"] or some other ordering of the same strings. If the string has repeated letters then some of the substrings will be repeated, that's ok.

Hint: use nested loops, one for index of the starting character of the substring, and within it one for the index of the ending character. Use two different loop variables (such as i and j) for the two loops.

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.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.