These are to be turned in as hard copy. You can write solutions out by hand, or write them on your computer and print them.
Papers must not have ragged, spiral-notebook edges. Multiple page solutions must be stapled. Solutions not following these guidelines will be returned ungraded. I regret this rigidity, but it is necessary given the number of students in CSSE 230 this term.
Late days may not be used for written assignments.
(30 points) Weiss problems 4.29 and 4.30. This isn’t exactly a “written problem”, but it comes from the book and requires very little code (if done correctly), so I’m including it in this written assignment.
Starting code is in your individual SVN repository. (See the first programming assignment for your repository URL.) Use Subclipse to check out the project
CountMatches
. It includes JUnit tests for all of the parts. I’ve put
TODO
items in the code indicating where you should add your solution and giving some additional tips for each part of the problem.
When you finish the
TODO
items and pass the tests, just commit your solution back to your repository. You don’t need to turn in this part on paper.
Eclipse provides a view that will list all the outstanding
TODO
and
FIXME
comments in a project. From the Eclipse Menus choose
Window → Show View → Tasks. The new view should appear at the bottom. You can double click on an item in the task list to jump to the corresponding comment in the code. (Eclipse 3.4.0 had a bug where this view wouldn’t update immediately. If you don’t see any
TODO
items for the CountMatches project, choose
Project → Clean...
to regenerate the list for
CountMatches
.)
for (int i=0; i < n; i++) sum++;
for (int i=0; i < n; i += 2) sum++;
for (int i=0; i < n; i++) for (int j=0; j < n; j++) sum++;
for (int i=0; i < n; i++) sum++; for (int j=0; j < n; j++) sum++;
These problems are for you to think about and convince yourself that you could do them. It would be good practice to actually do them, but you are not required to turn them in.
Weiss Exercise 6.5. (Be sure to begin thinking about this one early. You may have to come back to it a few times before you figure out what to do.) Here is a more detailed explanation:
The goal of this problem is to implement a collection data type that provides three operations and does them efficiently:
push(obj); // adds obj to the collection. obj = pop(); // removes and returns the most recently-added object. obj = findMin(); // returns the smallest object (assume that all // of the objects in the collection are Comparable).
A single stack can do the first two operations in constant time, but not the third. But if our implementation uses TWO stacks to represent a collection, it is possible to do each of the three operations in constant time. Is should be obvious that our new data structure’s
push
method will have to do more work (than the
push
operation for an ordinary stack would have to do) in order for
findMin
to also be a constant-time operation. All you need to do is show the code for the three operations. You may assume that there is already a
Stack
class that has its own
push
,
pop
, and
top
methods. The code for each of the three methods that you must provide is short. I hope that by making things a bit more specific, it will make it easier for you to get started on this problem.
To further assist you in understanding this problem, here is a framework in which your answers could go: A class declaration with stubs for the three methods you are supposed to write. Each of those methods should be constant time (no loops or recursion)
public class StackWithMin<E> { // TODO: Give these two stacks names indicating what they are used for: private Stack<E> stack1, stack2; public StackWithMin() { this.stack1 = new Stack<E>(); this.stack2 = new Stack<E>(); } public void push(E element) { /* TODO: fill in the details */ } public E pop() {/* TODO: fill in the details */ } public E findMin() {/* TODO: fill in the details */ } }