import java.util.Scanner; /////////////////////////////////////////////////////////////////////////// // Do not import any additional classes beyond the one included above // // Also, do not change any of the given code. Only add code immediately // // below the two "ADD CODE HERE, AS NEEDED" comments. // /////////////////////////////////////////////////////////////////////////// public class Multiply { private static final char SPACE = ' '; private static final int ROW_OF_FIRST_SUMMAND = 3; public static int value(char digit) { // presumes digit is in '0'..'9' or 'A'..'F' return ((digit >= '0' && digit <= '9') ? (int) (digit - '0') : (int) (digit - 'A' + 10)); } public static char digit(int value) { // presumes digit is in 0..15 return (value < 10 ? (char) (value + '0') : (char) (value - 10 + 'A')); } public static String workToStr(char[][] work) { String s = ""; for (int r = 0; r < work.length; r++) { s += (r == 1 ? 'x' : SPACE); for (int c = 0; c < work[0].length; c++) { s += work[r][c]; } s += System.lineSeparator(); } return s; } public static char[][] removeColumnsOfSpaces(char[][] w) { int numColsOfSpacesOnLeft = -1; boolean isEmpty = true; while (isEmpty) { for (int r = 0; r < w.length; r++) { isEmpty = isEmpty && (w[r][numColsOfSpacesOnLeft+1] == SPACE); } numColsOfSpacesOnLeft++; } char[][] wNew = new char[w.length][w[0].length-numColsOfSpacesOnLeft]; for (int r = 0; r < w.length; r++) { for (int c = 0; c < w[0].length - numColsOfSpacesOnLeft; c++) { wNew[r][c] = w[r][c+numColsOfSpacesOnLeft]; } } return wNew; } public static String multiply(String numOnTop, String numOnBottom, int base) { int numRows = numOnBottom.length()+5; // i.e., one line for each intermediate calculation (one per digit in number on bottom) plus // a line for each number multiplied, two horizontal lines, and the answer. ////////////////////////////// // ADD CODE HERE, AS NEEDED // ////////////////////////////// // all cells in the array below, which should contain the "work" of the multiplication // are initially filled with SPACE char[][] work = new char[numRows][maxNumDigitsInResult]; for (int rw = 0; rw < numRows; rw++) { for (int cw = 0; cw < maxNumDigitsInResult; cw++) work[rw][cw] = SPACE; } ////////////////////////////// // ADD CODE HERE, AS NEEDED // ////////////////////////////// // the code below fills in rows 1-3 and the second to last row for you // (i.e., which contain the first number; the second number, and the two dashed underlines) int workWidth = work[0].length; work[0] = ((""+SPACE).repeat(workWidth - numOnTop.length()) + numOnTop).toCharArray(); work[1] = ((""+SPACE).repeat(workWidth - numOnBottom.length()) + numOnBottom).toCharArray(); work[2] = "-".repeat(workWidth).toCharArray(); work[work.length-2] = "-".repeat(workWidth).toCharArray(); return workToStr(work); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter two numbers to multiply and the base in" + System.lineSeparator() + "which to multiply them (separated by spaces):"); String n = scanner.next(); String m = scanner.next(); int base = scanner.nextInt(); scanner.close(); String work = multiply(n,m,base); System.out.println(); System.out.println(work); } }