Question

Task Generics: GenericStack Class. Java and JUnit5 (Unit test) package Modul02; public class GenericStack<E> { private...

Task Generics: GenericStack Class. Java and JUnit5 (Unit test)

package Modul02;

public class GenericStack<E> {

private java.util.ArrayList<E> list = new java.util.ArrayList<>();

public int size() {

return list.size();

}

public E peek() {

return list.get(size() - 1);

}

public void push(E o) {

list.add(o);

}

public E pop() {

E o = list.get(size() - 1);

list.remove(size() - 1);

return o;

}

public boolean isEmpty() {

return list.isEmpty();

}

@Override

public String toString() {

return "stack: " + list.toString();

}

}

package Modul02;

public class TestGenericStack {

public static void main(String[] args) {

GenericStack gsString = new GenericStack<>();

gsString.push("one");

gsString.push("two");

gsString.push("three");

while (!(gsString.isEmpty())) {

System.out.println(gsString.pop());

}

GenericStack gsInteger = new GenericStack<>();

gsInteger.push(1);

gsInteger.push(2);

gsInteger.push(3);

//gsInteger.push("4");

while (!(gsInteger.isEmpty())) {

System.out.println(gsInteger.pop());

}

}

}

Create a new version of GenericStack that uses an array instead of an ArrayList (this version should also be generic). Be sure to check the size of the array before adding a new item; - if the array becomes full, double the size of the array, and you must copy elements from the old array to the new one.
Note that one cannot use the new operator on a generic type, new E is not allowed.
In order to create the array of the generic type, one must cast:
private E [] elements = (E[]) new Object[100];

Important that push checks that there is enough space, if not you have to double the stack size before push. Pushes should also not allow zero values ​​as arguments, because now they should only be ignored.

Tips: System.arraycopy (...); Public interface for the class is known, but start writing the tests first.

JUnit5(Unit test)
Write tests:
The tests will cover all scenarios (all possible ways you can use a stack). Since this is a generic class, test it for at least two different types (What if someone uses pop or peek on a blank stack?). Remember to include a test where the stack must be doubled to accommodate a new push (). Feel free to introduce a new public method capacity () that responds to the stack's current capacity. Also, create (if you haven't done) a constructor for the stack that takes in startup capacity (before you have one that creates a default capacity).

Homework Answers

Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Edit: JUnit test class is added.

// GenericStack.java

public class GenericStack<E> {

      // array

      private E[] array;

      // current size

      private int size;

      // initial capacity

      private final int DEFAULT_CAPACITY = 10;

      // constructor initializes an array of default capacity

      public GenericStack() {

            array = (E[]) new Object[DEFAULT_CAPACITY];

            // initializing size to 0

            size = 0;

      }

      // returns the size

      public int size() {

            return size;

      }

      // returns top element; or null if empty

      public E peek() {

            if (isEmpty()) {

                  // empty

                  return null;

            }

            // returning current top element

            return array[size - 1];

      }

      // adds an element to the top

      public void push(E o) {

            // preventing addition of null values

            if (o != null) {

                  // if array is full, doubling it

                  if (size == array.length) {

                        // creating new array of twice capacity

                        E[] newArr = (E[]) new Object[size * 2];

                        // copying array to newArr

                        System.arraycopy(array, 0, newArr, 0, array.length);

                        // replacing old array with new one

                        array = newArr;

                  }

                  // adding to index

                  array[size] = o;

                  // updating index

                  size++;

            }

      }

      // removes and returns the top element from stack, or null if empty

      public E pop() {

            if (isEmpty()) {

                  // empty stack

                  return null;

            }

            // getting element at top

            E element = array[size - 1];

            // updating size

            size--;

            // returning removed element

            return element;

      }

      // returns true if stack is empty

      public boolean isEmpty() {

            return size == 0;

      }

      @Override

      public String toString() {

            String str = "stack: [";

            // appending all elements of stack to a String

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

                  str += array[i];

                  if (i != size - 1) {

                        // appending a comma and space if this is not last element

                        str += ", ";

                  }

            }

            str += "]";

            return str;

      }

}

//modified TestGenericStack class to include more tests

public class TestGenericStack {

