Welcome to Visual J# Calculator Starter Kit!
This Calculator application developed using Visual J# aims to provide beginner programmers with a hands-on way to understand and refine programming concepts. It contains source code, comments and guidelines, and a number of specific extension and customization points. These will help programmers take a step-by-step approach to learning the concepts introduced in the application.
The application is modeled using Model-View-Controller (MVC) design pattern separating the view from the application logic. The application is designed to work right out of the box. You can build and run the application without changes to explore the basic functionality provided. You are also free to use the source code as the basis for your own projects, and share your work with others or upload it to the Internet. Additional functionality can be plugged into the application logic to enhance the application. The graphical components are built using Windows Forms libraries on the .NET Framework. By using Windows Forms, developers can easily manipulate the graphical environment using the drag-and-drop designers in Visual J#.
The application is designed to provide the following learning scenarios.
§ Simple Data: data types, variables and expressions; assignment
§ Control structures: iteration; conditionals
§ Message passing: simple methods; parameter passing by value and reference
§ String and Enumeration
§ Simple data structures: Arrays; Stack; Hashmap
§ Introduction to object-oriented programming: classes, interfaces and objects; syntax of class definitions; methods; members
§ Data abstraction, packages and access modifiers
§ Inheritance, Polymorphism and Dynamic Dispatch
§ Object-oriented analysis and design, design for reuse, introduction to design patterns [MVC pattern, Observer-Observable pattern, etc.]
§ Errors and Exception Handling
§ Event Handling
Running the application
The Calculator starter kit is a sample application that demonstrates what is possible with Visual J#. The application is highly configurable, allowing you to personalize it as you like, and all the code is included so you can see how it all works.
To Load and Run the Application
1. In the New Project dialog, choose Calculator Starter Kit from the installed templates and click OK.
A new project is opened, based on the Calculator Starter Kit Template.
2. Press F5 to run the application as is.
To browse through the code
1. From the View Menu, choose Solution Explorer if it’s not already open.
2. You could start by clicking Calculator.jsl in the Solution Explorer. Alternatively, you could browse through the UI package and open the main form in the designer by clicking CalculatorForm.jsl.
To extend the code
1. Modify a desired file and press F5 to build and run the extended application.
Features
The user interface is designed to be similar to existing calculator interfaces such as the scientific calculators used by students, the calc.exe application included in Windows. The application is designed to work without any modification. You can build and run the application as is, to explore the basic functionality documented below. Additional functionality can be plugged into the application, as described in section “Extending the Application”.
|
Label |
Description |
Keyboard symbol |
|
0 – 9 |
Displays digits 0 – 9. |
0 - 9 |
|
. |
Inserts a decimal point. |
. |
|
Pi |
Displays the value of pi (3.1415…). You can use Pi only with the decimal number system. |
|
|
+ |
Addition |
+ |
|
- |
Subtraction |
- |
|
* |
Multiplication |
* |
|
/ |
Division |
/ |
|
Mod |
Displays the modulus or remainder of x/y. Use this button as a binary operator. |
|
|
= |
Performs the given operation. |
|
|
MR |
Recalls the number stored in the memory. The number remains in the memory. |
|
|
MS |
Stores the displayed number in memory. |
|
|
M+ |
Adds the displayed number to any number already in memory but does not display the sum of these numbers. |
|
|
Backspace |
Deletes the last digit of the displayed number. |
Backspace |
|
CE |
Clears the displayed number. |
Delete |
|
C |
Clears the current calculation. |
Esc |
|
Dec |
Converts the displayed number to the decimal number system. |
|
|
Bin |
Converts the displayed number to the binary number system. |
|
|
Degrees |
Sets trigonometric input for degrees when in decimal mode. |
|
|
Inv |
Sets the inverse function for sin. The functions automatically turn off the inverse function after a calculation is completed. |
|
|
sin |
Calculates the sine of the displayed number. To calculate the arc sine, use Inv followed by sin |
|
|
nPr |
Calculates the permutation of n items taken r at a time. |
|
|
X^2 |
Squares the displayed number. |
|
|
F-E |
Turns scientific notation on and off. You can use F-E only with decimal number system. |
|
|
Ln |
Calculates natural (base e) logarithm. |
|
|
Exp |
Calculate e raised to the xth power, where x is the displayed number. |
|
Application Architecture
This application is modeled around the Model-View-Controller (MVC) architecture.
The MVC pattern separates the modeling of the application, the presentation, and the actions based on user input into three separate units as follows:
View
The view manages the display of information.
CalculatorForm.jsl contains the Windows Forms-based UI developed using the Windows Forms designer.
![]()
Model
The model manages the behavior and data of the application, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).
Engine.jsl manages the behavior and the state of the calculator. Engine instantiates the following classes:
§ Display.jsl inherits java.util.Observable, and manages the data to be displayed on the calculator. Implementing java.util.Observer allows CalculatorForm to get registered as a valid observer with Display, the Observable. This Observer-Observable combination (or pattern) allows CalculatorForm to automatically update itself whenever Display changes its state.
§ OperandStack.jsl contains a stack of operands that are popped out or pushed in depending on operator precedence.
§ OperationStack.jsl contains a stack of Operations.
§ Mode.jsl contains information about the underlying mode, e.g. whether “Inv” or “Hyp” mode are on or off. It also contains “enum” data types such as AngleMode (Degree, Radian, etc.) and Base (Binary, Decimal, etc.)
Controller
The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.
Calculator.jsl is the entry point of this application. It instantiates CalculatorForm and Engine, and acts as a bridge between the view and the model.
Operations Implemented
All operations implemented here leverage the powerful technique of runtime polymorphism. Each class belonging to the Operation type implements the operate() method declared in the Operation interface. Thus, Engine can dynamically invoke the operate() method on the actual type of the object (e.g. Sine, Pi or Addition etc.) without that being explicitly specified at compile time.
The Operation.jsl interface is implemented by abstract classes such as ZeroaryOperation.jsl, UnaryOperation.jsl, BinaryOperation.jsl and MemoryOperation.jsl.
![]()
The abstract class, UnaryOperation, is implemented by “Unary” functions such as
§ Sine.jsl – implements the application logic for calculating the sine of an angle.
§ NaturalLog.jsl – implements the logic for finding natural log of a displayed number.
§ Exponential.jsl – contains the logic for getting the exponential of a number.
§ Square.jsl – implements the “Square” functionality.
§ FEMode.jsl – implements the logic for switching the scientific notation on and off.
§ Equals.jsl – commits the operation.
The unary operations (calculator.model.unaryOperations package) are represented in the class diagram below.
![]()
The abstract class, BinaryOperation, is implemented by “Binary” functions such as
§ Addition.jsl
§ Subtraction.jsl
§ Multiplication.jsl
§ Division.jsl
§ Permutation.jsl – implements the logic and handles error conditions for calculating permutation.
§ Mod.jsl – calculates the Mod (or remainder) of x/y.
The binary operations, organized under calculator.model.binaryOperations package, are represented in the class diagram below.
![]()
The abstract class, MemoryOperation, is implemented by “Memory Management” functions such as
§ MemoryPlus.jsl
§ MemorySet.jsl
§ MemoryRead.jsl
The memory operations, organized under calculator.model.memoryOperations package, are represented in the class diagram below.
![]()
Note on Exception Handling: OperationParameterException
is thrown when an exception arises either due to invalid input or when the
operation results in “Infinity”.
Extending the Application
A fair degree of redundancy has been built in the application to encourage you to extend the code. Here are some extension scenarios you might want to try out.
1. Adding the Random Number generator functionality
§ Enable the “Ran#” button on the form by making the following change in disableExtendedComponents() method of CalculatorForm.jsl
btnRandom.set_Enabled(true);
Alternatively, you could just remove the existing line, since all buttons are already enabled through the designer.
btnRandom.set_Enabled(true);
§ Create RandomNumber.jsl and implement Operation, similar to the way Pi.jsl has been implemented. Use java.util.Random to generate random numbers.
§ In the initializeMap() method of CalculatorForm.jsl, map btnRandom to corresponding object.
mapButtonToAction.put(btnRandom, new RandomNumber());
This will “link” the application logic implemented in Random.jsl with the corresponding button.
2. Implementing the factorial functionality
§ Enable the “n!” button on the form by making the following change in disableExtendedComponents() method of CalculatorForm.jsl
btnFactorial.set_Enabled(true);
§ Create Factorial.jsl and implement UnaryOperation. Use the recursion algorithm to calculate factorial of a number.
§ In the initializeMap() method of CalculatorForm.jsl, map btnFactorial to corresponding object.
mapButtonToAction.put(btnFactorial, new Factorial());
This will “link” the application logic implemented in Factorial.jsl with the corresponding button.
3. Similarly, implement the functionality for all the grayed-out functions such as nCr, cos, 1/x, sqrt, log, etc.
4. Based on the way “Decimal” and “Binary” base modes are implemented, you could extend the Calculator to have Octal and Hexadecimal modes too.
5. Extend the calculator to support trigonometric functions such as sinh(x), cosh(x), etc. by implementing the application logic for “Hyp” button.
Next Steps
This topic has given you a brief introduction to the Calculator starter kit. You can use the application further, and get a broader understanding of how it is implemented. You might try to understand the functionality of the application by examining the source code, and identify the learning scenarios outlined in the first section. Finally, you might want to try extending the application, and develop it into a fully functional- Scientific Calculator.