A. Create ArrayIntStack.java (one that\'s REALLY O(constant), totally!!!) B. Cre
ID: 3669463 • Letter: A
Question
A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!)
B. Create the Javadoc web page (separate Assignment #15b).
A. Create your own Stack Class, call it ArrayIntStack.java and write four public stack methods (this is the easy part of the assignment). Use at least one private helper method, to ensure capacity of the array. All other methods in your ArrayIntStack must have O(constant) run-time. Your ArrayIntStack extends no other Classes from the Java API. You must provide a default constructor.
1. boolean empty(); Tests if this stack is empty.
2. int peek(); Looks at the object at the top of this stack without removing it from the stack.
3. int pop(); Removes the object at the top of this stack and returns that object as the value of this function.
4. int push(int item); Pushes an item onto the top of this stack, return what was pushed.
5. private class IntStackIterator { // similar to what our author shows for the ArrayIntListIterator.
6. public IntStackIterator iterator() method so the client can browse through the data with .next() etc...
Since we're using int for return, you must throw an "appropriate" Exception (e.g. EmptyStackException) for illegal peek, pop, etc... operations. Regarding size, let's say this remains O(constant) while the Stack size is less than 20 elements, past that the push(int) would need to throw an Exception
Explanation / Answer
make 3 files:
1) Stack.java
public interface Stack
{
public void push( int x ) throws StackException;
public int pop ( ) throws StackException;
public int peek ( ) throws StackException;
}
2) StackException.java
public class StackException extends Exception
{
public StackException( String message )
{
super( message ); // Calls Exception's constructor
}
}
3) ArrayIntStack.java
public class ArrayIntStack<T> {
protected int sp; // empty stack
protected T[] head; // array
private int size;
@SuppressWarnings("unchecked")
private ArrayIntStack() {
sp = -1;
size = 24; // sets the default size of the stack
head = (T[]) new Object[size];
}
public boolean isFull() {
return sp == this.size -1;
}
public boolean isEmpty() {
return sp == -1;
}
public T push(T t) throws StackException{
if (!isFull()){
head[++sp] = t;
return head[sp];
}
else{
throw new StackException("Stack overflow");
}
}
public T peek(T t) throws StackException{
if (isEmpty()) {
throw new StackException("Stack empty");
} else
return head[sp];
}
public T pop() throws StackException{
if (isEmpty()) {
throw new StackException("Stack emtpy");
} else
return head[sp--];
}
public static void main(String[] args) {
// Array Implementation
ArrayIntStack<String> as = new ArrayIntStack<String>();
String s = "Hello";
String s1 = "World";
String s2 = "Again";
try{
as.push(s);
as.push(s1);
as.push(s2);
System.out.println(as.pop());
System.out.println();
System.out.println(as.pop());
System.out.println();
System.out.println(as.pop());
System.out.println();
}
catch ( StackException e )
{
System.out.println("Error detected: " + e.getMessage() );
System.exit(1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.