1
我需要构建一个方法来查找给定矩形区域中矩形区域的总和。访问ArrayList中对象的属性
public class Homework {
public static void main(String[] args) {
ArrayList<Rectangle> test = new ArrayList<Rectangle>();
test.add(new Rectangle(10, 20, 30, 40));
test.add(new Rectangle(10, 10, 30, 40));
test.add(new Rectangle(20, 20, 40, 50));
test.add(new Rectangle(20, 10, 50, 30));
System.out.println(areaSum(test));
System.out.println("Expected: 5900.0");
System.out.println(areaAverage(test));
System.out.println("Expected: 1475.0");
}
public static double areaSum(ArrayList<Rectangle> rects){
//work goes here
}
我至今是:
public static double areaSum(ArrayList<Rectangle> rects){
double totalArea = 0;
for (Rectangle r : rects) {
double area = (x + width) * (y + height);
totalArea = totalArea + area;
}
return totalArea;
}
我怎么能拉矩形的尺寸通过测块建造的ArrayList
?我对这个领域很陌生,这是一项家庭作业,我似乎无法弄清楚。