In: Computer Science
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 stores 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 locate Largest(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 location of the largest element in the array.
Complete the program "The Location Class" - 9.13. Page 366.
Add to your class a public Class Level Variable named COUNT to keep counting how many objects have been created (instantiated); So, each time you are creating new object the Count var will be incrementing by 1
.In the main method create 3 objects, call all their methods, and print the final value of the Count var.
This is java language
JAVA PROGRAM
///////////Location.java//////////////////
package test.location;
public class Location {
//instance level variables
public int row,column;
public double maxValue;
//class level COUNT variable
public static int COUNT = 0;
//constructor
public Location(){
COUNT = COUNT+1;//increase
COUNT
}
/**
* Find the largest value and its position from the 2D
array
* return ocation instance with the above values
* @param a
* @return
*/
public static Location locateLargest(double[][]
a){
double max = a[0][0];
int maxRowIndex = 0;
int maxColIndex = 0;
for(int i = 0 ; i < a.length ;
i++){
for(int j = 0; j
< a[i].length; j++){
if(a[i][j] > max){
max = a[i][j];
maxRowIndex = i;
maxColIndex = j;
}
}
}
//create the location
instance
Location l = new Location();
l.maxValue = max;
l.row = maxRowIndex;
l.column = maxColIndex;
return l;//return location
instance
}
@Override
/**
* print current location instance
*/
public String toString() {
return "[rowIndex=" + row + ",
columnIndex=" + column + ", maxValue=" + maxValue + "]";
}
}
///////////TesstMain.java//////////////////
package test.location;
import java.util.Scanner;
public class TestMain {
public static void main(String[] args){
Scanner sc = new
Scanner(System.in);
TestMain tm = new TestMain();
//create 3 2d arrays from
user
double[][] dblArr1=
tm.get2DArrayFromUser(sc);
double[][] dblArr2=
tm.get2DArrayFromUser(sc);
double[][] dblArr3=
tm.get2DArrayFromUser(sc);
//call the class level
locateLargest method of location ckass to get location
instance
//to get the location of the max
valued index
Location l1 =
Location.locateLargest(dblArr1);
Location l2 =
Location.locateLargest(dblArr2);
Location l3 =
Location.locateLargest(dblArr3);
//print the location received
//the location instance's
toString
System.out.println("Location1=>"+l1);
System.out.println("Location2=>"+l2);
System.out.println("Location3=>"+l3);
System.out.println("Final count of
the COUNT var: "+Location.COUNT); //print the class level COUNT
variable
sc.close();//close scanner
}
/**
* The method get2DArrayFromUser will take number of
ros and columns from
* user and populate it will double values taken from
user
* @param sc
* @return
*/
private double[][] get2DArrayFromUser(Scanner
sc){
System.out.println("Enter number of
rows:");
int rows = sc.nextInt(); //read
rows
System.out.println("Enter number of
columns:");
int cols = sc.nextInt(); //read
cols
double[][] arr = new
double[rows][cols];
for(int i = 0; i < rows;
i++){
for(int j = 0; j
< cols; j++){
arr[i][j] = sc.nextDouble();//populate the 2D
array
}
}
return arr; //return array
}
}
===============================================
OUTPUT
=================================================
Enter number of rows:
3
Enter number of columns:
1
45.2
25.6
2.36
Enter number of rows:
2
Enter number of columns:
2
12.6
36.21
21.25
45.65
Enter number of rows:
3
Enter number of columns:
2
12.56
56.12
18.56
65.18
17.56
65.71
Location1=>[rowIndex=0, columnIndex=0, maxValue=45.2]
Location2=>[rowIndex=1, columnIndex=1, maxValue=45.65]
Location3=>[rowIndex=2, columnIndex=1, maxValue=65.71]
Final count of the COUNT var: 3