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 class 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 simply taking the code I provided and comparing your written out assumption of what the code does. 2nd and 3rd is where we take the codes given and modify them as the assignment below asks. 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
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
-MyStack.java = is the program needed for part 1
-MaxTest.java = program needed for part 2a
-Stack.java = this is a constructor program and implementation program for we need modify this code for part 2b
//////////////////////////////////////////////////////////////////////
//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
/////////////////////////////////////////////////////////////
//MyStack.java Program
import java.io.*;
public class MyStack
{
public static void main(String[] args) throws IOException, 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));
stack1.pop();
stack1.pop();
int x = ((Integer)stack1.top()).intValue();
stack1.push (new Integer(-12));
stack1.push (new Integer(x));
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.top());
tempStack.pop();
}
}
}
///////////////////////////////////////////////////////////////////////
//MaxTest.java Program
import java.io.*;
public class MaxTest
{
public static void main(String[] args) throws IOException, CloneNotSupportedException
{
Stack stack1 = new Stack();
stack1.push("Andy");
stack1.push("Allison");
stack1.push("Aaron");
stack1.push("Austin");
stack1.push("Abbey");
stack1.push("Alex");
System.out.println("findMax(stack1)="+findMax(stack1));
//System.out.println("stack1.findMax()="+stack1.findMax());
}
}
///////////////////////////////////////////////////////////////////////
//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
Building and Tracing a Stack Draw the stack that would be created following the code segment below. Use your Stack ADT handout as a guide. Stack stack! new Stack(); stackl.push (new Integer (27)); stackl.push (new Integer (0)); stackl.push (new Integer (-3)); stackl push (new Integer (-18)): stackl.push (new Integer (99))2 stackl.pop() stackl.pop (); int x ((Integer) stackl.top()).intValue stacki.push (new Integer (-12)) stackl.push (new Integer (x)); Now, compile and run the MyStack program to check your results. Writing Simple Class Methods &Instance Methods with Stacks Add a value-returning class method named findMax to your MaxTest program that can be used to return the largest String value stored in a stack of string values. By largest, we mean last in lexicographic (dictionary) order. Your method will have one parameter, a stack object, and should not modify this parameter. You are writing this method assuming you are a user of the Stack elass. Here, you'll merely be using the

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

Step: 3

blur-text-image

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

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago