Question

In: Computer Science

By using Java language, Complete the StackArray.java by using.stackArrayDemo.java. 1.Read the StackArrayDemo.java. 2. Use the demo...

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;
}
}

Solutions

Expert Solution


/*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*/


Related Solutions

JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4 grocery stores. Each store has 3 departments where product presentation affects sales (produce, meat, frozen). Every so often a department in a store gets a bonus for doing a really good job. You need to create a program that keeps a table of bonuses in the system for departments. Create a program that has a two dimensional array for these bonuses. The stores can...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs in the data base are one-way. Question: Write a program that can answer if there is a path between any to vertices. For the vertex pairs use this as your input example: AL...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs are two-way. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of vertices traversed from X to Y if there is a path....
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Objective: Using Java coding language, complete each "TO-DO" section for the following classes. Shape code (for...
Objective: Using Java coding language, complete each "TO-DO" section for the following classes. Shape code (for reference, nothing to complete here): import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.io.RandomAccessFile; public abstract class Shape extends Rectangle {    public int id;    public Color color;    public int xSpeed, ySpeed;       public Shape(int id, int x, int y, int width, int height, Color color, int xSpeed, int ySpeed) {        super(x,y,width,height);        this.id = id;        this.color...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT