In: Computer Science
Design a class named Location for locating a maximal value and its location in a 2-dimensional array. The class contains public data fields row, column andmaxValue that store the maximal value and its indices in a 2-dimensional array with row andcolumn as int types andmaxValue as a double type
Write the following method that returns the location of the largest element in a 2-dimensional array : public static Location locateLargest(double[][] a)
The return value is an instance of Location. Write a test program that prompts the user to enter a two-dimensional array and displays the locations of the largest element in the array.
Algorithm:
Create the Location class with data fields row, column, maxValue.
In main method of your test program called “Inclass09A, get number of rows and columns in the array from the console.
Create the array.
Ask the user to enter the values for the array and populate the array.
Create/Invoke the locateLargest method, passing it the array as an argument.
In the locateLargest method, create a Location object, then save the first element in the 2-dim array as the largest. Then traverse the array saving any element larger than the previous largest element and the associated row and column.
The locateLargest method returns a Location object that contains the maxValue, row, and column of the maxValue. Assign the Location object into a reference variable of the type Location.
Print out a message that indicates the largest element, and it’s location (row and column) referencing the Location object’s instance variables.
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 largest element, 45.0, is located at (1, 2)
Sample Run:
---------------------------------------------------
Enter the number of rows and columns of the array: 2 3
Enter the array:
1 2 3
4 4 3
The largest element, 4.0, is located at (1, 0)
import java.util.Scanner;
class Location{
public int row;
public int column;
public double maxValue;
}
public class Inclass09A{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int r, c;
System.out.print("Enter the number of rows and columns of the array: ");
r = in.nextInt();
c = in.nextInt();
System.out.println("Enter the array:");
double arr[][] = new double[r][c];
for(int i=0; i for(int j=0; j arr[i][j] = in.nextDouble(); } } Location loc = locateLargest(arr); System.out.println("The largest element, "+loc.maxValue+", is
located at ("+loc.row+", "+loc.column+")"); } public static Location locateLargest(double[][] a){ Location loc = new Location(); loc.row = 0; loc.column = 0; loc.maxValue = Double.MIN_VALUE; for(int i=0; i for(int j=0; j
if(loc.maxValue
loc.maxValue = a[i][j]; loc.row = i; loc.column = j; } } } return loc; } }