2. Make any corrections for the following codes if necessary import java.util.Ra
ID: 3754937 • Letter: 2
Question
2. Make any corrections for the following codes if necessary import java.util.Random; class Shape void drawO t void eraseO class circle extends Shape void drawO f System.out.println("circle.drawO") void erase) System.out.println("Circle.eraseO") class square extends Shape t void drawO System.out.println(" Square.drawO"): void eraseO System.out.println("Square.eraseO") class Triangle extends Shape t void draw System.out.printlnCTriangle.drawO"); void erase) System.out.printinC"Triangle.eraseO") // "factory" that randomly creates shapes: class RandomshapeGenerator t private Random rand new RandomO; public shape nextO switch (rand.nextInt(3)) default: case 0: return new circleO return new SquareO return new TriangleO; case 1: case 2: Page 2Explanation / Answer
The code looks good to me. The only thing which I think could have been better is that we can make the class Shape an Interface and all shapes like Circle, Square and Rectangle should implement it.
Hence, the class Shape would look like:
interface Shape {
void draw();
void erase();
}
And the shape class headers should look like:
class Circle implements Shape {
............
}
class Square implements Shape {
............
}
class Triangle implements Shape {
............
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.