Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Been stuck on this for a week now! In order to learn polymorphism, Imagine desig

ID: 672608 • Letter: B

Question

Been stuck on this for a week now!

In order to learn polymorphism, Imagine designing a graphics system that draws various objects like triangles, circles, squares, etc. You do not want to have to make drawing loops for all of the shapes a user might want to draw but rather want a polymorphic solution such as:

for(int i = 0; i < Figure.getNumberOfShapes(); i++){ f[i].draw(); }

While I do not want you to actually draw shapes – that would be too much work to just learn about polymorphism, I want you to design a console simulated graphics system. Here is the design of a base class Figure that will be inherited for any shape you create: (note that if you were in charge of this project, you could control what your designers produced by providing them with this abstract class and requiring them to inherit from it)

public abstract class Figure{

private int X, Y; // the center of the object.

private String name;

private static int numberOfShapes=0;

public Figure(){ X = 0; Y = 0; name = "none"; }

public Figure(int a, int b, String n){ setX(a); setY(b); setName(n); numberOfShapes++; }

public void setX(int a){X = a;}

public void setY(int b){Y = b;}

public void setName(String n){name = n;}

public int getX(){return X;}

public int getY(){return Y;}

public String getName(){return name;}

public static int getNumberOfShapes(){return numberOfShapes;}

public abstract void erase();

public abstract void draw();

public void center(){ System.out.println(" In Figure. Centering at ("+getX()+","+getY()+")");

} }

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

//Figure.java
public abstract class Figure
{
   private int X, Y; // the center of the object.
   private String name;
   private static int numberOfShapes=0;
   public Figure()
   {
       X = 0;
       Y = 0;
       name = "none";
   }

   public Figure(int a, int b, String n)
   {
       setX(a);
       setY(b);
       setName(n);
       numberOfShapes++;
   }

   public void setX(int a)
   {
       X = a;
   }

   public void setY(int b)
   {
       Y = b;
   }

   public void setName(String n)
   {
       name = n;
   }

   public int getX()
   {
       return X;
   }

   public int getY()
   {
       return Y;
   }

   public String getName()
   {
       return name;
   }

   public static int getNumberOfShapes()
   {
       return numberOfShapes;
   }

   public abstract void erase();

   public abstract void draw();

   public void center()
   {
       System.out.println(" In Figure. Centering at ("+getX()+","+getY()+")");

   }
}

--------------------------------------------------------------------------------------------------------------------------------------------


//Rectangle.java
//The class Rectangle extends the Figure class
public class Rectangle extends Figure
{
   int width, height;
  
   public Rectangle()
   {
       super(0,0,"none");
       setWidth(0);
       setHeight(0);
   }

   public Rectangle(String n, int a, int b, int w, int h)
   {
       super(a, b, n);
       setWidth(w);
       setHeight(h);

   }

   public String toString()
   {
       return "In Rectangle 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;
   }
}

--------------------------------------------------------------------------------------------------------------------------------------------


//Circle.java
//Extends the Figure class
//overide the methods draw and erase
public class Circle extends Figure
{

   int radius;
   public Circle()
   {
       super(0, 0, "none");
   }

   public Circle(String position,int lenth, int height, int radius)
   {
       super(lenth, height, position);
       this.radius=radius;
   }

   public String toString()
   {
       return "In Circle Drawing "+
               getName()+" centered at ("+getX()+","+getY()+") with radius "+getRadius();
   }

   public void erase()
   {
       System.out.println("In Hexagon erasing");
   }

   public void draw()
   {
       center();
       erase();
       System.out.println(""+this);
   }

   public int getRadius()
   {
       return radius;
   }
}

--------------------------------------------------------------------------------------------------------------------------------------------

//Hexagon.java

///Extends the figure class
//Override the method erase and draw
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;
   }
}

--------------------------------------------------------------------------------------------------------------------------------------------

/**Tester java program that creates 100 objects of different Figure
* shapes using late binding (polymorphism)*/
//DrawShapes.java
public class DrawShapes
{
   public static void main(String[] args)
   {
       int size=100;

      
       //Create an array of type Figure
       Figure[] figures=new Figure[size];

       //Iterate over the array to fill the objects of different
       //types based on the random number value and fill the array of
       //100 different object of Figure type
       for (int index = 0; index < figures.length; index++)
       {

           int rand=(int)(Math.random()*3+1);

           switch (rand)
           {
           case 1:
               //create Rectangle object with random values in between 0 to 255
               figures[index]=new Rectangle("r"+((index+1)), (int)(Math.random()*255),(int)(Math.random()*255),
                       (int)(Math.random()*255), (int)(Math.random()*255));              
               break;
           case 2:
               //create Hexagon object with random values in between 0 to 255
               figures[index]=new Hexagon("h"+((index+1)),(int)(Math.random()*255), (int)(Math.random()*255),
                       (int)(Math.random()*255), (int)(Math.random()*255));
               break;
           case 3:
               //create Circle object with random values in between 0 to 255
               figures[index]=new Circle("c"+((index+1)), (int)(Math.random()*255),
                       (int)(Math.random()*255),(int)(Math.random()*255));
               break;
           }

       }

      
       //print the figure objects
       for (int i = 0; i < figures.length; i++)
       {
           System.out.println(figures[i].toString());
       }

   }
}

--------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

