Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. ```java
  2. public class Printing {
  3.  
  4. public static void printStars(int amount) {
  5.  
  6. // Setting up the loop with counter
  7. // while the counter is smaller than the provided amount
  8. // Print a star and increase the counter
  9. int i = 0;
  10. while(i < amount){
  11. System.out.print("*");
  12. i++;
  13. }System.out.println("");
  14.  
  15. }
  16.  
  17. public static void printSquare(int sideSize) {
  18. int i = 0;
  19. // as long as i is smaller than given sideZie then it should printStars(sideSize)
  20. // And then increase the counter so we don't have an infinite loop
  21. while(i < sideSize) {
  22. printStars(sideSize);
  23. i++;
  24. }
  25.  
  26.  
  27. }
  28. public static void printRectangle(int width, int height) {
  29. // again creating a loop with counter
  30. int i = 0;
  31.  
  32. // while the counter is smaller than width & height print the stars with the given width & height
  33. while(i < width && i < height) {
  34. printStars(width + height);
  35. i++;
  36. }
  37.  
  38. }
  39.  
  40. public static void printTriangle(int size) {
  41.  
  42. // creating another loop with counter
  43. int i = 0;
  44.  
  45. // while i (counter) is small than the given size (in thise case 4)
  46. // then it'll print a star with the value of the counter and increase the counter.
  47. while ( i <= size){
  48. printStars(i);
  49. i++;
  50. }
  51.  
  52. }
  53.  
  54. public static void main(String[] args) {
  55. // Tests do not use main, yo can write code here freely!
  56. // if you have problems with tests, please try out first
  57. // here to see that the printout looks correct
  58.  
  59. printStars(3);
  60. printStars(5);
  61. printStars(7);
  62. printStars(9);
  63. System.out.println("\n---"); // printing --- to separate the figures
  64. printSquare(4);
  65. System.out.println("\n---");
  66. printRectangle(17, 3);
  67. System.out.println("\n---");
  68. printTriangle(4);
  69. System.out.println("\n---");
  70. }
  71.  
  72. }
  73.  
  74. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement