SlideShare a Scribd company logo
Need help with writing the test cases for the following code in java.
public class ArrayBasedStack<T> implements StackADT<T> {
private T[] stackArray;
private int size;
private int capacity;
@SuppressWarnings("unchecked")
public ArrayBasedStack(int initialCapacity) {
size = 0;
this.capacity = initialCapacity;
stackArray = (T[])new Object[capacity];
}
public ArrayBasedStack() {
this(100);
}
/**
* Checks if the stack is empty.
*
* @return Returns true if the stack is empty.
*/
@Override
public boolean isEmpty() {
return size == 0;
}
/**
* Checks the item at the top of the
* stack without removing it.
*
* @return Item at the top of the stack.
*/
@Override
public T peek() throws EmptyStackException {
if (isEmpty()) {
throw new EmptyStackException();
}
return stackArray[size - 1];
}
/**
* Removes the item at the top of
* the stack.
*
* @return The item that was removed.
*/
@Override
public T pop() throws EmptyStackException {
if (isEmpty()) {
throw new EmptyStackException();
}
T result = stackArray[size - 1];
stackArray[size - 1] = null;
size--;
return result;
}
/**
* Pushes an item onto the stack.
*
* @param item
* Item to be pushed
* onto the stack.
*/
@Override
public void push(T item) {
if (size == capacity) {
expandCapacity();
}
stackArray[size] = item;
size ++;
}
/**
* Checks if an item is in the stack.
*
* @param item
* Item to be looked for.
* @return Returns true if the item is
* somewhere in the stack.
*/
@Override
public boolean contains(T item) {
for (int i = size - 1; i >= 0; i--) {
if (stackArray[i].equals(item)) {
return true;
}
}
return false;
}
/**
* Number of items in the stack.
*
* @return The number of items in
* the stack.
*/
@Override
public int size() {
return size;
}
/**
* Clears the stack (removes all of
* the items from the stack).
*/
@Override
public void clear() {
for (int i = 0; i < size; i++) {
stackArray[i] = null;
}
size = 0;
}
/**
* Returns an array with a copy of each element in the stack with the top of
* the stack being the last element
*
* @return the array representation of the stack
*/
@Override
public Object[] toArray() {
@SuppressWarnings("unchecked")
T[] copy = (T[])new Object[this.size()];
for (int i = 0; i < this.size(); i++) {
copy[i] = this.stackArray[i];
}
return copy;
}
/**
* Expands the capacity of the stack by doubling its current capacity.
*/
private void expandCapacity() {
@SuppressWarnings("unchecked")
T[] newArray = (T[])new Object[this.capacity * 2];
for (int i = 0; i < this.capacity; i++) {
newArray[i] = this.stackArray[i];
}
this.stackArray = newArray;
this.capacity *= 2;
}
/**
* Returns the string representation of the stack.
*
* [] (if the stack is empty)
* [bottom, item, ..., item, top] (if the stack contains items)
*
* @return the string representation of the stack.
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append('[');
boolean firstItem = true;
for (int i = 0; i < this.size(); i++) {
if (!firstItem) {
builder.append(", ");
}
else {
firstItem = false;
}
// String.valueOf will print null or the toString of the item
builder.append(String.valueOf(this.stackArray[i]));
}
builder.append(']');
return builder.toString();
}
/**
* Two stacks are equal iff they both have the same size and contain the
* same elements in the same order.
*
* @param other
* the other object to compare to this
*
* @return {@code true}, if the stacks are equal; {@code false} otherwise.
*/
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (this.getClass().equals(other.getClass())) {
ArrayBasedStack<?> otherStack = (ArrayBasedStack<?>)other;
if (this.size() != otherStack.size()) {
return false;
}
Object[] otherArray = otherStack.toArray();
for (int i = 0; i < this.size(); i++) {
if (!(this.stackArray[i].equals(otherArray[i]))) {
return false;
}
}
return true;
}
return false;
}
}

More Related Content

PDF
Implement the ADT stack by using an array stack to contain its entri.pdf
PDF
Please implement Stack using Array (capacity 100)- Use template to.pdf
DOCX
@author Derek Harter @cwid 123 45 678 @class .docx
PDF
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
DOCX
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
PDF
C program for array implementation of stack#include stdio.h.pdf
PDF
I have a stack in Java populated with integers. Im trying to compa.pdf
PDF
Please review my code (java)Someone helped me with it but i cannot.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
Please implement Stack using Array (capacity 100)- Use template to.pdf
@author Derek Harter @cwid 123 45 678 @class .docx
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
C program for array implementation of stack#include stdio.h.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf

