0
\$\begingroup\$

My professor is super strict and just want to know if I have solved this correctly.

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 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 code is:

public static void main(String[] args) { System.out.println("Please enter the row and column size"); int row = kk.nextInt(); int column = kk.nextInt(); System.out.println("Enter the values between 1 and 10"); double[][] d = getArray(row,column); Location l = locateLargest(d); System.out.println(l.toString()); } public static double[][] getArray(int row,int column){ double [][] a = new double[row][column]; double input; for (int i=0;i<a.length;i++){ for (int j=0;j<a[i].length;j++){ a[i][j] = kk.nextDouble(); } } return a; } public static Location locateLargest(double[][] a){ int rowIndex=0; int columIndex=0; double max = a[rowIndex][columIndex]; for (int i=0;i<a.length;i++){ for (int j=0;j<a[i].length;j++){ if (a[i][i]>max) { max = a[i][j]; rowIndex=i; columIndex=j; } } } return new Location(rowIndex,columIndex,max); } } class Location{ int row,column; double maxValue; Location(){ } Location(int row,int column,double maxValue){ this.row = row; this.column = column; this.maxValue = maxValue; } public String toString(){ return "The largest value is "+maxValue+" at row "+row+" and column "+column; } } 
\$\endgroup\$
4
  • \$\begingroup\$There is nothing object-oriented in this code, so you should have chosen a better title.\$\endgroup\$CommentedMay 7, 2017 at 6:12
  • 2
    \$\begingroup\$Does that code compile at all?\$\endgroup\$CommentedMay 7, 2017 at 8:18
  • \$\begingroup\$"just want to know if I have solved this correctly" Code posted at Code Review should work to the best of your knowledge. If you don't know, it's not ready for review.\$\endgroup\$
    – Mast
    CommentedMay 7, 2017 at 15:56
  • \$\begingroup\$It is to the best of my knowledge because Im a beginner. This is code review no? Where I can have my code reviewed and receive feedback to gain knowledge. Everybody here likes to make a big deal of all the questions.\$\endgroup\$
    – george129
    CommentedMay 7, 2017 at 16:34

1 Answer 1

1
\$\begingroup\$
  • The class Location must be a public class Location since it doesn't make sense to have a public method with a non-public return type.
  • The fields of the Location class must be public int and public double, since the task description​ says so.
  • The fields of the Location class should be public final int and public final double since they won't change after being initialized once.
  • The no-arguments constructor then contains a compilation error and should be removed completely.
  • You should let the IDE format your code so that it is nice to read: Ctrl+Shift+F in Eclipse, or Ctrl+Alt+L in IntelliJ.
  • Your implementation is completely correct, congratulations.
\$\endgroup\$
3
  • \$\begingroup\$Great tips! Is my formatting wrong? IntelliJ says it's properly formatted\$\endgroup\$
    – george129
    CommentedMay 7, 2017 at 6:18
  • \$\begingroup\$At least the spacing is inconsistent. And there should be a space after each comma.\$\endgroup\$CommentedMay 7, 2017 at 6:26
  • \$\begingroup\$I think the formatting became issue with the copy paste to this site. You should paste it as is here, then selec all and press the {} button at the top of the editor.\$\endgroup\$
    – Imus
    CommentedMay 7, 2017 at 8:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.