You need to implement the shapes rectangle and circle and a demonstration progra
ID: 3557974 • Letter: Y
Question
You need to implement the shapes rectangle and circle and a demonstration program. Here is my implementation of a Hexagon: public class Hexagon extends Figure{ int width, height; public Hexagon(){ super(0,0,"none"); setWidth(0); setHeight(0); } public Hexagon(String n, int a, int b, int w, int h){ super(a,b,n); setWidth(w); setHeight(h); } public String toString(){ return "In Hexagon Drawing "+getName()+" centered at ("+getX()+","+getY()+") width "+getWidth()+" height "+getHeight(); } public void erase(){ System.out.println("In Hexagon erasing"); } public void draw(){ center(); erase(); System.out.println(""+this); } public void setWidth(int w){width = w;} public void setHeight(int h){height = h;} public int getWidth(){return width;} public int getHeight(){return height;} } In your derived classes, only implement draw and erase not center. Assume Figure knows how to center a graphics object. At the same time, Figure has no idea of how to draw or erase your object. A rectangle needs length, height and position (X,Y). A circle needs a radius and position (X,Y). Also, name them (this could be very useful in a real world application). Add all necessary additional constructors, variables and methods in the derived classes. Call super to get the base class constructor. Write a demo program to test your classes. Your test program must store your objects polymorphically in a Figure array. Suppose I had 100 shapes and did not have polymorphism, I would have to make 100 loops! for(i = 0; i < numCircles; i++) circleArray[i].draw(); for(i = 0; i < numRect; i++) rectArray[i].draw(); for(i = 0; i < numTri; i++) triArray[i].draw(); etc. Until 100 shapes were drawn. Because we make figure abstract, we can make draw abstract which means any shape that inherits from figure able to give its own definition of draw. Because all the shapes inherit from the same base, they can be put into the same array. The compiler figures out which draw to use via polymorphism or late binding. Possible Example output: ----jGRASP exec: java DrawShapes In Figure. Centering at (25,100) In Rectangle erasing In Rectangle Drawing r1 centered at (25,100) width 10 height 15 In Figure. Centering at (22,45) In Hexagon erasing In Hexagon Drawing h2 centered at (22,45) width 10 height 50 In Figure. Centering at (5,10) In Rectangle erasing In Circle Drawing c1 centered at (5,10) radius 3 In Figure. Centering at (33,14) In Hexagon erasing In Hexagon Drawing h1 centered at (33,14) width 100 height 50
Explanation / Answer
been stuck on this one for awhlie
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.