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

Please write this Java program as simple as possible and with comments throughou

ID: 3733233 • Letter: P

Question


Please write this Java program as simple as possible and with comments throughout explaining what is happening. Thank you Exercise 3: Design and implement a Java program for programming exercise 8.13, page 310 (name it LocateLargestElement) as described in the problem statement. Write method locateLargest) as specified. The method locateLargest() must be designed to handle two-dimensional arrays with any number of rows and columns. Notice that this method retuns a one-dimensional array that contains two elements. These two elements indicate the row and column indices of the largest element in the two-dimensional array. To test this method, the main method of your program prompts the user to enter a two-dimensional array and displays the location of the largest element in the array. Use the sample run to format your output. Design the main method of your program to handle all input and output and to allow the user to re-run the program with different sets of inputs (i.e., use a loop). Document your code and organize the output using appropriate formatting techniques

Explanation / Answer

import java.util.Scanner;

public class LocateLargestElement {

   public static int[] locateLargest(double[][] a) {

       int[] loc = new int[2];

       double max = a[0][0];

       for(int i=0; i<a.length; i++) {

           for(int j=0; j<a[i].length; i++) {

               if(max < a[i][j]) {

                   max = a[i][j];

                   loc[0] = i;

                   loc[1] = j;

               }

           }

       }

       return loc;

   }

   public static void main(String[] args) {

      

       int r, c;

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter the number of rows and columns of the array: ");

       r = sc.nextInt();

       c = sc.nextInt();

      

       double[][] a = new double[r][c];

       System.out.println("Enter the array: ");

      

       for(int i=0; i<a.length; i++) {

           for(int j=0; j<a[i].length; i++) {

               a[i][j] = sc.nextDouble();

           }

       }

      

       int[] loc = locateLargest(a);

      

      

       System.out.println("The location of the largest element is at ("+loc[0]+","+loc[1]+")");

   }

}