2014-03-13 105 views
0

我不知道如何解釋這一點,但我會嘗試。我的任務是製作一個名爲createSphere的控制器類,它將創建一個球體對象,然後將其添加到我的數組列表中。下面是我的課我需要創建一個控制器類來實現一個方法,該方法將創建一個繼承自一個抽象類的對象

球類有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; 
} 
} 

終於控制器類,我不知道是做什麼用

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; 
} 

} 

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 
} 
} 

回答

0

我不確定我瞭解你的問題。 如果你只是必須使用控制器類球體添加到您的ArrayList這已經足夠了:

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

}

+0

我來試試。感謝您的建議! – WhoYou

+0

它的工作,謝謝你!我不知道爲什麼我不明白這一點。 – WhoYou

+0

我很高興它幫助! – Karura91

相關問題