In Rectangle Drawing r1 centered at (209,169) width 16 height 53
In Hexagon Drawing h2 centered at (73,212) width 207 height 169
In Rectangle Drawing r3 centered at (42,134) width 69 height 156
In Rectangle Drawing r4 centered at (134,91) width 165 height 144
In Circle Drawing c5 centered at (41,82) with radius 126
In Circle Drawing c6 centered at (64,193) with radius 22
In Circle Drawing c7 centered at (97,11) with radius 159
In Hexagon Drawing h8 centered at (78,251) width 224 height 138
In Rectangle Drawing r9 centered at (19,44) width 148 height 74
In Circle Drawing c10 centered at (12,61) with radius 92
In Rectangle Drawing r11 centered at (116,202) width 63 height 159
In Hexagon Drawing h12 centered at (77,116) width 111 height 170
In Circle Drawing c13 centered at (47,100) with radius 24
In Hexagon Drawing h14 centered at (245,168) width 101 height 211
In Rectangle Drawing r15 centered at (223,37) width 147 height 142
In Hexagon Drawing h16 centered at (237,120) width 252 height 181
In Rectangle Drawing r17 centered at (121,76) width 250 height 73
In Circle Drawing c18 centered at (229,30) with radius 230
In Hexagon Drawing h19 centered at (22,69) width 12 height 190
In Circle Drawing c20 centered at (51,32) with radius 10
In Circle Drawing c21 centered at (175,89) with radius 47
In Hexagon Drawing h22 centered at (33,41) width 68 height 48
In Circle Drawing c23 centered at (174,117) with radius 236
In Rectangle Drawing r24 centered at (244,103) width 168 height 150
In Rectangle Drawing r25 centered at (153,161) width 100 height 113
In Circle Drawing c26 centered at (192,152) with radius 117
In Circle Drawing c27 centered at (202,81) with radius 201
In Circle Drawing c28 centered at (107,217) with radius 171
In Rectangle Drawing r29 centered at (50,1) width 105 height 237
In Circle Drawing c30 centered at (166,210) with radius 33
In Circle Drawing c31 centered at (55,16) with radius 133
In Hexagon Drawing h32 centered at (177,203) width 47 height 55
In Hexagon Drawing h33 centered at (84,116) width 242 height 200
In Circle Drawing c34 centered at (116,26) with radius 87
In Hexagon Drawing h35 centered at (159,188) width 1 height 16
In Rectangle Drawing r36 centered at (106,245) width 90 height 214
In Hexagon Drawing h37 centered at (96,126) width 169 height 51
In Circle Drawing c38 centered at (13,16) with radius 44
In Hexagon Drawing h39 centered at (214,171) width 148 height 80
In Rectangle Drawing r40 centered at (39,74) width 28 height 212
In Hexagon Drawing h41 centered at (154,135) width 2 height 152
In Hexagon Drawing h42 centered at (57,153) width 246 height 75
In Circle Drawing c43 centered at (104,11) with radius 218
In Circle Drawing c44 centered at (186,17) with radius 68
In Circle Drawing c45 centered at (110,160) with radius 14
In Circle Drawing c46 centered at (200,43) with radius 181
In Circle Drawing c47 centered at (129,102) with radius 26
In Rectangle Drawing r48 centered at (123,51) width 131 height 60
In Circle Drawing c49 centered at (72,150) with radius 37
In Circle Drawing c50 centered at (214,216) with radius 73
In Rectangle Drawing r51 centered at (117,221) width 97 height 231
In Circle Drawing c52 centered at (132,226) with radius 20
In Rectangle Drawing r53 centered at (159,26) width 250 height 29
In Circle Drawing c54 centered at (141,191) with radius 181
In Rectangle Drawing r55 centered at (42,97) width 149 height 11
In Hexagon Drawing h56 centered at (134,201) width 181 height 150
In Rectangle Drawing r57 centered at (164,235) width 232 height 67
In Rectangle Drawing r58 centered at (50,0) width 96 height 233
In Circle Drawing c59 centered at (213,18) with radius 186
In Hexagon Drawing h60 centered at (106,192) width 210 height 253
In Hexagon Drawing h61 centered at (67,177) width 91 height 248
In Circle Drawing c62 centered at (226,39) with radius 135
In Hexagon Drawing h63 centered at (108,115) width 98 height 104
In Circle Drawing c64 centered at (62,100) with radius 101
In Rectangle Drawing r65 centered at (158,74) width 109 height 50
In Circle Drawing c66 centered at (203,191) with radius 165
In Hexagon Drawing h67 centered at (185,167) width 75 height 105
In Rectangle Drawing r68 centered at (198,64) width 222 height 99
In Circle Drawing c69 centered at (184,140) with radius 205
In Rectangle Drawing r70 centered at (186,153) width 123 height 179
In Rectangle Drawing r71 centered at (184,77) width 22 height 226
In Rectangle Drawing r72 centered at (56,105) width 71 height 214
In Hexagon Drawing h73 centered at (17,100) width 1 height 85
In Circle Drawing c74 centered at (66,198) with radius 83
In Rectangle Drawing r75 centered at (5,238) width 49 height 211
In Circle Drawing c76 centered at (238,155) with radius 72
In Circle Drawing c77 centered at (31,76) with radius 148
In Circle Drawing c78 centered at (6,54) with radius 145
In Hexagon Drawing h79 centered at (203,81) width 49 height 173
In Hexagon Drawing h80 centered at (124,35) width 221 height 169
In Circle Drawing c81 centered at (57,123) with radius 55
In Circle Drawing c82 centered at (156,28) with radius 18
In Rectangle Drawing r83 centered at (30,48) width 125 height 48
In Rectangle Drawing r84 centered at (133,78) width 79 height 85
In Hexagon Drawing h85 centered at (96,44) width 96 height 97
In Circle Drawing c86 centered at (22,204) with radius 111
In Circle Drawing c87 centered at (109,21) with radius 119
In Circle Drawing c88 centered at (182,161) with radius 81
In Circle Drawing c89 centered at (78,49) with radius 10
In Hexagon Drawing h90 centered at (99,38) width 101 height 165
In Circle Drawing c91 centered at (193,229) with radius 44
In Circle Drawing c92 centered at (160,245) with radius 156
In Hexagon Drawing h93 centered at (27,66) width 236 height 167
In Hexagon Drawing h94 centered at (25,76) width 177 height 22
In Hexagon Drawing h95 centered at (208,12) width 220 height 129
In Rectangle Drawing r96 centered at (29,208) width 243 height 157
In Rectangle Drawing r97 centered at (113,68) width 18 height 210
In Circle Drawing c98 centered at (135,33) with radius 125
In Hexagon Drawing h99 centered at (188,130) width 182 height 111
In Hexagon Drawing h100 centered at (5,180) width 61 height 245

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote