TableUtilities.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import java.util.ArrayList;
  2. public class TableUtilities {
  3. public static String getSmallMultiplicationTable() {
  4. //String to store the answer
  5. String answer = "";
  6. //create an array list to hold all the multiples
  7. ArrayList<Integer> list = new ArrayList<Integer>();
  8. //loop through the first numbers being multiplied
  9. for(int i = 1; i <= 5; i ++)
  10. {
  11. //loop through the second numbers being multiplied
  12. for(int m = 1; m <= 5; m ++)
  13. {
  14. //store the product in the array
  15. int num = i * m;
  16. list.add(num);
  17. }
  18. }
  19. //loop through the array of products
  20. for(int u = 0; u < list.size(); u++)
  21. {
  22. //convert the value of the current iterration to a string
  23. int num = list.get(u);
  24. //get the length of the greatest number in the array
  25. String greatestNum = String.valueOf(list.get(list.size() - 1));
  26. int spaces = greatestNum.length() + 1;
  27. //format the string and add it to the answer
  28. answer += String.format("%"+ spaces +"s |", num);
  29. //if the current itteration is the end of the row, start a new row.
  30. if((u + 1) % 5 == 0)
  31. {
  32. answer += "\n";
  33. }
  34. }
  35. return answer;
  36. }
  37. public static String getLargeMultiplicationTable() {
  38. //String to store the answer
  39. String answer = "";
  40. //create an array list to hold all the multiples
  41. ArrayList<Integer> list = new ArrayList<Integer>();
  42. //loop through the first numbers being multiplied
  43. for(int i = 1; i <= 10; i ++)
  44. {
  45. //loop through the second numbers being multiplied
  46. for(int m = 1; m <= 10; m ++)
  47. {
  48. //store the product in the array
  49. int num = i * m;
  50. list.add(num);
  51. }
  52. }
  53. //loop through the array of products
  54. for(int u = 0; u < list.size(); u++)
  55. {
  56. //convert the value of the current iterration to a string
  57. int num = list.get(u);
  58. //get the length of the greatest number in the array
  59. String greatestNum = String.valueOf(list.get(list.size() - 1));
  60. int spaces = greatestNum.length();
  61. //format the string and add it to the answer
  62. answer += String.format("%"+ spaces +"s |", num);
  63. //if the current itteration is the end of the row, start a new row.
  64. if((u + 1) % 10 == 0)
  65. {
  66. answer += "\n";
  67. }
  68. }
  69. return answer;
  70. }
  71. public static String getMultiplicationTable(int tableSize) {
  72. //String to store the answer
  73. String answer = "";
  74. //create an array list to hold all the multiples
  75. ArrayList<Integer> list = new ArrayList<Integer>();
  76. //loop through the first numbers being multiplied
  77. for(int i = 1; i <= tableSize; i ++)
  78. {
  79. //loop through the second numbers being multiplied
  80. for(int m = 1; m <= tableSize; m ++)
  81. {
  82. //store the product in the array
  83. int num = i * m;
  84. list.add(num);
  85. }
  86. }
  87. //loop through the array of products
  88. for(int u = 0; u < list.size(); u++)
  89. {
  90. //convert the value of the current iterration to a string
  91. int num = list.get(u);
  92. //get the length of the greatest number in the array
  93. String greatestNum = String.valueOf(list.get(list.size() - 1));
  94. int spaces = greatestNum.length();
  95. //there will always be a minimum of 3 spaces.
  96. if(spaces < 3){
  97. spaces = 3;
  98. }
  99. //format the string and add it to the answer
  100. answer += String.format("%"+ spaces +"s |", num);
  101. //if the current itteration is the end of the row, start a new row.
  102. if((u + 1) % tableSize == 0)
  103. {
  104. answer += "\n";
  105. }
  106. }
  107. return answer;
  108. }
  109. public int testStringLen(String test) {
  110. return test.length();
  111. }
  112. }