Similar to Need help with writing the test cases for the following code in java-.docx (20)

PDF
ReversePoem.java ---------------------------------- public cl.pdf
PDF
A linked stack is implemented using a standard Node class as follows.pdf
PPT
Stack Implementation
DOCX
(674335607) cs2309 java-lab-manual
DOCX
package algs13;import stdlib.;import java.util.Iterator;im.docx
PDF
Were writing code for a project that dynamically allocates an arra.pdf
PDF
So I have this code(StackInAllSocks) and I implemented the method but.pdf
DOCX
PDF
1. Suppose you want to implement an ADT in which you can insert valu.pdf
PDF
create a new interface called DropoutStackADT for representing a dro.pdf
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Lec 4 Stack of Data Structures & Algorithms
PDF
Here is what I got so far, I dont know how to write union, interse.pdf
DOCX
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
ReversePoem.java ---------------------------------- public cl.pdf
A linked stack is implemented using a standard Node class as follows.pdf
Stack Implementation
(674335607) cs2309 java-lab-manual
package algs13;import stdlib.;import java.util.Iterator;im.docx
Were writing code for a project that dynamically allocates an arra.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
1. Suppose you want to implement an ADT in which you can insert valu.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
Stack queue
Stack queue
Stack queue
Stack queue
Stack queue
Stack queue
Stack queue
Lec 4 Stack of Data Structures & Algorithms
Here is what I got so far, I dont know how to write union, interse.pdf
Java Foundations StackADT-java --- - Defines the interface to a stack.docx

More from LucasmHKChapmant (20)

