MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: OOP experiments in Mathematica- The Stack

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38658] Re: OOP experiments in Mathematica- The Stack
  • From: "Steve S. Shaw" <steve at shawstudio.com>
  • Date: Fri, 3 Jan 2003 00:16:36 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

A series of OOP experiments in Mathematica.
msg#2 - The Stack, part#3: a simple Java implementation of a stack.

Important features of the Java code:

1. A Java "interface" is used to represent the specification.

P: There is no (compilable) way to represent the PRECONDITIONS in an
"interface".
I have simply stuck them in as comments.

COMMENTARY: A severe lack on Java's part, for development of large-scale
systems.

2. I have used "assert"s within the implementation, to catch the
pre-conditions.
I've just read that this is incorrect Java style.

Any publicly visible constraints on the incoming parameters are supposed to
be part of the signature of the method, using "throws <someException>".
This is as close as Java comes to representing preconditions visibly - by
showing what exception gets thrown if something is wrong.

3. Those "assert"s won't compile on Java's older than Java 2, version 1.4.
Comment them out if your compiler doesn't accept them.

4. "MainTest.main()" is the entry point to this example.  It produces the
output
shown way below.

-----------------------------------------------------------------
/* IStack.java
 * Created on January 1, 2003
 * @author  ToolmakerSteve at ShawStudio.com
 */
// Protocol for all stacks (elements not typed).
public interface IStack {
    //--- Query Actions. ---
    boolean isEmpty();
    Object top();  // REQUIRE: !isEmpty()

    //--- Change Actions. ---
    void push( Object ob );
    void drop();  // REQUIRE: !isEmpty()

    // --- Compound Actions. ---
    Object pop();  // REQUIRE: !isEmpty()
}


-----------------------------------------------------------------

/* AppendStack.java
 * Created on January 1, 2003
 * @author  ToolmakerSteve at ShawStudio.com
 */
// Concrete stack (elements not typed), by "appending" to a list.

// TO DO: put inside a package, so can enable assertions on that package.

import java.util.LinkedList;

public class AppendStack implements IStack {
    //===============================================
    //--- Initialization Actions. ---

    /** Creates a new instance of AppendStack */
    public AppendStack() {
    }

    //===============================================
    //--- Query Actions. ---

    public boolean isEmpty() {
        return items.isEmpty();
    }

    // REQUIRE: !isEmpty()
    public Object top() {
        assert (isEmpty());
        return items.getLast();
    }

    //===============================================
   //--- Change Actions. ---

    public void push(Object ob) {
        items.addLast( ob );
    }

    // REQUIRE: !isEmpty()
    public void drop() {
        assert (isEmpty());
        items.removeLast();
    }

    //===============================================
    // --- Compound Actions. ---

    // REQUIRE: !isEmpty()
    // Equivalent: hold=top(); drop(); return hold.
    public Object pop() {
        assert (isEmpty());
        return  items.removeLast();
    }

    //===============================================
    // --- Implementation Details. ---

    private LinkedList items = new LinkedList();

}

-----------------------------------------------------------------

/* MainTest.java
 * Created on December 28, 2002
 * @author  ToolmakerSteve at ShawStudio.com
 */
// Here is a class to test the implementation.
public class MainTest {

    public MainTest() {
    }

    public static void main( String[] args ) {
        System.out.println( "Hello." );

        System.out.println( "new stack s1" );
        IStack s1 = new AppendStack();
        print_top_or_empty( s1 );

        s1.push( "x1" );
        System.out.println( "push x1." );
        print_top_or_empty( s1 );

        s1.push( "x2" );
        System.out.println( "push x2." );
        print_top_or_empty( s1 );

        s1.drop();
        System.out.println( "drop." );
        print_top_or_empty( s1 );

        System.out.print( "pop: " );
        System.out.println( s1.pop() );
        print_top_or_empty( s1 );

        // Deliberate errors (on an empty stack).
        //s1.top();
        //s1.drop();

        System.out.println( "Goodbye." );
    }

    //===============================================
    // --- Implementation Details. ---

    // if s1 empty then say so else print its top item.
    private static void print_top_or_empty( IStack s1 ) {
        if (s1.isEmpty()) {
            System.out.println( "--s1 is empty--" );
        } else {
            System.out.print( "--s1.top: " );
            System.out.println( s1.top() );
        }
    }

}

-----------------------------------------------------------------

Hello.
new stack s1
--s1 is empty--
push x1.
--s1.top: x1
push x2.
--s1.top: x2
drop.
--s1.top: x1
pop: x1
--s1 is empty--
Goodbye.

-----------------------------------------------------------------
-- Steve S.




  • Prev by Date: Re: Solving Alphametics with Mathematica
  • Next by Date: Re: Pasting tables into Excel
  • Previous by thread: OOP experiments in Mathematica- The Stack
  • Next by thread: Re: OOP experiments in Mathematica- The Stack