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

java 3. Design a class named Location for locating a maximal value and its locat

ID: 3702948 • Letter: J

Question

java

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

Write the following method that returns the location of the largest element in a two – 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 display the location of the largest element in the array. Here is the sample run:

Enter the number of rows and columns in 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 45 is at (1, 2)

3. Design a class named Location for locating a maximal value and its location in a two- dimensional array. The class contains public data field's row, column, and maxValue that store the maximal value and its indices in a two-dimensional array with row and column as int types and maxValue as a double type Write the following method that returns the location of the largest element in a two - 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 display the location of the largest element in the array. Here is the sample run: Enter the number of rows and columns in 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 45 is at (1,2)

Explanation / Answer

Location.java

public class Location {

public int row,column;

public double maxValue;

public Location(int row, int column, double maxValue){

this.row = row;

this.column =column;

this.maxValue = maxValue;

}

}

TestLocation.java

import java.util.Scanner;

public class TestLocation {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

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

int rows = scan.nextInt();

int cols = scan.nextInt();

double d[][] = new double[rows][cols];

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

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

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

d[i][j] = scan.nextDouble();

}

}

Location l = locateLargest(d);

System.out.println("The location of the largest element is "+l.maxValue+" at ("+l.row+", "+l.column+")");

}

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

double max = a[0][0];

int row =0 , col=0;

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

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

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

max = a[i][j];

row = i;

col = j;

}

}

}

Location l = new Location(row,col,max);

return l;

}

}

Output:

Enter the number of rows and columns in 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 45.0 at (1, 2)