Please review my code java Someone helped me with it but i c

Please review my code (java)

Someone helped me with it but i cannot get it to compile i tried to separate the code that didnt work.

package ch02.stacks;
import java.lang.Iterable;
import java.util.*;
import java.lang.*;
import java.io.*;

public class ArrayBoundedStack<T> implements StackInterface<T>

public class StackUnderflowException extends RuntimeException
class StackUnderflowException extends RuntimeException
{
public StackUnderflowException()
{
    super();
}

public StackUnderflowException(String message)
{
    super(message);
}
}
class StackOverflowException extends RuntimeException
{
public StackOverflowException()
{
    super();
}

public StackOverflowException(String message)
{
    super(message);
}
}
class ArrayBoundedStack<T> implements StackInterface<T>
{
protected final int DEFCAP = 100; // default capacity
protected T[] elements;           // holds stack elements
protected int topIndex = -1;      // index of top element in stack

public ArrayBoundedStack()
{
    elements = (T[]) new Object[DEFCAP];
}

public ArrayBoundedStack(int maxSize)
{
    elements = (T[]) new Object[maxSize];
}

public void push(T element)
// Throws StackOverflowException if this stack is full,
// otherwise places element at the top of this stack.
{  
    if (isFull())
      throw new StackOverflowException(\"Push attempted on a full stack.\");
    else
    {
      topIndex++;
      elements[topIndex] = element;
    }
}

public void pop()
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
{              
    if (isEmpty())
      throw new StackUnderflowException(\"Pop attempted on an empty stack.\");
    else
    {
      elements[topIndex] = null;
      topIndex--;
    }
}

public T top()
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element of this stack.
{             
    T topOfStack = null;
    if (isEmpty())
      throw new StackUnderflowException(\"Top attempted on an empty stack.\");
    else
      topOfStack = elements[topIndex];
    return topOfStack;
}

public boolean isEmpty()
// Returns true if this stack is empty, otherwise returns false.
{          
    return (topIndex == -1);
}

public boolean isFull()
// Returns true if this stack is full, otherwise returns false.
{          
    return (topIndex == (elements.length - 1));
}
public String toString()
{
    String ans=new String();
    int counter=topIndex;
    while(counter>=0)
    {
      ans=ans+elements[topIndex].toString()+\"--\";
      counter--;
    }

}
public int size()
{
    return topIndex+1;
}
public void popSome(int count)
{
    if(size()<count)
      throw new StackUnderflowException(\"Less number of elements in stack.\");
    while(count>0)
    {
      elements[topIndex] = null;
      topIndex--;
      count--;
    }
}
public boolean swapStart()
{
    if(size()<2)
      return false;
    T topOfStack = null;
    topOfStack=elements[topIndex];
    elements[topIndex]=elements[topIndex-1];
    elements[topIndex-1]=topOfStack;
    return true;
}
public T poptop()
{
    if(topIndex==-1)
      throw new StackUnderflowException(\"Top attempted on an empty stack.\");
    else
    {
      T t=null;
      t=elements[topIndex--];
      return t;
    }
}
}
public class ArrayBounded
{
public static void main(String args[])
{
    Stack<Integer> s=new ArrayBoundedStack<Integer>();
    s.push(10);
    s.push(20);
    s.push(30);
    System.out.println(s.toString());
    System.out.println(s.size());
    System.out.println(s.poptop());

}
}

Solution

Follow is the proper code ( rectified the errors and flow ).

********

Class 1: Create ArrayBoundedTest.java file and paste the below code...

package test;

import java.util.Stack;

public class ArrayBoundedTest
{
public static void main(String args[])
{
ArrayBoundedStack<Integer> s=new ArrayBoundedStack<Integer>();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.toString());
System.out.println(s.size());
System.out.println(s.poptop());
}
}

***********************

Class 2: create the ArrayBoundedStack.java file and paste the below code

package test;


import java.lang.Iterable;
import java.util.*;
import java.lang.*;
import java.io.*;

public class ArrayBoundedStack<T> {

protected final int DEFCAP = 100; // default capacity
protected T[] elements; // holds stack elements
protected int topIndex = -1; // index of top element in stack
public ArrayBoundedStack()
{
elements = (T[]) new Object[DEFCAP];
}
public ArrayBoundedStack(int maxSize)
{
elements = (T[]) new Object[maxSize];
}
public void push(T element)
// Throws StackOverflowException if this stack is full,
// otherwise places element at the top of this stack.
{
if (isFull())
throw new StackOverflowException(\"Push attempted on a full stack.\");
else
{
topIndex++;
elements[topIndex] = element;
}
}
public void pop()
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
{
if (isEmpty())
throw new StackUnderflowException(\"Pop attempted on an empty stack.\");
else
{
elements[topIndex] = null;
topIndex--;
}
}
public T top()
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element of this stack.
{
T topOfStack = null;
if (isEmpty())
throw new StackUnderflowException(\"Top attempted on an empty stack.\");
else
topOfStack = elements[topIndex];
return topOfStack;
}
public boolean isEmpty()
// Returns true if this stack is empty, otherwise returns false.
{
return (topIndex == -1);
}
public boolean isFull()
// Returns true if this stack is full, otherwise returns false.
{
return (topIndex == (elements.length - 1));
}
public String toString()
{
String ans=new String();
int counter=topIndex;
while(counter>=0)
{
ans=ans+elements[topIndex].toString()+\"--\";
counter--;
}
return ans;
}
public int size()
{
return topIndex+1;
}
public void popSome(int count)
{
if(size()<count)
throw new StackUnderflowException(\"Less number of elements in stack.\");
while(count>0)
{
elements[topIndex] = null;
topIndex--;
count--;
}
}
public boolean swapStart()
{
if(size()<2)
return false;
T topOfStack = null;
topOfStack=elements[topIndex];
elements[topIndex]=elements[topIndex-1];
elements[topIndex-1]=topOfStack;
return true;
}
public T poptop()
{
if(topIndex==-1)
throw new StackUnderflowException(\"Top attempted on an empty stack.\");
else
{
T t=null;
t=elements[topIndex--];
return t;
}
}
}

***********************************

class 3: create the StackOverflowException.java file and paste the below code

package test;

public class StackOverflowException extends RuntimeException
{
public StackOverflowException()
{
super();
}
public StackOverflowException(String message)
{
super(message);
}
}

******************************

class 4: create the StackUnderflowException.java file and paste the below code

package test;
public class StackUnderflowException extends RuntimeException

{
public StackUnderflowException()
{
super();
}
public StackUnderflowException(String message)
{
super(message);
}
}

**************************************

Note.. create all classes the the package test.

Please review my code (java) Someone helped me with it but i cannot get it to compile i tried to separate the code that didnt work. package ch02.stacks; import
Please review my code (java) Someone helped me with it but i cannot get it to compile i tried to separate the code that didnt work. package ch02.stacks; import
Please review my code (java) Someone helped me with it but i cannot get it to compile i tried to separate the code that didnt work. package ch02.stacks; import
Please review my code (java) Someone helped me with it but i cannot get it to compile i tried to separate the code that didnt work. package ch02.stacks; import
Please review my code (java) Someone helped me with it but i cannot get it to compile i tried to separate the code that didnt work. package ch02.stacks; import
Please review my code (java) Someone helped me with it but i cannot get it to compile i tried to separate the code that didnt work. package ch02.stacks; import

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site