package de.janchristiangruenhage.math.multiplication; import de.janchristiangruenhage.math.Number; import de.janchristiangruenhage.math.addition.Addition; import java.util.LinkedList; /** * The class Multiplication 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 addends 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"; LinkedList addends = new LinkedList<>(); //list of addends, where the resulting addends will go to for (int i = 0; i < factorTwo.length(); i++) { //iterates through the second factor, therefore through the addends, // since there is a addend for each digit in the second factor String intermediateResult = ""; //constructs the new line string int carry = 0; //creates new carry variable for (int j = 0; j < i; j++) { intermediateResult = 0 + intermediateResult; //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 intermediateResult = result.toString() + intermediateResult; //adds the result to the line } if (carry != 0) { intermediateResult = carry + intermediateResult; //if there is a carry left in the end, it gets added to the same line } while (intermediateResult.length() < (lineLength - 1)) { intermediateResult = " " + intermediateResult; //elongates the line with spaces } if (i > 0) { intermediateResult = "+" + intermediateResult; //adds a plus symbol to the line } else { intermediateResult = " " + intermediateResult; //adds another space to the line } addends.add(intermediateResult); } while (carry.length() < (lineLength - 1)) { carry = " " + carry; //elongates the carries line with spaces } carry = "+" + carry; //adds a plus symbol to the carries line addends.add(carry); //adds carry as last line Addition addition = new Addition(addends, splitLines, factorOne.length(), !highestMultiplication, lineLength); //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; } }