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

Consider the following class hierarchy: Shape is an abstract class (declaration

ID: 3692055 • Letter: C

Question

Consider the following class hierarchy: Shape is an abstract class (declaration provided in subsequent section). All other classes are non-abstract. Color and GenPoint classes are the same as used in lab10in. An example main is provided that processes all of the shapes in the form of the menu. The main displays the following menu: The user is always prompted for the Color used for coloring the exterior (i.e., contour) of the shape. Suppose the user enters selection number 1 (Circle) as shown below. He is also prompted for the color which he enters for this example (255,0,0) - red. The user is then prompted for the radius and center point for the Circle as shown below: For this example the radius is 50 and x/y coordinate for the Center is (100,100). The graphics window will then display the following information pertaining to this Circle object: The user is then prompted to repeat the program as shown below: Upon repeating the menu is redisplayed. Suppose the user enters choice #2 as shown below: The user is then prompted for the 4 points of the Quadrilateral: The graphics window then displays the following for the Quadrilateral selection: The draw method for Quadrilateral displays the desired Quadrilateral as shown. The print method for Quadrilateral displays the information at the bottom. Repeating the program and selecting choice 3 (Rectangle) prompts the user for 2 points (upper left/lower right) as shown below: The graphic window displays the Recangle information as shown: The draw method for GenRectangle draws the red rectangle above. Text data both at the top/bottom are displayed by print method of GenRectangle. Assume sides of GenRectangle are parallel to x/y axis. Repeating the program and selecting menu item #4 processes the Trapezoid data by first prompting the user for 4 points: The graphics window then displays the following for Trapezoid: The draw method for Trapezoid draws the red trapezoid above. Text data at both top/bottom is displayed by the print method for Trapezoid. Assume bases of Trapezoid are parallel to x/y axis. Repeating the program and selecting menu item #5 processes the Triangle data by first prompting the user for 3 points: The graphics window then displays the Triangle information as shown below: The draw method for Triangle draws the red Triangle above. Text data at top is displayed by the print method for Triangle. Repeating the program and selecting menu item #6 processes the RightTriangle data by first prompting the user for the a. base of the Right Triangle b. height of the Right Triangle c. vertex (intersection of base/height) of the Right Triangle The graphics window then displays the RightTriangle information as shown below: The draw method for Triangle draws the red RightTriangle above. Text data at top/bottom is displayed by the print method for RightTriangle. Asume RightTriangle’s base/height are parallel to x/y axis. Class Declarations The Shape class is declared as follows: #ifndef SHAPE_H #define SHAPE_H #include "GenPoint.h" #include "Color.h" class Shape { protected: Color color; //Color class used in lab7in public: Shape(); //Default Constructor - sets color to (0,0,0) Color getColor(); //Getter for Color void setColor(Color color); //Setter for Color virtual double getPerimeter() = 0; virtual void print()= 0; virtual void draw() = 0; }; #endif Methods Description Shape(); //Default Constructor - sets color to (0,0,0) Default Constructor for Shape class. Sets the Color datafield (color) to all 0’s - (0,0,0) Getter/Setter for Color Color getColor(); //Getter for Color void setColor(Color color); //Setter for Color virtual double getPerimeter() = 0; This method must be overriden in a subclass. Computes the perimeter for a given shape. virtual void print()= 0; This method must be overriden in a subclass. Displays textual information for a given shape in the graphics window. virtual void draw() = 0; This method must be overriden in a subclass. Draws a given shape in the graphics window. The Quadrilateral class is declared as follows: #include "GenPoint.h" #include "Color.h" class Quadrilateral : public Shape { protected: GenPoint a; GenPoint b; GenPoint c; GenPoint d; public: Quadrilateral(); Quadrilateral(GenPoint a, GenPoint b, GenPoint c, GenPoint d, Color color); void setPoints(GenPoint a, GenPoint b, GenPoint c, GenPoint d); double getPerimeter(); void print(); void draw(); }; #endif Datafield Description GenPoint a; GenPoint b; GenPoint c; GenPoint d; The 4 GenPoint points that define the Quadrilateral are the vertexes of a Quadrilateral. Methods Description Quadrilateral(); Default Constructor for Quadrilateral. Initializes 4 points to (0,0) each. Quadrilateral(GenPoint a, GenPoint b, GenPoint c, GenPoint d, Color color); Parameterized Constructor for Quadrilateral. Initializes 4 GenPointspoints that are the datafields to values of the input parameters a,b,c and d. The color of the quadrilateral is also assigned the value of the input parameter color. void setPoints(GenPoint a, GenPoint b, GenPoint c, GenPoint d); Setter for the datafields a,b,c and d. double getPerimeter(); Computes and returns the perimeter of the Quadrilateral. void print(); Prints out the textual information of the Quadrilateral in the graphics window as shown on page 3. void draw(); Draws the Quadrilateral in the graphics window as shown on page 3. The GenRectangle class is declared as: #ifndef GENRECTANGLE_H #define GENRECTANGLE_H #include "Quadrilateral.h" class GenRectangle: public Quadrilateral { public: GenRectangle(); GenRectangle(GenPoint ul, GenPoint lr, Color c); void setPoints(GenPoint ul, GenPoint lr); double getArea(); void print(); }; #endif Methods Description GenRectangle(); Default Constructor for GenRectangle. Initializes all inherited datafields as done in Base classes. GenRectangle(GenPoint ul, GenPoint lr, Color c); Parameterized Constructor for GenRectangle. Initializes all inherited data fields based on the upper left coordinate of the rectangle (e.g., ul) and the lower right coordinate of the rectangle (e.g., lr). The Color (e.g., c) is also provided as an input parameter. void setPoints(GenPoint ul, GenPoint lr); Setter that initializes all inherited points based on the upper left coordinate (e.g., ul) and the lower right coordinate (e.g., lr). double getArea(); Computes and returns the area of the rectangle. void print(); Prints out the textual information of the GenRectangle in the graphics window as shown on page 4. The Trapezoid class is declared as #ifndef TRAPEZOID_H #define TRAPEZOID_H #include "Quadrilateral.h" class Trapezoid : public Quadrilateral { public: Trapezoid(); Trapezoid(GenPoint a, GenPoint b, GenPoint c, GenPoint d, Color color); void setPoints(GenPoint a, GenPoint b, GenPoint c, GenPoint d); double getArea(); void print(); }; #endif : Methods Description Trapezoid(); Default Constructor for Trapezoid. Initializes all inherited datafields as done in Base classes. Trapezoid(GenPoint a, GenPoint b, GenPoint c, GenPoint d, Color color); Parameterized Constructor for GenRectangle. Initializes all inherited data fields based on the 4 input parameters - a,b,c,d. The Color (e.g., color) is also provided as an input parameter. void setPoints(GenPoint a, GenPoint b, GenPoint c, GenPoint d); Sets inherited GenPoints to values contained in a,b,c,d. double getArea(); Computes and returns the area of the trapezoid. The area is computed as The bases and height are identified on page 5. void print(); Prints out the textual information of the Trapezoid in the graphics window as shown on page 5. The Triangle class is Declared as: #ifndef TRIANGLE_H #define TRIANGLE_H #include "Shape.h" class Triangle : public Shape { protected: GenPoint a; GenPoint b; GenPoint c; public: Triangle(); Triangle(GenPoint a, GenPoint b, GenPoint c, Color color); void setPoints(GenPoint a, GenPoint b, GenPoint c); double getArea(); double getPerimeter(); void print(); void draw(); }; #endif Datafield Description GenPoint a; GenPoint b; GenPoint c; The three GenPoint points that identify the vertexes of the Triangle. Methods Description Triangle(); Default Constructor for Triangle. Sets all GenPoint points to (0,0). Triangle(GenPoint a, GenPoint b, GenPoint c, Color color); Parameterized Constructor for Triangle. Sets the GenPoint datafields (a,b,c) to the values of the input parametes (a,b,c). The inherited color datafield is also set to the input parameter color. void setPoints(GenPoint a, GenPoint b, GenPoint c); Setter for the datafields a,b,c. double getArea(); Computes and returns the area of a general Triangle using the following formula: double s = (side1 + side2 + side3)/2.0; double area = sqrt(s*(s-side1)*(s-side2)*(s-side3)); Where side1, side2, and side3 are the length of each side of the Triangle. double getPerimeter(); Computes and returns the perimeter of the Triangle. void print(); Prints out the textual information of the Triangle in the graphics window as shown on page 6. void draw(); Draws the triangle in the graphics window as shown on page 6. The RightTriangle class is Declared as: #ifndef RIGHT_TRIANGLE_H #define RIGHT_TRIANGLE_H #include "Triangle.h" class RightTriangle : public Triangle { public: RightTriangle(); RightTriangle(GenPoint vertex, int height, int base, Color color); void print(); }; #endif Methods Description RightTriangle(); Default Constructor for RightTriangle. Sets all inherited GenPoint points to (0,0). RightTriangle(GenPoint vertex, int height, int base, Color color); Parameterized Constructor for Right Triangle. Initializes all inherited points based on input parametes vertex, height, and base (see illustration on page 7). Also initializes the color based on input parameter color. void print(); Prints out the textual information of the Triangle in the graphics window as shown on page 7. The Circle class is Declared as: #ifndef CIRCLE_H #define CIRCLE_H #include "Shape.h" class Circle : public Shape { protected: int radius; GenPoint center; const static double PI; public: Circle(); Circle(GenPoint center, int radius, Color c); void setCenter(GenPoint center); void setRadius(int radius); double getPerimeter(); double getArea(); void print(); void draw(); }; #endif Data Fields Description int radius; Radius for the circle GenPoint center; Center for the circle const static double PI; Sets PI to 3.1415926. Methods Description Circle(); Default Constructor for Circle. Sets the radius to 0 and center to (0,0). Circle(GenPoint center, int radius, Color c); Parameterized Constructor for Circle. Sets datafields (center, radius, color) to the corresponding input parameters. Setters void setCenter(GenPoint center); void setRadius(int radius); double getPerimeter(); Computes and returns the perimeter of the Circle. double getArea(); Computes and returns the area of the Circle. void print(); Prints out the textual information of the Triangle in the graphics window as shown on page 2. void draw(); Draws the Circle in the graphics window as shown on page 2. Example main You can use the following main with your project. Feel free to implement your own main if you like. #include #include "graph1.h" #include "Circle.h" #include "Shape.h" #include "Color.h" #include "GenPoint.h" #include "Quadrilateral.h" #include "GenRectangle.h" #include "RightTriangle.h" #include "Trapezoid.h" #include "Triangle.h" int main() { //Variable Declaration int x = 0; int y = 0; int r = 0; int g = 0; int b = 0; int choice = 0; int radius = 0; int base = 0; int height = 0; char repeat = 'y'; Shape* shape = NULL; GenPoint a1; GenPoint b1; GenPoint c1; GenPoint d1; Color color; //Display graphics displayGraphics(); //Repeat program as many times as desired do { //Display the menu cout << "1. Circle" << endl; cout << "2. Quadrilateral" << endl; cout << "3. Rectangle" << endl; cout << "4. Trapezoid" << endl; cout << "5. Triangle" << endl; cout << "6. Right Triangle" << endl; cout << "Enter choice: "; cin >> choice; //Enter Color cout << " Enter Color For Object: "; cin >> r >> g >> b; color.setColor(r,g,b); switch(choice) { case 1: //Process Circle info cout << "Enter radius: "; cin >> radius; cout << "Enter Center x/y coordinate: "; cin >> x >> y; //Create circle shape = new Circle(GenPoint(x,y),radius,color); break; case 2: //Process Quadrilateral Info cout << "Enter Point a: "; cin >> x >> y; a1.setPoint(x,y); cout << "Enter Point b: "; cin >> x >> y; b1.setPoint(x,y); cout << "Enter Point c: "; cin >> x >> y; c1.setPoint(x,y); cout << "Enter Point d: "; cin >> x >> y; d1.setPoint(x,y); shape = new Quadrilateral(a1,b1,c1,d1,color); break; case 3: //Process Rectangle info cout << "Enter upper left: "; cin >> x >> y; a1.setPoint(x,y); cout << "Enter lower right: "; cin >> x >> y; b1.setPoint(x,y); shape = new GenRectangle(a1,b1,color); break; case 4: //Process Trapezoid info cout << "Enter Point a: "; cin >> x >> y; a1.setPoint(x,y); cout << "Enter Point b: "; cin >> x >> y; b1.setPoint(x,y); cout << "Enter Point c: "; cin >> x >> y; c1.setPoint(x,y); cout << "Enter Point d: "; cin >> x >> y; d1.setPoint(x,y); shape = new Trapezoid(a1,b1,c1,d1,color); break; case 5: //Process Triangle Info cout << "Enter Point a: "; cin >> x >> y; a1.setPoint(x,y); cout << "Enter Point b: "; cin >> x >> y; b1.setPoint(x,y); cout << "Enter Point c: "; cin >> x >> y; c1.setPoint(x,y); shape = new Triangle(a1,b1,c1,color); break; case 6: //Process Right Triangle Info cout << "Enter base width: "; cin >> base; cout << "Enter base height: "; cin >> height; cout << "Enter vertex coordinates: "; cin >> x >> y; a1.setPoint(x,y); shape = new RightTriangle(a1,height,base,color); break; default: break; } //Process the shape info shape->print(); shape->draw(); //Repeat program? cout << "Repeat program? (y/n): "; cin >> repeat; //Clear console/graphics system("cls"); clearGraphics(); }while( (repeat == 'y') || (repeat == 'Y') ); return 0; }

Explanation / Answer

import java.awt.*;
public class Shapes extends Frame{

//constants for menu shortcuts
private static final int kControlA = 65;
private static final int kControlD = 68;
private static final int kControlC = 67;
private static final int kControlR = 82;
private static final int kControlP = 80;
private static final int kControlT = 84;
private static final int kControlX = 88;

public Shapes() {
//set frame's title
super("Simple Drawing Tool");
//add menu
addMenu();
//set frame size
this.setSize(400, 400);
//make this frame visible
this.setVisible(true);
}
private void addMenu()
{
//Add menu bar to our frame
MenuBar menuBar = new MenuBar();
Menu file = new Menu("File");
Menu shape = new Menu("Shapes");
Menu about = new Menu("About");
//now add menu items to these Menu objects
file.add(new MenuItem("Exit", new MenuShortcut(kControlX)));

shape.add(new MenuItem("Rectangle", new MenuShortcut(kControlR)));
shape.add(new MenuItem("Circle", new MenuShortcut(kControlC)));
shape.add(new MenuItem("Triangle", new MenuShortcut(kControlT)));
shape.add(new MenuItem("Quadrilateral", new MenuShortcut(kControlP)));
shape.add(new MenuItem("Draw Polygon", new MenuShortcut(kControlD)));

about.add(new MenuItem("About", new MenuShortcut(kControlA)));
//add menus to menubar
menuBar.add(file);
menuBar.add(shape);
menuBar.add(about);
//menuBar.setVisible(true);
if(null == this.getMenuBar())
{
this.setMenuBar(menuBar);
}
}//addMenu()
//Inner class to handle events
private class WindowHandler extends WindowAdapter implements ActionListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}

public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
//check to see if the action command is equal to exit
if(e.getActionCommand().equalsIgnoreCase("exit"))
{
System.exit(0);
}
else if(e.getActionCommand().equalsIgnoreCase("About"))
{
JOptionPane.showMessageDialog(null, "This small freeware program is written by Yasir Feroze Minhas.", "About", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "You asked for a "+e.getActionCommand(), "A Simple Drawing Tool", JOptionPane.PLAIN_MESSAGE);
}
}//actionPerformed()
}//windowHandler - Inner Class ends here

}

