HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Implementing Basic Data Structures in Java"
Vol. 1, Issue 2, P.36

	

Listing 1: Conversion from an int to an Integer

//first we declare the variables
int		myInt;
Integer	myInteger;

//give myInt a value of 5
myInt = 5;

//now create a new Integer object with contains the value 5
myInteger = new Integer( myInt );

//the value can be pulled out of the Integer object 
//with the following line
int theInt = myInteger.intValue();

Listing 2: The floatStack class extends Stack to allow floats as input

//declare the class
//note that most of this code can be explained 
//by tables x.1, and x.2 in
//cases where
//it is not, comments have been added.
class floatStack extends java.util.Stack {

//we'll need to be able to push onto the stack
public Object Push( float f ) {
Float myFloat = new Float( f );
return super.push( myFloat );
} //Push

//and we will need to be able to pop back off the stack
public float Pop() {
try { return (super.pop()).floatValue(); }
catch ( EmptyStackException e ) {}
} //Pop

//we will need to check if the stack is empty, this class
//actually does not need to appear in our floatStack 
//declaration as it is identical to the empty method 
//contained in java.util.Stack. It was provided for 
//stylistic reasons only.
public boolean Empty() {
return super.empty()
}
} //class

Listing 3: The postFixExal function

public float postFixEval( String inputString ) {
char token;
int intToken;
//length = size of array
len = inputString.length();
//make array of characters in input string
char[] inputArray = new char[len];
inputArray = inputString.toCharArray();
//computations go on here
while ( moreTokens(inputArray[]) ) {
	//get next token
	token = inputArray[i];
	//is it an int? if so push onto evalStack
	if ( this.isInt(token) ) {
		intToken = toInt(token);
		evalStack.Push((float)intToken);
	}

//binary operator, eval top two ints and 
//push result onto stack
else if ( this.isBinary(token) ){
try { float opp2 = evalStack.Pop(); }
catch ( EmptyStackException e ) {}
try { float opp1 = evalStack.Pop(); }
catch ( EmptyStackException e ) {}
evalStack.Push( evalBinary(opp1, opp2, token) );
}

//unary operator
	else {
	try { float opp = evalStack.Pop(); }
	catch ( EmptyStackException e ) { }
	evalStack.Push( evalUnary(opp, token) );
	}
}
try { return evalStack.Pop(); }
catch ( EmptyStackException e ) { }
}

Listing 4: A queue class

class queue {
private Vector queueVector = new Vector();
private Object tempObject;

//constructor
public queue() {
queueVector = new Vector();
}

//our put method
public void put( Object i ) {
//addElement adds at the vector, this is what we need
queueVector.addElement( i );
}

//the get method
public Object get() {
try { tempObject = queueVector.firstElement(); }
catch ( NoSuchElementException e ) {}
try { queueVector.removeElementAt( 0 ); }
catch ( ArrayIndexOutOfBoundsException e) {}
return tempObject;
}

public Object top() {
try { tempObject = queueVector.firstElement(); }
catch ( NoSuchElementException e ) {}
return tempObject;
}

public boolean isEmpty() {
return queueVector.isEmpty()
}

}

 note that the line numbers
next to this method are so i can easily refer to the code as I step through
it. The code should be laid out in a way such that the line numbers are
obviously not part of the code.


Listing 5: The insert method

1.	public void insert( int i ) {
2.	if (i < key) {
3.	  if (left != null)
4.	    left.insert( i );
5.	  else
6.	    left = new binaryTree( i );
7.	} else if (i > key) {
8.	if (right != null)
9.	  right.insert( i );
10.	else
11.	right = new binaryTree( i );
12.   }
13. }

Listing 6: Our binaryTree class

class binaryTree {
private int key;
public binaryTree left;
public binaryTree right;

//constructor
public binaryTree( int i ) {
	key = i;
	this.left = null;
	this.right = null;
}

//returns the current value of key
public int getKey() {
return this.key;
}

//allows us to alter the value of key
private void setKey( int k ) {
this.key = k;
}

//inserts the new int at its correct position
public void insert( int i ) {
if (i <= key) {
      if (left != null)
                left.insert( i );
            else
                left = new binaryTree( i );
        } else if (i >= key) {
            if (right != null)
                right.insert( i );
            else
                right = new binaryTree( i );
        }
    }
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.