      public static void main(String[] args) {

            GenericStack<String> gsString = new GenericStack<String>();

            System.out.println("Current top element: " + gsString.peek());

            gsString.push("one");

            gsString.push("two");

            gsString.push(null); // will not be added since element is null

            gsString.push("three");

            System.out.println("Current top element: " + gsString.peek());

            System.out.println("popping until empty");

            while (!(gsString.isEmpty())) {

                  System.out.println(gsString.pop());

            }

            // attempting to pop an empty stack, should display null

            System.out.println("pop on empty stack: " + gsString.pop());

            GenericStack<Integer> gsInteger = new GenericStack<Integer>();

            System.out.println("Current top element: " + gsInteger.peek());

            // adding numbers from 1 to 20 to integer stack

            for (int i = 1; i <= 20; i++) {

                  gsInteger.push(i);

            }

            gsInteger.push(null); // will not be added since element is null

            System.out.println("Current top element: " + gsInteger.peek());

            while (!(gsInteger.isEmpty())) {

                  System.out.println(gsInteger.pop());

            }

            // attempting to pop an empty stack, should display null

            System.out.println("pop on empty stack: " + gsInteger.pop());

      }

}

// GenericStackTest.java (JUnit test)

import static org.junit.Assert.*;

import org.junit.Test;

public class GenericStackTest {

      private GenericStack<Integer> stack;

      @Test

      public void testGenericStackConstructor() {

            // ensuring that constructor creates an empty stack

            stack = new GenericStack<Integer>();

            assertEquals(stack.size(), 0);

            assertEquals(stack.isEmpty(), true);

            assertEquals(stack.toString(), "stack: []");

      }

      @Test

      public void testSize() {

            // ensuring that stack size is 0 initially and increments after every

            // push operation

            stack = new GenericStack<Integer>();

            assertEquals(stack.size(), 0);

            stack.push(1);

            assertEquals(stack.size(), 1);

            for (int i = 1; i < 100; i++) {

                  stack.push(i);

            }

            // after 100 valid push(), ensuring that size is 100

            assertEquals(stack.size(), 100);

      }

      @Test

      public void testPeek() {

            // ensuring that peek method always return top value, or null if stack

            // is empty

            stack = new GenericStack<Integer>();

            assertEquals(stack.peek(), null);

            stack.push(1);

            assertEquals(stack.peek(), (Integer) 1);

            stack.push(999);

            assertEquals(stack.peek(), (Integer) 999);

      }

      @Test

      public void testPush() {

            // ensuring that push method always add elements to the top

            stack = new GenericStack<Integer>();

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

                  stack.push(i);

                  assertEquals(stack.peek(), (Integer) i);

            }

      }

      @Test

      public void testPop() {

            stack = new GenericStack<Integer>();

            //adding elements from 0 to 100 to stack

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

                  stack.push(i);

            }

            //ensuring that elements are popped in reverse order

            for (int i = 100; i >= 0; i--) {

                  assertEquals(stack.pop(), (Integer) i);

            }

      }

      @Test

      public void testIsEmpty() {

            //ensuring that isEmpty works as needed

            stack = new GenericStack<Integer>();

            assertEquals(stack.isEmpty(), true);

            stack.push(1234);

            assertEquals(stack.isEmpty(), false);

            stack.pop();

            assertEquals(stack.isEmpty(), true);

      }

      @Test

      public void testToString() {

            //thoroughly testing toString method of the stack

            stack = new GenericStack<Integer>();

            assertEquals(stack.toString(), "stack: []");

            stack.push(1234);

            assertEquals(stack.toString(), "stack: [1234]");

            stack.push(999);

            assertEquals(stack.toString(), "stack: [1234, 999]");

            stack.push(0);

            assertEquals(stack.toString(), "stack: [1234, 999, 0]");

            stack.pop();

            assertEquals(stack.toString(), "stack: [1234, 999]");

            stack.pop();

            assertEquals(stack.toString(), "stack: [1234]");

            stack.pop();

            assertEquals(stack.toString(), "stack: []");

      }

}

/*OUTPUT*/

Current top element: null

Current top element: three

popping until empty

three

two

one

pop on empty stack: null

Current top element: null

Current top element: 20

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

pop on empty stack: null

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return make; } } class Node<T>{ public T data; public Node next; public Node(T data){ this.data = data; this.next = null; } } public class StackLinkedList<T>{ //Single field of type Node    //to represent the front of the stack //operation push() /* The push operation is equivalent to the inserting a node at the head of the list. */ public void push(T data){ } //operation...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
10.6 LAB: Implement a stack using an array Given main() complete the Stack class by writing...
10.6 LAB: Implement a stack using an array Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(),...
Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(), which returns true if and only if the stack is empty; public T pop() throws StackUnderflowException, which removes and returns the top element of the stack (if the stack is empty, it throws StackUnderflowException); public T peek() throws StackUnderflowException, which returns the top element of the stack (but does not remove it; it throws exception if stack is empty); public void push(T element), which...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT