CIT 591 Assignment 8: Balanced Ternary Calculator
Fall 2008, David Matuszek

Purposes of this assignment

General idea of the assignment

Create a calculator (integer only) for BalancedTernary numbers. Make it look and act as much like a "real" calculator as possible.

Because I want to make sure everyone gets some experience with GUI programming, this is an individual assignment. You will not have a partner.

You may discuss the code with other people, get help debugging your code, help other people with problems, etc. However, you should write your own code, and should not allow anyone to copy your code.


You should have three classes: BalancedTernary, for doing all the calculations; BalancedTernaryTest, for testing BalancedTernary; and BTCalculator, to provide the GUI. I will provide the BalancedTernaryTest class. BTCalculator should be tested manually, not by JUnit.

class BalancedTernary

This class performs all the necessary arithmetic.

This part should be easy. You have written balanced ternary code in Python. You should be able to take this code and translate it into Java. Or, you can work from my version of

To make these methods as similar to the Python functions as possible, I'm making all the methods static. You can call a static method by asking the class, not an object, to perform the operation for you; for example, bt3 = BalancedTernary.add(bt1, bt2).

You should have (at least) these methods:

class BTCalculator extends JFrame

An inexpensive calculator has a single-line display. Use a JTextField for this. You should "disable" this field to prevent the user from typing into it.

A regular calculator has buttons for ten decimal digits. Yours will have buttons for three "trits" (ternary digits): 0, 1, and N. These keys are used to enter balanced ternary numbers.

You should have buttons for C (clear), AC (All Clear) +, -, *, /, and = (or, if you want to get fancy, figure out how to label your buttons with C, AC, +, -, , , and =). Your calculator should have these same buttons, and they should behave the same way. Also have a +/- or neg key, to negate the number in the display (for example, from 1NN011N to N110NN1, or vice versa).

Note: The Clear button on a calculator "clears" the number being entered to zero, but it does not clear any pending operation. For example, 5 + 3 C 2 = should display 7. (This example is in decimal.) The All Clear button should clear everything, including errors.

Your calculator should also have a dec button to change the number in the display to decimal. This will only affect the display; the calculator should continue to use the balanced ternary number that was in the display.

Even more details

When you are entering a number, pressing a trit key (0, 1, or N) appends that trit to the number in the display. When you press any other key, the number is considered to be "complete," and some operation is performed. Pressing a digit starts entry of a new number.

If you try to divide by zero, the calculator should display Error. It's up to you whether you want to check for overflow (a number too large or too negative to be represented by an int).

The program should quit when you close the calculator window.

Save your complete program in an executable .jar file, in the same directory as your other Balanced Ternary files. Eclipse will create this file for you--Export your project to a Jar file, step through the menus selecting Next> until you can choose BalancedTernary as your Main class. If Java is installed correctly on your computer, double-clicking this jar file will run your calculator.

Supplied code

Due date:

Before midnight, Thursday November 18. Zip your entire project and submit it to Blackboard.