SlideShare ist ein Scribd-Unternehmen logo
1 von 9
NEED THE JAVA CODE FOR TEST METHODS OF THE FOLLOWING CODE,
PLEASE HELP.
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;
}
}

Weitere ähnliche Inhalte

Ähnlich wie NEED THE JAVA CODE FOR TEST METHODS OF THE FOLLOWING CODE- PLEASE HELP.docx

ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdfI have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
JUSTSTYLISH3B2MOHALI
 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdfA linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdf
kisgstin23
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import  stdlib.;import java.util.Iterator;im.docxpackage algs13;import  stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
gerardkortney
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
aksahnan
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdfWere writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdf
fsenterprises
 
1. Suppose you want to implement an ADT in which you can insert valu.pdf
1. Suppose you want to implement an ADT in which you can insert valu.pdf1. Suppose you want to implement an ADT in which you can insert valu.pdf
1. Suppose you want to implement an ADT in which you can insert valu.pdf
forwardcom41
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
arihantpatna
 
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
Given the following class in Java-  public class ThreeTenDynArray-T- {.pdfGiven the following class in Java-  public class ThreeTenDynArray-T- {.pdf
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
NicholasflqStewartl
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
firstchoiceajmer
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more
steviesellars
 
create a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdfcreate a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
f3apparelsonline
 

Ähnlich wie NEED THE JAVA CODE FOR TEST METHODS OF THE FOLLOWING CODE- PLEASE HELP.docx (20)

ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdfI have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdfA linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdf
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import  stdlib.;import java.util.Iterator;im.docxpackage algs13;import  stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdfWere writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdf
 
1. Suppose you want to implement an ADT in which you can insert valu.pdf
1. Suppose you want to implement an ADT in which you can insert valu.pdf1. Suppose you want to implement an ADT in which you can insert valu.pdf
1. Suppose you want to implement an ADT in which you can insert valu.pdf
 
Posfix
PosfixPosfix
Posfix
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
 
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
Given the following class in Java-  public class ThreeTenDynArray-T- {.pdfGiven the following class in Java-  public class ThreeTenDynArray-T- {.pdf
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
 
Stack, queue and hashing
Stack, queue and hashingStack, queue and hashing
Stack, queue and hashing
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more
 
create a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdfcreate a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 

Mehr von LucasmHKChapmant

Liquidity Risk and Expected Stock Returns Lubos Pastor and Robert F- S.docx
Liquidity Risk and Expected Stock Returns Lubos Pastor and Robert F- S.docxLiquidity Risk and Expected Stock Returns Lubos Pastor and Robert F- S.docx
Liquidity Risk and Expected Stock Returns Lubos Pastor and Robert F- S.docx
LucasmHKChapmant
 
Mechanism Diagrams - Special Senses PrSO-001 5- Vision Note- absence o.docx
Mechanism Diagrams - Special Senses PrSO-001 5- Vision Note- absence o.docxMechanism Diagrams - Special Senses PrSO-001 5- Vision Note- absence o.docx
Mechanism Diagrams - Special Senses PrSO-001 5- Vision Note- absence o.docx
LucasmHKChapmant
 
Match the pattern of change in allele frequencies with the mechanisa b.docx
Match the pattern of change in allele frequencies with the mechanisa b.docxMatch the pattern of change in allele frequencies with the mechanisa b.docx
Match the pattern of change in allele frequencies with the mechanisa b.docx
LucasmHKChapmant
 
Match these cellular network acronyms to their function- IMSI IMEI ICC.docx
Match these cellular network acronyms to their function- IMSI IMEI ICC.docxMatch these cellular network acronyms to their function- IMSI IMEI ICC.docx
Match these cellular network acronyms to their function- IMSI IMEI ICC.docx
LucasmHKChapmant
 
Match the description or function with the term or concept- a) Allows.docx
Match the description or function with the term or concept- a) Allows.docxMatch the description or function with the term or concept- a) Allows.docx
Match the description or function with the term or concept- a) Allows.docx
LucasmHKChapmant
 

Mehr von LucasmHKChapmant (20)

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

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

NEED THE JAVA CODE FOR TEST METHODS OF THE FOLLOWING CODE- PLEASE HELP.docx

  • 1. NEED THE JAVA CODE FOR TEST METHODS OF THE FOLLOWING CODE, PLEASE HELP. 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()) {
  • 3. 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 ++; } /**
  • 4. * 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
  • 5. 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")
  • 6. 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)
  • 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
  • 8. * 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]))) {