Question: Create a class named NumStack.java, copy the content from NumStack.txt then implement the public int compareTo(NumStacj s) method. public int compareTo(NumStack s) compares two stack

Create a class named NumStack.java, copy the content from NumStack.txt then implement the public int compareTo(NumStacj s) method.

public int compareTo(NumStack s) compares two stack from the bottom of each stack, for example, s1[2,0, 9] and s2[9,0,9], calling s1.compareTo(s2) should return -1, s1 s2 top 9 9 3rd: 9==9 we get 0 0 0 2nd: 0==0 we get 0 bottom 2 9 1st: 2<9 we get -1 ------------------------------------------ add all and return: if the two stacks have different sizes, then consider ith item is greater than "none-existing item" in smaller stack. for example, s1[8,0, -9] s2[1], calling s1.compareto(s2) should return 3, s1 s2 top -9 3rd: no ->1 0 2nd: no item in s2 ->1 bottom 8 1 1st: 8>1 ->1 ----------------------------------------- add all and return: 3

Note: This method should not change the structure or contents of the caller and argument stack

public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap]; index =-1; } public boolean isEmpty(){ return index == -1; } public boolean isFull(){ return index==data.length -1; } public NumStack pop(){ if(!isEmpty()) data[index--]=null; return this; } public int[] peeking(){ int[] tmp=new int[index+1]; for(int i=0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!