TriangleUtilities.java 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import java.util.*;
  2. public class TriangleUtilities {
  3. public static String getRow(int numberOfStars) {
  4. // Use StringBuilder and a loop
  5. StringBuilder stars = new StringBuilder();
  6. for (int i = 0; i < numberOfStars; i++) {
  7. stars.append("*");
  8. }
  9. String starTriangle = stars.toString();
  10. return starTriangle;
  11. }
  12. public static String getTriangle(int numberOfRows) {
  13. StringBuilder stars = new StringBuilder();
  14. for (int i = 1; i <= numberOfRows; i++) {
  15. for (int j = i; j > 0; j--) {
  16. stars.append("*");
  17. }
  18. stars.append("\n");
  19. }
  20. String starTriangle = stars.toString();
  21. return starTriangle;
  22. }
  23. public static String getSmallTriangle() {
  24. StringBuilder stars = new StringBuilder();
  25. for (int i = 1; i < 5; i++) {
  26. for (int j = i; j > 0; j--) {
  27. stars.append("*");
  28. }
  29. stars.append("\n");
  30. }
  31. String starTriangle = stars.toString();
  32. return starTriangle;
  33. }
  34. public static String getLargeTriangle() {
  35. StringBuilder stars = new StringBuilder();
  36. for (int i = 1; i < 10 ; i++) {
  37. for (int j = i; j > 0; j--) {
  38. stars.append("*");
  39. }
  40. stars.append("\n");
  41. }
  42. String starTriangle = stars.toString();
  43. return starTriangle;
  44. }
  45. }