【问题标题】:How to set up constructor and methods with ArrayList?如何使用 ArrayList 设置构造函数和方法?
【发布时间】:2014-12-14 07:44:59
【问题描述】:

我正在尝试了解构造函数和方法的工作原理。我几乎已经掌握了这些概念,只是我不知道如何设置它们。我特别不知道如何将它们与 ArrayLists 一起使用。

我正在尝试计算和打印矩形的面积,注释部分是我不知道如何设置的部分。

import java.util.ArrayList;
class RectangleStats
{
// Private instance variables
// length is an ArrayList which will contain integer values, width is an array which will contain integer
// values, and area is an array which will contain decimal values.

//code goes here for instance variables goes here


// The constructor for the RectangleStats class takes an ArrayList and an array as parameters for
// length and width, respectively.

// code for constructor goes here

// The calcRectangleArea() method calculates the area of rectangles using the length and
// the width assigned to the private instance variables and assigns the results to the area array of type
// double. This method does not return anything.

// code for the calcRectangleArea() method goes here

// The printArea() method prints the values assigned to the area array using the most appropriate
// type of loop. This method does not return anything.

// code for the printArea() method goes here
}

// The RectangleStatsTesterClass assigns the length of two rectangles to an ArrayList and assigns the
// width of two rectangles to an array. The calcRectangleArea() and printArea() methods are invoked to
// calculate and print the area of the two rectangles.
public class RectangleStatsTesterClass2
{
public static void main(String[] args)
{
ArrayList intLength = new ArrayList();
intLength.add(7);
intLength.add(5);
int [ ] intWidth = {3, 4};

RectangleStats rectStats = new RectangleStats(intLength, intWidth);
rectStats.calcRectanglePerimeter();
rectStats.printArea();
}
}

【问题讨论】:

  • 这是一个相当广泛的问题。请参阅这篇文章,让您对如何使用面向对象的编程概念有一个很好的了解。 stackoverflow.com/questions/26676160/…
  • 这又是一个糟糕的设计。为什么要使用列表作为宽度,使用数组作为长度?为什么不一致?另外,与其分别存储宽度和长度,不如定义一个具有宽度和长度的类 Rectangle,定义一个 perimeter() 和一个 area() 方法,并简单地将 Rectangle 对象的集合传递给上述类。它所要做的就是遍历矩形并求和它们的面积/周长。那将是正确的面向对象设计。真的,你的老师在 OO 设计方面很糟糕。
  • 大声笑,我可能会在下学期重修 MOOC 或其他课程。有没有什么地方我可以看到更合适的面向对象设计?

标签: java methods constructor


【解决方案1】:

试试这样:

import java.util.ArrayList;
import java.util.List;
class RectangleStats
{

    private List<Integer> length;

    private int[ ] width;

    double areas[];

    RectangleStats(List<Integer> length, int[] width){
        this.length = length;
        this.width = width;
        this.areas = new double[this.width.length];
    }

    private void calcRectangleArea(){
        for(int i=0; i<width.length; i++){
            areas[i] = length.get(i) * width[i];
        }
    }

    public void printArea(){
        calcRectangleArea();
        for(int i=0; i<areas.length; i++){
            System.out.println("Area " + i + " : " + areas[i]);
        }
    }

    public void calcRectanglePerimeter(){
        for(int i=0; i<width.length; i++){
            System.out.println("Parameter for "+ i +"th Rectangle : " + (2 * (length.get(i) + width[i])));
        }
    }
}

public class RectangleStatsTesterClass2
{
    public static void main(String[] args)
    {
        ArrayList<Integer> intLength = new ArrayList<Integer>();
        intLength.add(7);
        intLength.add(5);
        int [ ] intWidth = {3, 4};

        RectangleStats rectStats = new RectangleStats(intLength, intWidth);
        rectStats.calcRectanglePerimeter();
        rectStats.printArea();
    }
}

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 2013-10-21
    • 1970-01-01
    • 2017-01-03
    • 2015-02-06
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多