Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```java
- public class Printing {
- public static void printStars(int amount) {
- // Setting up the loop with counter
- // while the counter is smaller than the provided amount
- // Print a star and increase the counter
- int i = 0;
- while(i < amount){
- System.out.print("*");
- i++;
- }System.out.println("");
- }
- public static void printSquare(int sideSize) {
- int i = 0;
- // as long as i is smaller than given sideZie then it should printStars(sideSize)
- // And then increase the counter so we don't have an infinite loop
- while(i < sideSize) {
- printStars(sideSize);
- i++;
- }
- }
- public static void printRectangle(int width, int height) {
- // again creating a loop with counter
- int i = 0;
- // while the counter is smaller than width & height print the stars with the given width & height
- while(i < width && i < height) {
- printStars(width + height);
- i++;
- }
- }
- public static void printTriangle(int size) {
- // creating another loop with counter
- int i = 0;
- // while i (counter) is small than the given size (in thise case 4)
- // then it'll print a star with the value of the counter and increase the counter.
- while ( i <= size){
- printStars(i);
- i++;
- }
- }
- public static void main(String[] args) {
- // Tests do not use main, yo can write code here freely!
- // if you have problems with tests, please try out first
- // here to see that the printout looks correct
- printStars(3);
- printStars(5);
- printStars(7);
- printStars(9);
- System.out.println("\n---"); // printing --- to separate the figures
- printSquare(4);
- System.out.println("\n---");
- printRectangle(17, 3);
- System.out.println("\n---");
- printTriangle(4);
- System.out.println("\n---");
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement