Question: need help with this code. What I have so far: public class PascalsTriangle { int[][] array; public PascalsTriangle(int number) { createTriangle(number); } private void createTriangle(int

need help with this code.

 need help with this code. What I have so far: publicWhat I have so far:

public class PascalsTriangle

{

int[][] array;

public PascalsTriangle(int number)

{

createTriangle(number);

}

private void createTriangle(int r)

{

array = new int[r][];

for (int i = 0 ; i

{

array[i] = new int [6] ;

}

for (int[] array1 : array) {

for (int col = 0; col

array1[col] = 1;

}

}

}

public String toString()

{

StringBuffer grid= new StringBuffer();

for(int index=0; index

for(int index2=0; index2

grid.append(array[index][index2]).append(" ");

}

grid.append(" ");

}

return grid.toString();

}

}

===============

public class Test { public static void main(String[] args) { int numberOfRows = 0; Scanner input = new Scanner(System.in); System.out.println("Provide number between 1 - 13 for the " + " rows of the Pascals' Triangle"); while(input.hasNextInt()) { numberOfRows = input.nextInt(); PascalsTriangle newTriangle = new PascalsTriangle(numberOfRows); System.out.println(newTriangle); if (input.hasNext() == false) { System.out.println("The number you entered is not between 1 - 13"); System.out.println("Please, provide number between 1 - 13 for the " + " rows of the Pascals' Triangle "); } } input.close(); } }

Use a loop to coerce the user to enter a valid number of rows from 1 to 13, inclusive. If the user input is not a valid int, print an error message and make the user try again Otherwise, check whether it is between 1 and 13, inclusive. If so, create the PascalsTriangle and print the triangle returned by toStringl). Otherwise, print an error message and have the user try again. Your code must allow the user any number of attempts to enter a valid number Hints: " To test whether the next token is a valid int, use Scanner class method hasNextint " Code the input loop last, only after you have completed the PascalsTriangle class Your main() method should enable the user to create any number of triangles in each run

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!