These are to be turned in to the WA2 Drop Box. You can write solutions out by hand and scan them (there is a networked scanner in F-217), or create a file on your computer directly. After you have submitted, click on the drop box again to verify that your submission was successful.
A single late day may be used or earned for a written assignment: you must turn in all parts early to earn a late day; if you turn in any part late, you will use a late day.
Problem numbers [in square brackets] are the numbers from the 3rd edition of the Weiss book.
public
static <T
extends Comparable<?
super
T>> T min(T arg1, T arg2)
public
static <T
extends Comparable<?
super
T>> T max(T arg1, T arg2)
public
static
<T
extends
Comparable<?
super
T>> T min(T[] args)
public
static <T
extends Comparable<?
super
T>> T[] max2(T[] args)
Creating the Generic array for the return value of the last part is also tricky; here is a way to do it:
T[] result = (T[]) Array.newInstance(args[0].getClass(), 2);
for (int i=0; i < n; i++) for (int j=0; j < n * n; j++) sum++;
for (int i=0; i < n; i++) for (int j=i; j < n; j++) sum++;
for (int i=0; i < n; i++) for (int j=0; j < i * n; j++) for (int k=0; k < n; k++) sum++;
for (int i=n; i >= 1; i = i / 2) 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 [6.5]. (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. We 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 */ } }