Computer Science program

profilewuzhong712
new_folder.zip

New folder/jsjf/ArrayStack.class

                package jsjf;

                public 
                synchronized 
                class ArrayStack 
                implements StackADT {
    
                private 
                static 
                final int 
                DEFAULT_CAPACITY = 100;
    
                private int 
                top;
    
                private Object[] 
                stack;
    
                public void ArrayStack();
    
                public void ArrayStack(int);
    
                public void 
                push(Object);
    
                private void 
                expandCapacity();
    
                public Object 
                pop() 
                throws exceptions.EmptyCollectionException;
    
                public Object 
                peek() 
                throws exceptions.EmptyCollectionException;
    
                public int 
                size();
    
                public boolean 
                isEmpty();
    
                public String 
                toString();
}

            

New folder/jsjf/ArrayStack.java

New folder/jsjf/ArrayStack.java

package  jsjf ;
import  jsjf . exceptions . * ;
import  java . util . Arrays ;

// -------------------------------------------------------
// Author: Yifu Wu
// Date: 03/10/16
// Source Name: ArrayStack<T>
// Due date: 03/10/16
// Description:
/**
 * An array implementation of a stack in which the bottom of the
 * stack is fixed at index 0.
 *
 *  @author  Java Foundations
 *  @version  4.0
 */
public   class   ArrayStack < T >   implements   StackADT < T >
{
     private   final   static   int  DEFAULT_CAPACITY  =   100 ;

     private   int  top ;
     private  T []  stack ;

     /**
     * Creates an empty stack using the default capacity.
     */
     public   ArrayStack ()
     {
         this ( DEFAULT_CAPACITY );
     }

     /**
     * Creates an empty stack using the specified capacity.
     *  @param  initialCapacity the initial size of the array
     */
     public   ArrayStack ( int  initialCapacity )
     {
        top  =   0 ;
        stack  =   ( T [])( new   Object [ initialCapacity ]);
     }

     /**
     * Adds the specified element to the top of this stack, expanding
     * the capacity of the array if necessary.
     *  @param  element generic element to be pushed onto stack
     */
     public   void  push ( T element )
     {
         if   ( size ()   ==  stack . length )
            expandCapacity ();

        stack [ top ]   =  element ;
        top ++ ;
     }

     /**
     * Creates a new array to store the contents of this stack with
     * twice the capacity of the old one.
     */
     private   void  expandCapacity ()
     {
         //stack = Arrays.copyOf(stack, stack.length * 2);
         System . out . println ( "Expanding stack capacity\n" );
       T []  temp  =   ( T [])    ( new   Object   [ 2 * top ]);
         for   ( int  i = 0 ;  i <  top ;  i ++ )
            temp [ i ]   =  stack [ i ];
            stack  =  temp ;
     }

     /**
     * Removes the element at the top of this stack and returns a
     * reference to it.
     *  @return  element removed from top of stack
     *  @throws  EmptyCollectionException if stack is empty
     */
     public  T pop ()   throws   EmptyCollectionException
     {
         if   ( isEmpty ())
             throw   new   EmptyCollectionException ( "stack" );

        top -- ;
        T result  =  stack [ top ];
        stack [ top ]   =   null ;

         return  result ;
     }

     /**
     * Returns a reference to the element at the top of this stack.
     * The element is not removed from the stack.
     *  @return  element on top of stack
     *  @throws  EmptyCollectionException if stack is empty
     */
     public  T peek ()   throws   EmptyCollectionException
     {
         if   ( isEmpty ())
             throw   new   EmptyCollectionException ( "stack" );

         return  stack [ top - 1 ];
     }

     /**
     * Returns the number of elements in this stack.
     *  @return  the number of elements in the stack
     */
     public   int  size ()
     {
         // To be completed as a Programming Project
         return  top ;
     }

     /**
     * Returns true if this stack is empty and false otherwise.
     *  @return  true if this stack is empty
     */
     public   boolean  isEmpty ()
     {

       // To be completed as a Programming Project
         if   ( size () == 0 )
             return   true ;
         else
             return   false ;

     }


     /**
     * Returns a string representation of this stack.
     *  @return  a string representation of the stack
     */
     public   String  toString ()
     {
         // To be completed as a Programming Project
         String  result  =   "" ;
         for   ( int  i  =  top - 1 ; >= 0 ; i -- )
            result  +=   "[" + ( + 1 )   +   "]"   +  stack [ i ]. toString ()   +   "\n" ;
         return  result ;
     }
}

New folder/jsjf/exceptions/EmptyCollectionException.class

                package jsjf.exceptions;

                public 
                synchronized 
                class EmptyCollectionException 
                extends RuntimeException {
    
                public void EmptyCollectionException(String);
}

            

