Home
 Introduction
 Using Holon
 Holon Tools
 Contact


 HolonJ

 HolonJForth
 UsingHolonJ

 Clock
 TicTacToe

Calculator Applet  

The calculator is built as an application, which is then wrapped into an applet. -- You can also run the application together with the inner class WAction, both are contained in calculator.zip. Start the application in your java/bin directory with the command java Calculator.

This program has been inspired by the Java version published by Ralf Kühnel in "Die Java 1.1 Fibel", Addison-Wesley, 2nd edition, 1997. 

 

The Program

The imports are not listed. See the clock applet for a representative list. 

Class: WAction extends WindowAdapter 

i: wa.<init>   ( -- )
   initSuper ()  ; 

i: wa.windowClosing     ( windowevent e -- )
   0 getRuntime runExit  ; 

Class: Calculator extends Panel implements ActionListener 

Panel ControlPanel
Textfield Numberfield 
double NewArgument
double LeftAdd
double LeftMul
double RightMul 
double Memory boolean MemoryRepeated boolean CancelRepeated boolean isNewArgument boolean DotExists string Argument char Arg0 \ ('*','/','=') char MulOp \ ('+','-','=') char AddOp Code: addcomp addcomponent target drop ; \ Create a button with label Btext. i: butn ( string btext -- ) btext new Button ( string -- ) dup Controlpanel addComp this swap addButtonListener ; i: CreateButtons ( -- ) " MS" butn " 7" butn " 8" butn " 9" butn " /" butn " M+" butn " 4" butn " 5" butn " 6" butn " *" butn " MR" butn " 1" butn " 2" butn " 3" butn " -" butn " CE" butn " 0" butn " ." butn " =" butn " +" butn ; i: Calculat.<init> ( -- ) InitSuper () new BorderLayout () this setLayout new ControlPanel () 4 5 1 1 new GridLayout ( int int int int -- ) ControlPanel setLayout CreateButtons " 0" 29 new TextField ( string int -- ) =: Numberfield " North" Numberfield this addTextComp drop " Center" ControlPanel this addTextComp drop ' = dup =: AddOp =: MulOp ; i: UpdateNumber ( string Numbertext -- ) try Numbertext NumberField setText NumberText ">d =: NewArgument catch NumberFormException ." number exception" cr ; i: SetNumberText ( double Number -- ) Number d>" UpdateNumber ; i: MulOpCase ( -- ) MulOp case ' * of LeftMul RightMul d* =: LeftMul endof ' / of LeftMul RightMul d/ =: LeftMul endof ' = of RightMul =: LeftMul endof drop endcase ; i: AddOpCase ( -- ) AddOp case ' + of LeftAdd LeftMul d+ =: LeftAdd endof ' - of LeftAdd LeftMul d- =: LeftAdd endof ' = of LeftMul =: LeftAdd endof drop endcase LeftAdd setNumberText 0.0 =: LeftMul Arg0 case ' + of ' + =: AddOp ' = =: MulOp endof ' - of ' - =: AddOp ' = =: MulOp endof ' = of ' = =: AddOp ' = =: MulOp LeftAdd =: RightMul endof drop endcase ; i: MemoryButton ( -- ) 1 Argument charAt case ' S of NewArgument =: Memory endof ' + of NewArgument += Memory endof drop MemoryRepeated if 0.0 =: Memory false =: MemoryRepeated else Memory setNumbertext Memory =: RightMul true =: MemoryRepeated true =: DotExists then endcase ; i: NumberButton ( -- ) isNewArgument if numberField getText Argument "+ else Argument then UpdateNumber true =: isNewArgument ; i: DotButton ( -- ) DotExists if exit then isNewArgument if NumberField getText Argument "+ else " 0." then UpdateNumber true =: isNewArgument true =: DotExists ; i: CancelButton ( -- ) 0.0 =: RightMul CancelRepeated if 0.0 =: LeftMul 0.0 =: LeftAdd false =: CancelRepeated ' = dup =: MulOp =: AddOp else true =: CancelRepeated then " 0" UpdateNumber false =: isNewArgument false =: DotExists ; i: OperationButton ( -- ) isNewArgument if NewArgument =: RightMul then false =: isNewArgument MulOpCase Arg0 case ' * of LeftMul SetNumbertext ' * =: MulOp endof ' / of LeftMul SetNumbertext ' / =: MulOp endof drop AddOpCase endcase ; i: DoButton ( -- ) Arg0 case ' M of MemoryButton endof ' C of CancelButton endof ' 0 ' 9 partof NumberButton endof ' . of DotButton endof drop OperationButton endcase ; i: C.actionPerformed ( actionevent Event -- ) Event getSource checkcast Button getLabel =: Argument 0 Argument charAt =: Arg0 Argument " MR" "<> if false =: MemoryRepeated then Argument " CE" "<> if false =: CancelRepeated then DoButton ; : Calculat.main ( string [] args -- ) frame myFrame " Calculator in HolonJ" new myFrame ( string -- ) new Calculator () myFrame addComponent drop new WAction () myFrame addWindowListener myFrame packWindow myFrame showWindow ; class: Calcappl extends Applet private Calculator myCalc i: ca.<init> ( -- ) initsuper () ; i: ca.init ( -- ) new myCalc () ; i: ca.start ( -- ) myCalc this addComponent drop ; i: ca.stop ( -- ) myCalc this removeComponent ;  
© 1999-2005 Wolf Wejgaard