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
Hello need help on this lab- what you need to do is add a code to Arra.pdf
DOCX
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
PDF
java write a program to evaluate the postfix expressionthe program.pdf
PDF
Please review my code (java)Someone helped me with it but i cannot.pdf
PPT
Stack Implementation
PDF
package ADTs public interface CollectionADTltTgt .pdf
PDF
Given the following errors and class in Java- How are these errors fix.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
java write a program to evaluate the postfix expressionthe program.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
Stack Implementation
package ADTs public interface CollectionADTltTgt .pdf
Given the following errors and class in Java- How are these errors fix.pdf

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

DOCX
(674335607) cs2309 java-lab-manual
PPT
Stack, queue and hashing
PPT
Educational slides by venay magen
PDF
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
PDF
1 The goal is to implement DataStructuresArrayStack accor.pdf
PPT
Stacks_As_Advanced_Data_Strcutures11.ppt
PPT
PDF
Please implement Stack using Array (capacity 100)- Use template to.pdf
PDF
Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
PDF
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
PPTX
Data Structure - Stack.pptx
PDF
output and explain There is Mylist There is MyArrayList pa.pdf
PDF
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
PDF
I have a stack in Java populated with integers. Im trying to compa.pdf
PDF
create a new interface called DropoutStackADT for representing a dro.pdf
PPT
ch14ILFVJHCHFCXCJHBKVJGCHGCHGXZDFUHJU.ppt
PDF
So I have this code(StackInAllSocks) and I implemented the method but.pdf
PDF
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
PDF
public class TrequeT extends AbstractListT { .pdf
(674335607) cs2309 java-lab-manual
Stack, queue and hashing
Educational slides by venay magen
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
Stacks_As_Advanced_Data_Strcutures11.ppt
Please implement Stack using Array (capacity 100)- Use template to.pdf
Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
Data Structure - Stack.pptx
output and explain There is Mylist There is MyArrayList pa.pdf
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
ch14ILFVJHCHFCXCJHBKVJGCHGCHGXZDFUHJU.ppt
So I have this code(StackInAllSocks) and I implemented the method but.pdf
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
public class TrequeT extends AbstractListT { .pdf

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)

DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Computing-Curriculum for Schools in Ghana
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Paper A Mock Exam 9_ Attempt review.pdf.
Weekly quiz Compilation Jan -July 25.pdf
Microbial disease of the cardiovascular and lymphatic systems
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Orientation - ARALprogram of Deped to the Parents.pptx
Yogi Goddess Pres Conference Studio Updates
LDMMIA Reiki Yoga Finals Review Spring Summer
A systematic review of self-coping strategies used by university students to ...
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Chinmaya Tiranga quiz Grand Finale.pdf
01-Introduction-to-Information-Management.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Updated Idioms and Phrasal Verbs in English subject
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Computing-Curriculum for Schools in Ghana
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

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;