简体   繁体   English

用Java构造一个类

[英]Construction of a class in java

I should carry out this exercise in the creation of a class, I uploaded this is the professor's solution, in sum and product methods can not quite figure out what place and why use "A". 我应该在创建班级时进行此练习,我上传了这是教授的解决方案,总之和乘积方法无法完全弄清楚在什么地方以及为什么使用“ A”。

class Vettore {
    private int[] V = new int[6];
    public Vettore(int[] X) {
        if (X.length != 6)
            throw new BadDataException();
        for (int i = 0; i < 6; i++)
            if (X[i] < 0)
                throw new BadDataException();
            else
                V[i] = X[i];
    }
    public Vettore() {}
    public Vettore somma(Vettore X) {
        int[] A = new int[6];
        for (int i = 0; i < 6; i++)
            A[i] = V[i] + X.V[i];
        return new Vettore(A);
    }
    public Vettore prodotto(Vettore X) {
        int k = 0;
        for (int i = 0; i < 6; i++)
            k += V[i] * X.V[i];
        return k;
    }
    public int get(int i) {
        if (i < 0 || i > 5)
            throw new BadDataException();
        return V[i];
    }
    public String toString() {
        String t = "( ";
        for (int i = 0; i < 6; i++)
            t += V[i] + (i == 5 ? " " : ", ");
        return t + ")";
    }
    public boolean equals(Vettore X) {
        for (int i = 0; i < 6; i++)
            if (V[i] != X.V[i])
                return false;
        return true;
    }
}

As far as I see it, and assuming somma means sum and prodotto means product , The A is needed because you have to store the sum values of the V and XV arrays for every index . 据我somma ,假设somma表示sumprodotto表示product ,则需要A ,因为您必须为每个索引存储VXV数组的和值。 If you didn't use another array for this, you wouldn't be able to achive adding the appropriate indexes in somma for example. 如果您不为此使用另一个array ,那么您将无法例如在somma添加适当的索引。 This method stands for - as I see it - Adding the two arrays' appropriate elements . 正如我所看到的,此方法代表Adding the two arrays' appropriate elements

EDIT: another thing. 编辑:另一件事。 Are you sure that the return types match variables to return? 您确定返回类型与要返回的变量匹配吗? I elaborated the use of somma but didn't pay attention that prodotto has a wrong return type, just as it was said in the comments. 我已经详细说明了somma的用法,但没有注意到prodotto的返回类型错误,就像评论中所说的那样。

  1. You might want to correct the prodotto method definition as - 您可能需要将prodotto方法定义更正为-

     public Vettore prodotto(Vettore X) { int[] K = new int[6]; // deault values are 0 for (int i = 0; i < 6; i++) K[i] += V[i] * XV[i]; return new Vettore(K); } 

This would evaluate the product of the array field V for two instances of class Vettore namingly X the input param and the current instance that you would call the method from. 这将为Vettore类的两个实例评估数组字段VVettore命名为X输入参数和从中调用该方法的当前实例。

  1. return new Vettore(K); creates a new instance of Vettore class with K as arrays field, while executing the constructor logic in place as follows - 使用K作为数组字段创建Vettore类的新实例,同时按如下方式执行构造函数逻辑-

     public SumAndProductExercise(int[] X) { if (X.length != 6) { // length of the array is 6 or not throw new BadDataException(); } for (int i = 0; i < 6; i++) { if (X[i] < 0) { // all the elements of array are >=0 or not throw new BadDataException(); } else { V[i] = X[i]; // the field on the new instance } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM