I'm having trouble getting my program to compile.

I have to add the method equalStack to the class StackClass that takes as a parameter a StackClass object, say otherStack, and returns true if this stack is the same as other Stack.

When I compile the program I get lots of errors. Can someone please help me?

Here is my code:

import java.util.*;

public class Stacks
{
    public static void main(String[] args)
    {
        StackClass stack1 = new StackClass();
        StackClass stack2 = new StackClass();

        try
        {
            stack1.push(new IntElement(5));
            stack1.push(new IntElement(7));
            stack1.push(new IntElement(1));
            stack1.push(new IntElement(3));
            stack1.push(new IntElement(2));
        }
        catch(StackOverflowException sofe)
        {
            System.out.println(sofe.toString());
            System.exit(0);
        }

        stack1.copyStack(stack1);

        System.out.print("stack1 elements are: ");

        while (!stack1.isEmptyStack())
        {
            System.out.print(stack1.top() + " ");
            stack1.pop();
          }
           System.out.println();

        try
        {
            stack2.push(new IntElement(1));
            stack2.push(new IntElement(3));
            stack2.push(new IntElement(2));
            stack2.push(new IntElement(5));
            stack2.push(new IntElement(7));
            stack2.push(new IntElement(0));

        }
        catch(StackOverflowException sofe)
        {
             System.out.println(sofe.toString());
             System.exit(0);
        }

        stack2.copyStack(stack2);

        System.out.print("stack2 elements are: ");

        while (!stack2.isEmptyStack())
        {
             System.out.print(stack2.top() + " ");
              stack2.pop();
       }
        System.out.println();

        if(stack1.equalStack(stack2))
        System.out.println("The two Stacks have the same content");
        else
        System.out.println("The two Stacks have NOT the same content");
        
    }
}
public class StackClass
{
    private  int maxStackSize;    //variable to store the maximum
                                  //stack size
    private  int stackTop;        //variable to point to the top
                                  //of the stack
    private  DataElement[] list;  //array of reference variables

        //default constructor
        //Create an array of size 100 to implement the stack.
        //Postcondition: The variable list contains the base
        //               address of the array, stackTop = 0, and
        //               maxStackSize = 100.
    public StackClass()
    {
        maxStackSize = 100;
        stackTop = 0;         //set stackTop to 0
        list = new DataElement[maxStackSize];   //create the array
    }//end default constructor

        //constructor with a parameter
        //Create an array of size stackSize to implement the stack.
        //Postcondition: The variable list contains the base
        //               address of the array, stackTop = 0, and
        //               maxStackSize = stackSize.
    public StackClass(int stackSize)
    {
        if(stackSize <= 0)
        {
           System.err.println("The size of the array to implement "
                            + "the stack must be positive.");
           System.err.println("Creating an array of size 100.");

           maxStackSize = 100;
        }
        else
           maxStackSize = stackSize;   //set the stack size to
                                       //the value specified by
                                       //the parameter stackSize
        stackTop = 0;    //set stackTop to 0
        list = new DataElement[maxStackSize]; //create the array
    }//end constructor

        //Method to initialize the stack to an empty state.
        //Postcondition: stackTop = 0
    public void initializeStack()
    {
        for(int i = 0; i < stackTop; i++)
            list[i] = null;
        stackTop = 0;
    }//end initializeStack


        //Method to determine whether the stack is empty.
        //Postcondition: Returns true if the stack is empty;
        //               otherwise, returns false.
    public boolean isEmptyStack()
    {
        return (stackTop == 0);
    }//end isEmptyStack

        //Method to determine whether the stack is full.
        //Postcondition: Returns true if the stack is full;
        //               otherwise, returns false.
    public boolean isFullStack()
    {
        return (stackTop == maxStackSize);
    }//end isFullStack

        //Method to add newItem to the stack.
        //Precondition: The stack exists and is not full.
        //Postcondition: The stack is changed and newItem
        //               is added to the top of stack.
        //               If the stack is full, the method throws
        //               StackOverflowException
    public void push(DataElement newItem) throws StackOverflowException
    {
        if(isFullStack())
            throw new StackOverflowException();

        list[stackTop] = newItem.getCopy(); //add newItem at the
                                            //top of the stack
        stackTop++;                         //increment stackTop
    }//end push


        //Method to return the top element of the stack.
        //Precondition: The stack exists and is not empty.
        //Postcondition: If the stack is empty, the method throws
        //               StackUnderflowException; otherwise, a
        //               reference to a copy of the top element
        //               of the stack is returned.
    public DataElement top() throws StackUnderflowException
    {
        if(isEmptyStack())
            throw new StackUnderflowException();

        DataElement temp = list[stackTop - 1].getCopy();

        return temp;
    }//end top