DOCX
Liquidity Risk and Expected Stock Returns Lubos Pastor and Robert F- S.docx
DOCX
Microlensing of a background star by a planet Decreases the brightness.docx
DOCX
message until the user enters Q- in the format H- Hydrogen- periodic-p.docx
DOCX
Measurement of project quality and performance- The most crucial infor.docx
DOCX
Mechanism Diagrams - Special Senses PrSO-001 5- Vision Note- absence o.docx
DOCX
McConnell Corporation has bonds on the market with 12 years to maturit.docx
DOCX
Match the pattern of change in allele frequencies with the mechanisa b.docx
DOCX
Match the description to the correct vessel name- the longest vein in.docx
DOCX
Match the statements with the accounting principles- A partnership cha.docx
DOCX
Match these cellular network acronyms to their function- IMSI IMEI ICC.docx
DOCX
Match the best answer on the right to the item on the left- Discrete v.docx
DOCX
Match the law wht theet Hunction or pupose- A Aederal law far health c.docx
DOCX
Match the following terms related to soil profiles- The base geologica.docx
DOCX
Match the scenarios given in the left-hand column with the type of dec (1).docx
DOCX
Majority of people engage this region of the brain to understand the e.docx
DOCX
Match the following genera with correct characteristics- aerobic bacte.docx
DOCX
Match the following terms to their correct descriptions- pop culture c.docx
DOCX
Match the description or function with the term or concept- a) Allows.docx
DOCX
Match the following species with correct characteristics (hint- recall.docx
DOCX
Marco expects his investment returns to be 9-53 percent and inflation.docx
Liquidity Risk and Expected Stock Returns Lubos Pastor and Robert F- S.docx
Microlensing of a background star by a planet Decreases the brightness.docx
message until the user enters Q- in the format H- Hydrogen- periodic-p.docx
Measurement of project quality and performance- The most crucial infor.docx
Mechanism Diagrams - Special Senses PrSO-001 5- Vision Note- absence o.docx
McConnell Corporation has bonds on the market with 12 years to maturit.docx
Match the pattern of change in allele frequencies with the mechanisa b.docx
Match the description to the correct vessel name- the longest vein in.docx
Match the statements with the accounting principles- A partnership cha.docx
Match these cellular network acronyms to their function- IMSI IMEI ICC.docx
Match the best answer on the right to the item on the left- Discrete v.docx
Match the law wht theet Hunction or pupose- A Aederal law far health c.docx
Match the following terms related to soil profiles- The base geologica.docx
Match the scenarios given in the left-hand column with the type of dec (1).docx
Majority of people engage this region of the brain to understand the e.docx
Match the following genera with correct characteristics- aerobic bacte.docx
Match the following terms to their correct descriptions- pop culture c.docx
Match the description or function with the term or concept- a) Allows.docx
Match the following species with correct characteristics (hint- recall.docx
Marco expects his investment returns to be 9-53 percent and inflation.docx

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Complications of Minimal Access Surgery at WLH
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Pre independence Education in Inndia.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
TR - Agricultural Crops Production NC III.pdf
O7-L3 Supply Chain Operations - ICLT Program
01-Introduction-to-Information-Management.pdf
Renaissance Architecture: A Journey from Faith to Humanism
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPH.pptx obstetrics and gynecology in nursing
FourierSeries-QuestionsWithAnswers(Part-A).pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
VCE English Exam - Section C Student Revision Booklet
Complications of Minimal Access Surgery at WLH
GDM (1) (1).pptx small presentation for students
Pre independence Education in Inndia.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra

Need help with writing the test cases for the following code in java-.docx

  • 1. Need help with writing the test cases for the following code in java. public class ArrayBasedStack<T> implements StackADT<T> { private T[] stackArray; private int size; private int capacity; @SuppressWarnings("unchecked") public ArrayBasedStack(int initialCapacity) { size = 0; this.capacity = initialCapacity; stackArray = (T[])new Object[capacity]; } public ArrayBasedStack() { this(100); } /** * Checks if the stack is empty. * * @return Returns true if the stack is empty. */ @Override public boolean isEmpty() { return size == 0; }
  • 2. /** * Checks the item at the top of the * stack without removing it. * * @return Item at the top of the stack. */ @Override public T peek() throws EmptyStackException { if (isEmpty()) { throw new EmptyStackException(); } return stackArray[size - 1]; } /** * Removes the item at the top of * the stack. * * @return The item that was removed. */ @Override public T pop() throws EmptyStackException { if (isEmpty()) { throw new EmptyStackException();
  • 3. } T result = stackArray[size - 1]; stackArray[size - 1] = null; size--; return result; } /** * Pushes an item onto the stack. * * @param item * Item to be pushed * onto the stack. */ @Override public void push(T item) { if (size == capacity) { expandCapacity(); } stackArray[size] = item; size ++; } /** * Checks if an item is in the stack.
  • 4. * * @param item * Item to be looked for. * @return Returns true if the item is * somewhere in the stack. */ @Override public boolean contains(T item) { for (int i = size - 1; i >= 0; i--) { if (stackArray[i].equals(item)) { return true; } } return false; } /** * Number of items in the stack. * * @return The number of items in * the stack. */ @Override public int size() {
  • 5. return size; } /** * Clears the stack (removes all of * the items from the stack). */ @Override public void clear() { for (int i = 0; i < size; i++) { stackArray[i] = null; } size = 0; } /** * Returns an array with a copy of each element in the stack with the top of * the stack being the last element * * @return the array representation of the stack */ @Override public Object[] toArray() { @SuppressWarnings("unchecked") T[] copy = (T[])new Object[this.size()];
  • 6. for (int i = 0; i < this.size(); i++) { copy[i] = this.stackArray[i]; } return copy; } /** * Expands the capacity of the stack by doubling its current capacity. */ private void expandCapacity() { @SuppressWarnings("unchecked") T[] newArray = (T[])new Object[this.capacity * 2]; for (int i = 0; i < this.capacity; i++) { newArray[i] = this.stackArray[i]; } this.stackArray = newArray; this.capacity *= 2; } /** * Returns the string representation of the stack. * * [] (if the stack is empty) * [bottom, item, ..., item, top] (if the stack contains items) *
  • 7. * @return the string representation of the stack. */ @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append('['); boolean firstItem = true; for (int i = 0; i < this.size(); i++) { if (!firstItem) { builder.append(", "); } else { firstItem = false; } // String.valueOf will print null or the toString of the item builder.append(String.valueOf(this.stackArray[i])); } builder.append(']'); return builder.toString(); } /** * Two stacks are equal iff they both have the same size and contain the * same elements in the same order.
  • 8. * * @param other * the other object to compare to this * * @return {@code true}, if the stacks are equal; {@code false} otherwise. */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other == null) { return false; } if (this.getClass().equals(other.getClass())) { ArrayBasedStack<?> otherStack = (ArrayBasedStack<?>)other; if (this.size() != otherStack.size()) { return false; } Object[] otherArray = otherStack.toArray(); for (int i = 0; i < this.size(); i++) { if (!(this.stackArray[i].equals(otherArray[i]))) { return false;