Class Calculator

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by Calculator
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class Calculator
extends Applet

A tiny whole-number calculator — stack architecture.

An object of this class is an Applet, featuring the logic (state machine) and the display of an integer stack calculator. It has four stack registers (x, y, z and t just like the famous HP pocket calculators).

Decimal and hexadecimal display and entry are supported.
The number range is (Java) long (64 bit, 2s complement).

This Applet can be used to demonstrate (Java) integer arithmetic as well as the stack computer architecture.

The keyboard for digits, "Enter" (the stack push) and the operations is not part of the Applet. (In that sense the Applet is a pure demo of MatrixDisplay, MatrixTextDisplay and DisplayPattern.) The (key) entries are supplied by public methods. The keypad may thus be implemented by buttons in the "forms" of an embedding HTML page.

See the minimal demo page calculator.html and the source.

Copyright 2001, 2003, 2004, 2009   Albrecht Weinert

Version:
46 (28.04.2010)
Author:
Albrecht Weinert
See Also:
MatrixDisplay, DisplayPattern, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.applet.Applet
Applet.AccessibleApplet
 
Nested classes/interfaces inherited from class java.awt.Panel
Panel.AccessibleAWTPanel
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static int ANZ_EL
          Number of 5*8 elements in display.
protected  boolean autoEnter
          State: next digit entry will do an Enter (push stack) before.
protected  boolean enterTo0
          State: next digit entry will clear X before.
protected  boolean hex
          State: Hex calculator.
protected  MatrixTextDisplay md
          The display.
protected static byte[][] MINI_E
          Display pattern for minified (5*3) E.
protected static byte[][] MINI_H
          Display pattern for minified (5*3) H.
static String NVC
          Name, version, copyright.
protected  long t
          Stack register T (Stack is x, y, z, t).
protected  boolean thrOn
          Do the marquee (running) version display.
protected  long x
          Main register X (=display register).
protected  long y
          Stack register Y (Stack is x, y, z, t).
protected  long z
          Stack register Z (Stack is x, y, z, t).
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
Calculator()
           
 
Method Summary
 void chgSign()
          Change the sign of x: x = -x.
 void chgXY()
          Exchange x and y.
 void clrXclrAll()
          Clear X or all registers.
 void digIn(int digit)
          Entry of a digit (0..9; ..15).
 void divide()
          Division (integer) x = y / x.
 void divide2()
          Division and remainder (integer) x,y = (y / x), (x mod y).
 void enter()
          Push operation respectively Enter.
 void init()
          Initialising.
 void invHex()
          Hex-Decimal toggle.
 void minus()
          Subtraction x = y - x.
 void mod()
          Modulo (integer remainder) x = y % x.
 void multiply()
          Multiplication x = x * y.
 void paint(Graphics g)
          Paint the display.
 void plus()
          Addition x = y + x.
 void reset()
          Reset to "power on" state.
 void rollDown()
          Roll down the stack (t becomes the previous x).
 void showX()
          Renew the X display.
 void update(Graphics g)
          Update the display.
 void yHx()
          Exponent (y power x) x = y ** x.
 
Methods inherited from class java.applet.Applet
destroy, getAccessibleContext, getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameter, getParameterInfo, isActive, newAudioClip, play, play, resize, resize, setStub, showStatus, start, stop
 
Methods inherited from class java.awt.Panel
addNotify
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NVC

public static final String NVC
Name, version, copyright.

See Also:
Constant Field Values

x

protected long x
Main register X (=display register).


y

protected long y
Stack register Y (Stack is x, y, z, t).


z

protected long z
Stack register Z (Stack is x, y, z, t).


t

protected long t
Stack register T (Stack is x, y, z, t).


MINI_H

protected static final byte[][] MINI_H
Display pattern for minified (5*3) H.

Used as "Hex" marker.


MINI_E

protected static final byte[][] MINI_E
Display pattern for minified (5*3) E.

Used as "Error" marker.


ANZ_EL

public static final int ANZ_EL
Number of 5*8 elements in display.

