123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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<Integer> list = new ArrayList<Integer>();
- //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<Integer> list = new ArrayList<Integer>();
- //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<Integer> list = new ArrayList<Integer>();
- //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();
- }
- }
|