public static void main(String[] args) {
Shapes shape = new Shapes();
}

}


public abstract class Shapes
{

/**abstract method draw()
@return void
*/
public abstract void draw(java.util.List list, Graphics g);

}
//different implementations of Shape class
class RectangleShape extends Shapes
{
Point sPoint = null;
Point ePoint = null;
public void draw(java.util.List list, Graphics g)
{
Iterator it = list.iterator();
//if the list does not contain the required two points, return.
if(list.size()<2)
{
return;
}
sPoint = (Point)it.next();
ePoint = (Point)it.next();
if(sPoint == null || ePoint == null)
{
return;
}
else
{
g.fillRect((int)sPoint.getX(), (int)sPoint.getY(), (int)(ePoint.getX()-sPoint.getX()),
(int)(ePoint.getY()-sPoint.getY()));
}//end of if
list.clear();
}//end of draw for rectangle
}//rectangle
class OvalShape extends Shapes
{
Point sPoint = null;
Point ePoint = null;
public void draw(java.util.List list, Graphics g)
{
Iterator it = list.iterator();
//if the list does not contain the required two points, return.
if(list.size()<2)
{
return;
}
sPoint = (Point)it.next();
ePoint = (Point)it.next();
if(sPoint == null || ePoint == null)
{
return;
}
else
{
g.fillOval((int)sPoint.getX(), (int)sPoint.getY(), (int)(ePoint.getX()-sPoint.getX()),
(int)(ePoint.getY()-sPoint.getY()));
}//end of if
list.clear();
}//end of draw for Oval
}//OvalShape
class TriangleShape extends Shapes
{
public void draw(java.util.List list, Graphics g)
{
Point point = null;
Iterator it = list.iterator();
//if the list does not contain the required two points, return.
if(list.size()<3)
{
return;
}
Polygon p = new Polygon();
for(int i = 0; i < 3; i++)
{
point = (Point)it.next();
p.addPoint((int)point.getX(), (int)point.getY());
}

g.fillPolygon(p);
list.clear();
}//end of draw for Triangle
}//Triangle
class PolygonShape extends Shapes
{
public void draw(java.util.List list, Graphics g)
{
Point point = null;
Iterator it = list.iterator();
//if the list does not contain the required two points, return.
if(list.size()<3)
{
return;
}
Polygon p = new Polygon();
for(;it.hasNext();)
{
point = (Point)it.next();
p.addPoint((int)point.getX(), (int)point.getY());
}
g.fillPolygon(p);
list.clear();
}//end of draw for Polygon
}//Polygon