Value: 20

See Also:
Constant Field Values

md

protected MatrixTextDisplay md
The display.

Die display itself will be implemented as a MatrixTextDisplay object.

It will be made for one line of 20 (ANZ_EL) 5 * 8 elements with 2 dots horizontal spacing. The matrix dots itself are sized 4 * 4 pixels and are densely packed.

Thus the applet's minimal size is about 596 * 60 (width * height).


hex

protected boolean hex
State: Hex calculator.


enterTo0

protected boolean enterTo0
State: next digit entry will clear X before.


autoEnter

protected boolean autoEnter
State: next digit entry will do an Enter (push stack) before.


thrOn

protected boolean thrOn
Do the marquee (running) version display.

Constructor Detail

Calculator

public Calculator()
Method Detail

init

public void init()
Initialising.

Overrides:
init in class Applet

update

public void update(Graphics g)
Update the display.

This method does not clear the background (to background colour) as paint() will update all matrix dots without flickering.

Overrides:
update in class Container

paint

public void paint(Graphics g)
Paint the display.

This method is equivalent to update().

Overrides:
paint in class Container

showX

public void showX()
Renew the X display.


digIn

public void digIn(int digit)
Entry of a digit (0..9; ..15).

This method has to be called with a parameter of 0..9..15, according to the digit key 0..9, A..F pressed.

The display will be updated accordingly. After a previous calculation / operation enter() will be processed before.

Implementation hint: As this method implements the numeric key pad of a pocket calculator it assumes that the number being entered / constructed in stack register x is not negative. (Changing sign would be an operation that ends the number entry.)

Parameters:
digit - The digit key entry: 0..9 decimal respectively 0..15 if hexadecimal. Values out of range will be ignored.

multiply

public void multiply()
Multiplication x = x * y.

This method uses the two lower stack registers as operands popping them and pushing the result. (t is copied to z as side effect).


divide

public void divide()
Division (integer) x = y / x.

This method uses the two lower stack registers as operands popping them and pushing the result. (t is copied to z as side effect).


divide2

public void divide2()
Division and remainder (integer) x,y = (y / x), (x mod y).

This method calculates the integer quotient y / x and the integer remainder y mod x (y % x in Java). The new content of x is the quotient and y will be the remainder. By exchanging chgXY() they can be viewed alternately.
Registers z and t remain unchanged.


mod

public void mod()
Modulo (integer remainder) x = y % x.

This method uses the two lower stack registers as operands popping them and pushing the result. (t is copied to z as side effect).
The remainder calculation uses Java y % x (sign) rules as they (surprisingly to many) are.


yHx

public void yHx()
Exponent (y power x) x = y ** x.

This method uses the two lower stack registers as operands popping them and pushing the result. (t is copied to z as side effect).


plus

public void plus()
Addition x = y + x.


minus

public void minus()
Subtraction x = y - x.


chgSign

public void chgSign()
Change the sign of x: x = -x.

The other stack registers remain unchanged.


invHex

public void invHex()
Hex-Decimal toggle.

This method changes the display and entering mode from decimal to hexadecimal and vice versa.


chgXY

public void chgXY()
Exchange x and y.

This method interchanges the content of x and y, leaving z and t unchanged.


rollDown

public void rollDown()
Roll down the stack (t becomes the previous x).

This method rolls the stack down or, to say it unambiguously, toward x. The former x content is put to t.

Calling this four times restores the original condition.


enter

public void enter()
Push operation respectively Enter.

This method pushes the stack one up (towards t). The former t content is lost and x and y have now the same content.
But x will be auto-cleared before the next digit entry.


reset

public void reset()
Reset to "power on" state.

This method clears all stack registers to 0, clear the error flag and sets the decimal mode.

Additionally marquee (running) display of the version info is started.
Enter (enter()) starts the normal operation.


clrXclrAll

public void clrXclrAll()
Clear X or all registers.

This method just sets x to 0 if not yet so. Otherwise all registers are set to 0.