Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hello, this is for programming with the java language. This is for lab class. Its dealing with stacks and we are learning how to make
Hello, this is for programming with the java language. This is for lab class. Its dealing with stacks and we are learning how to make constructor classes that also have implementation code in it as well such as finding a max value. Please help, this assignment has 3 parts. 1st part is creating a code called Pez.java that does Uses stacks and we want to get all green strings out of our stack. Note that it mentions to use a Stack ADT, I think they are referring to the Stack.java constructor program, Im not sure. 2nd part Im not really sure what its asking actually. I have included all codes that I believe have to do with this part of the assignment. Please make it clear which code is what and separate them as I did In order to symbolize that its a separate different program. Thank you very much!
Important programs by Professor, includes:
-StackException. Java = I believe it is a error thing for stacks.
-StackInterface.java = I believe this is the constructor class for the stack programs... maybe not the stacks.java program makes more sense but Ill include this way because you might understand it more than I do.
-Node.java = I believe this is the constructor class of Node programs
-Stack.java = this is a constructor program and implementation program * I believe in part 1 this is the Stack ADT program they were referring to...
-JCFStack.java = for part 2
//////////////////////////////////////////////////////////////////////
//StackException.java constructor program
public class StackException extends RuntimeException {
public StackException(String s) {
super(s);
} // end constructor
} // end StackException
/////////////////////////////////////////////////////////////
//StackInterface.java Constructor Program
public interface StackInterface {
public boolean isEmpty();
// Determines whether the stack is empty.
// Precondition: None.
// Postcondition: Returns true if the stack is empty;
// otherwise returns false.
public void popAll();
// Removes all the items from the stack.
// Precondition: None.
// PostCondition: Stack is empty.
public void push(Object newItem) throws StackException;
// Adds an item to the top of a stack.
// Precondition: newItem is the item to be added.
// Postcondition: If insertion is successful, newItem
// is on the top of the stack.
// Exception: Some implementations may throw
// StackException when newItem cannot be placed on
// the stack.
public Object pop() throws StackException;
// Removes the top of a stack.
// Precondition: None.
// Postcondition: If the stack is not empty, the item
// that was added most recently is removed from the
// stack and returned.
// Exception: Throws StackException if the stack is
// empty.
public Object peek() throws StackException;
// Retrieves the top of a stack.
// Precondition: None.
// Postcondition: If the stack is not empty, the item
// that was added most recently is returned. The
// stack is unchanged.
// Exception: Throws StackException if the stack is
// empty.
} // end StackInterface
//////////////////////////////////////////////////////////////
//Node.java constructor program
public class Node {
private Object item;
private Node next;
public Node(Object newItem) {
item = newItem;
next = null;
} // end constructor
public Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
} // end constructor
public void setItem(Object newItem) {
item = newItem;
} // end setItem
public Object getItem() {
return item;
} // end getItem
public void setNext(Node nextNode) {
next = nextNode;
} // end setNext
public Node getNext() {
return next;
} // end getNext
} // end class Node
///////////////////////////////////////////////////////////////////////
//Stack.java
public class Stack {
private Node top;
public Stack() {
top = null;
} // end default constructor
public boolean isEmpty() {
return top == null;
} // end isEmpty
public void push(Object newItem) {
top = new Node(newItem, top);
} // end push
public Object pop() throws StackException {
if (!isEmpty()) {
Node temp = top;
top = top.getNext();
return temp.getItem();
}
else {
throw new StackException("StackException on " +
"pop: stack empty");
} // end if
} // end pop
public void popAll() {
top = null;
} // end popAll
public Object top() throws StackException {
if (!isEmpty()) {
return top.getItem();
}
else {
throw new StackException("StackException on " +
"peek: stack empty");
} // end if
} // end top
public Object clone() throws CloneNotSupportedException
{
Stack copy = new Stack();
Node curr = top, prev = null;
while (curr != null)
{
Node temp = new Node(curr.getItem());
if (prev == null)
copy.top = temp;
else
prev.setNext(temp);
prev = temp;
curr = curr.getNext();
}
return copy;
}
// Your findMax instance method goes HERE
} // end Stack
//////////////////////////////////////////////////////////////////////
// JCFStack.java program
public class JCFStack
{
public static void main(String[] args) throws CloneNotSupportedException
{
Stack stack1 = new Stack();
stack1.push(new Integer(27));
stack1.push(new Integer(0));
stack1.push(new Integer(-3));
stack1.push(new Integer(-18));
stack1.push(new Integer(99));
printStack (stack1);
}
private static void printStack (Stack s) throws CloneNotSupportedException
{
Stack tempStack = (Stack) (s.clone());
if (! tempStack.isEmpty())
System.out.println("*** Printing Out Stack: ");
while (! tempStack.isEmpty())
{
System.out.println(tempStack.peek());
tempStack.pop();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started