Question: In what follows, you should complete the java code for the class Stack and MyMain based on the below questions. Put your code inside the
In what follows, you should complete the java code for the class Stack and MyMain based on the below questions. Put your code inside the java code given below.
- Write the code of the method pop( ) which removes and returns the element on top of the stack, the last element in list. (5 points)
- Write the code of the method push(E o) which adds element o to the stack, at first available position in list. (5 points)
- Write the code of the method push(Stack
other) : it concatenates this stack with the other stack by popping the other stack and adding the popped element to this stack. It means that if this stack has elements {10,2} and the other stack has {20,30,50}, it modifies this stack such that it contains {10,2,50,30,20} and other will become empty {}. (20 points) - Write the code of the method compareTo(Stack
other) which compares the size of this Stack object with the size of the other Stack. It returns 1 if this stack has a bigger size than other, 0 if they are equal, and -1 otherwise. (10 points) - In the main method, create two stacks: s1 and s2 to store integers. Ask the user to enter 11 numbers in s1 and 5 numbers in s2 and scan these numbers. Concatenate s1 to s2, s1 will be modified to contain the concatenation of the two stacks. Print the contents of the two stacks. (15 points)
| public interface Comparable public int compareTo(E o); } |
|
import java.util.ArrayList; public class Stack
private ArrayList
public boolean isEmpty(){ return list.isEmpty(); }
public int getSize(){ return list.size(); }
public String toString(){ return "Stack: "+list.toString(); } // Answer question 1 here (5 pts) // Answer question 2 here (5 pts) // Answer question 3 here (20 pts)
// Answer question 4 here (10 pts) } |
|
import java.util.ArrayList; import java.util.Scanner; public class MyMain{ // Answer question 5 here (15 pts) public static void main(String []args){ } }
|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
