You will write a Number class which will allow you to do arithmetic with unlimited digits and accuracy. As an example, this class was used to compute 1000! as: See image.
You will write a (rather feeble) calculator with only a few operations which will do basic arithmetic with these numbers. Running the calculator may give a display like: See image.
You will note that your program will not do division. You can be thankful for this.
Numbers will be stored in doubly-linked lists (do not use generics here). Each node will have an intvalue field which will hold one digit (0 through 9) and two pointer fields, prev and next. The Number class will have five fields: See image.
high points to the high-order digit's node, low points to the low-order digit's node, digitCount is the number of digits stored in the list, decimalPlaces is the number of digits (nodes) after the decimal place and negative gives the sign. For example, the number 3.1416 would be represented as: See image.
A number of operations will be provided by public methods:
There will be no other public methods, but you may provide any private methods you wish.
You will write (private) Number addAbsolute(Number n), Number subtractAbsolute(Number n) and Number compareToAbsolute(Number n) methods which will perform operations disregarding signs (i.e. the negative field). These can then be call by the public Number add(Number n) and public Number subtract(Number n) methods. The trickiest part of these methods is aligning the decimal points.
If this.digitCount and n.digitCount were equal and this.decimalPlaces and n.decimalPlaceswere equal (which, of course, they generally are not) the following pseudo-code would work: See image.
This must be adjusted to align the decimal points and to account for differing digit counts. If, for example, the numbers as written on paper would look like: See image.
you would have to start and end the traversals of the two lists at different times and treat non-existent nodes as holding 0.
Subtraction is also done much as it is done with paper and pencil. You must first determine which of the two values has largest magnitude (use your intcompareToAbsolute(Number n) method). If this has the largest magnitude and if the digitCounts and decimalPlaces match the following pseudo-code would work: See image.
This code must be adjusted to align decimal points and account for different digit counts.
Multiplication of absolute values is done in a slightly different way from pencil and paper. We read the first Number to be multiplied from-right-to-left and the second from left-to-right using "Horner's method." See image.
No issues with decimal point alignment or differing digit counts occur here.
public Number add(Number n) and public Number Subtract(Number n) are now easily implemented using private Number addAbsolute(Number n), private Number subtractAbsolute(Number n), and private intcompareToAbsolute(Number n) and carefully observing signs. (privateintcompareToAbsolute(Number n) presents digit and decimal point alignment issues.)
All arithmetic operations must be finished by trimming the result. Here you will remove all extra leading 0s which precede the decimal point and all extra trailing 0s which follow the decimal point.
The String representation of a Number should have leading zeros (left of the decimal point) and trailing zeros (right of the decimal point) removed (done by trim()). The number 0 should, however, be displayed as "0". Integers (decimalPlaces 0) should be displayed without a decimal point.
All user input must be validated. In the Number class you should write a validate() method which assures that the strings passed to accept() or the constructor represent valid numbers. If input fails this validation your program will throw an exception. (You make up the exception or use an existing exception such as NumberFormatException.) Your calculator should put all user input which is destined for the Number class in try/catch blocks. The action of the catch block need only be a message (and perhaps a beep: System.out.print((char)7)) and allow the program to continue.