Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please complete this Java lab project. The needed codes to complete the lab are provided below. public class stack { // Members private int top;

Please complete this Java lab project.

image text in transcribed

The needed codes to complete the lab are provided below.

public class stack { // Members private int top; // TODO private int maxSize; // TODO private String stackItems[]; public stack() { this.maxSize = 5; this.top = -1; this.stackItems = new String[maxSize]; } /** * @param maxSize - sets maximum size of the stack */ public stack(int maxSize) { this.maxSize = maxSize; this.top = -1; this.stackItems = new String[maxSize]; } /** * @return */ public boolean isFull() { return top == maxSize -1; // TODO } /** * @return */ public boolean isEmpty() { // TODO return false; // Possibly you will remove this line, this is for running Unit Tests before writing code

} /** * @return */ public int size() { // TODO return -1; // Possibly you will remove this line, this is for running Unit Tests before writing code

} /** * @return * @throws StackEmptyException */ public String peek() throws StackEmptyException{ if (! this.isEmpty()) return stackItems[top]; throw new StackEmptyException(); } /** * @return */ public String pop() { String item = null; // TODO return item; // Possibly you will remove this line, this is for running Unit Tests before writing code } /** * */ public void push(String item) { //TODO } public String printStackUp() throws StackEmptyException { String stackString = new String(); // TODO return stackString; // Possibly you will remove this line, this is for running Unit Tests before writing code }

}

---------------------------------------------------------------------------------------------------------------------------------

public class StackEmptyException extends Exception {

}

--------------------------------------------------------------------------------------------------------------------------------

public class StackFullException extends Exception {

}

-------------------------------------------------------------------------------------------------------------------------------

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import StackLab.*;

class stackLabTest {

@Test void testCreateStack() { // ARRANGE stack myStack = new stack(1); boolean actual; // ACT actual = myStack.isEmpty(); // ASSERT assertTrue(actual); }

@Test void testIsEmptyTrue() { // ARRANGE stack myStack = new stack(1); boolean actual, expected; // ACT actual = myStack.isEmpty(); expected = true; // ASSERT assertEquals(expected, actual); }

@Test void testIsEmptyFalse() throws StackFullException { // ARRANGE stack myStack = new stack(1); String item = "Java is Fun!"; boolean actual, expected; // ACT myStack.push(item); actual = myStack.isEmpty(); expected = false; // ASSERT assertEquals(expected, actual); }

@Test void testIsFullTrue() throws StackFullException { // ARRANGE stack myStack = new stack(1); String item = "Java is Fun!"; boolean actual, expected; // ACT myStack.push(item); actual = myStack.isFull(); expected = true; // ASSERT assertEquals(expected, actual); }

@Test void testIsFullFalse() throws StackFullException { // ARRANGE stack myStack = new stack(2); String item = "StackItem"; boolean actual, expected; // ACT myStack.push(item + "1"); actual = myStack.isFull(); expected = false; // ASSERT assertEquals(expected, actual); }

@Test void testPushStack() throws StackFullException, StackEmptyException { // ARRANGE stack myStack = new stack(2); String item = "StackItem"; String expected, actual; expected = "StackItem2"; // ACT myStack.push(item + "1"); myStack.push(item + "2"); actual = myStack.peek(); // ASSERT assertEquals(expected, actual); assertFalse(myStack.isEmpty()); }

@Test void testPushFullStack() throws StackFullException { // ARRANGE stack myStack = new stack(1); String item = "StackItem"; myStack.push(item + "1"); // ACT // ASSERT assertThrows(StackFullException.class, () -> myStack.push(item)); }

@Test void testPopStack() throws StackFullException, StackEmptyException { // ARRANGE stack myStack = new stack(2); String item = "StackItem"; String actual1, actual2, expected1, expected2; expected1 = "StackItem2"; expected2 = "StackItem1"; myStack.push(item + "1"); myStack.push(item + "2"); // ACT actual1 = myStack.pop(); actual2 = myStack.pop(); // ASSERT assertEquals(expected1, actual1); assertEquals(expected2, actual2); assertTrue(myStack.isEmpty()); }

@Test void testStackSizeZero() { // ARRANGE stack myStack = new stack(2); int actual, expected; expected = 0; // ACT actual = myStack.size(); // ASSERT assertEquals(expected, actual); }

@Test void testStackSizeNonZero() throws StackFullException { // ARRANGE stack myStack = new stack(2); String item = "StackItem"; int actual, expected; expected = 1; // ACT myStack.push(item + "1"); actual = myStack.size(); // ASSERT assertEquals(expected, actual); }

@Test void testPopEmptyStack() { // ARRANGE stack myStack = new stack(1); // ACT // ASSERT assertThrows(StackEmptyException.class, () -> myStack.pop()); }

@Test void testPeekStack() throws StackLab.StackEmptyException, StackFullException { // ARRANGE stack myStack = new stack(1); String item = "StackItem"; String actual, expected; // ACT myStack.push(item); expected = item; actual = myStack.peek(); // ASSERT assertEquals(expected, actual); }

@Test void testPeekEmptyStack() { // ARRANGE stack myStack = new stack(1); // ACT // ASSERT assertThrows(StackEmptyException.class, () -> myStack.peek()); }

@Test void testPrintStackUp() throws StackEmptyException, StackFullException { // ARRANGE stack myStack = new stack(2); String item = "StackItem"; String actual, expected; expected = "StackItem1 StackItem2 "; // ACT myStack.push(item + "1"); myStack.push(item + "2"); actual = myStack.printStackUp(); // ASSERT assertEquals(expected, actual); assertFalse(myStack.isEmpty()); }

@Test void testPrintStackUpEmptyStack() { // ARRANGE stack myStack = new stack(1); // ACT // ASSERT assertThrows(StackEmptyException.class, () -> myStack.printStackUp()); } }

Run the Unit Tests First run the Unit Tests to determine the test that are not passing (RED). You will refactor those until the corresponding tests are passing (GREEN). Do not change the tests to make them pass, you are only changing the code to make your tests GREEN. Some tests might be passing, but still are dependent on other code and may not pass in later runs, indicating you may need to change update other code. Always remove TODO when you have completed the task. Stack Class Class members These are provided. This is an array of string. Look at what other members are necessary: top and maxSize. These are integers. Add comments beside maxSize and top that explain what they represent. isFull() This is written for you. Since the array holds the items, this stack will have a limit. Some stacks may be implemented using other collections (List, ArrayList, etc.) that have sizes which can be increased and isfull() might not need to be tested. In this lab you are limited in stack size. Add comments on how this tests is Fullo) isEmpty() This is not written. Since the array holds the items in the stack, what will the isEmpty() stack look like? HINT: look at the default no-arg constructor and look at isFull(). peek() This is written for you, look at the code and add comments about the if-statement and why it is necessary. Notice the exception thrown for error. pop() This is incomplete. Add the code needed to complete this, remove the return line. HINT: look at peek() push() This is not written. Write the coded need to push() an item on the stack. Refer to pop() and peek(). What if statement would you include here? Is an exception needed? Write the code to an item to the top of stack (array) How do you add to the top of the stack (array) o What if statement would you include here? o is an exception needed? o What happens with top? Add comments about the above code. printspackup) This is hot written. Write this to return a string that will be printed in a driver/tester. When the stack is empty throws StackEmptyException

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

Database And Expert Systems Applications 23rd International Conference Dexa 2012 Vienna Austria September 2012 Proceedings Part 1 Lncs 7446

Authors: Stephen W. Liddle ,Klaus-Dieter Schewe ,A Min Tjoa ,Xiaofang Zhou

2012th Edition

3642325998, 978-3642325991

More Books

Students also viewed these Databases questions

Question

10. Describe the relationship between communication and power.

Answered: 1 week ago