In: Computer Science
By using Java language, Complete the StackArray.java by using.stackArrayDemo.java.
1.Read the StackArrayDemo.java.
2. Use the demo to complete StackArray.java
StackArrayDemo.java:
public class StackArrayDemo {
public static void main(String [] args) {
StackArray a = new StackArray();
int score = 0;
if (a.peek() == null)
score += 10;
else
System.out.println("Check peek()");
a.push("Orange");
System.out.println(a);
a.push("Apple");
System.out.println(a);
a.push("Guava");
System.out.println(a);
if (a.peek().equals("Guava"))
score += 10;
else
System.out.println("Check push()");
a.pop();
System.out.println(a);
a.pop();
System.out.println(a);
if (a.peek().equals("Orange"))
score += 10;
else
System.out.println("Check pop()");
a.push("Mango");
System.out.println(a);
if (a.getStackSize() == 2)
score += 10;
else
System.out.println("Check getStackSize()");
System.out.printf("Your score is %d/40\n", score);
}
}
StackArray.java
public class StackArray <T> {
public static int CAPACITY = 100;
private final T[] elements;
private int topIndex;
// Constructor
public StackArray() {
// Initialize elements
// Initialize topIndex to -1
}
public T peek() {
// If topIndex is less than zero, return null.
// Otherwise, return element from top of the stack.
}
public T pop() {
// If topIndex is less than zero, return null.
// Otherwise return element from top of the stack, and decrement
topIndex
}
public void push(T obj) {
// Push obj into top of the stack
// And, increment topIndex
}
public int getStackSize() {
// Return stack size, i.e., number of elements
}
@Override
public String toString() {
String s = "The stack is: ";
for (int i = topIndex; i >= 0; i--)
s += elements[i] + " ";
return s;
}
}
/*StackArray.java*/
public class StackArray <T> {
public static int CAPACITY = 100;
private final T[] elements;
private int topIndex;
// Constructor
public StackArray() {
topIndex = -1;
elements = (T[]) new Object[CAPACITY];
}
public T peek() {
if(topIndex < 0){
return null;
}
return elements[topIndex];
}
public T pop() {
if(topIndex < 0) return null;
return elements[topIndex--];
}
public void push(T obj) {
if(topIndex > CAPACITY) System.out.println("OverFlow");
elements[++topIndex] = obj;
}
public int getStackSize() {
return topIndex;
}
@Override
public String toString() {
String s = "The stack is: ";
for (int i = topIndex; i >= 0; i--)
s += elements[i] + " ";
return s;
}
}
/* StackArrayDemo.java*/
public class StackArrayDemo {
public static void main(String [] args) {
StackArray a = new StackArray();
int score = 0;
if (a.peek() == null)
score += 10;
else
System.out.println("Check peek()");
a.push("Orange");
System.out.println(a);
a.push("Apple");
System.out.println(a);
a.push("Guava");
System.out.println(a);
if (a.peek().equals("Guava"))
score += 10;
else
System.out.println("Check push()");
a.pop();
System.out.println(a);
a.pop();
System.out.println(a);
if (a.peek().equals("Orange"))
score += 10;
else
System.out.println("Check pop()");
a.push("Mango");
System.out.println(a);
if (a.getStackSize() == 2)
score += 10;
else
System.out.println("Check getStackSize()");
System.out.printf("Your score is %d/40\n", score);
}
}
/* OUTPUT*/