1
0
Fork 0
StringMultiplication/src/de/janchristiangruenhage/math/multiplication/Multiplication.java
Jan Christian Grünhage 76f4567346 Initial commit
2016-01-13 18:48:57 +01:00

141 lines
5.8 KiB
Java

package de.janchristiangruenhage.math.multiplication;
import de.janchristiangruenhage.math.Number;
import de.janchristiangruenhage.math.addition.Addition;
/**
* The class <tt>Multiplication</tt> represents a Multiplication fo two relatively large factors,
* with about 25 digits per factor being a reasonable limit.
*/
public class Multiplication {
private String factorOne;
private String factorTwo;
private int splitLines;
private boolean highestMultiplication;
private String carry;
private String resultCarry;
/**
* Constructs a new Multiplication Object
*
* @param factorOne First Factor
* @param factorTwo Second Factor
* @param splitLines How many lines of the multiplication will be added in one addition,
* If != 0 and smaller than the number of digits in the second factor,
* there will be multiple Additions per Multiplication.
* @param highestMultiplication If this is a single or the top one of a row of multiplications,
* there is no carry that will be passed over from this one to another Multiplication,
* so the part that would be the carry has not to be enclosed in braces.
*/
public Multiplication(String factorOne, String factorTwo, int splitLines, boolean highestMultiplication) {
this.factorOne = factorOne;
this.factorTwo = factorTwo;
this.splitLines = splitLines;
this.highestMultiplication = highestMultiplication;
this.carry = "0";
//Initializes carry as zero in case it is the lowest multiplication,
//In case it is not set to another value later
}
/**
* Creates and easily readable Multiplication and returns it.
*
* @return String representation of the Calculation
*/
public String printCalculation() {
String retVal = "";
for (int i = 0; i < 2; i++) {
retVal += " ";
}
//Makes the First line some characters longer
retVal += factorOne + " * " + factorTwo;
//Adds the two factors and the Multiplication symbol to the first Line
int lineLength = retVal.length();
//Saves the length of the first line, this is needed later on since all lines need to have the same length
retVal += "\n";
for (int i = 0; i < lineLength; i++) {
retVal += "_";
}
//Draws a line as the second line
retVal += "\n";
String[] lines;
//array of lines, where the resulting addends will go to
lines = new String[factorTwo.length() + 1];
lines[lines.length - 1] = carry;
//adds carry as last line
while (lines[lines.length - 1].length() < (lineLength - 1)) {
lines[lines.length - 1] = " " + lines[lines.length - 1];
//elongates the carries line with spaces
}
lines[lines.length - 1] = "+" + lines[lines.length - 1];
//adds a plus symbol to the carries line
for (int i = 0; i < factorTwo.length(); i++) {
//iterates through the second factor, therefore through the lines,
// since there is a line for each digit in the second factor
lines[i] = "";
//constructs the new line string
int carry = 0;
//creates new carry variable
for (int j = 0; j < i; j++) {
lines[i] = 0 + lines[i];
//adds zeroes to the addends, depending on the digit they get multiplied with,
//for example if its the line of the 3 in 344, then two zeroes get added
}
Number factorTwoNum = Number.parseNumber(factorTwo.charAt(factorTwo.length() - i - 1));
//Parses the first one digit factor from the first factor string
for (int j = 0; j < factorOne.length(); j++) {
Number factorOneNum = Number.parseNumber(factorOne.charAt(factorOne.length() - j - 1));
//Parses the second one digit factor from the first factor string
Number result = Number.multiply(factorOneNum, factorTwoNum, carry);
//Multiplies the factors, adds the carry after that
carry = result.getCarry();
//assigns the carry it's new value
lines[i] = result.toString() + lines[i];
//adds the result to the line
}
if (carry != 0) {
lines[i] = carry + lines[i];
//if there is a carry left in the end, it gets added to the same line
}
while (lines[i].length() < (lineLength - 1)) {
lines[i] = " " + lines[i];
//elongates the line with spaces
}
if (i > 0) {
lines[i] = "+" + lines[i];
//adds a plus symbol to the line
} else {
lines[i] = " " + lines[i];
//adds another space to the line
}
}
Addition addition = new Addition(lines, splitLines, factorOne.length(), !highestMultiplication);
//Construct the new addition
retVal += addition.printCalculation();
//prints the addition and adds that to the retVal string
resultCarry = addition.getResultCarry();
//saves the resultCarry of the addition,
// this is used for the higher multiplications, if there are any
return retVal;
}
/**
* Setter for the carry
*
* @param carry the carry
*/
public void setCarry(String carry) {
this.carry = carry;
}
/**
* Getter for the carry of the result
*
* @return the carry of the result
*/
public String getResultCarry() {
return resultCarry;
}
}