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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started