Advertisement
Guest User

Hshaha

a guest
Oct 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. public class Pym {
  2. //Declare 5 PIVs for later use
  3. private String stringList[][];
  4. private int height, rows, numPyms;
  5.  
  6. //Public constructor Height, Rows, Number of Pyms
  7. public Pym(int h, int r, int n) {
  8. height = h;
  9. rows = r;
  10. numPyms = n;
  11. /*----------------------------------------/
  12. / Setup the appropriate size for /
  13. / The 2D array. r being rows of Pyms /
  14. / h being the amount of rows in each pym. /
  15. /----------------------------------------*/
  16. stringList = new String[r][h];
  17. //Build the expected 2D output of pyms
  18. buildOutput();
  19. }
  20.  
  21. //Calls Left or Right pym builder
  22. public void addPym(Boolean left, int index) {
  23. if (left) addLeftPym(index);
  24. else addRightPym(index);
  25. }
  26.  
  27. //Left does NOT contain middle
  28. private void addLeftPym(int index) {
  29. //Index being the row Count
  30. //i tracking current row on the pym
  31. for (int i = 0; i < height; i++) {
  32. String tempString = "";
  33. //k tracks how many Stars based
  34. //On the row in the pym
  35. for (int k = height; k > 0; k--) {
  36. if (k > i) tempString += " ";
  37. else tempString += "*";
  38. }
  39. //If undeclared string, set equal to
  40. if (stringList[index][i] == null) stringList[index][i] = tempString;
  41. //Otherwise, add to the string
  42. else stringList[index][i] += tempString;
  43. }
  44. }
  45.  
  46. //Right CONTAINS MIDDLE
  47. private void addRightPym(int index) {
  48. //Index being the row Count
  49. //i tracking current row on the pym
  50. for (int i = 0; i < height; i++) {
  51. String tempString = "";
  52. //k tracks how many Stars based
  53. //On the row in the pym
  54. for (int k = 0; k < height; k++) {
  55. if (k > i) tempString += " ";
  56. else tempString += "*";
  57. }
  58. //If undeclared string, set equal to
  59. if (stringList[index][i] == null) stringList[index][i] = tempString;
  60. //Otherwise, add to the string
  61. else stringList[index][i] += tempString;
  62. }
  63. }
  64.  
  65. //Build the output of expected object
  66. public void buildOutput() {
  67. //If the row should start
  68. //with the right side of the triangle.
  69. boolean oddRow = false;
  70. //i tracks the current row
  71. for (int i = 0; i < rows; i++) {
  72. //k is used to track which side
  73. //of the triangle to build
  74. for (int k = 0; k < numPyms * 2; k++) {
  75. if (k % 2 == 0) addPym(!oddRow, i);
  76. else addPym(oddRow, i);
  77. }
  78. oddRow = !oddRow;
  79. }
  80. }
  81.  
  82. //Simple 2D array print Algo
  83. public String toString() {
  84. String buildString = "";
  85. for (String[] i: stringList)
  86. for (String k: i)
  87. buildString += k + "\n";
  88. return buildString;
  89. }
  90.  
  91. //Runner creates a Pym object, and prints it.
  92. public static void main(String[] args) {
  93. Pym a = new Pym(6, 4, 6);
  94. System.out.println(a);
  95.  
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement