Question

In: Computer Science

1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...

1. You are given Stack.java, an interface class for Stacks.
/* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in ArrayStack.
Requirement: If the stack is full, you need to use double growth to create a new stack and test this in the main. You may want to accomplish this also using a method.

Java Language

Solutions

Expert Solution

// Stack.java

public interface Stack<T>

{

/**

* @return true if the stack is empty

*/

public boolean isEmpty();

/**

* @return true if the stack is full

*/

public boolean isFull();

/**

* @return the element at top without removing it, null if stack is empty

*/

public T peek();

/**

* @return the element at top after removing it, null if stack is empty

*/

public T pop();

/**

* pushes the item into the stack, no changes will be made if stack is full

*/

public void push(T item);

/**

* resets the stack to empty

*/

public void clear();

/**

* @return the size of the stack

*/

public int size();

/**

* reverses the stack using another stack

*/

public void reverse();

/**

* display the contents of the stack from front to the rear (top)

*/

public void display();

}

// ArrayStack.java

public class ArrayStack<T> implements Stack<T>

{

private int max;// maximum size

private T[] stack;// array of elements

private int size;// current number of elements

public ArrayStack(int max_capacity)

{

max = max_capacity;

// initializing array of T elements

stack = (T[]) (new Object[max]);

size = 0;

}

@Override

public boolean isEmpty()

{

return size == 0;

}

@Override

public boolean isFull()

{

return size == stack.length;

}

@Override

public T peek()

{

if (!isEmpty())

{

// returning the element without removing it

return stack[size - 1];

}

return null;

}

@Override

public T pop()

{

if (!isEmpty())

{

// returning the element after removing it

T removedData = stack[size - 1];

size--;

return removedData;

}

return null;

}

@Override

public void push(T item) {

if (!isFull()) {

// adding to the end of the array if array is not null

stack[size] = item;

size++;

}

}

@Override

public void clear()

{

// resetting

stack = (T[]) (new Object[max]);

size = 0;

}

@Override

public int size() {

return size;

}

@Override

public void reverse()

{

/**

* creating another stack

*/

ArrayStack<T> tmpStack = new ArrayStack<T>(max);

/**

* removing all elements from this stack and adding to the newly created

* stack

*/

while (!isEmpty()) {

tmpStack.push(this.pop());

}

/**

* updating the stack and size variables to that of the newly created

* stack (moving the variables)

*/

this.stack = tmpStack.stack;

this.size = tmpStack.size;

}

@Override

public void display()

{

if (!isEmpty())

{

System.out.println(toString());

} else

{

System.out.println("--Empty--");

}

}

@Override

public String toString() {

String data = "";

//appending all values to a string

for (int i = 0; i < size; i++) {

data += stack[i] + " ";

}

return data;

}

}

// ListStack.java

public class ListStack<T> implements Stack<T>

{

private int max;

private Node<T> head;// points to the front node

private Node<T> top;// points to the rear node

private int size;

public ListStack(int max_capacity)

{

max = max_capacity;

size = 0;

head = null;

top = null;

}

@Override

public boolean isEmpty() {

return size == 0;

}

@Override

public boolean isFull() {

return size == max;

}

@Override

public T peek()

{

if (!isEmpty())

{

// returning without removing

return top.data;

}

return null;

}

@Override

public T pop() {

if (!isEmpty()) {

T removedData = top.data;

if (head == top)

{

/**

* removing first element

*/

head = null;

top = null;

size = 0;

}

else

{

Node<T> tmp = head;

/**

* looping until the next node is top

*/

while (tmp.next != top) {

tmp = tmp.next;

}

// setting current top node to null

tmp.next = null;

// updating top node

top = tmp;

size--;

}

return removedData;

}

return null;

}

@Override

public void push(T item) {

if (!isFull()) {

Node<T> node = new Node<T>(item);

if (isEmpty()) {

// first entry

head = node;

top = head;

size++;

} else {

// appending to the last node

top.next = node;

top = node;

size++;

}

}

}

@Override

public void clear()

{

// resetting

head = null;

top = null;

size = 0;

}

@Override

public int size()

{

return size;

}

@Override

public void reverse()

{

// same technique used in array stack

ListStack<T> tmpStack = new ListStack<T>(max);

while (!isEmpty())

{

tmpStack.push(this.pop());

}

// updating variables

this.head = tmpStack.head;

this.top = tmpStack.top;

this.size = tmpStack.size;

}

@Override

public void display()

{

if (!isEmpty())

{

System.out.println(toString());

}

else

{

System.out.println("--Empty--");

}

}

@Override

public String toString() {

String data = "";

Node<T> tmp = head;

while (tmp != null) {

data += tmp.data + " ";

tmp = tmp.next;

}

return data;

}

/**

* a private inner class to represent one Node in the list

*/

private class Node<T>

{

T data;

Node next;

public Node(T data)

{

this.data = data;

}

}

}

// QuestionOne.java

public class QuestionOne

{

public static void main(String[] args)

{

//creating An array stack and list stack of 20 capacity

ArrayStack<Integer> arrayStack = new ArrayStack<Integer>(20);

ListStack<Integer> listStack = new ListStack<Integer>(20);

for (int i = 1; i <= 20; i++)

{

//pushing 1-20 into array stack

arrayStack.push(i);

//pushing 20-1 into list stack

listStack.push(21 - i);

}

System.out.println("Array Stack:");

/**

* performing required operations in proper order

*/

// display

arrayStack.display();

// reverse

arrayStack.reverse();

// display

arrayStack.display();

// peek

System.out.println(arrayStack.peek());

// pop

System.out.println(arrayStack.pop());

// pop

System.out.println(arrayStack.pop());

// reverse

arrayStack.reverse();

// size

System.out.println(arrayStack.size());

// isFull

System.out.println(arrayStack.isFull());

// isEmpty

System.out.println(arrayStack.isEmpty());

// display

arrayStack.display();

// clear

arrayStack.clear();

// display

arrayStack.display();

// isEmpty

System.out.println(arrayStack.isEmpty());

System.out.println("\nList Stack:");

/**

* performing the same operations for list stack in proper order

*/

// display

listStack.display();

// reverse

listStack.reverse();

// display

listStack.display();

// peek

System.out.println(listStack.peek());

// pop

System.out.println(listStack.pop());

// pop

System.out.println(listStack.pop());

// reverse

listStack.reverse();

// size

System.out.println(listStack.size());

// isFull

System.out.println(listStack.isFull());

// isEmpty

System.out.println(listStack.isEmpty());

// display

listStack.display();

// clear

listStack.clear();

// display

listStack.display();

// isEmpty

System.out.println(listStack.isEmpty());

}

}

/*OUTPUT*/

Array Stack:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

1

1

2

18

false

false

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

--Empty--

true

List Stack:

20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

20

20

19

18

false

false

18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

--Empty--

true


Related Solutions

Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Show how to implement three stacks in one array (in Java)
Show how to implement three stacks in one array (in Java)
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or a dynamic array to implement the data structure. A queue is a specialised form of list in which you can only get and remove the first element in the queue. The class should be able to work with the following code: SimpleQueue queue = new MyQueue(); NB: You cannot import anything from the standard library for this task. The data structure must be of...
We are trying to use two stacks to implement a queue. Name the two stacks as...
We are trying to use two stacks to implement a queue. Name the two stacks as E and D. We will enqueue into E and dequeue from D. To implement enqueue(e), simply call E.push(e). To implement dequeue(), simply call D.pop(), provided that D is not empty. If D is empty, iteratively pop every element from E and push it onto D, until E is empty, and then call D.pop(). Considering the worst case running time, what is the performance in...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer for all of them please. answer for all of them please
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class Polynomial (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. (3) define the following methods: a. public Polynomial()    POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0)    POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p)    POSTCONDITION: Creates...
you are asked to implement a C++ class to model a sorted array of unsigned integers....
you are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Class will provide (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be used...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT