CIT 591 Assignment 7: Balanced Ternary
Fall 2008, David Matuszek

Purposes of this assignment

General idea of the assignment

Modern computers use the binary number system. However, an early Russian computer, the Setun, used a different number system, balanced ternary.

Your assignment is to write a BalancedTernary API for Java. There are a lot of required methods, but only a couple of them require much work.

About balanced ternary

The decimal number system uses ten digits, 0 through 9, and a system in which each position in a number is worth ten times the value of the position to its immediate right. Thus, for example, the number 7204 indicates "seven thousands, two hundreds, no tens, and four":

103 = 1000 102 = 100 101 = 10 100 = 1
7 (times 103 is 7000) 2 (times 102 is 200) 0 (times 101 is 0) 4 (times 100 is 4)
7*103 + 2*102 + 0*101 + 4*100 = 7204

Similarly, the ternary system uses three digits, usually 0, 1, and 2, and a system in which each position in a number is worth three times the value of the position to its immediate right.

This assignment, however, is to implement a balanced ternary system, in which the three digits have the values 0, 1, and -1. To avoid the awkwardness of using two characters, '-' and 1, to represent a single digit, we will instead use N to stand for the digit -1. Similar to the above example, then, the number 10N1 indicates "one 27, no 9s, negative one 3s, and 1:

33 = 27 32 = 9 31 = 3 30 = 1
1 (times 33 is 27) 0 (times 32 is 0) N (times 31 is 0) 1 (times 30 is 4)
1*33 + 0*32 + (-1)*31 + 1*30 = 10N1 (or, in decimal, 27 + 0 - 3 + 1 = 25)

In balanced ternary, as in more familiar number systems, there is exactly one way to represent any (integer) number. Unlike familiar number systems, there is no need for an additional symbol to indicate that a number is negative; for example, -5 is represented as N11

Here are some more examples:

Balanced ternary N11 NN N0 N1 N 0 1 1N 10 11 1NN
Decimal equivalent -5 -4 -3 -2 -1 0 1 2 3 4 5

It will take some time to get used to balanced ternary. Fortunately, you do not have to be comfortable with the number system before you start writing the program.


Create a project named BalancedTernary.

Within the project, create a package named balancedTernary.

Within the package, create the class BalancedTernary extends Number implements Comparable.


Implement the following constructors for balanced ternary numbers.

public BalancedTernary(String s) throws NumberFormatException
Constructs a BalancedTernary number from the String s, which may contain the characters N, 0, 1, and commas. Throws a NumberFormatException if the input string does not have this form.
public BalancedTernary(int n)
Constructs a BalancedTernary number equivalent to the decimal integer n.

Note 1: You can keep the balanced ternary number as a String, or as an int, or both.

Note 2: You should be able to represent any int value, from Integer.MIN_VALUE to Integer.MAX_VALUE, as a balanced ternary number. Since the conversion between int and balanced ternary may involve using numbers outside this range, you should use long numbers to do the conversions.


Implement the following methods for doing arithmetic with balanced ternary numbers.

public String value()
Returns the value of this balanced ternary number as a String consisting of digits (N, 0, and 1) only.
public String toString()
Returns the value of this balanced ternary number as a String consisting of digits (N, 0, and 1), with commas to separate groups of three digits, as in decimal. For example, 1N,N00,1N1. The String contains no spaces or other characters.
public BalancedTernary add(BalancedTernary number)
Returns the sum this + number.
public BalancedTernary subtract(BalancedTernary number)
Returns the difference this - number.
public BalancedTernary multiply(BalancedTernary number)
Returns the product this * number.
public BalancedTernary divide(BalancedTernary number)
Returns the quotient this / number. Integer division rules apply; that is, any remainder is discarded.
public BalancedTernary remainder(BalancedTernary number)
Returns the remainder this % number.
public boolean equals(BalancedTernary number)
Returns true if this and number are equal, false otherwise .
public BalancedTernary abs()
Returns the absolute value of this.
public int signum()
Returns -1 if this is negative, 0 if this is zero, or 1 if this is positive.

Because this class extends Number, it should also override the methods intValue(), byteValue(), shortValue(), longValue(), floatValue(), and doubleValue(). For example,

public int intValue()
Returns the value of this balanced ternary number as a int.

If you have the basic int <--> BalancedTernary conversions working, the rest of these are one-line methods.

Because this class implements Comparable, it must also provide the following method.

public int compareTo(BalancedTernary number)
Returns a negative number if number is less than this, zero if they are equal, and a positive number if number is greater than this.

JUnit tests

Test all the above methods, and the constructors. (For the constructor that takes an int, test whether you can get the correct value back as a String. For the constructor that takes a String, test whether you can get the correct value back as an int.)

Again, I strongly recommend (1) writing the tests first, and (2) whoever writes the test for a method should also write that method.

Due date:

Before midnight, Thursday October 30. Submit to Blackboard one copy of your project directory, properly zipped. Both your names should be in Javadoc comments in the classes. In addition, whichever of you turns the program in, should put your partner's name in the comments field in Blackboard.