Advertisement
Guest User

Untitled

a guest
Mar 26th, 2025
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1.  
  2. public class AdvancedAstrology {
  3.  
  4. public static void printStars(int number) {
  5. // part 1 of the exercise
  6. int i = 0;
  7. while (i < number) {
  8. System.out.print("*");
  9. i++;
  10. }
  11. System.out.println("");
  12. }
  13.  
  14. public static void printSpaces(int number) {
  15. // part 1 of the exercise
  16. int i = 0;
  17. while (i < number) {
  18. System.out.print(" ");
  19. i++;
  20. }
  21. }
  22.  
  23. public static void printTriangle(int size) {
  24. // part 2 of the exercise
  25. // let's start from 1 because the first line should have one star!
  26. int i = 1;
  27. while (i <= size) {
  28. printSpaces(size - i);
  29. printStars(i);
  30. i++;
  31. }
  32. }
  33.  
  34. public static void christmasTree(int height) {
  35. // part 3 of the exercise
  36. // Let's first print the top of the tree.
  37. // This works almost like the triangle.
  38. int i = 1;
  39. while (i <= height) {
  40. printSpaces(height - i);
  41. printStars(i + (i - 1));
  42. i++;
  43. }
  44.  
  45. // End then the bottom.
  46. printSpaces(height - 2);
  47. printStars(3);
  48. printSpaces(height - 2);
  49. printStars(3);
  50. }
  51.  
  52. public static void main(String[] args) {
  53. // The tests are not checking the main, so you can modify it freely.
  54.  
  55. printTriangle(5);
  56. System.out.println("---");
  57. christmasTree(4);
  58. System.out.println("---");
  59. christmasTree(10);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement