Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem #2 (10 marks) Design and implement a special kind of stack (lets call it FL_Stack which stand for Fixed Length Stack). A FL_Stack behaves

Problem #2 (10 marks) Design and implement a special kind of stack (lets call it FL_Stack which stand for Fixed Length Stack). A FL_Stack behaves exactly like a regular stack except that it abandons the first element which is pushed into the stack at first when the stack is full, and a new element is pushed into the stack. In other words, the stack does not expand and keeps the initial capacity. Implement the FL_Stack class with all stack methods using an array. Do not extend the ArrayStack class. Add the following method: public T peekFirst(): returns a reference to the element at the bottom of this stack. The element is not removed from the stack. @return element on bottom of stack @throws EmptyCollectionException if stack is empty Write a driver program to test ALL methods. Display the content of the stack to show whether the stack works according to the description above.

please send me the solution in java as soon as possible. there is a sceleton code below.........

package jsjf;

import jsjf.exceptions.*; import java.util.Arrays;

/** * An array implementation of a stack in which the bottom of the * stack is fixed at index 0. * * @author Java Foundations * @version 4.0 */ public class ArrayStack implements StackADT { private final static int DEFAULT_CAPACITY = 100; private int top; private T[] stack; /** * Creates an empty stack using the default capacity. */ public ArrayStack() { this(DEFAULT_CAPACITY); } /** * Creates an empty stack using the specified capacity. * @param initialCapacity the initial size of the array */ public ArrayStack(int initialCapacity) { top = 0; stack = (T[])(new Object[initialCapacity]); } /** * Adds the specified element to the top of this stack, expanding * the capacity of the array if necessary. * @param element generic element to be pushed onto stack */ public void push(T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; } /** * Creates a new array to store the contents of this stack with * twice the capacity of the old one. */ private void expandCapacity() { stack = Arrays.copyOf(stack, stack.length * 2); } /** * Removes the element at the top of this stack and returns a * reference to it. * @return element removed from top of stack * @throws EmptyCollectionException if stack is empty */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack"); top--; T result = stack[top]; stack[top] = null; return result; } /** * Returns a reference to the element at the top of this stack. * The element is not removed from the stack. * @return element on top of stack * @throws EmptyCollectionException if stack is empty */ public T peek() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack"); return stack[top-1]; } /** * Returns true if this stack is empty and false otherwise. * @return true if this stack is empty */ public boolean isEmpty() { // To be completed as a Programming Project return true; // temp } /** * Returns the number of elements in this stack. * @return the number of elements in the stack */ public int size() { // To be completed as a Programming Project return 0; // temp } /** * Returns a string representation of this stack. * @return a string representation of the stack */ public String toString() { // To be completed as a Programming Project return ""; // temp } }

package jsjf.exceptions;

/** * Represents the situation in which a collection is empty. * * @author Java Foundations * @version 4.0 */ public class EmptyCollectionException extends RuntimeException { /** * Sets up this exception with an appropriate message. * @param collection the name of the collection */ public EmptyCollectionException(String collection) { super("The " + collection + " is empty."); } }

package jsjf;

/** * Defines the interface to a stack collection. * * @author Java Foundations * @version 4.0 */ public interface StackADT { /** * Adds the specified element to the top of this stack. * @param element element to be pushed onto the stack */ public void push(T element); /** * Removes and returns the top element from this stack. * @return the element removed from the stack */ public T pop(); /** * Returns without removing the top element of this stack. * @return the element on top of the stack */ public T peek(); /** * Returns true if this stack contains no elements. * @return true if the stack is empty */ public boolean isEmpty(); /** * Returns the number of elements in this stack. * @return the number of elements in the stack */ public int size(); /** * Returns a string representation of this stack. * @return a string representation of the stack */ public String toString(); }

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago

Question

To find the integral of 3x/(x - 1)(x - 2)(x - 3)

Answered: 1 week ago

Question

What are Fatty acids?

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago