-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomStarWedge.java
187 lines (180 loc) · 7.58 KB
/
CustomStarWedge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Name: Bill Mao
*
* Date: 10/20/2017
*
* School: East Chapel Hill High School
*
* Program: Prog 673a Asterisk Revisited
*
* Description: This program is designed to print out a left, right, or center aligned triangle of stars according to the user's wishes.
* The program begins by declaring any variables needed in the main method.
* The program then prints out a menu with three triangle shapes and asks the user for which one they want.
* The triangle shapes are are:
* Left (1)
* Right (2)
* Center (3)
* The triangle that the user wanted is then stored via a scanner object as an integer from 1-3.
* The number of rows is then also assigned to the rows variable via a Scanner object.
* A switch statement then executes a method printing out the desired triangle shape with the desired amount of rows.
* The amount of rows is passed to each method via a parameter.
*
* Difficulties: I had some trouble deciding what type of loop to use in the triangle printing methods, but I settled on for loops with a local variable...
* ,as they did not change the overall stars and spaced variable and were the most concise.
*
* What I Learned: I got some more practice with documenting methods and writing multiple methods in a class in Java. I also learned how to pass a primitive variable to a method.
*/
import java.util.*;
import java.io.*;
public class CustomStarWedge
{
/**
* Menu for the user to select their desired triangle shape and rows
* post: Triangle shape method selected and number of rows passed to the corresponding method
*/
public static void main(String args[])
{
Scanner kbInput = new Scanner(System.in); //Creates scanner object in order to get user input
int rows, triangleType; //Rows and triangle alignment declared
//Tells user of to select a triangle printing methods by typing in an integer from 1-3
System.out.println("Types of Triangles");
System.out.println("1. Left");
System.out.println("2. Right");
System.out.println("3. Center\n");
System.out.println("Enter a number of Triangle Type (1-3): ");
///...and then assigns the number to the triangleType variable.
triangleType = kbInput.nextInt();
//Same is done for the rows variable
System.out.println("How many rows?");
rows = kbInput.nextInt();
//Switch statement then executes the corresponding method, and passes along the amount of rows (arguement) as an parameter of the method.
switch (triangleType)
{
case 1: leftTriangle(rows);
break;
case 2: rightTriangle(rows);
break;
case 3: centerTriangle(rows);
break;
}
System.out.println(""); //Space is printed for formatting reasons.
}
/**
* Prints a left aligned triangle of stars
* post: Left aligned triangle that has x amount of rows according to the user printed to the console
*/
public static void leftTriangle(int rows)
{
int stars = 1; //Amount of stars in each row
while(rows >= 1) //Increments from the starting size of rows to the last row
{
for(int rowOfStars = stars; rowOfStars >= 1; rowOfStars--)
{
//Prints out stars for however many stars there should be per row
//The local variable rowOfStars is used to avoid changing the stars variable.
System.out.print("*");
}
System.out.println(""); //A new line is printed once all the stars are printed for one row to separate the rows.
rows--; //For each new row, the row variable is decreased so that the correct amount of rows is printed
stars++; //The stars variable is increased as each new row has a new star
}
}
/**
* Prints a right aligned triangle of stars
* post: Right aligned triangle that has x amount of rows according to the user printed to the console
*/
public static void rightTriangle(int rows)
{
int stars = 1; //Amount of stars in each row
int spaces = rows - 1; //Amount of spaces in each row
while(rows >= 1) //Increments from the starting size of rows to the last row
{
for(int rowOfSpaces = spaces; rowOfSpaces >= 1; rowOfSpaces--)
{
//Prints out spaces for however many stars there should be per row
//The local variable rowOfSpaces is used to avoid changing the stars variable.
System.out.print(" "); //Prints out a space for each iteration
}
for(int rowOfStars = stars; rowOfStars >= 1; rowOfStars--)
{
//Prints out stars for however many stars there should be per row
//The local variable rowOfStars is used to avoid changing the stars variable.
System.out.print("*"); //Prints a star for each iteration
}
System.out.println(""); //A new line is printed once all the stars are printed for one row to separate the rows.
rows--; //For each new row, the row variable is decreased so that the correct amount of rows is printed
spaces--; //For each new row, the spaces variable is decreased so that the correct amount of spaces is printed
stars++; //The stars variable is increased as each new row has a new star
}
}
/**
* Prints a center aligned triangle of stars
* post: Center aligned triangle that has x amount of rows according to the user printed to the console
*/
public static void centerTriangle(int rows)
{
int stars = 1; //Amount of stars in each row
int spaces = rows - stars; //Amount of spaces in each row
while(rows >= 1) //Increments from the starting size of rows to the last row
{
for(int rowOfSpaces = spaces; rowOfSpaces >= 1; rowOfSpaces--) //Iterates for however many stars there should be per row
{
//Prints out spaces for however many stars there should be per row
//The local variable rowOfSpaces is used to avoid changing the stars variable.
System.out.print(" "); //Prints a star for each iteration
}
for(int rowOfStars = stars; rowOfStars >= 1; rowOfStars--) //Iterates for however many stars there should be per row
{
//Prints out stars for however many stars there should be per row
//The local variable rowOfStars is used to avoid changing the stars variable.
System.out.print("*"); //Prints a star for each iteration
}
System.out.println(""); //A new line is printed once all the stars are printed for one row to separate the rows.
rows--; //For each new row, the row variable is decreased so that the correct amount of rows is printed
stars = stars + 2; //The stars variable is increased by 2 as each new row has two a new stars due to center spacing
spaces--; //For each new row, the spaces variable is decreased so that the correct amount of spaces is printed
}
}
}
//Output:
//Types of Triangles
//1. Left
//2. Right
//3. Center
//
//Enter a number of Triangle Type (1-3):
//1
//How many rows?
//5
//*
//**
//***
//****
//*****
//
//Types of Triangles
//1. Left
//2. Right
//3. Center
//
//Enter a number of Triangle Type (1-3):
//2
//How many rows?
//3
// *
// **
//***
//
//Types of Triangles
//1. Left
//2. Right
//3. Center
//
//Enter a number of Triangle Type (1-3):
//3
//How many rows?
//4
// *
// ***
// *****
//*******