Question
5.14 Lab 5c Patterned Matrix Objectives Create a program that initializes and prints a 2-dimensional array. Experience the exception message generated by a negative index
5.14 Lab 5c Patterned Matrix
Objectives
Create a program that initializes and prints a 2-dimensional array.
Experience the exception message generated by a negative index value.
Use printf() instead of print() or println() methods.
Background Reading
ZyBook: 6. Arrays, 8.3 printf()
General Instructions
Write a Java class PatternedArray.java that will create an 2-dimensional array and print it in a table format.
The program should:
Create a two-dimensional array and that is filled with whole numbers such that the elements have the following pattern: Row 1: 10+numRows*1+ 0, 10+numRows*1+1,10+numRows*1+2, Row 2: 10+numRows*2+ 0, 10+numRows*2+1, 10+numRows*2+2, Row 3: 10+numRows*3+ 0, 10+numRows*2+1, 10+numRows*3+2,
Print the array by row where each element printed has a minimum width of 4 spaces and is left-justified. Use System.out.printf(). [Note: printf() is identical to format() End each row with a newline and complete the output with a blank line. See ZyBooks Chapter 8.3 for printf() explanation.
Sample output where input is 3 5
13 14 15 16 17 16 17 18 19 20 19 20 21 22 23
Sample output where input is 1 4
11 12 13 14
Sample output where input is 5 3
15 16 17 20 21 22 25 26 27 30 31 32 35 36 37
Hint:
System.out.printf("%3d", array[0][0]); would print the value at array[0][0] with a minimum width of three spaces right-justified.
If row is 3 and col is 0, System.out.printf("%-8d", array[row][col]); would print the value at array[3][0] with a minimum width of eight spaces left-justified.
public class PatternedMatrix { public static void main(String[] args) { //Grab the umber of rows and columns from the user, in that order //create a 2-D array (matrix) that is rox x col //use a nested for loops that fill the array with the pattern shown //in the problem statement //again using nested for loops, print the contents of the matrix //with each row on it's own line and each number followed by a space } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started