167 lines
4.7 KiB
Java
167 lines
4.7 KiB
Java
package de.janchristiangruenhage.math;
|
|
|
|
/**
|
|
* The class <tt>Number</tt> represents a one digit number and a multiple digit carry
|
|
* <p>
|
|
* Examples: The Number representation of:
|
|
* 12 would be 2 carry 1
|
|
* 123 would be 3 carry 12
|
|
* 5351 would be 1 carry 535
|
|
* <p>
|
|
* and so on.
|
|
*/
|
|
public class Number {
|
|
private int number;
|
|
private int carry;
|
|
|
|
/**
|
|
* Constructs a new Number using a number and a carry.
|
|
* <p>
|
|
* if the number has more than one digit, it will be split
|
|
* and the carry will be enlarged accordingly.
|
|
*
|
|
* @param number the number
|
|
* @param carry the carry
|
|
*/
|
|
public Number(int number, int carry) {
|
|
this.number = intToNumber(number).getNumber();
|
|
this.carry = carry + intToNumber(number).getCarry();
|
|
}
|
|
|
|
private Number(int number, int carry, boolean calculateNumber) {
|
|
if (calculateNumber) {
|
|
this.number = intToNumber(number).getNumber();
|
|
this.carry = carry + intToNumber(number).getCarry();
|
|
} else {
|
|
this.number = number;
|
|
this.carry = carry;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Constructs a new Number using an int only.
|
|
* <p>
|
|
* The lowest value digit will be the number, the rest will be the carry
|
|
*
|
|
* @param number the int input
|
|
*/
|
|
public Number(int number) {
|
|
this.number = intToNumber(number).getNumber();
|
|
this.carry = intToNumber(number).getCarry();
|
|
}
|
|
|
|
/**
|
|
* Adds a number to this number.
|
|
*
|
|
* @param number the number to be added
|
|
*/
|
|
public void add(Number number) {
|
|
Number tmp = add(this, number);
|
|
this.number = tmp.getNumber();
|
|
this.carry = tmp.getCarry();
|
|
}
|
|
|
|
/**
|
|
* Parses a number from a char.
|
|
*
|
|
* @param input the char input
|
|
* @return a number that represents this char
|
|
*/
|
|
public static Number parseNumber(char input) {
|
|
switch (input) {
|
|
case '0':
|
|
return new Number(0);
|
|
case '1':
|
|
return new Number(1);
|
|
case '2':
|
|
return new Number(2);
|
|
case '3':
|
|
return new Number(3);
|
|
case '4':
|
|
return new Number(4);
|
|
case '5':
|
|
return new Number(5);
|
|
case '6':
|
|
return new Number(6);
|
|
case '7':
|
|
return new Number(7);
|
|
case '8':
|
|
return new Number(8);
|
|
case '9':
|
|
return new Number(9);
|
|
default:
|
|
return new Number(0);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Getter for the number variable.
|
|
*
|
|
* @return the number variable
|
|
*/
|
|
public int getNumber() {
|
|
return number;
|
|
}
|
|
|
|
/**
|
|
* Getter for the carry variable.
|
|
*
|
|
* @return the carry variable
|
|
*/
|
|
public int getCarry() {
|
|
return carry;
|
|
}
|
|
|
|
/**
|
|
* Multiplies two numbers without a carry and the carry to the result num.
|
|
*
|
|
* @param factorOne first factor
|
|
* @param factorTwo second factor
|
|
* @param carry carry
|
|
* @return (new Number(factorOne * factorTwo + carry))
|
|
*/
|
|
public static Number multiply(Number factorOne, Number factorTwo, int carry) {
|
|
int result = (factorOne.getNumber() * factorTwo.getNumber()) + carry;
|
|
return intToNumber(result);
|
|
}
|
|
|
|
/**
|
|
* Adds up two numbers and their carries
|
|
*
|
|
* @param addendOne first addend
|
|
* @param addendTwo second addend
|
|
* @return the resulting number
|
|
*/
|
|
public static Number add(Number addendOne, Number addendTwo) {
|
|
int result = addendOne.getNumber()
|
|
+ addendTwo.getNumber()
|
|
+ addendOne.getCarry() * 10
|
|
+ addendTwo.getCarry() * 10;
|
|
//The carries have to be multiplied by ten,
|
|
// since they are ten times as weighted as the normal numbers
|
|
return intToNumber(result);
|
|
}
|
|
|
|
private static Number intToNumber(int input) {
|
|
Number result;
|
|
int number = input % 10;
|
|
//Gets the first digit
|
|
int carry = (input - number) / 10;
|
|
//removes the first digit from the input and assign that to the carry
|
|
result = new Number(number, carry, false);
|
|
//private constructor as only way to instantiate
|
|
// the Number with the ability to directly set the number and carry
|
|
// without them getting pushed through this method again
|
|
// for ensuring that the number is a one digit number.
|
|
// If they were pushed through this method again,
|
|
// it would result in an endless loop resulting in a Stack Overflow
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "" + number;
|
|
}
|
|
}
|