0

So this is my code:


public Shape(ArrayList<Double> s, String c, boolean str) {
        numOfSides = s.size(); 
        sides = s;
        color = c;
        striped = str;
    }

    //gets:
    public int getNumOfSides(){
        return numOfSides;
    }
    public ArrayList<Double> getSides(){
        return sides;
    }
    public String getColor(){
        return color;
    }
    public boolean getStriped(){
        return striped;
    }
    
    //sets:
    public void setNumOfSides(int i){
        numOfSides = i;
    }
    public void setSide(int i, double j){
        sides.set(i,j);
    }
    public void setColor(String i){
        color = i;
    }
    public void setStriped(boolean i){
        striped = i;
    }
    
    //methods:
    public double perimeter(){
        double peri = 0;
        for(int i = 0; i < numOfSides; i++){
            peri+=sides.get(i);
        }
        return peri;
    }
    
    public String toString(){
        return "shape has " + numOfSides + " sides, is the color " + color + " and is " + striped + " that it is striped.";
    }
}

class Rectangle extends Shape{
    private double length;
    private double width;
    
    public Rectangle(){
        length = 1.0;
        width = 1.0;
    }
    public Rectangle(double l, double w){
        super(ArrayList<Double>(){l,w,l,w}, "White", false);
        length = l;
        width = w;
    }
}

The relevant part is the last method in the code.

I’m trying to use the length and width of the rectangle as the inputs to the super constructor, but I can’t find how to do the syntax, and java won’t let me make a new ArrayList first and then call the constructor, because the super must be the first line in a constructor. How do I do this?

Ari Yeger
  • 1
  • 1
  • 1
    `new ArrayList<>(Arrays,asList(l,w,l,w))` - with Java 9 or newer `new ArrayList<>(List.of(l,w,l,w))` - BTW it would be *better* if the Shape class was declared/implemented to use `List` ... see ["Program to an interface". What does it mean?](https://stackoverflow.com/q/1992384/16320675) – user16320675 Feb 14 '22 at 16:40

0 Answers0