Tag Archives: Java Virtual Machine

M255 Java Calculator

M255 Java Calculator

Having completed the Open University course M255 I decided I wanted to put my new found knowledge to use. I needed a project, and wanted to learn about how user interfaces are programmed in Java.

A Calculator seemed like a good project, but I set myself a few restrictions, they were;

  • Not allowed to use an ‘Integrated Development Environment’, I used the VI text editor instead.
  • Use compiler (javac) and Java Virtual Machine (java) to run it from the command line.
  • Not allowed to look at how other people had programmed a calculator
  • The only source of help was my knowledge of Java from M255, and, The Java Tutorials at the Sun website.

One of the reasons for putting the details of the project on the web was to show others what can be achieved after completing M255, and to welcome feedback from people, including experienced developers.

Source and Design

This project has been split into phases to make development more manageable within the limited time I have. Initialy I wanted to create a user interface that responded to all the events I needed and then make it work like a calculator. What surprised me was that it only took an hour and a half to create the user interface, being that I am no expert means I have probably not implemented it as well as I could have, but thats why ive signed up for M257.

Version Description Classes
29th October 2006
Original version with basic calculator features Calculator


Future enhancements

As anyone working in software development knows, programs always evolve, and the Calculator is no different, I see the code/implementation changing as I receive comments from more experienced Java developers, and to add the following;

  • Convert into an Applet so people can see the Calculator on the web without having to download it and run it locally.
  • Add support for keyboard use, at present this is purely mouse driven.
  • Nested brackets
  • Improve the layout of the interface, it could look better.
  • Add other scientific functions, maybe even graphs as I get really adventurous.
  • … and more as either I think of them or people suggest them.


As I am no expert on Java or Java user interfaces I obviously have no idea if there is a better way of achieving what I want, or for that matter if I have committed any sins in the code I have created; however, I hope this project evolves and can become a case study tracing the evolution of the calculator from the original source (v1.0) through it’s evolution into a more show case calculator program.

This is a purely educational project; I don’t see this being of any real use to any one other than to learn from.

Java: Calculator Instructions


Should you not have the Java Virtual Machine (JVM) you can find it at java.sun.com, this is used to run Java programs. There are several ways you can run the Calculator, the way I have been running it is to place both class files in a directory, compile them both and then run the Calculator class, I used cygwin but a DOS prompt or any other shell should also be ok.

You can use an Integration Development Environment (IDE) to run the Calculator, instead of the command line; however, if your not using an IDE you might find the following usefull.

Compiling and running using a shell

Once you have your shell/DOS prompt you will want to create a suitably named directory, and then place both class files in it. The following shows where you should be in the directory structure and what files you should see in there.

$ pwd

user@user ~/java/Calculator
$ ls
Calculation.java  Calculator.java

Then from within the directory where the class files are, compile the class files using javac as follows.

$ javac Calculator.java

user@user ~/java/Calculator
$ javac Calculation.java

Once compiled you should see a few extra files, these will have a .class file extension, and they contain the bytecode which JVM will uses to execute, in this case resulting in the Calculator running and the user interface being displayed.

user@user ~/java/Calculator
$ ls
Calculation.class  Calculator$1.class  Calculator.java
Calculation.java   Calculator.class

Now you are ready to run the Calculator using the following command;

user@user ~/java/Calculator
$ java -classpath . Calculator

Assuming you have done everything correctly you should see something similar to the following;