QUESTION 1 Write a Java program that asks the users to enter an integer number n
ID: 3708361 • Letter: Q
Question
QUESTION 1 Write a Java program that asks the users to enter an integer number n. You then initialize a two-dimensional array with n rows and n columns and ask the user to fill it with real numbers. You then display the array along with the following information - The sum of all the numbers in the diagonal of the array (in bold red in the following example) - The maximum of the values in the upper triangle of the array (shown in red in the example For example: If the users enters the following array: 0 3.5 4 7.2 42.1 13 2 S 2 1.3 The sum of all the numbers in the diagonal of the array is the sum of 3,3.5, 2.1, and 2: 10.6 The maximum of the values in the upper triangle of the array is: 4Explanation / Answer
Solution:
Please find below the code : MatrixRunner.java
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;
public class MatrixRunner {
private static double [][] matrix;
public static final String RED_BOLD = "u001B[31m"; //For red bold color
public static final String RESET = "u001B[0m"; // Text Reset
public static void main(String [] args){
//declaring scanner
Scanner scanner = new Scanner(System.in);
//taking user input for value of n
System.out.println("Please enter an integer : " );
int n = scanner.nextInt();
matrix = new double[n][n];
//START - Asking user to take input for the values of the matrix
System.out.println("Enter the elementss for the Matrix: ");
for(int row = 0; row < matrix.length; row++){
for(int column = 0 ;column < matrix[row].length; column++){
matrix[row][column] = Double.valueOf(scanner.next());
}
}
//END - Asking user to take input for the values of the matrix
//START - calculating sum of all the numbers in the diagonal
double sum = 0;
double[] diagonalArray = new double[n];
int k=0;
for(int row = 0; row < matrix.length; row++){
for(int column = 0 ;column < matrix[row].length; column++){
if(row == column){
sum = sum + matrix[row][column];
diagonalArray[k++] = matrix[row][column];
}
}
}
//START - calculate the maximum value in the upper triangle of the matrix
double maximum=0;
maximum=matrix[0][0];
for(int row = 0;row < n ; row++) {
for(int column = 0;column < n ; column++) {
if(row <= column) {
if(matrix[row][column] > maximum){
maximum=matrix[row][column];
}
}
}
}
//END - calculate the maximum value in the upper triangle of the matrix
//START - print the matrix in desired format
DecimalFormat format = new DecimalFormat("0.#");
for(int row = 0; row < matrix.length; row++){
for(int column = 0 ;column < matrix[row].length; column++){
System.out.print(RED_BOLD + format.format(matrix[row][column]) + RESET + " ");// = Double.valueOf(scanner.next());
}
System.out.println();
}
//END - print the matrix in desired format
System.out.println("The sum of all the members in the diagonal of the array of :"+ Arrays.toString(diagonalArray) +" "+ sum);
System.out.println("The maximum of the value in the upper triangle of the array is :" + format.format(maximum));
scanner.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.