123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. public class TableUtilities {
  2. public static String getSmallMultiplicationTable() {
  3. StringBuilder table = new StringBuilder();
  4. String cell = "%3d |";
  5. for(int row=1; row<=5; row++){
  6. for(int i=1; i<=5; i++){
  7. int value = row * i;
  8. table.append(String.format(cell, value));
  9. }
  10. table.append("\n");
  11. }
  12. return table.toString();
  13. }
  14. public static String getLargeMultiplicationTable() {
  15. StringBuilder table = new StringBuilder();
  16. String cell = "%3d |";
  17. for(int row=1; row<=10; row++){
  18. for(int i=1; i<=10; i++){
  19. int value = row * i;
  20. table.append(String.format(cell, value));
  21. }
  22. table.append("\n");
  23. }
  24. return table.toString();
  25. }
  26. public static String getMultiplicationTable(int tableSize) {
  27. StringBuilder table = new StringBuilder();
  28. String cell = "%3d |";
  29. for(int row=1; row<=tableSize; row++){
  30. for(int i=1; i<=tableSize; i++){
  31. int value = row * i;
  32. table.append(String.format(cell, value));
  33. }
  34. table.append("\n");
  35. }
  36. return table.toString();
  37. }
  38. }