Page 3 of 3
I've shown you the technique for extending your applications as they execute. Now it's time for me to put my money where my mouth is and show you a functional demo. Luckily, I am prepared. Take a look at the applet below.
This applet is an implementation of a simple stack-based machine, which consists of a stack of items -- in this case, a collection of zero or more numbers stacked one on top of another, just like in the figure below.
A stack of numbers
There are only two valid operations you can perform on this stack of numbers. You can either push a number on the top of the stack or you can pop a number off the top of the stack. The figure below illustrates these operations.
Pushing onto and popping off of a stack
Take some time to peruse Stack.java, the source code for the stack. Stack.java is a subclass of Panel, and it displays the top five numbers in the stack.
The stack is embedded within a control panel. The control panel includes a field in which commands can be entered and a selector from which the previously entered command can be selected.
Believe it or not, this simple stack forms the basis of a very powerful calculating machine. All we need to complete the program
are a few operators (add, subtract, and so on) to operate on the numbers in the stack. However, the operators can only operate on the numbers via the two operations
mentioned earlier -- push and pop. Let me give you an example.
Consider an operator called add. This operator pops the top two numbers off the stack, adds them, and then pushes the result back on the stack. When the
operation is complete, the stack will be one item shorter and the top two numbers will have been replaced by their sum.
Now add is only one of many different operators. I can think of several more off hand -- subtract, divide, factor, truncate, and convert to francs, to name a few. There are plenty more where those came from, but I wouldn't want to have to sit down and think of them all,
much less code them.
Which brings me to my next point. Rather than trying to think up and code all possible operators, I'd like to build the framework and perhaps provide a few common operators. I'll leave the fancy operators -- such as converting to francs -- for you to develop and add into the program as you need them.
With that in mind, take a look at a method that accepts a string, locates the correct class, loads it, and invokes the proper method:
private void invoke(String str) throws Exception
{
Double d = null;
// parse the string
try
{
d = new Double(str);
}
catch (NumberFormatException nfexp)
{
;
}
// if the string is not a number, it must be
// a operator... let's try it...
if (d == null)
{
_ch.addItem(str);
// load the class...
Class c = Class.forName("howto.operators." + str);
// create a new instance...
Object o = c.newInstance();
// cast it...
Operator op = (Operator)o;
// do it...
op.operate(_s);
}
else
{
_s.push(d.doubleValue());
}
}
Pretty simple, isn't it? The complete code for the control panel is in Main.java.
Let me tell you a bit more about the applet before I conclude. You can push numbers on the stack by typing them in the text
field. Once you've pushed a few numbers, try out the following operators: add, subtract, clone, pop, rotate, and swap. I bet you can you guess what they do.
Class online http://www.javasoft.com/products/jdk/1.0.2/api/java.lang.Class.html
GreatBy Anonymous on March 30, 2009, 9:27 amreally helpfull, hope its still usable today :)
Reply | Read entire comment
View all comments