Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

User interfaces for object-oriented systems, Part 6: The RPN calculator

The RPN-calculator application demonstrates object-oriented UI principles

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
This month we continue the process begun in the January Java Toolbox by completing the RPN (Reverse Polish Notation) calculator application. The RPN calculator is a small but nontrivial application that demonstrates some of the important object-oriented UI principles we've looked at in previous Java Toolbox articles, specifically the visual-proxy design pattern. This application isn't just a toy; I use it virtually every day -- every time I need a calculator, in fact.

A word of warning: the following discussion will be incomprehensible if you haven't read the earlier installments of this series.

TEXTBOX: TEXTBOX_HEAD: Build user interfaces for object-oriented systems: Read the whole series!

Reverse Polish Notation was championed for years by Hewlett-Packard, though I've been told that their most recent calculators don't support it anymore (a pity). It is one of those things that are difficult to learn but wonderful to use once you understand them. The RPN's basic notion is built around an arithmetic stack. Numbers, when entered, are pushed on the stack, and all operations use stack items as operands.

For example, when you press the add (+) key, the two items closest to the top of the stack are popped and added together, and the resulting sum is pushed, effectively replacing the original operands. Although that might seem like a strange way to do things, you never need to use parentheses, and once you get used to it, you'll probably rather like it. I've been using my PalmPilot as a handheld calculator, using Russ Webb's great RPN calculator (see Resources), but I wanted one for my computer too. Being a programmer, I thought building one was the easiest way to get exactly what I wanted.

The analysis model

Since the structure of the calculator's UI is closely related to the object model, let's start by looking at the analysis-level static model, shown in Figure 1.

As is the case with all object-oriented designs, the analysis-level classes are found in the implementation as well. Figure 1, therefore, is really an implementation-level diagram of the original analysis diagram. In object-oriented systems, the design-level model is nothing but the analysis-level model with implementation-level detail added.

Figure 1. The analysis-level static model



The first analysis-level class of interest -- Rpn -- contains the main() method and little else. Though they aren't shown in the diagram, the class also contains a constructor and implementations of all the methods required by the interfaces. In an attempt to reduce the clutter a bit, I usually don't show these in a Unified Model Language (UML) diagram. As is the case with many object-oriented programs, main() (and the Rpn class, for that matter) aren't much to look at. Object-oriented systems tend to be networks of cooperating objects, with no central God class that controls everything from above. There's no spider sitting in the middle of the web pulling strands. An object-oriented system's main() method, as a consequence, typically creates a few objects, hooks them up to each other, and then terminates. That's exactly what happens here: main() creates an instance of the Parser and Math_stack, hooks them up to each other, and terminates.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • The code
  • All the code for this article (and past articles) is available on my Website. Click Articles on the site's navigation bar. You can find an index to all my previous JavaWorld articles on the same page
    http://www.holub.com.
  • An example RPN calculator
  • Russ Webb's RPN calculator provides a good example of Reverse Polish Notation for the PalmPilot platform
    http://www.nthlab.com
  • UML resources
  • The Unified Modeling Language (UML) notation is gradually winning the notation wars. UML is an emerging standard, with the Object Management Group (OMG) in charge of the process. Get more information from OMG at
    http://www.omg.org/uml
  • UML Distilled, by Martin Fowler and Kendall Scott (Addison-Wesley, 2000), is my favorite quick introduction to UML
    http://www1.fatbrain.com/asp/BookInfo/BookInfo.asp?theisbn=020165783X&from=NCN454
  • The UML specification can be downloaded from
    http://www.rational.com/uml/resources/documentation
  • A word of warningThe foregoing link is to the Rational Web site because the principal designers of UMLGrady Booch, James Rumbaugh, and Ivor Jacobsen all work for Rational. Nonetheless, Rational Rose, their object-oriented CASE tool, is one of the weakest -- though most popular -- of the object-oriented CASE tools available. For one thing, Rose supports only a small subset of UML and doesn't do a particularly good job of it. The drawings in this article were created using Visio (which I'm also not ecstatic about) using templates of my own devising. You can get these templates from my Website in the Object-Oriented Design subsection of the Goodies section.
    http://www.holub.com.
  • Design patterns
  • Swing architecture