Answered step by step
Verified Expert Solution
Link Copied!

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!
image text in transcribed
image text in transcribed
image text in transcribed
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();
}
}
}
The Pez Candy Program Suppose we have a stack of strings representing the colors of Pez candy as shown below. We're interested in writing a method for a program which uses the Stack class that can remove all the green candy from this stack of Pez. After removing our green candy, our non-green stack candy should maintain its prior order. reen red yellow yellow green green red yellow ili reid Now, let's write a Pez program that uses the Stack ADT. First off, write a method called addPez that adds eight strings to a stack in the proper order to create the stack above Include a call to your method from the main method. Now, write a class method called removeGreen that it takes a stack object of string values, and removes all green candy. Hint: Create an empty temporary stack, push onto it all non-green candy from your original stack, and then reverse your temp stack. You'll be emptying out your original stack temporarily, and then simply pushing back onto it all the items from your temp stack The Pez Candy Program Suppose we have a stack of strings representing the colors of Pez candy as shown below. We're interested in writing a method for a program which uses the Stack class that can remove all the green candy from this stack of Pez. After removing our green candy, our non-green stack candy should maintain its prior order. reen red yellow yellow green green red yellow ili reid Now, let's write a Pez program that uses the Stack ADT. First off, write a method called addPez that adds eight strings to a stack in the proper order to create the stack above Include a call to your method from the main method. Now, write a class method called removeGreen that it takes a stack object of string values, and removes all green candy. Hint: Create an empty temporary stack, push onto it all non-green candy from your original stack, and then reverse your temp stack. You'll be emptying out your original stack temporarily, and then simply pushing back onto it all the items from your temp stack

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Progress Monitoring Data Tracking Organizer

Authors: Teacher'S Aid Publications

1st Edition

B0B7QCNRJ1

More Books

Students also viewed these Databases questions