|
CS 596 Java Programming Fall Semester, 1998 Conceptual OO Definition |
|
|---|---|---|
|
© 1998, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 22-Oct-98 |
The following Stack class is not a production level class.
class Stack {
private float[] elements;
private int topOfStack = -1;
public Stack( int stackSize ) {
elements = new float[ stackSize ];
}
public void push( float item ) {
elements[ ++topOfStack ] = item;
}
public float pop() {
return elements[ topOfStack-- ];
}
public boolean isEmpty() {
if ( topOfStack < 0 ) return true;
else return false;
}
public boolean isFull() {
if ( topOfStack >= elements.length ) return true;
else return false;
}
}
Stack me = new Stack( 20 ); me.push( 5 ); me.push( 12 ); System.out.println( me.pop() ); System.out.println( me.pop() );
struct Stack
{
float stack[100];
int topOfStack;
};
void push(Stack& it, int item)
{
it.stack[(it.topOfStack)++] = item;
}
float pop(Stack& it)
{
return it.stack[--(it.topOfStack)];
}
main()
{
Stack tryThisOut;
Stack yours, mine;
tryThisOut.topOfStack = 0;
yours.topOfStack = 0;
push( tryThisOut, 5.0 );
push( yours, 3.3 );
push( tryThisOut, 9.9 );
cout << pop( tryThisOut ) << endl;
}
Stack troubleAhead; troubleAhead.topOfStack = 13; troubleAhead.stack[ 8 ] = 29;
class StackData
{
public float[] elements = new float[100];
public int topOfStack = 0;
}
class Test
{
static void push(Stack it, int item)
{
it.stack[ ( it.topOfStack )++ ] = item;
}
static float pop(Stack it)
{
return it.stack[--(it.topOfStack)];
}
public void static main( String[] args)
{
Stack yours, mine;
push( yours, 3.3 );
push( mine, 9.9);
}
class StackData
{
private float[] elements = new float[100];
private int topOfStack = -1;
public int getTopOfStack()
{
return topOfStack;
}
public void setTopOfStack( int newTop )
{
topOfStack = newTop;
}
public float getElement( int elementIndex )
{
return elements[ elementIndex ];
}
public void setElement( int elementIndex, float element )
{
elements[ elementIndex ] = element;
}
}
class Stack {
private float[] elements = new float[ 100 ];
private int topOfStack = -1;
public void push( )
{
float item = Console.readFloat( "Type a float to push");
elements[ ++topOfStack ] = item;
}
public void pop()
{
Console.println( "Top stack item: " +
elements[topOfStack--];
}
}
class Test
{
public void static main( String[] args)
{
Stack yours ohNo = new Stack();
ohNo.push( );
ohNo.push( );
ohNo.pop( );
}
class PhysicalHiding
{
private int a;
private int b;
public int getA()
{
return a;
}
public int getB()
{
return b;
}
public int setA( int newA )
{
a = newA;
}
public int setB( int newB )
{
b = newB;
}
}
class Point
struct Node
{
float data;
struct Node* next;
};
package MyLinkedList;
public class LinkedList {
private Node frontOfList = null;
public void addFirst( float data )
{
Node newNode = new Node();
newNode.data = data;
newNode.next = frontOfList;
frontOfList = newNode;
}
public String toString() {
String listAsString = "";
Node current = frontOfList;
while (current != null ) {
listAsString = listAsString + " " + current.data;
current = current.next;
}
return listAsString;
}
// Other methods deleted
}
class Node {
float data;
Node next = null;
}
public class Test
{
public static void main( String args[] ) throws Exception
{
//new ClassMethodView( Test.class );
LinkedList test = new LinkedList();
test.addFirst( 1);
test.addFirst( 2);
test.addFirst( 3);
System.out.println( test );
}
}
3.0 2.0 1.0
class Node {
private float data;
private Node next = null;
private Node previous = null;
public Node( float initialData) {
this( initialData, null, null );
}
public Node( float initialData,
Node previousInList,
Node nextInList ) {
data = initialData;
next = nextInList;
previous = previousInList;
if ( next != null )
next.previous = this;
if ( previous != null )
previous.next = this;
}
public void append( float data ) {
new Node( data, this, next);
}
public void prepend( float data ) {
new Node( data, previous, this);
}
public void remove() {
if ( previous != null )
previous.next = next;
if ( next != null )
next.previous = previous;
}
public Node getNext() {
return next;
}
public Node getPrevious() {
return previous;
}
public float getData() {
return data;
}
public void setData( float newData ) {
data = newData;
}
}
public class LinkedList {
private Node frontOfList = null;
private Node endOfList = null;
public void addFirst( float data ) {
frontOfList = new Node( data, null, frontOfList );
if (endOfList == null )
endOfList = frontOfList;
}
public void addLast( float data ) {
endOfList = new Node( data, endOfList, null );
if (frontOfList == null )
frontOfList = endOfList;
}
public float removeFirst( ) {
Node oldFirst = frontOfList;
frontOfList = frontOfList.getNext();
oldFirst.remove();
return oldFirst.getData();
}
public String toString() {
String listAsString = "";
Node current = frontOfList;
while (current != null ) {
listAsString = listAsString + " " + current.getData();
current = current.getNext();
}
return listAsString;
}
}
