Question: 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 ,

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 and maxValue that store the maximal value and its indices in a 2-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 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 its location (row and column) referencing the Location objects 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)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!