For class assignment, I have several class,
and I have problem with Composite class and Picture class.
it said Composite class have only one constructor with no argument,
but it need chain constructor in Picture class.
this is my Composite class
public class Composite extends Picture {
private ArrayList<Picture> components;
public Composite (){
//I have no idea how to chain.
}
...
and this is Picture's constructor
public abstract class Picture {
private PicPoint base;
public Picture(PicPoint base) {
this.base = base;
}
...
For chaining, my assignment instruction said
"A Composite is simply a collection of pictures which all take care of their own position. So, while Composite must have a base point, it really doesn’t mean anything"
"Composite will have a single constructor with no arguments. However, the constructor needs to chain with the constructor for Picture, which requires a point. Pass the PicPoint with coordinates NaN and NaN to the Picture constructor"
But I have no idea how to chain constructor.
P.S. this is part of method from test class.
PicPoint p0 = new PicPoint(0,0);
PicPoint p1 = new PicPoint(3,0);
PicPoint p2 = new PicPoint(0,4);
Triangle t1 = new Triangle(p0,p1,p2);
Circle c1 = new Circle(p1, 2);
Rectangle r1 = new Rectangle(p2, 3, 4);
Composite cmp1 = new Composite();
cmp1.getComponents().add(t1);
cmp1.getComponents().add(c1);
cmp1.getComponents().add(r1);
PicPoint p3 = new PicPoint(17, 23);
PicPoint p4 = new PicPoint(-14,-5);
PicPoint p5 = new PicPoint(3, -18);
Triangle t2 = new Triangle(p3,p4,p5);
Circle c2 = new Circle(p4, 2);
Rectangle r2 = new Rectangle(p5, 3, 4);
cmp2 = new Composite();
cmp2.getComponents().add(t2);
cmp2.getComponents().add(c2);
cmp2.getComponents().add(r2);
cmp2.getComponents().add(cmp1);
/*I tried
public Composite(){
super(new PicPoint(0, 0));
}
and that test class gave me tons of nullPointException*/
The idea behind constructor chaining is that when a child object is instantiated, its constructor makes a call to its parent constructor. A good way to see this is that the child constructor only handles the logic that is unique to the child, while the super call handles all other logic.
Based on my understanding of the problem, Composite's constructor should look something like this:
public Composite() {
super(new PicPoint(Double.NaN, Double.NaN)); //parent constructor call;
// any other logic that is specific to the child class Composite
}