        //Method to remove the top element of the stack.
        //Precondition: The stack exists and is not empty.
        //Postcondition: The stack is changed and the top
        //               element is removed from the stack.
        //               If the stack is empty, the method throws
        //               StackUnderflowException
    public void pop() throws StackUnderflowException
    {
        if(isEmptyStack())
           throw new StackUnderflowException();

        stackTop--;       //decrement stackTop
        list[stackTop] = null;
    }//end pop


        //Method to make a copy of otherStack.
        //This method is used only to implement the methods
        //copyStack and copy constructor
        //Postcondition: A copy of otherStack is created and
        //               assigned to this stack.
    private void copy(StackClass otherStack)
    {
         list = null;
         System.gc();

         maxStackSize = otherStack.maxStackSize;
         stackTop = otherStack.stackTop;

         list = new DataElement[maxStackSize];

               //copy otherStack into this stack
         for(int i = 0; i < stackTop; i++)
             list[i] = otherStack.list[i].getCopy();
    }//end copy


         //copy constructor
    public StackClass(StackClass otherStack)
    {
        copy(otherStack);
    }//end constructor

        //Method to make a copy of otherStack.
        //Postcondition: A copy of otherStack is created and
        //               assigned to this stack.
    public void copyStack(StackClass otherStack)
    {
        if(this != otherStack) //avoid self-copy
           copy(otherStack);
    }//end copyStack


        //Method to determine if a given stack has the
        //same content as the current one
        //Postcondition: Returns true if the two stacks have
        //               same content, false otherwise.
   public boolean equalStack(StackClass otherStack)
    {
        if (this.isEmptyStack() == otherStack.isEmptyStack())
         {
            try
            {
                while(top().compareTo( otherStack.top())==0)
                {
                    pop();
                    otherStack.pop();
                }
                    return false;
            }
            catch (StackUnderflowException e)
            {
                return true;
            }
        }
        else
            return false;
    }
}

One, you've made your StackClass class public, so it needs to be in a file called StackClass.java. Two, you refer to some classes called IntElement and DataElement, but don't provide them. There are a few more errors in there, I think, but fixing those will get rid of a bunch of them.

I made some revisions to the program, but I am still getting an error. The error is as follows: TestProgStack.java:7: illegal start of expression
public class StackClass

Not sure where I am going wrong. Please help.

import java.util.*;

