is this method correct. i tried myself
package stack;
public class MaxElement {
private int[] data;
private int[] arr;
private int top;
public MaxElement(int capacity) throws Exception{
if(capacity<1) {
throw new Exception(“INVALID CAPACITY”);
}
this.data=new int[capacity];
this.arr=new int[capacity];
this.top=-1;
}
public int size(){
return this.top+1;
}
public void push(int value) throws Exception{
if(this.size()==this.data.length) {
throw new Exception("STACK IS FULL");
}
this.top++;
this.data[this.top]=value;
}
public void pop() throws Exception{
if(this.size()==0) {
throw new Exception("STACK IS EMPTY");
}
int rv=this.data[this.top];
this.data[this.top]=0;
this.top--;
}
public int top() throws Exception{
if(this.size()==0) {
throw new Exception("STACK IS EMPTY");
}
int rv=this.data[this.top];
return rv;
}
public void display(){
for(int i=this.top;i>=0;i--) {
System.out.print("<<"+this.data[i]);
}
System.out.println(" END");
}
}
code 2-
package stack;
public class MaxElement_Client {
public static void main(String[] args) throws Exception{
MaxElement mx=new MaxElement(8);
int[] ary= {1,2,3,1,4,5,2,3,6};
int max=0;
int k=0,j=0;
for(int i=0;i<=ary.length;i++) {
if(mx.size()<3) {
mx.push(ary[i]);
if(ary[i]>max) {
max=ary[i];
}
}
else {
j++;
i=j-1;
System.out.println(max);
while(mx.size()!=0) {
mx.pop();
}
}
}
}
}