473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need help.

28
I need a help in my method equalStack in my class StackClass.

Expand|Select|Wrap|Line Numbers
  1. public class StackClass
  2. {
  3.     private  int maxStackSize;    //variable to store the maximum
  4.                                   //stack size
  5.     private  int stackTop;        //variable to point to the top
  6.                                   //of the stack
  7.     private  DataElement[] list;  //array of reference variables
  8.  
  9.         //default constructor
  10.         //Create an array of size 100 to implement the stack.
  11.         //Postcondition: The variable list contains the base
  12.         //               address of the array, stackTop = 0, and
  13.         //               maxStackSize = 100.
  14.     public StackClass()
  15.     {
  16.         maxStackSize = 100;
  17.         stackTop = 0;         //set stackTop to 0
  18.         list = new DataElement[maxStackSize];   //create the array
  19.     }//end default constructor
  20.  
  21.         //constructor with a parameter
  22.         //Create an array of size stackSize to implement the stack.
  23.         //Postcondition: The variable list contains the base
  24.         //               address of the array, stackTop = 0, and
  25.         //               maxStackSize = stackSize.
  26.     public StackClass(int stackSize)
  27.     {
  28.         if(stackSize <= 0)
  29.         {
  30.            System.err.println("The size of the array to implement "
  31.                             + "the stack must be positive.");
  32.            System.err.println("Creating an array of size 100.");
  33.  
  34.            maxStackSize = 100;
  35.         }
  36.         else
  37.            maxStackSize = stackSize;   //set the stack size to
  38.                                        //the value specified by
  39.                                        //the parameter stackSize
  40.         stackTop = 0;    //set stackTop to 0
  41.         list = new DataElement[maxStackSize]; //create the array
  42.     }//end constructor
  43.  
  44.         //Method to initialize the stack to an empty state.
  45.         //Postcondition: stackTop = 0
  46.     public void initializeStack()
  47.     {
  48.         for(int i = 0; i < stackTop; i++)
  49.             list[i] = null;
  50.         stackTop = 0;
  51.     }//end initializeStack
  52.  
  53.  
  54.         //Method to determine whether the stack is empty.
  55.         //Postcondition: Returns true if the stack is empty;
  56.         //               otherwise, returns false.
  57.     public boolean isEmptyStack()
  58.     {
  59.         return (stackTop == 0);
  60.     }//end isEmptyStack
  61.  
  62.         //Method to determine whether the stack is full.
  63.         //Postcondition: Returns true if the stack is full;
  64.         //               otherwise, returns false.
  65.     public boolean isFullStack()
  66.     {
  67.         return (stackTop == maxStackSize);
  68.     }//end isFullStack
  69.  
  70.         //Method to add newItem to the stack.
  71.         //Precondition: The stack exists and is not full.
  72.         //Postcondition: The stack is changed and newItem
  73.         //               is added to the top of stack.
  74.         //               If the stack is full, the method throws
  75.         //               StackOverflowException
  76.     public void push(DataElement newItem) throws StackOverflowException
  77.     {
  78.         if(isFullStack())
  79.             throw new StackOverflowException();
  80.  
  81.         list[stackTop] = newItem.getCopy(); //add newItem at the
  82.                                             //top of the stack
  83.         stackTop++;                         //increment stackTop
  84.     }//end push
  85.  
  86.  
  87.         //Method to return the top element of the stack.
  88.         //Precondition: The stack exists and is not empty.
  89.         //Postcondition: If the stack is empty, the method throws
  90.         //               StackUnderflowException; otherwise, a
  91.         //               reference to a copy of the top element
  92.         //               of the stack is returned.
  93.     public DataElement top() throws StackUnderflowException
  94.     {
  95.         if(isEmptyStack())
  96.             throw new StackUnderflowException();
  97.  
  98.         DataElement temp = list[stackTop - 1].getCopy();
  99.  
  100.         return temp;
  101.     }//end top
  102.  
  103.         //Method to remove the top element of the stack.
  104.         //Precondition: The stack exists and is not empty.
  105.         //Postcondition: The stack is changed and the top
  106.         //               element is removed from the stack.
  107.         //               If the stack is empty, the method throws
  108.         //               StackUnderflowException
  109.     public void pop() throws StackUnderflowException
  110.     {
  111.         if(isEmptyStack())
  112.            throw new StackUnderflowException();
  113.  
  114.         stackTop--;       //decrement stackTop
  115.         list[stackTop] = null;
  116.     }//end pop
  117.  
  118.  
  119.         //Method to make a copy of otherStack.
  120.         //This method is used only to implement the methods
  121.         //copyStack and copy constructor
  122.         //Postcondition: A copy of otherStack is created and
  123.         //               assigned to this stack.
  124.     private void copy(StackClass otherStack)
  125.     {
  126.          list = null;
  127.          System.gc();
  128.  
  129.          maxStackSize = otherStack.maxStackSize;
  130.          stackTop = otherStack.stackTop;
  131.  
  132.          list = new DataElement[maxStackSize];
  133.  
  134.                //copy otherStack into this stack
  135.          for(int i = 0; i < stackTop; i++)
  136.              list[i] = otherStack.list[i].getCopy();
  137.     }//end copy
  138.  
  139.  
  140.          //copy constructor
  141.     public StackClass(StackClass otherStack)
  142.     {
  143.         copy(otherStack);
  144.     }//end constructor
  145.  
  146.         //Method to make a copy of otherStack.
  147.         //Postcondition: A copy of otherStack is created and
  148.         //               assigned to this stack.
  149.     public void copyStack(StackClass otherStack)
  150.     {
  151.         if(this != otherStack) //avoid self-copy
  152.            copy(otherStack);
  153.     }//end copyStack
  154.  
  155.  
  156.         //Method to determine if a given stack has the
  157.         //same content as the current one
  158.         //Postcondition: Returns true if the two stacks have
  159.         //               same content, false otherwise.
  160.    public boolean equalStack(StackClass otherStack)
  161.     {
  162.         if (this.isEmptyStack() == otherStack.isEmptyStack())
  163.          {
  164.             try
  165.             {
  166.                 while(top().compareTo( otherStack.top())==0)
  167.                 {
  168.                     pop();
  169.                     otherStack.pop();
  170.                 }
  171.                     return false ;
  172.             }
  173.             catch (StackUnderflowException e)
  174.             {
  175.                 return true;
  176.             }
  177.         }
  178.         else
  179.             return false;
  180.     }
  181. }

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class TestProgStack
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         StackClass stack1 = new StackClass();
  8.         StackClass stack2 = new StackClass();
  9.  
  10.         try
  11.         {
  12.             stack1.push(new IntElement(5));
  13.             stack1.push(new IntElement(7));
  14.             stack1.push(new IntElement(1));
  15.             stack1.push(new IntElement(3));
  16.             stack1.push(new IntElement(2));
  17.         }
  18.         catch(StackOverflowException sofe)
  19.         {
  20.             System.out.println(sofe.toString());
  21.             System.exit(0);
  22.         }
  23.  
  24.         stack1.copyStack(stack1);
  25.  
  26.         System.out.print("stack1 elements are: ");
  27.  
  28.         while (!stack1.isEmptyStack())
  29.         {
  30.             System.out.print(stack1.top() + " ");
  31.             stack1.pop();
  32.           }
  33.            System.out.println();
  34.  
  35.         try
  36.         {
  37.             stack2.push(new IntElement(1));
  38.             stack2.push(new IntElement(3));
  39.             stack2.push(new IntElement(2));
  40.             stack2.push(new IntElement(5));
  41.             stack2.push(new IntElement(7));
  42.             stack2.push(new IntElement(0));
  43.  
  44.         }
  45.         catch(StackOverflowException sofe)
  46.         {
  47.              System.out.println(sofe.toString());
  48.              System.exit(0);
  49.         }
  50.  
  51.         stack2.copyStack(stack2);
  52.  
  53.         System.out.print("stack2 elements are: ");
  54.  
  55.         while (!stack2.isEmptyStack())
  56.         {
  57.              System.out.print(stack2.top() + " ");
  58.               stack2.pop();
  59.        }
  60.         System.out.println();
  61.  
  62.         if(stack1.equalStack(stack2))
  63.         System.out.println("The two Stacks have the same content");
  64.         else
  65.         System.out.println("The two Stacks have NOT the same content");
  66.  
  67.     }
  68. }
