1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import java.util.*;
-
- public class TriangleUtilities {
-
- public static String getRow(int numberOfStars) {
- // Use StringBuilder and a loop
-
- StringBuilder stars = new StringBuilder();
-
- for (int i = 0; i < numberOfStars; i++) {
- stars.append("*");
- }
-
- String starTriangle = stars.toString();
-
- return starTriangle;
- }
-
- public static String getTriangle(int numberOfRows) {
- StringBuilder stars = new StringBuilder();
-
- for (int i = 1; i <= numberOfRows; i++) {
- for (int j = i; j > 0; j--) {
- stars.append("*");
- }
- stars.append("\n");
- }
-
- String starTriangle = stars.toString();
- return starTriangle;
-
- }
-
-
- public static String getSmallTriangle() {
- StringBuilder stars = new StringBuilder();
-
- for (int i = 1; i < 5; i++) {
- for (int j = i; j > 0; j--) {
- stars.append("*");
- }
- stars.append("\n");
- }
-
- String starTriangle = stars.toString();
- return starTriangle;
-
- }
-
- public static String getLargeTriangle() {
- StringBuilder stars = new StringBuilder();
-
- for (int i = 1; i < 10 ; i++) {
- for (int j = i; j > 0; j--) {
- stars.append("*");
- }
- stars.append("\n");
- }
-
- String starTriangle = stars.toString();
- return starTriangle;
-
- }
- }
|