Here is a problem im working on for my intro to java class:
1.Design a class named Location for locating a maximal value and its location in a twodimensional
array. The class contains:
2.Private data fields row and column that store the indices it a two dimensional
array as int type.
3.A no-arg constructor that creates a location with default values.
4.A constructor with the arguments for row and column.
5.The method named locateLargest(double [][] x) that returns the location of the
largest element in a two-dimensional array. The return value is an instance of
Location. This method should also print the largest value of the array.
2/3
The method is: public Location locateLargest(double [][] x) {}
6.A toString() method that prints a Location object in the form [row][column].
My code:
package classAssignment; import java.util.Scanner; public class Location { private int row; private int column; private int[][] array = new int[row][column]; public static void main(String[] args) { Scanner numberInput = new Scanner(System.in); System.out.println("Enter the number of rows and columns of the array: "); int row = numberInput.nextInt(); int column = numberInput.nextInt(); Location l1 = new Location(row, column); }//end main Location(){}//end constructor Location(int row, int column){ this.row = row; this.column = column; }//end arg constructor public Location locateLargest(double[][] x){ double max = 0; for (int i = 0; i < x.length; i++){ for (int j = 0; j < x.length; i ++){ if (x[i][j] > max) max = x[i][j]; } } return x; }//end locateLargest() public int getRow(){ return this.row; }//end getRow() public int getColumn(){ return this.column; }//end getColumn() }//end class
My problem is in the locateLargest method. I can't return the max because it is of type location and I have no idea how to convert it into a double. I've even tried declaring the array in multiple other places like in another class or in the locateLargest method itself but those ideas didn't work either. Any help would be appreciated.