public class TestProgStack
{
    public static void main(String[] args)
    {
	 public class StackClass
	 {

    private  int maxStackSize;    //variable to store the maximum
                                  //stack size
    private  int stackTop;        //variable to point to the top
                                  //of the stack
    private  DataElement[] list;  //array of reference variables

        //default constructor
        //Create an array of size 100 to implement the stack.
        //Postcondition: The variable list contains the base
        //               address of the array, stackTop = 0, and
        //               maxStackSize = 100.
    public StackClass()
    {
        maxStackSize = 100;
        stackTop = 0;         //set stackTop to 0
        list = new DataElement[maxStackSize];   //create the array
    }//end default constructor

        //constructor with a parameter
        //Create an array of size stackSize to implement the stack.
        //Postcondition: The variable list contains the base
        //               address of the array, stackTop = 0, and
        //               maxStackSize = stackSize.
    public StackClass(int stackSize)
    {
        if(stackSize <= 0)
        {
           System.err.println("The size of the array to implement "
                            + "the stack must be positive.");
           System.err.println("Creating an array of size 100.");

           maxStackSize = 100;
        }
        else
           maxStackSize = stackSize;   //set the stack size to
                                       //the value specified by
                                       //the parameter stackSize
        stackTop = 0;    //set stackTop to 0
        list = new DataElement[maxStackSize]; //create the array
    }//end constructor

        //Method to initialize the stack to an empty state.
        //Postcondition: stackTop = 0
    public void initializeStack()
    {
        for(int i = 0; i < stackTop; i++)
            list[i] = null;
        stackTop = 0;
    }//end initializeStack


        //Method to determine whether the stack is empty.
        //Postcondition: Returns true if the stack is empty;
        //               otherwise, returns false.
    public boolean isEmptyStack()
    {
        return (stackTop == 0);
    }//end isEmptyStack

        //Method to determine whether the stack is full.
        //Postcondition: Returns true if the stack is full;
        //               otherwise, returns false.
    public boolean isFullStack()
    {
        return (stackTop == maxStackSize);
    }//end isFullStack

        //Method to add newItem to the stack.
        //Precondition: The stack exists and is not full.
        //Postcondition: The stack is changed and newItem
        //               is added to the top of stack.
        //               If the stack is full, the method throws
        //               StackOverflowException
    public void push(DataElement newItem) throws StackOverflowException
    {
        if(isFullStack())
            throw new StackOverflowException();

        list[stackTop] = newItem.getCopy(); //add newItem at the
                                            //top of the stack
        stackTop++;                         //increment stackTop
    }//end push


        //Method to return the top element of the stack.
        //Precondition: The stack exists and is not empty.
        //Postcondition: If the stack is empty, the method throws
        //               StackUnderflowException; otherwise, a
        //               reference to a copy of the top element
        //               of the stack is returned.
    public DataElement top() throws StackUnderflowException
    {
        if(isEmptyStack())
            throw new StackUnderflowException();

        DataElement temp = list[stackTop - 1].getCopy();

        return temp;
    }//end top

        //Method to remove the top element of the stack.
        //Precondition: The stack exists and is not empty.
        //Postcondition: The stack is changed and the top
        //               element is removed from the stack.
        //               If the stack is empty, the method throws
        //               StackUnderflowException
    public void pop() throws StackUnderflowException
    {
        if(isEmptyStack())
           throw new StackUnderflowException();

        stackTop--;       //decrement stackTop
        list[stackTop] = null;
    }//end pop


        //Method to make a copy of otherStack.
        //This method is used only to implement the methods
        //copyStack and copy constructor
        //Postcondition: A copy of otherStack is created and
        //               assigned to this stack.
    private void copy(StackClass otherStack)
    {
         list = null;
         System.gc();

         maxStackSize = otherStack.maxStackSize;
         stackTop = otherStack.stackTop;

         list = new DataElement[maxStackSize];

               //copy otherStack into this stack
         for(int i = 0; i < stackTop; i++)
             list[i] = otherStack.list[i].getCopy();
    }//end copy


         //copy constructor
    public StackClass(StackClass otherStack)
    {
        copy(otherStack);
    }//end constructor

        //Method to make a copy of otherStack.
        //Postcondition: A copy of otherStack is created and
        //               assigned to this stack.
    public void copyStack(StackClass otherStack)
    {
        if(this != otherStack) //avoid self-copy
           copy(otherStack);
    }//end copyStack


        //Method to determine if a given stack has the
        //same content as the current one
        //Postcondition: Returns true if the two stacks have
        //               same content, false otherwise.
   public boolean equalStack(StackClass otherStack)
    {
        if (this.isEmptyStack() == otherStack.isEmptyStack())
         {
            try
            {
                while(top().compareTo( otherStack.top())==0)
                {
                    pop();
                    otherStack.pop();
                }
                    return false;
            }
            catch (StackUnderflowException e)
            {
                return true;
            }
        }
        else
            return false;
    }
}
        StackClass stack1 = new StackClass();
        StackClass stack2 = new StackClass();

        try
        {
            stack1.push(new IntElement(5));
            stack1.push(new IntElement(7));
            stack1.push(new IntElement(1));
            stack1.push(new IntElement(3));
            stack1.push(new IntElement(2));
        }
        catch(StackOverflowException sofe)
        {
            System.out.println(sofe.toString());
            System.exit(0);
        }

        stack1.copyStack(stack1);

        System.out.print("stack1 elements are: ");

        while (!stack1.isEmptyStack())
        {
            System.out.print(stack1.top() + " ");
            stack1.pop();
          }
           System.out.println();

        try
        {
            stack2.push(new IntElement(1));
            stack2.push(new IntElement(3));
            stack2.push(new IntElement(2));
            stack2.push(new IntElement(5));
            stack2.push(new IntElement(7));
            stack2.push(new IntElement(0));

        }
        catch(StackOverflowException sofe)
        {
             System.out.println(sofe.toString());
             System.exit(0);
        }

        stack2.copyStack(stack2);

        System.out.print("stack2 elements are: ");

        while (!stack2.isEmptyStack())
        {
             System.out.print(stack2.top() + " ");
              stack2.pop();
       }
        System.out.println();

        if(stack1.equalStack(stack2))
        System.out.println("The two Stacks have the same content");
        else
        System.out.println("The two Stacks have NOT the same content");
        
    }
}

You cannot declare a class inside your main method.

I'm really confused now.

Look at your main method. You declare public class StackClass as the first statement. You cannot declare a public class in that method. (You could declare a local class there but I really doubt that is your intent)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.