Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is one problem in 2 parts you must use part 1 to solve part 2 so this must be answered in total Hint: Use

This is one problem in 2 parts you must use part 1 to solve part 2 so this must be answered in total

Hint: Use Stack.peek()

image text in transcribed

image text in transcribed

PLATFORM INDEPENDENCE Please see Syllabus for more information. Your solution has to be platform-independent; it must run on any platforms including any online IDEs: You solution should be free of any use of packages. Example, absolutely no . /* Java Program Example - Java Import Packages package YOUR_CUSTOM_PACKAGE HERE; Your solution, regardle of how many classes it has, should be in one .java file. Your solution has to run on any online IDEs. - If your solution fails the platform-independence criteria, you will have to resubmit work subject to the late penalty. - The Late Work policy applies for all late submissions. PROBLEM 1 (Use java.util.Stack) (10 points) Design a stack class by importing the available java.util.Stack to have the following features: push(x) -- push element x onto stack, where x is anywhere between Integer.MIN_VALUE and Integer.MAX_VALUE. .pop() -- remove the element on top of the stack. top() -- get the top element. getMax() -- retrieve the max element in the stack in constant time (i.e., O(1)). STARTER CODE Your code should have the following shape and form, all in one.java file. Note the styling and documentation API already included for the target class, MaxStack. import java.util.Stack; public class Solution1 { public static void main(String[] args) { // Your main() is not graded so you can // have any implementation in this area MaxStack obj = new MaxStack(); obj.push(12); obj.push(1); obj.push(-12); obj.pop(); System.out.println(obj.top()); System.out.println(obj.getMax()); // etc. } } /** * The MaxStack program implements a Stack class with the following features: * push(x) -- push element x onto stack -- remove the element on top of the stack top() -- get the top element. * getMax() -- retrieve the max element in the stack in constant time (i.e., 0(1) */ class MaxStack { // Initialize your data structure in constructor * pop() // or here; choice is yours. public MaxStack() { // YOUR CODE HERE } public void push(int x) { // YOUR CODE HERE } public void pop() { // YOUR CODE HERE } public int top() { // YOUR CODE HERE } public int getMax() { // YOUR CODE HERE } } EXAMPLES MaxStack maxStack = new MaxStack(); maxStack.push(-2); maxStack.push(0); maxStack.push(-3); maxStack.getMax(); // returns 0 maxStack.pop(); maxStack.top(); // returns 0 maxStack.getMax(); // returns 0 CONSTRAINTS AND ASSUMPTIONS For this problem you are ONLY allowed to use Java's reference class Stack e. Failure to do so will receive 5 points off. MaxStack does not mean elements have to be ordered in increasing or decreasing values in the Stack. For this problem, pop() will not be called on an empty Stack which means you won't have to handle EmptyStackException. Error handling is not required for this course.

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

Online Market Research Cost Effective Searching Of The Internet And Online Databases

Authors: John F. Lescher

1st Edition

0201489295, 978-0201489293

More Books

Students also viewed these Databases questions