New folder/jsjf/exceptions/EmptyCollectionException.java

New folder/jsjf/exceptions/EmptyCollectionException.java


// -------------------------------------------------------
// Author: Yifu Wu
// Date: 03/10/16
// Source Name: EmptyCollectionException
// Due date: 03/10/16
// Description:
/**
 * Represents the situation in which a collection is empty.
 *
 *  @author  Java Foundations
 *  @version  4.0
 */

package  jsjf . exceptions ;
public   class   EmptyCollectionException   extends   RuntimeException
{
     /**
     * Sets up this exception with an appropriate message.
     *  @param  collection the name of the collection
     */
     public   EmptyCollectionException ( String  collection )
     {
         super ( "The "   +  collection  +   " is empty." );
     }
}

New folder/jsjf/StackADT.class

                package jsjf;

                public 
                abstract 
                interface StackADT {
    
                public 
                abstract void 
                push(Object);
    
                public 
                abstract Object 
                pop();
    
                public 
                abstract Object 
                peek();
    
                public 
                abstract boolean 
                isEmpty();
    
                public 
                abstract int 
                size();
    
                public 
                abstract String 
                toString();
}

            

New folder/jsjf/StackADT.java

New folder/jsjf/StackADT.java

package  jsjf ;

// -------------------------------------------------------
// Author: Yifu Wu
// Date: 03/10/16
// Source Name: StackADT<T>
// Due date: 03/10/16
// Description:
/**
 * Defines the interface to a stack collection.
 *
 *  @author  Java Foundations
 *  @version  4.0
 */
public   interface   StackADT < T >
{
     /**
     * Adds the specified element to the top of this stack.
     *  @param  element element to be pushed onto the stack
     */
     public   void  push ( T element );

     /**
     * Removes and returns the top element from this stack.
     *  @return  the element removed from the stack
     */
     public  T pop ();

     /**
     * Returns without removing the top element of this stack.
     *  @return  the element on top of the stack
     */
     public  T peek ();

     /**
     * Returns true if this stack contains no elements.
     *  @return  true if the stack is empty
     */
     public   boolean  isEmpty ();

     /**
     * Returns the number of elements in this stack.
     *  @return  the number of elements in the stack
     */
     public   int  size ();

     /**
     * Returns a string representation of this stack.
     *  @return  a string representation of the stack
     */
     public   String  toString ();
}

New folder/PostfixEvaluator2.class

                public 
                synchronized 
                class PostfixEvaluator2 {
    
                private 
                static 
                final char 
                ADD = 43;
    
                private 
                static 
                final char 
                SUBTRACT = 45;
    
                private 
                static 
                final char 
                MULTIPLY = 42;
    
                private 
                static 
                final char 
                DIVIDE = 47;
    
                private jsjf.ArrayStack 
                stack;
    
                public void PostfixEvaluator2();
    
                public int 
                evaluate(String);
    
                private boolean 
                isOperator(String);
    
                private int 
                evaluateSingleOperator(char, int, int);
}

            

New folder/PostfixEvaluator2.java

New folder/PostfixEvaluator2.java


// -------------------------------------------------------
// Author: Yifu Wu
// Date: 03/10/16
// Source Name: PostfixEvaluator2
// Due date: 03/10/16
// Description:
  /* Represents an integer evaluator of postfix expressions. Assumes
 * the operands are constants.
 *
 * @author Java Foundations
 * @version 4.0
 */


import  jsjf . * ;
import  java . util . Stack ;
import  java . util . Scanner ;
public   class   PostfixEvaluator2
{
     private   final   static   char  ADD  =   '+' ;
     private   final   static   char  SUBTRACT  =   '-' ;
     private   final   static   char  MULTIPLY  =   '*' ;
     private   final   static   char  DIVIDE  =   '/' ;

     private   ArrayStack < Integer >  stack ;

     /**
     * Sets up this evalutor by creating a new stack.
     */
     public   PostfixEvaluator2 ()
     {
        stack  =   new   ArrayStack < Integer > ();
     }

     /**
     * Evaluates the specified postfix expression. If an operand is
     * encountered, it is pushed onto the stack. If an operator is
     * encountered, two operands are popped, the operation is
     * evaluated, and the result is pushed onto the stack.
     *  @param  expr string representation of a postfix expression
     *  @return  value of the given expression
     */
     public   int  evaluate ( String  expr )
     {
         int  op1 ,  op2 ,  result  =   0 ;
         String  token ;
         Scanner  parser  =   new   Scanner ( expr );

         while   ( parser . hasNext ())
         {
            token  =  parser . next ();

             if   ( isOperator ( token ))
             {
                op2  =   ( stack . pop ()). intValue ();
                op1  =   ( stack . pop ()). intValue ();
                result  =  evaluateSingleOperator ( token . charAt ( 0 ),  op1 ,  op2 );
                stack . push ( new   Integer ( result ));
             }
             else
                stack . push ( new   Integer ( Integer . parseInt ( token )));
         }

         return  result ;
     }

