University of Calgary

big number class

Big Number Class
source: Katrin Becker 2002
CONTENTS of THIS PAGE...
Goals, Skills & Concepts Introduction & Background Description Specifications
Approach Minimal Requirements) Testing and Marking Bonuses Challenge
Goals, Skills and Concepts:
- learn basic syntax of new language
- design and implement a crelatively simple class
Introduction and Background:
Since we don't have much time for this assignment we will build a new class into the program we just completed. We will create a class that will allow us to do arbitrary precision (yes, I know Java already provides us with one of these but we'll do a sinple one ourselves anyways).

Description:
You have been given a Java application that emulates a simple calulator ('calc.java' ). This program reads 3 things: an integer, followed by an operator, and then another integer. It is assumed that all items will be typed on the same line and that they will be seperated from each other by at least one blank. Invalid integers are treated as zeros. The program calcualtes the expression given and then prints the result. Users may loop as often as desired.
What we need to create is a class to 'replace' the ordinary integers in the program that will allow for integers of any size. [HINT: use an array with one digit per element]. We will need to create at least the following operations for this class:
  • read
  • write
  • add
  • mult
  • sub
  • div

Specifications:
- may use own application from last assignment if desired but not necessary
- may NOT change the basic functionality of the program
- do not re-arrange the existing program.

Approach:
This is a smallish addition, requiring no new material except a simple class. For a sample of the 'calc program that uses this big class, see: 'calc2.java' . There is also a bytecode version of the BigNum class. This is the one used by calc2. All can be found in ~becker/Courses/233/A2/BigNumClass [also here]
Requirements & Grading: Basic Version:
- new class, no frills
- seperate class (in seperate file, for instance BigNum.Java)
- allow for at least 15 digits in number, handle "overflow" gracefully
- need not grow larger
- replaces int with almost no change to the code
- both operands are assumed to be the same size [i.e. same max # of digits]
- multiply can be implemented as a repeated add operation
- NO subtract or divide [they are stubs]

'B' Version:
- everything the 'basic' version does, but instead of just 15 digits, it grows the array as necessary
(i.e. if the number gets too big for the array it will make the array bigger)
- still assumes both operands are the same max size
- implement subtract
- divide can be implemented as a repeated subtract operation

'A' Version:
- everything the 'B' version does but allows for operands of varying sizes
Testing:
Test your program using at least the following expressions :
  1. 1223456789 * 1234546789
  2. 1000000000 * 10000
  3. 1000000000 * 100000
  4. 463 - 4
  5. 120 + 99
  6. 10 - 4
  7. 4 - 10
  8. -8 * 2
  9. 14 / 4
  10. 12.5 - 4
BONUSES:
1. implements a "typical" multiply [the way you would do it on paper]
2. Add other operators
3. Add unary operators
4. Handle variables 'a' - 'z' in addition to literals
Challenges:
Not this time. On some assingments, the most involved and challenging bonuses (OK, sometimes just stupidly hard or rediculous amounts of work) will be listed under this category.



Updated: October 3, 2005 11:25 AM