【发布时间】:2018-04-16 19:50:26
【问题描述】:
我正在解决以下问题:
编写一个只有一个实例变量的对象类,一个整数数组。您的构造函数应该将整数数组作为其唯一参数。编写所有访问器、修改器、toString 和 equals 方法。还要编写一个额外的 RECURSIVE 方法,该方法返回数组中所有元素的总和(您的方法应该有一个数组作为它的参数,并且在一般情况下应该在其返回中包含递归调用)。编写一个客户端类来测试你所有的方法,尤其是你的递归方法。
由于某种原因,我的 toString 方法有一个错误,我的递归方法也有一个错误,它应该返回数组的总和。我可能无法使用其中的一些代码,因此不胜感激。
这是我目前所拥有的......
客户端类(用于测试方法)
package weeklylab9;
public class Client {
public static void main(String[] args) {
ObjectClass numArray = new ObjectClass; //trying to create new object so I can use toString method
numArray.toString(); //calling toString method?
}
}
对象类
package weeklylab9;
import java.util.Arrays;
import java.util.stream.*;
public class ObjectClass {
int[] numArray = {10,10,20,20,40};
public int[] getArray() { //this is my accessor method
return numArray;
}
public int[] setArray(int[] newa) { //this is my mutator method
this.numArray = newa;
return this.numArray;
}
public String toString() { //this is my toString method
Arrays.toString(int[], numArray);
}
public int recursionSum(int[] numArray) { //this should be a recursion method that returns sum of array elements
int sum = IntStream.of(numArray).sum();
System.out.println("The sum is : " + sum);
}
}
【问题讨论】:
-
我讨厌老师布置作业要求学生使用递归解决非递归问题。它导致认为递归是一切的解决方案。正则表达式相同。
-
您还没有告诉我们错误是什么;也请更新您的帖子并更正代码格式。
-
在 toString() 方法中我收到错误消息:-语法错误,插入“.class”以完成参数列表-类型数组中的方法 toString(int[]) 不适用于arguments (Class
, int[]) -line breakpoint:ObjectClass [line 20] - toString() -
...错误说明了什么?
-
抱歉,我一直按 Enter 而不是 shift+enter,
标签: java arrays object recursion