Question: Write a test class for the following . Please use assert while testing the methods. LinkedStack.java import java.util.*; import stack.*; import java.util.EmptyStackException; import java.util.Random; import

Write a test class for the following . Please use assert while testing the methods.

LinkedStack.java

import java.util.*;

import stack.*;

import java.util.EmptyStackException; import java.util.Random;

import java.awt.Color; import java.math.*; public class LinkedStack implements StackInterface{

private int size=0; private Node topNode=null; public LinkedStack(){ //hi! }

public void clear() { // TODO Auto-generated method stub this.topNode=null; this.size=0; }

public boolean isEmpty() { if (this.size()==0){ return true; } return false; } public String toString(){ super.toString(); Node temp=this.topNode; StringBuilder string=new StringBuilder(); string.append("["); while(temp != null){ string.append(temp.data.toString()); temp=temp.next; } string.append("]"); return string.toString(); }

public T peek() { if(this.size<=0){ throw new EmptyStackException(); } return this.topNode.getData(); }

public T pop() { if(size<=0){ throw new EmptyStackException(); } Node temp = this.topNode; this.topNode=temp.getNextNode(); size--; return temp.getData(); }

public void push(T arg0) { Node newNode= new Node(topNode,arg0); this.topNode=newNode; size++; } public int size(){ System.out.println("Returning Size..."); return this.size; } private class Node{ private Node next; private T data; public Node(T data){ this.next=null; this.data=data; } public Node(Node nextNode, T data){ this.next=nextNode; this.data=data; } public Node getNextNode(){ return this.next; } public T getData(){ return this.data; } public void setNextNode(Node newNext){ this.next=newNext; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!