Question: Could someone help me Re-implement the draw method below so that it iterates over the ArrayList of primitives and calls draw. The resulting code should

Could someone help me Re-implement the draw method below so that it iterates over the ArrayList of primitives and calls draw.

The resulting code should be a loop with one statement and no instanceOf calls.

Consider using a foreach loop.

Thanks!

public static void draw(ArrayList primitives) {

for (Primitive primitive : primitives) { if (primitive instanceof Text) ((Text)primitive).draw(ui); else if (primitive instanceof Square) ((Square)primitive).draw(ui); else if (primitive instanceof Rectangle) ((Rectangle)primitive).draw(ui);

else if (primitive instanceof Triangle) ((Triangle)primitive).draw(ui);

else if (primitive instanceof Circle) ((Circle)primitive).draw(ui);

else if (primitive instanceof Oval) ((Oval)primitive).draw(ui);

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!