In the following exercise you are not premitted to \"plink\" values into the mat
ID: 3777580 • Letter: I
Question
In the following exercise you are not premitted 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.
- Do not change main
- Just fill in these methods below main
zeros()
diagonal_1()
diagonal_2()
border()
-Don't change any other code or write any other methods
OUTPUT LOOKS LIKE:
DIEGON 00004 00030 00200 01000 00000 ZEROS 00000 00000 00000 00000 00000 DIAGONAL 2 00000 01000 00200 00030 00004 ZEROS 00000 00000 00000 00000 00000 BORDER 01234 10003 20002 30001 43210 00 2 1 -40000 00000 -00004 00000 43210 03000 00000 00030 00000 30001 O00200 S00000 000200 S000 00 E20002 00010 00000 01000 00000 10003 D00000 z00000 D00000 Z000 00 B01234Explanation / Answer
/*
Exercise1.java
*/
import java.io.*;
import java.util.*;
class Exercise1
{
public static void main( String[] args )
{
try // We will learn about Exceptions soon
{
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
*/
}
catch( Exception e )
{
System.out.println("EXCEPTION CAUGHT: " + e );
System.exit(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 )
{
for(int i=0;i<=4;i++)
{
for(int j=4;j>=0;j--)
{
if(i+j == 4)
matrix[i][j] = j;
}
}
}
// 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 )
{
for(int i=0;i<=4;i++)
{
for(int j=0;j<=4;j++)
{
if(i==j)
matrix[i][j] = i;
}
}
}
// 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 )
{
int i=0;
for(int j=0;j<=4;j++)
{
matrix[i][j] = j;
}
int j=0;
for(i=0;i<=4;i++)
{
matrix[i][j] = i;
}
j=4;
int k =j;
for(i=0;i<=4;i++)
{
matrix[i][j] = k--;
}
i=4;
k = i;
for(j=0;j<=4;j++)
{
matrix[i][j] = k--;
}
}
// 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 )
{
for(int i=0;i<=4;i++)
{
for(int j=4;j>=0;j--)
{
matrix[i][j] = 0;
}
}
}
// - - - - - - - - - - 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<matrix.length ; ++row) // matrix.length is the number of rows
{
for (int col=0 ; col < matrix[row].length ; ++col )
System.out.print( matrix[row][col] + " ");
System.out.println(); // newline after each row
} // END FOR EACH ROW
System.out.println(); // puts a blank line betwen next printout
} // END PRINTMATRIX
} // END CLASS
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.