public void actionPerformed(ActionEvent e)
{
//check to see if the action command is equal to exit
if(e.getActionCommand().equalsIgnoreCase("exit"))
{
System.exit(0);
}
else if(e.getActionCommand().equalsIgnoreCase("Rectangle"))
{
Menu menu = getMenuBar().getMenu(1);
for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlR)).setEnabled(false);
panel.drawShape(rectangle);

}
else if(e.getActionCommand().equalsIgnoreCase("Circle"))
{
Menu menu = getMenuBar().getMenu(1);
for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlC)).setEnabled(false);
panel.drawShape(oval);
}
else if(e.getActionCommand().equalsIgnoreCase("Triangle"))
{
Menu menu = getMenuBar().getMenu(1);
for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlT)).setEnabled(false);
panel.drawShape(triangle);
}
else if(e.getActionCommand().equalsIgnoreCase("Polygon"))
{
Menu menu = getMenuBar().getMenu(1);
for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlP)).setEnabled(false);
panel.drawShape(polygon);
}
else if(e.getActionCommand().equalsIgnoreCase("Draw Polygon"))
{
Menu menu = getMenuBar().getMenu(1);
for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlP)).setEnabled(false);
panel.repaint();
}
else if(e.getActionCommand().equalsIgnoreCase("About"))
{
JOptionPane.showMessageDialog(null, "This small freeware program is written by Yasir Feroze Minhas.", "About", JOptionPane.PLAIN_MESSAGE);
}
}//actionPerformed()