     /**
     * Determines if the specified token is an operator.
     *  @param  token the token to be evaluated
     *  @return  true if token is operator
     */
     private   boolean  isOperator ( String  token )
     {
         return   (  token . equals ( "+" )   ||  token . equals ( "-" )   ||
                 token . equals ( "*" )   ||  token . equals ( "/" )   );
     }

     /**
     * Peforms integer evaluation on a single expression consisting of
     * the specified operator and operands.
     *  @param  operation operation to be performed
     *  @param  op1 the first operand
     *  @param  op2 the second operand
     *  @return  value of the expression
     */
     private   int  evaluateSingleOperator ( char  operation ,   int  op1 ,   int  op2 )
     {
         int  result  =   0 ;

         switch   ( operation )
         {
             case  ADD :
                result  =  op1  +  op2 ;
                 break ;
             case  SUBTRACT :
                result  =  op1  -  op2 ;
                 break ;
             case  MULTIPLY :
                result  =  op1  *  op2 ;
                 break ;
             case  DIVIDE :
                result  =  op1  /  op2 ;
         }

         return  result ;
     }
}

New folder/PostfixTester2.class

                public 
                synchronized 
                class PostfixTester2 {
    
                public void PostfixTester2();
    
                public 
                static void 
                main(String[]);
}

            

New folder/PostfixTester2.java

New folder/PostfixTester2.java


// -------------------------------------------------------
// Author: Yifu Wu
// Date: 03/10/16
// Source Name: PostfixTester2
// Due date: 03/10/16
// Description:
// Demonstrates the use of a stack to evaluate postfix expressions.
// @author Java Foundations
// @version 4.0

import  java . util . Scanner ;
public   class   PostfixTester2
//-------------------------------------
// To Compile:  javac PostfixTester.java
//-------------------------------------
{
     /**
     * Reads and evaluates multiple postfix expressions.
     */
     public   static   void  main ( String []  args )
     {
         String  expression ,  again ;
         int  result ;

         Scanner  in  =   new   Scanner ( System . in );

         do
         {
             PostfixEvaluator2  evaluator  =   new   PostfixEvaluator2 ();
             System . out . println ( "Enter a valid post-fix expression one token "   +
                 "at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)" );
             System . out . println ( "Each token must be an integer or an operator (+,-,*,/)" );
            expression  =  in . nextLine ();

            result  =  evaluator . evaluate ( expression );
             System . out . println ();
             System . out . println ( "That expression equals "   +  result );

             System . out . print ( "Evaluate another expression [Y/N]? " );
            again  =  in . nextLine ();
             System . out . println ();
         }
         while   ( again . equalsIgnoreCase ( "y" ));
    }
}

//************************************************** Output Display **********************************************************
//Enter a valid post-fix expression one token at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)
//Each token must be an integer or an operator (+,-,*,/)
//5 3 + 2 1 - 4 5 6 * + - -

//That expression equals 41
//Evaluate another expression [Y/N]? Y

//Enter a valid post-fix expression one token at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)
//Each token must be an integer or an operator (+,-,*,/)
//2 4 6 7  + - * 3 - 5 4 7 + + -

//That expression equals -37
//Evaluate another expression [Y/N]?

//Enter a valid post-fix expression one token at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)
//Each token must be an integer or an operator (+,-,*,/)
//8 7 2 1 + + - 6 / 8 7 - -

//That expression equals -1
//Evaluate another expression [Y/N]?

New folder/tp022871.BAT

@ECHO OFF C: CD "\Users\y.wu2\Desktop\New folder" "C:\Program Files (x86)\Java\jdk1.7.0\bin\java.exe" PostfixTester2 PAUSE

New folder/tp06515e.BAT

@ECHO OFF C: CD "\Users\y.wu2\Desktop\New folder" "C:\Program Files (x86)\Java\jdk1.7.0\bin\java.exe" PostfixTester2 PAUSE

New folder/tp0657b8.BAT

@ECHO OFF C: CD "\Users\y.wu2\Desktop\New folder" "C:\Program Files (x86)\Java\jdk1.7.0\bin\java.exe" PostfixTester2 PAUSE

New folder/tp065a13.BAT

@ECHO OFF C: CD "\Users\y.wu2\Desktop\New folder" "C:\Program Files (x86)\Java\jdk1.7.0\bin\java.exe" PostfixTester2 PAUSE