我需要创建一个控制器类来实现一个方法,该方法将创建一个从抽象类继承的对象

时间:2014-03-13 15:55:34

标签: arraylist controller

我不知道如何解释这一点,但我会尝试。我的任务是创建一个控制器类,该类具有一个名为createSphere的方法,该方法将创建一个球体对象,然后将其添加到我的arraylist中。这是我的课程

球体类

public class Sphere extends ThreeDShape {

private double radius;
private double surfaceArea;
private double volume;

public Sphere(int x, int y, int z, double radius) {
    super(x, y, z);
    this.radius = radius;
}

public double getRadius() {
    return this.radius;
}


public boolean equals(ThreeDShape aShape) {
    Sphere aSphere = new Sphere(1, 2, 3, 5);
    return aSphere.equals(aShape);
}

public String toString() {
    return "This sphere has a radius of: " + this.radius + " and is located at ("
            + this.getX() + ", " + this.getY() + ", " + this.getZ() + ")";
}

@Override
public double getSurfaceArea() {
    return this.surfaceArea;
}

@Override
public double getVolume() {
    return this.volume;
}

}

具有arraylist的类

public class ShapeManager {


private ArrayList<ThreeDShape> shapes;

public ShapeManager() {
    this.shapes = new ArrayList<ThreeDShape>();
}

public void addShape(ThreeDShape newShape) {
    this.shapes.add(newShape);
}

public void addSphere(Sphere newSphere) {
    this.shapes.add(newSphere);
}

public ArrayList<ThreeDShape> getShapes() {
    return this.shapes;
}

public String toString() {
    if (this.shapes.size() < 0) {
        throw new IllegalArgumentException("Cannot have less that zero items in arraylist");
    }

    String results = "";
    for (ThreeDShape aShape : this.shapes) {
        results += aShape.toString();
    }
    return results;
}
}

最后是我不知道如何处理

的控制器类
import edu.westga.cs1302.project03.model.ShapeManager;

public class ShapeController {

private ShapeManager manager = new ShapeManager();

public void createSphere(int x, int y, int z, double radius) {
    // TODO : code
}
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解了你的问题。 如果你只需要使用Controller类向arrayList添加一个球体,那就足够了:

public void createSphere(int x, int y, int z, double radius) {
    this.manager.addSphere(new Sphere(x,y,z,radius));

}

相关问题