Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the work I have for part one of the assignment: import java.util.EmptyStackException; public class ArrayStack { private double[] stack; private int size; private

Here is the work I have for part one of the assignment:

import java.util.EmptyStackException;

public class ArrayStack {

private double[] stack;

private int size;

private int index = 0;

public ArrayStack(int size) {

this.stack = new double[size];

this.size = size;

}

private boolean isFull(){

return this.index >= this.size;

}

private boolean isEmpty(){

return this.index == 0;

}

public void push(double item) throws StackOverflowError{

if(this.isFull()){

throw new StackOverflowError("Stack has reached it's size limit");

}

this.stack[index++] = item;

}

public double pop() throws EmptyStackException{

if(this.isEmpty()){

throw new EmptyStackException();

}

return this.stack[--index];

}

public double peek(int n) throws ArrayIndexOutOfBoundsException{

if(n < 0 || n >= size){

throw new ArrayIndexOutOfBoundsException("Requested position not in the stack's range");

}

return this.stack[n];

}

}

And my TestArray stack both work as intended. Here is that:

import java.util.EmptyStackException;

import java.util.InputMismatchException;

import java.util.Scanner;

public class TestArrayStack {

static ArrayStack stack;

public static void startTests(int n){

System.out.println("Creating stack of size "+n+" ...");

try{

TestArrayStack.stack = new ArrayStack(n);

}catch(Exception e){

System.out.println("Failed test : Creation test Exception : "+e.getMessage());

System.exit(0);

}

System.out.println("Creation test passed succesfully! ");

/////////////////////////////////////////////////////////////////////////

System.out.println("EmptyStackException test ...");

try{

TestArrayStack.stack.pop();

System.out.println("Failed Test : Exception was not thrown for popping a value from an empty stack");

System.exit(0);

}catch(EmptyStackException e){

System.out.println("EmptyStackException test passed succesfully! ");

}

/////////////////////////////////////////////////////////////////////////

System.out.println("Pushing values in the stack ...");

double[] values = new double[n+1];

for(int i=0; i < n; i++){

values[i] = Math.round(Math.random()*100);

try{

TestArrayStack.stack.push(values[i]);

}catch(StackOverflowError e){

System.out.println("Failed test : Pushing values into stack Exception : "+e.getMessage());

System.exit(0);

}

}

System.out.println("Pushing test passed succesfully! ");

/////////////////////////////////////////////////////////////////////////

System.out.println("StackOverFlow test ...");

try{

TestArrayStack.stack.push(0);

System.out.println("Failed Test : Exception was not thrown for depassing the stack's size");

System.exit(0);

}catch(StackOverflowError e){

System.out.println("StackOverFlow test passed succesfully! ");

}

/////////////////////////////////////////////////////////////////////////

System.out.println("Peeking into stack ...");

try{

TestArrayStack.stack.peek(-1);

System.out.println("Failed Test : Exception was not thrown for accessing a position out of the stack's range");

System.exit(0);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("Negative position test passed");

}

try{

TestArrayStack.stack.peek(n+1);

System.out.println("Failed Test : Exception was not thrown for accessing a position out of the stack's range");

System.exit(0);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("OutOfBound position test passed");

System.out.println("ArrayIndexOutOfBoundsException test passed successfully!");

}

int pos = (int)Math.round(Math.random()*n);

double value = TestArrayStack.stack.peek(pos);

if(value != values[pos]){

System.out.println("Failed test : Returned value from stack is not related to the position");

System.exit(0);

}else{

System.out.println("Accessing test passed successfully! ");

}

/////////////////////////////////////////////////////////////////////////

System.out.println("Popping values from stack ...");

for(int i=n-1; i >= 0; i--){

value = TestArrayStack.stack.pop();

if(value != values[i]){

System.out.println("Failed test : Returned value from stack is not related to the position");

System.exit(0);

}

}

try{

TestArrayStack.stack.pop();

System.out.println("Failed test : Stack not fully emptied");

System.exit(0);

}catch(EmptyStackException e){

System.out.println("Popping test passed successfully");

}

/////////////////////////////////////////////////////////////////////////

System.out.println("ALL TESTS PASSED SUCCSSEFULL");

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

try{

System.out.print("Provide the stack's size to begin tests : ");

int n = sc.nextInt();

TestArrayStack.startTests(n);

}catch(InputMismatchException e){

System.out.println(e.getMessage());

}

}

}

So there is a part two of the assignment which I will post below. My question is how do I start by beginning to add these into what I have. These stack arrays are completely new to me and I have gotten not to far with it. Can anyone point me in the right direction?

The part two of the assignment in which I don't know where to begin to add these stacks or understand the first one as explained:

Implement a pure abstract stack class named AbstractStack that has no implementation. It will not have a private array of double, because that is an implementation detail of ArrayStack that would not be found in other implementations such as LinkedStack. Nor will it have the constructor public AbstractClass(int size), or methods public double peek(int n) or public int count(), since these methods are convenience features that are easily implemented for ArrayStack, but not other implementations of stack.

Include the method public double peek(), and add the method public void clear(), resulting in the following abstract methods.

opublic abstract void push(double item) -Standard stack action; Throws an exception if the stack is full.

opublic abstract double pop() - Standard stack action; Throws an exception if the stack is empty.

opublic abstract boolean isEmpty() - Returns true if the stack is empty and false otherwise.

opublic abstract double peek() - returns the value of the item located at the specified position on the stack; throws an exception if the array bounds are exceeded or if a nonexistent element is requested.

opublic abstract void clear() - empties the stack if it is not already empty.

Do not specify a constructor. Depend on the default constructor.

Modify the existing ArrayStack.java file to include the phrase "extends AbstractStack."

Add an implementation for the methods void clear() and double peek(), which overloads the existing double peek(int n). Provide a default constructor that creates an array that will hold three elements.

opublic ArrayClass(int size) - constructor that creates an ArrayClass instance containing an array of the specified size. Remember that in Java, arrays are indexed from 0!

opublic push(double item) - standard stack action; throws an exception if the array bounds are exceeded.

opublic double pop() - standard stack action; throws an exception if the array bounds are exceeded.

opublic boolean isEmpty() - returns true if the stack is empty and false otherwise.

opublic double peek(int n) - returns the value of the item located at the specified position on the stack; throws an exception if the array bounds are exceeded or if a nonexistent element is requested; peek(0) will return the top element of the stack.

opublic int count() - returns the number of items currently pushed onto the stack.

Modify the existing TestArrayStack.java file to include tests for void clear() and double peek(). Note that it is easiest to do "incremental testing" in which you code a little then test a little.

Create* an interface with additional methods.

Create* a new file called Forth.java. Make this a public interface file. Add the following methods to the interface.

opublic add() - pops two values from the stack, adds them together, and pushes the result back onto the stack. Throws an exception if there are not at least 2 items on the stack.

opublic sub() - pops two values from the stack, subtracts the second number popped from the first number popped, and pushes the result back onto the stack. Throws an exception if there are not at least 2 items on the stack.

opublic mult() - pops two values from the stack, multiplies them together, and pushes the result back onto the stack. Throws an exception if there are not at least 2 items on the stack.

opublic div() - pops two values from the stack, divides the second number popped by the first number popped, and pushes the result back onto the stack. Throws an exception if there are not at least 2 items on the stack.

opublic dup() - peeks at the top value on the stack and pushes a copy of that value onto the stack. Throws an exception if the stack is empty or full.

opublic twoDup() - peeks at the top two values on the stack and pushes a copy of both values onto the stack (in the same order). Throws an exception if the stack does not have at least 2 items or room for 2 additional items.

Create* another file called ForthStack.java. Make this file extend ArrayStack and implement Forth. Code concrete implementations of each of the methods in the interface.

Test by making a copy of the file TestArrayStack.java and name it TestForth. Change the name of the class inside the file. Add tests for add, sub, mult, div, dup, and twoDup.

Where do I begin these strings in the code I have?

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

\f

Answered: 1 week ago