Oct 31 '06 #1
5 1987
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. //How about making use of the size of the stack 
  2. public boolean equalStack(StackClass otherStack) {
  3.     if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
  4.         return false;
  5.     }
  6.     else {
  7.         while(!isEmpty()) {
  8.             if(top().compareTo(otherStack.top()) != 0) {
  9.                 return false;
  10.             }
  11.             else {
  12.                 try {
  13.                     pop();
  14.                     otherStack.pop();
  15.                 }
  16.                 catch (StackUnderflowException e) {//This will no longer be neccessary?
  17.                     return true;
  18.                 }
  19.  
  20.             }
  21.         }
  22.     }
  23.     return true;
  24. }
  25.  
Nov 1 '06 #2
satan
28
[quote=r035198x]
Expand|Select|Wrap|Line Numbers
  1. //How about making use of the size of the stack 
  2. public boolean equalStack(StackClass otherStack) {
  3.     if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
  4.         return false;
  5.     }
  6.     else {
  7.         while(!isEmpty()) {
  8.             if(top().compareTo(otherStack.top()) != 0) {
  9.                 return false;
  10.             }
  11.             else {
  12.                 try {
  13.                     pop();
  14.                     otherStack.pop();
  15.                 }
  16.                 catch (StackUnderflowException e) {//This will no longer be neccessary?
  17.                     return true;
  18.                 }
  19.  
  20.             }
  21.         }
  22.     }
  23.     return true;
  24. }
  25.  
[/QUOTE


I've tried iy but it's not working.
Nov 1 '06 #3
r035198x
13,262 8TB
[quote=satan]
Expand|Select|Wrap|Line Numbers
  1. //How about making use of the size of the stack 
  2. public boolean equalStack(StackClass otherStack) {
  3.     if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
  4.         return false;
  5.     }
  6.     else {
  7.         while(!isEmpty()) {
  8.             if(top().compareTo(otherStack.top()) != 0) {
  9.                 return false;
  10.             }
  11.             else {
  12.                 try {
  13.                     pop();
  14.                     otherStack.pop();
  15.                 }
  16.                 catch (StackUnderflowException e) {//This will no longer be neccessary?
  17.                     return true;
  18.                 }
  19.  
  20.             }
  21.         }
  22.     }
  23.     return true;
  24. }
  25.  
[/QUOTE


I've tried iy but it's not working.
Where is it not working? Is it not compiling? Please explain the problem more clearly.
Nov 2 '06 #4
satan
28
[quote=r035198x]

Where is it not working? Is it not compiling? Please explain the problem more clearly.
Yes, it's not compiling. it can not find the symbol getSize in my method StackClass
Nov 2 '06 #5
r035198x
13,262 8TB
[quote=satan]
Yes, it's not compiling. it can not find the symbol getSize in my method StackClass
Ah now surely you should have realised that you need that method for your stack and put it in. Write a method that returns the number of items in the stack and call it getSize, if you already have one use it in place of getSize.
Nov 2 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.