class DrawingPanel extends Panel implements MouseListener
{

private Point sPoint = null;
private Point ePoint = null;
private Shapes shape = null;
private java.util.ArrayList list = new java.util.ArrayList();
//override panel paint method to draw shapes
public void paint(Graphics g)
{
g.setColor(Color.green);
shape.draw(list, g);
}
public void drawShape(Shapes shape)
{
this.shape = shape;
}
//define mouse handler
public void mouseClicked(MouseEvent e)
{
//if user wants to draw triangle, call repaint after 3 clicks
if(shape instanceof TriangleShape)
{
list.add(e.getPoint());
if(list.size() > 2)
{
repaint();
}
}
else if(shape instanceof PolygonShape)
{
list.add(e.getPoint());
}
}//mouseClicked
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
sPoint = e.getPoint();
}//mousePressed
public void mouseReleased(MouseEvent e)
{
ePoint = e.getPoint();
if(ePoint.getX() < sPoint.getX())
{
Point temp = ePoint;
ePoint = sPoint;
sPoint = temp;
}
if(ePoint.getY() < sPoint.getY())
{
int temp = (int)ePoint.getY();
ePoint.y = (int)sPoint.getY();
sPoint.y = temp;
}
if(shape instanceof RectangleShape || shape instanceof OvalShape)
{
list.clear();
list.add(sPoint);
list.add(ePoint);
repaint();
}
}//mouseReleased
}//DrawingPanel

note-the code given in the question is not in proper format to refer and the above code can help to answer the given question.

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