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

(The Location class) Design a class named Location for locating a maximal value

ID: 3621504 • Letter: #

Question

(The Location class) Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximal value and its indices in a two dimensional array with row and column as int type and maxValue as double type.

Write the following method that returns the location of the largest element in a two-dimensional array.

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

The return value is 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. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array. Here is a sample run:

Enter the number of rows and columns of the array: 3 4
Enter the array:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at (1, 2)

Explanation / Answer

this is a better solution than the one above

//3 4    23.5 35 2 10   4.5 3 45 3.5   35 44 5.5 9.6

import java.util.Scanner;
class Location {
    static int row, col;
    static double maxValue = 0;

    Location(int row, int col, double maxValue){
        this.row = row;
        this.col = col;
        this.maxValue = maxValue;
    }
  
    public static Location locateLargest(double[][] a) {
        int maxRow = 0, maxCol = 0, cl;
      
        for (int rw = 0; rw < a[0].length - 1; rw++) {
            double currMax = a[rw][0];
            int currMaxRow = rw;
            int currMaxCol = 0;
            for (cl = 0; cl < a[rw].length; cl++) {
                if (currMax < a[rw][cl]) {
                    currMax = a[rw][cl];  
                    currMaxRow = rw;
                    currMaxCol = cl;
                }
            }
            if (maxValue < currMax) {
                maxRow = currMaxRow;
                maxCol = currMaxCol;
                maxValue = currMax;
            }
        }
        return new Location(maxRow, maxCol, maxValue);     
    }  
}

public class Exercise09_13 {
    public static void main(String[] args) {
        double maxValue = 0;
        Location location;
        Scanner input = new Scanner(System.in);
      
      
        System.out.println("enter the number of rows and columns in the array: ");
        int row = input.nextInt();
        int col = input.nextInt();
        double[][] list = new double[row][col];
        int cl, rw;
      
        System.out.println("enter the array");
      
        for (rw = 0; rw < row; rw++)
            for (cl = 0; cl < col; cl++)
                list[rw][cl] = input.nextDouble();
      
                
        new Location(row, col, list[row - 1][col - 1]);
        Location.locateLargest(list);
      
        System.out.println(" ");
      
        System.out.printf("the location of the largest element is %.2f at "
                + "(%d, %d)", Location.maxValue, Location.row, Location.col);
    }
}