The program

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.

The Number class

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:

  • public Number(). A constructor.
  • public Number(String str). A constructor which takes a String representation of a number (e.g. "-21.507"). Calls accept.
  • public void accept(String str). Builds a list representation of the number represented by the string.
  • public Number add(Number n). Returns a Number which represents "this + n".
  • public Number subtract(Number n). Returns a Number which represents "this - n".
  • public Number multiply(Number n). Returns a Number which represents "this * n".
  • public void reverseSign(). Reverses the value of negative.
  • publictoString(). Returns a String representation of the Number (so it can be displayed by System.out.print()).

There will be no other public methods, but you may provide any private methods you wish.

Implementing the operations

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.

private Number addAbsolute(Number n)

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.

private Number subtractAbsolute(Number n)

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.

public Number multiply(Number n)

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.

Finishing the arithmetic operations

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.

toString()

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.

Input validation

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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.