import java.util.ArrayList; public class TableUtilities { public static String getSmallMultiplicationTable() { //String to store the answer String answer = ""; //create an array list to hold all the multiples ArrayList list = new ArrayList(); //loop through the first numbers being multiplied for(int i = 1; i <= 5; i ++) { //loop through the second numbers being multiplied for(int m = 1; m <= 5; m ++) { //store the product in the array int num = i * m; list.add(num); } } //loop through the array of products for(int u = 0; u < list.size(); u++) { //convert the value of the current iterration to a string int num = list.get(u); //get the length of the greatest number in the array String greatestNum = String.valueOf(list.get(list.size() - 1)); int spaces = greatestNum.length() + 1; //format the string and add it to the answer answer += String.format("%"+ spaces +"s |", num); //if the current itteration is the end of the row, start a new row. if((u + 1) % 5 == 0) { answer += "\n"; } } return answer; } public static String getLargeMultiplicationTable() { //String to store the answer String answer = ""; //create an array list to hold all the multiples ArrayList list = new ArrayList(); //loop through the first numbers being multiplied for(int i = 1; i <= 10; i ++) { //loop through the second numbers being multiplied for(int m = 1; m <= 10; m ++) { //store the product in the array int num = i * m; list.add(num); } } //loop through the array of products for(int u = 0; u < list.size(); u++) { //convert the value of the current iterration to a string int num = list.get(u); //get the length of the greatest number in the array String greatestNum = String.valueOf(list.get(list.size() - 1)); int spaces = greatestNum.length(); //format the string and add it to the answer answer += String.format("%"+ spaces +"s |", num); //if the current itteration is the end of the row, start a new row. if((u + 1) % 10 == 0) { answer += "\n"; } } return answer; } public static String getMultiplicationTable(int tableSize) { //String to store the answer String answer = ""; //create an array list to hold all the multiples ArrayList list = new ArrayList(); //loop through the first numbers being multiplied for(int i = 1; i <= tableSize; i ++) { //loop through the second numbers being multiplied for(int m = 1; m <= tableSize; m ++) { //store the product in the array int num = i * m; list.add(num); } } //loop through the array of products for(int u = 0; u < list.size(); u++) { //convert the value of the current iterration to a string int num = list.get(u); //get the length of the greatest number in the array String greatestNum = String.valueOf(list.get(list.size() - 1)); int spaces = greatestNum.length(); //there will always be a minimum of 3 spaces. if(spaces < 3){ spaces = 3; } //format the string and add it to the answer answer += String.format("%"+ spaces +"s |", num); //if the current itteration is the end of the row, start a new row. if((u + 1) % tableSize == 0) { answer += "\n"; } } return answer; } public int testStringLen(String test) { return test.length(); } }