Friday, July 07, 2006

Java: A Basic Stack Class, Keyboard Input, and using the Random Number Generator

Since I have been playing around with Eclipse to a greater extent recently, I decided it was time to dive back into Java programming. It has been roughly ten years since my last foray into Java, so it is interesting to go back.

First, let me say a few things about Java. I like Java, I really do. The main reason Java isn’t my programming language of choice is not due to any negative factors on Javas part. My philosophy in regards to programming languages has always been “the proper tool for the job”. Java, as far as a language goes, has some excellent features. Coming from a C/C++ background, the automatic garbage collection is a godsend if you disregard some of the third party garbage collectors for the C/C++ platform. Not having to worry about destroying objects is great, and allows a developer to focus more on the problem domain instead of the platform specifics. In fact, the main reason I don’t use Java more often is more due to the unwillingness to learn a new set of libraries and classes. The large number of re-usable code in the Java language is both a blessing and a curse. From my perspective, it is overwhelming. There are the Java main libraries, such as java.util.*, java.io.*, java.net.*, java.lang.*, etc etc. Then there is the third party Java libraries, such as the OMG libraries, the Eclipse Libraries, the Apache libraries, just to name a few. So much power, and so much to learn. I suppose it is time to get my feet wet.

My first program, of course, was Hello World. Instead of showing that, I hopped into something a little more interesting. I took the old “Stack” problem from my early Computer Science days and implemented it in Java. Very basic stuff, but at least I got something done in it that works.

The following program implements my basic Stack class. The Stack itself uses a Stack of integers. There are only three methods, push, pop, and getCount. Push and Pop are self explanatory, and getCount just returns the size of the current stack. There are not bounds checking in this stack implementation. There are more efficient ways to implement this in Java, such as using Java’s built-in list class, however I wanted to use a little less hand holding on this one to familiarize myself with the basics of the language.

The Stack Class is below:

package stacktest;

import java.util.*;

public class Stack {
     private int [] itemStack;
     private int stackCount;
     
     public Stack() {
          stackCount = 0;
          itemStack = new int[255];
     }
     
     void push(int x)
     {
          itemStack[stackCount] = x;
          stackCount++;
     }
     
     int pop()
     {
          int temp;
          
          if (stackCount > 0)
          {
               stackCount--;
               temp = itemStack[stackCount];
          }     
          else
          {
               temp = -1;
          }
          return temp;
     }
     
     int getSize()
     {
          return stackCount;
     }
}

The program itself simply implements said stack, generates a random number of elements to populate onto the stack using Java’s Random Number Generator, and prompts the user to enter a number to push onto the stack. There is no type checking. The program is below:

package stacktest;

import java.io.*;
import java.util.*;

public class Main {
        public static void main(String[] args) {
              Stack s = new Stack();
              String input = "";
              int x;

              BufferedReader br = new BufferedReader(
              new InputStreamReader(System.in));
              Random gen = new Random();
            
              for (x = 0; x < gen.nextInt(10); x++)
              {
                   System.out.print( "Enter a number: " );
                  try
                  {
                       input = br.readLine();
                  }
                  catch (IOException err)
                  {
                       System.out.println("There was an error");
                  }
                      
                  if (Integer.parseInt(input)!= -1)
                  {
                       s.push(Integer.parseInt(input));
                       System.out.println("Number of elements in stack: " + s.getSize());
                  }
              }

              System.out.println("The values in the stack are: ");
              for (x = s.getSize(); x > 0; x--)
              {
                 System.out.print(s.pop() + " ");
              }
        }
}

As far as the old IDE debate for Java in regards to Eclipse vs NetBeans, I am undecided. Both had excellent features. At first, Eclipse was not responsive and did not have the class/function hints that I have grown used to in other languages, however after a few minutes, that seems to clear up. NetBeans is a little more responsive, however it is not extensible like Eclipse. I will definitely have to play around with these a little more. However, I feel that I may lean a little more towards Eclipse since BIRT is implemented using the Eclipse platform and my main goal is to work with extending and customizing BIRT.

No comments: