Implementing a Stack using an Array Part 1 - class Stack using an array 1. Given
ID: 3793756 • Letter: I
Question
Implementing a Stack using an Array
Part 1 - class Stack using an array
1. Given the following specification of the top operation as an instance method of the ArrayStack class:
Item Type Top
Function: Returns copy of the last item put onto the stack.
Precondition: Stack is not empty.
Postcondition: Function value = copy of item at top of stack. Stack is not changed.
- design and implement the function top as a class method of the new class MyArrayStackUtility (do not use instance top() method).
2. Design and implement the following operations as an instance method of the ArrayStack class
replace(ItemType oldItem, ItemType newItem)
Function: Replaces all occurences of oldItem with newItems.
Precondition: Stack has been initialized.
Postconditions: Each occurrence of oldItem in stack has been changed to newItem.
3. Design and implement the function replace() from 2 as a class method of the MyArrayStackUtility class.
4. Design and implement the following operation as a instance method of the ArrayStack class
bool identical(ArrayStack stack1)
Function: Determines if stack1 and self are identical.
Preconditions: stack1 and self have been initialized.
Postconditions: stack1 and self are unchanged. Function value = (self and stack1 are identical).
5. Design and implement the method identical() from 4 as a class method of the MyArrayStackUtility class.
6. Design and implement a class method reverse() of the MyArrayStackUtility class that reverses an array passed as a parameter.
For part 1: Design and implement a test driver Driver class(main()) to test all implemented methods.
In main() use the method reverse() to reverse the words in a String. You can find the following program fragment useful:
String sentence = "This is a sentence." ;
String[]words = sentences.split("[.] + ") ;
for (String word : words)
{
System.out.println(word) ;
}
Part 2 - class Stack using a Vector
For this part use the initial project for part 1.
Design and implement a new class ArrayStack2 which uses (has a) class Vector to store the elements.
- Design and implement a test driver (main()) to test all methods.
Part 3 - class Stack using an ArrayList
For this part use the project from part 1.
Design and implement a new class ArrayStack3 which uses (has a) class ArrayList to store the elements.
- Design and implement a test driver (main()) to test all the methods.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
########## ArrayStack.java ##########
public class ArrayStack<ItemType extends Comparable<ItemType>> {
// instance variables
private int size;
private int capacity;
private ItemType[] array;
// constructor
public ArrayStack(int maxSize) {
// initializing all variables
capacity = maxSize;
size = 0;
array = (ItemType [])new Object[capacity];
}
ItemType Top(){
if(size == 0)
return null;
return array[size-1];
}
public void push(ItemType item){
if(size != capacity){
array[size++] = item;
}
}
public ItemType pop(){
ItemType temp = null;
if(size != 0){
temp = array[size-1];
size--;
}
return temp;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void replace(ItemType oldItem, ItemType newItem){
for(int i=0; i<size; i++){
if(array[i].compareTo(oldItem) == 0)
array[i] = newItem;
}
}
public boolean identical(ArrayStack<ItemType> stack1){
if(size != stack1.size)
return false;
for(int i=0; i<size; i++){
if(array[i].compareTo(stack1.array[i]) != 0)
return false;
}
return true;
}
public void reverse(){
int i=0;
int j = size-1;
while(i < j){
ItemType temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
}
################# MyArrayStackUtility.java ############
public class MyArrayStackUtility {
public static <ItemType extends Comparable<ItemType>> ItemType top(ArrayStack<ItemType> stack){
return stack.Top();
}
public static <ItemType extends Comparable<ItemType>> void replace(ArrayStack<ItemType> stack,
ItemType oldItem, ItemType newItem){
stack.replace(oldItem, newItem);
}
public static <ItemType extends Comparable<ItemType>> boolean identical(
ArrayStack<ItemType> stack1, ArrayStack<ItemType> stack2){
return stack1.identical(stack2);
}
public static <ItemType extends Comparable<ItemType>> void reverse(ArrayStack<ItemType> stack){
stack.reverse();
}
}
I have not implemented second question.
Please repost separately