Class DecimalNumber

java.lang.Object
  |
  +--DecimalNumber

public class DecimalNumber
extends java.lang.Object

The DecimalNumber class enables use of decimal numbers for MIDlets. Contains methods for adding, subtracting, multiplying, dividing numbers and for "pretty output" with decimals.

Author:
Hallvord R. M. Steen

Field Summary
 int decPos
          the number of decimals after the point
 int number
          the base number value of this object
 
Constructor Summary
DecimalNumber(int number)
          the DecimalNumber(int) constructor accepts one integer to create a DecimalNumber object with 0 digits after the decimal
DecimalNumber(int number, int decPos)
          the DecimalNumber(int, int) constructor accepts two integers describing a base number and the "decimal position" (number of digits after the point).
DecimalNumber(java.lang.String number)
          the DecimalNumber(String) constructor accepts a string containing digits and possibly a decimal point - for instance "3.14"
 
Method Summary
 void add(DecimalNumber n)
          Addition method for DecimalNumber.
 void add(int n)
          addition method for DecimalNumber
 void add(java.lang.String n)
          addition method for DecimalNumber.
 void divide(DecimalNumber n)
          division method for DecimalNumber.
 void divide(int i)
          division method for DecimalNumber.
 void divide(java.lang.String s)
          division method for DecimalNumber.
private  int expOfTen(int i)
          10 to the power of i
 byte[] getBytes()
          DecimalNumber serialized to byte array.
 boolean isLargerThan(DecimalNumber n)
          Checks if the value of this is larger than the value of the argument
 boolean isLargerThan(int i)
          Checks if the value of this is larger than the value of the argument
 boolean isSmallerThan(DecimalNumber n)
          Checks if the value of this is smaller than the value of the argument
 boolean isSmallerThan(int i)
          Checks if the value of this is smaller than the value of the argument
 void multiply(DecimalNumber n)
          multiplication method for DecimalNumber.
 void multiply(int i)
          multiplication method for DecimalNumber.
 void multiply(java.lang.String s)
          multiplication method for DecimalNumber.
 java.lang.String round(int numDec)
          Function that returns a "prettified" string with the correct number of decimals.
 void subtract(DecimalNumber n)
          subtraction method for DecimalNumber.
 void subtract(int n)
          subtraction method for DecimalNumber.
 void subtract(java.lang.String n)
          subtraction method for DecimalNumber.
 java.lang.String toString()
          Returns a string representation with decimal point in the correct position and superfluous trailing 0s stripped.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

decPos

public int decPos
the number of decimals after the point


number

public int number
the base number value of this object

Constructor Detail

DecimalNumber

public DecimalNumber(int number)
the DecimalNumber(int) constructor accepts one integer to create a DecimalNumber object with 0 digits after the decimal


DecimalNumber

public DecimalNumber(int number,
                     int decPos)
              throws java.lang.IllegalArgumentException
the DecimalNumber(int, int) constructor accepts two integers describing a base number and the "decimal position" (number of digits after the point).

Throws:
java.lang.IllegalArgumentException - if second argument is smaller than 0

DecimalNumber

public DecimalNumber(java.lang.String number)
the DecimalNumber(String) constructor accepts a string containing digits and possibly a decimal point - for instance "3.14"

Method Detail

add

public void add(DecimalNumber n)
Addition method for DecimalNumber. Ensures both DecimalNumber objects have the same number of digits behind the decimal point before adding them.

Parameters:
n - DecimalNumber to add

add

public void add(int n)
addition method for DecimalNumber


add

public void add(java.lang.String n)
addition method for DecimalNumber. Ensures both DecimalNumber objects have the same number of digits behind the decimal point before adding them.


divide

public void divide(DecimalNumber n)
division method for DecimalNumber. Divides the factors and calculates the new decimal position.


divide

public void divide(int i)
division method for DecimalNumber. Divides the factors and calculates the new decimal position.


divide

public void divide(java.lang.String s)
division method for DecimalNumber. Divides the factors and calculates the new decimal position.


expOfTen

private int expOfTen(int i)
10 to the power of i

Returns:
10 to the power of i

getBytes

public byte[] getBytes()
DecimalNumber serialized to byte array. This is just a wrapper for the String.getBytes() function.

Returns:
byte array

isLargerThan

public boolean isLargerThan(DecimalNumber n)
Checks if the value of this is larger than the value of the argument

Returns:
boolean true if the value of the argument is smaller

isLargerThan

public boolean isLargerThan(int i)
Checks if the value of this is larger than the value of the argument

Returns:
boolean true if the value of the argument is smaller

isSmallerThan

public boolean isSmallerThan(DecimalNumber n)
Checks if the value of this is smaller than the value of the argument

Returns:
boolean true if the value of the argument is greather

isSmallerThan

public boolean isSmallerThan(int i)
Checks if the value of this is smaller than the value of the argument

Returns:
boolean true if the value of the argument is greather

multiply

public void multiply(DecimalNumber n)
multiplication method for DecimalNumber. Multiplies the factors and calculates the new decimal position.


multiply

public void multiply(int i)
multiplication method for DecimalNumber. Multiplies the factors and calculates the new decimal position.


multiply

public void multiply(java.lang.String s)
multiplication method for DecimalNumber. Multiplies the factors and calculates the new decimal position.


round

public java.lang.String round(int numDec)
Function that returns a "prettified" string with the correct number of decimals. Rounds up if removing a digit that is larger than 4. Using '0' as argument will return the integer value of this object.

Returns:
string with the correct number of decimals

subtract

public void subtract(DecimalNumber n)
subtraction method for DecimalNumber. Ensures both DecimalNumber objects have the same number of digits behind the decimal point before subtracting them.


subtract

public void subtract(int n)
subtraction method for DecimalNumber. Ensures both DecimalNumber objects have the same number of digits behind the decimal point before subtracting them.


subtract

public void subtract(java.lang.String n)
subtraction method for DecimalNumber. Ensures both DecimalNumber objects have the same number of digits behind the decimal point before subtracting them.


toString

public java.lang.String toString()
Returns a string representation with decimal point in the correct position and superfluous trailing 0s stripped. If you wish to control the number of decimals and avoid stripping trailing 0s see the round(int) method.

Overrides:
toString in class java.lang.Object
Returns:
String representing the value of the object as a string of digits with decimal point correctly placed.