Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that asks the users to enter an integer number n. You then

ID: 3826238 • Letter: W

Question

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: 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: 4

Explanation / Answer

CODE :

package com.cts.randomTestProgram;

import java.util.Scanner;

public class ArrayTest {

   public float[][] fill(int n) {
       float[][] data = new float [n][n];
       Scanner in = new Scanner(System.in);
       for (int row = 0; row < n; row++) {
           for (int col = 0; col < n; col++) {
               System.out.println("enter the elements in (" +row+","+col+") for the Matrix");
               data[row][col] = in.nextFloat();
           }
           System.out.println();
       }

       for (int row = 0; row < n; row++) {
           System.out.println(" ");
           for (int col = 0; col < n; col++) {
               System.out.print(data[row][col]+" ");
           }
           System.out.println();
       }
       return data;
   }

   public static void main(String[] args) {

       Scanner s = new Scanner(System.in);
       System.out.println("Enter the Number : ");
       int n = s.nextInt();
       ArrayTest arrT = new ArrayTest();
       float[][] arr= arrT.fill(n);
      
       float sum = getSum(arr);
      
       float max = getMaximum(arr);
      
       System.out.println("The SUM of the numbers in the Diagonal of the array is : "+sum);
       System.out.println("The maximum of the numbers in the upper triangle of the array is : "+max);
   }

   private static float getMaximum(float[][] arr) {
      
       int length = arr.length;
      
       float max = 0;
      
       for (int row = 0; row < length; row++) {
           for (int col = 0; col < length; col++) {
               if(row<=col)
               {
                   float val =arr[row][col];
                   if(val>=max)
                       max = val;
               }
           }
           System.out.println();
       }
      
       return max;
      
   }

   private static float getSum(float[][] arr) {
      
       int length = arr.length;
       float sum =0;
      
       for(int i =0 ; i<length;i++)
       {
           sum = sum+arr[i][i];
       }
      
      
       return sum;
      
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote