Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Pym {
- //Declare 5 PIVs for later use
- private String stringList[][];
- private int height, rows, numPyms;
- //Public constructor Height, Rows, Number of Pyms
- public Pym(int h, int r, int n) {
- height = h;
- rows = r;
- numPyms = n;
- /*----------------------------------------/
- / Setup the appropriate size for /
- / The 2D array. r being rows of Pyms /
- / h being the amount of rows in each pym. /
- /----------------------------------------*/
- stringList = new String[r][h];
- //Build the expected 2D output of pyms
- buildOutput();
- }
- //Calls Left or Right pym builder
- public void addPym(Boolean left, int index) {
- if (left) addLeftPym(index);
- else addRightPym(index);
- }
- //Left does NOT contain middle
- private void addLeftPym(int index) {
- //Index being the row Count
- //i tracking current row on the pym
- for (int i = 0; i < height; i++) {
- String tempString = "";
- //k tracks how many Stars based
- //On the row in the pym
- for (int k = height; k > 0; k--) {
- if (k > i) tempString += " ";
- else tempString += "*";
- }
- //If undeclared string, set equal to
- if (stringList[index][i] == null) stringList[index][i] = tempString;
- //Otherwise, add to the string
- else stringList[index][i] += tempString;
- }
- }
- //Right CONTAINS MIDDLE
- private void addRightPym(int index) {
- //Index being the row Count
- //i tracking current row on the pym
- for (int i = 0; i < height; i++) {
- String tempString = "";
- //k tracks how many Stars based
- //On the row in the pym
- for (int k = 0; k < height; k++) {
- if (k > i) tempString += " ";
- else tempString += "*";
- }
- //If undeclared string, set equal to
- if (stringList[index][i] == null) stringList[index][i] = tempString;
- //Otherwise, add to the string
- else stringList[index][i] += tempString;
- }
- }
- //Build the output of expected object
- public void buildOutput() {
- //If the row should start
- //with the right side of the triangle.
- boolean oddRow = false;
- //i tracks the current row
- for (int i = 0; i < rows; i++) {
- //k is used to track which side
- //of the triangle to build
- for (int k = 0; k < numPyms * 2; k++) {
- if (k % 2 == 0) addPym(!oddRow, i);
- else addPym(oddRow, i);
- }
- oddRow = !oddRow;
- }
- }
- //Simple 2D array print Algo
- public String toString() {
- String buildString = "";
- for (String[] i: stringList)
- for (String k: i)
- buildString += k + "\n";
- return buildString;
- }
- //Runner creates a Pym object, and prints it.
- public static void main(String[] args) {
- Pym a = new Pym(6, 4, 6);
- System.out.println(a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement