Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the following exercise you are not permitted to plink values into the matrix with solitary assignment statements. You must always use a loop to

In the following exercise you are not permitted to "plink" values into the matrix with solitary assignment statements. You must always use a loop to initialize a row, col or diagonal in the matrix. Some exercise may need more than one loop - just don't do any individual single statements to put single values into the matrix. Do not change main mehtod. Just fill in these methods below main

zeros()

diagonal_1()

diagonal_2()

border()

Here is the code:

/* Exercise1.java */ import java.io.*; import java.util.*; public class Exercise1 { public static void main( String[] args ) { int[][]m = new int[5][5]; /* JAVA automatically initializes all the elements to 0. If printed will look like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 */ fillDiagonal_1( m ); // You fill in code for method fillDiagonal_1 (below main) printMatrix( "DIAGONAL_1",m ); /* After fillDiagonal_1() the print of the matrix must look like this: 0 0 0 0 4 0 0 0 3 0 0 0 2 0 0 0 1 0 0 0 0 0 0 0 0 */ zeroMatrix( m ); // You fill in code for method zeroMatrix (below main) printMatrix( "ZEROS",m ); // should come out as all zeros fillDiagonal_2( m ); // You fill in code for method fillDiagonal_2 (below main) printMatrix( "DIAGONAL_2",m ); /* After fillDiagonal_2() the print of the matrix must look like this: 0 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 4 */ zeroMatrix( m ); // You fill in code for method zeroMatrix (below main) printMatrix( "ZEROS",m ); // should come out all zeros again fillBorder( m ); // You fill in code for method fillBorder (below main) printMatrix( "BORDER",m ); /* After fillBorder() the print of the matrix must look like this: 0 1 2 3 4 1 0 0 0 3 2 0 0 0 2 3 0 0 0 1 4 3 2 1 0 */ } // END MAIN // - - - - - - - - - - Y O U W R I T E T H E S E F O U R M E T H O D S ----------- // You must use only one for loop // You are NOT allowed to use a hardcoded literal 4 or 5 for row col length // You must always use expressions like matrix.length or matrix.length-1 // or matrix[row].length -1 etc. // Do not use a literal number except the number "1" as in length-1 etc private static void fillDiagonal_1( int[][] matrix ) { // YOUR CODE HERE } // You must use only one for loop // You are NOT allowed to use a hardcoded literal 4 or 5 for row col length etc // You must always use expressions like matrix.length or matrix[row].length etc. private static void fillDiagonal_2( int[][] matrix ) { // YOUR CODE HERE } // You may use as many as 4 (four) for loops // You are NOT allowed to use a hardcoded literal 4 or 5 for row col length etc // You must always use expressions like matrix.length or matrix[row].length etc. private static void fillBorder( int[][] matrix ) { // YOUR CODE HERE } // Use a nested for loop (for loop in a for loop) to set every elemnt to zero // HINT: Look at the code in printMatrix - it's identical to what you need here except // instead of printing matrix[row][col], you assign it a 0. You may use literal 0 of course here. private static void zeroMatrix( int[][] matrix ) { // YOUR CODE HERE } // - - - - - - - - - - D O N O T M O D I F Y. U S E A S I S ---------------- // IMPORTANT: matrix.length produces the number of rows. The num of rows IS the length of matrix // IMPORTANT: matrix[i].length produces the number of columns in the i'th row private static void printMatrix( String label, int[][] matrix ) { System.out.println(label); for (int row=0 ; row

The output of your program should look like this: DIAGONAL 1 0 0 0 0 4 0 0 0 3 0 0 0 2 0 0 0 10 0 0 ZEROS DIAGONAL 2 0 10 0 0 0 0 2 0 0 000 3 0 00 0 0 4 ZEROS BORDER 0 12 3 4 1 0 0 0 3 3 00 0 1 4 3 2 1 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

More Books

Students also viewed these Databases questions

Question

=+ c. a company president deciding whether to open a new factory

Answered: 1 week ago