首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java OOP HomeWork

Java OOP HomeWork
EN

Code Review用户
提问于 2017-05-07 05:52:02
回答 1查看 2.4K关注 0票数 0

我的教授非常严格,只想知道我是否正确地解决了这个问题。

设计一个名为Location的类来定位一个最大值及其在二维数组中的位置。该类包含公共数据字段行、列和maxValue,它们将最大值及其索引存储在二维数组中,行和列作为int类型,maxValue作为双重类型。编写以下方法,返回二维数组中最大元素的位置:

代码语言:javascript
复制
  public static Location locateLargest(double[][] a)

守则是:

代码语言:javascript
复制
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;
    }

}
EN

回答 1

Code Review用户

回答已采纳

发布于 2017-05-07 06:09:08

  • Location必须是public class Location,因为使用非public返回类型的public方法是没有意义的。
  • Location类的字段必须是public intpublic double,因为任务描述​这样说。
  • Location类的字段应该是public final intpublic final double,因为它们在初始化一次后不会更改。
  • 然后,没有参数的构造函数包含一个编译错误,应该完全删除.
  • 您应该让IDE格式化您的代码,这样阅读起来就更好了: Eclipse中的Ctrl+Shift+F或IntelliJ中的Ctrl+Alt+L
  • 恭喜你的实现是完全正确的。
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/162723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档