Stack in Action Using Java


The Java version of the stackDemo program combines the definition of the Stack class and the stackDemo application class definition in the same file, which is shown in the next example. Java doesn t provide any facility for separating the definition from the implementation as C and C++ do.

Usually, programmers will place the Stack class definition in a Java package rather than in the Java application class. As you ll recall from your Java programming class, a Java package is a collection of Java class definitions that can be incorporated into the Java source code. Packages are used partly to organize the classes into logical groups, but more importantly packages are used for name resolution. The JDK has three classes called Element , but they reside in three different packages. You can tell the JDK which one you re referring to by using the package name. C++ uses namespaces for the same purpose.

You ll notice as you read through the next example that the Stack class definition contains the same attributes and member methods as described in previous Java examples. However, there is a new attribute defined in the first statement of the class definition: the DEFAULT_SIZE attribute, whose value determines the default size of the stack, which is set to 10 elements.

The DEFAULT_SIZE attribute is available only to member methods of the class as is signified by the keyword private . The DEFAULT_SIZE is also designed as static and final . The keyword static means that one copy of this attribute is placed into memory, regardless of how many instances of the class are created. That is, any member method of any instance can change the value of DEFAULT_SIZE , and the change affects all instances. This is different than other nonstatic attributes because each instance has its own set of nonstatic attributes. A change in the value of one of the attributes doesn t affect the same attribute of another instance. The keyword final states that the value of DEFAULT_SIZE cannot be changed once this statement initializes it. Attempts to change its value will cause an error.

Notice that there are two constructors defined for the Stack class. The no argument constructor calls the second constructor, passing it a value of DEFAULT_SIZE to initialize the stack. Java does not permit default parameter values like C and C++, so this is a way to accomplish the same thing in Java.

The Java application class definition performs basically the same functionality as the statements in the main() function of the C++ program. The program creates a stack and then uses the reference name to the stack and the dot operator to call the push() method three times. The value passed to the push() member method is placed on the stack each time.

The last portion of the Java application class definition calls the pop() member method three times. The value returned by the pop() method is displayed on the screen each time.

 public class Stack {  private static final int DEFAULT_SIZE = 10;  private int size;  private int top;  private int[] values;  public Stack()  {  this(DEFAULT_SIZE);  }  public Stack(int size)  {  this.size = size;  values = new int[size];  top = -1;  }  public boolean isFull()  {  if(top < size-1)  {  return false;  }  else  {  return true;  }  }  public boolean isEmpty()  {  if(top == -1)  {  return true;  }  else  {  return false;  }  }  public void push(int x)  {  if(!isFull())  {  top++;  values[top] = x;  }  }  public int pop()  {  int retVal = 0;  if(!isEmpty())  {  retVal = values[top];  top--;  }  return retVal;  } } public stackDemo{  void main(String args[]){  Stack stack = new Stack();  stack.push(10);  stack.push(20);  stack.push(30);  for(int i=0; i<3; i++)  {  System.out.println(stack.pop());      }  } } 



Data Structures Demystified
Data Structures Demystified (Demystified)
ISBN: 0072253592
EAN: 2147483647
Year: 2006
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net