Modify the MyLine , MyOval and MyRectangle classes. create MyShape class and mus
ID: 3648555 • Letter: M
Question
Modify the MyLine , MyOval and MyRectangle classes. create MyShape class and must be abstract . The data representing the coordinates and color of the shapes in the hierarchy should be declared asprivate members of class MyShape . In addition to the common data, class MyShape should declare the following methods : 1. A no-argument constructor that sets all the coordinates of the shape to 0 and the color to Color.BLACK . 2. A constructor that initializes the coordinates and color to the values of the arguments supplied. 3. Set methods for the individual coordinates and color that allow the programmer to set any piece of data independently for a shape in the hierarchy. 4. Get methods for the individual coordinates and color that allow the programmer to retrieve any piece of data independently for a shape in the hierarchy. 5. The abstract method public abstract void draw( Graphics g );Explanation / Answer
package Lab8; import java.awt.Color; import java.awt.Graphics; public class MyOval { // declare instance variables private int x1, y1, x2, y2; private Color myColor; private boolean fill; // to fill or not // the constructors public MyOval () { // the default this.x1 = 0; this.y1 = 0; this.x2 = 0; this.y2 = 0; this.myColor = Color.BLACK; this.fill = false; } // end default constructor public MyOval (int x1, int y1, int x2, int y2, Color color, boolean fill) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.myColor = color; this.fill = fill; } // end constructor // sets/gets public void setX1 (int x1) { this.x1 = x1; } public void setY1 (int y1) { this.y1 = y1; } public void setX2 (int x2) { this.x2 = x2; } public void setY2 (int y2) { this.y2 = y2; } public void setColor (Color color) { this.myColor = color; } public void setFill (boolean fill) { this.fill = fill; } public int getX1 () { return x1; } public int getY1 () { return y1; } public int getX2 () { return x2; } public int getY2 () { return y2; } public Color getColor () { return myColor; } public boolean getFill () { return fill; } // now the gets for calculated values public int getUpperLeftX () { // defined as the minimum of x1 and x2 return Math.min (x1, x2); } public int getUpperLeftY () { // defined as the minimum of y1 and y2 return Math.min (y1, y2); } public int getWidth () { return Math.abs (x1 - x2); } public int getHeight () { return Math.abs (y1 - y2); } // draw the shape using calculated values public void draw (Graphics g) { g.setColor (myColor); // draw the right type of shape if (fill) g.fillOval(getUpperLeftX (), getUpperLeftY (), getWidth (), getHeight ()); else g.drawOval (getUpperLeftX (), getUpperLeftY (), getWidth (), getHeight ()); } // end method draw } //MyRectangle.java - implementation of MyRectangle objects - lab #8. //Mike Qualls package Lab8; import java.awt.Graphics; import java.awt.Color; public class MyRectangle { // declare instance variables private int x1, y1, x2, y2; private Color myColor; private boolean fill; // to fill or not // the constructors public MyRectangle () { // the default this.x1 = 0; this.y1 = 0; this.x2 = 0; this.y2 = 0; this.myColor = Color.BLACK; this.fill = false; } // end default constructor public MyRectangle (int x1, int y1, int x2, int y2, Color color, boolean fill) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.myColor = color; this.fill = fill; } // end constructor // sets/gets public void setX1 (int x1) { this.x1 = x1; } public void setY1 (int y1) { this.y1 = y1; } public void setX2 (int x2) { this.x2 = x2; } public void setY2 (int y2) { this.y2 = y2; } public void setColor (Color color) { this.myColor = color; } public void setFill (boolean fill) { this.fill = fill; } public int getX1 () { return x1; } public int getY1 () { return y1; } public int getX2 () { return x2; } public int getY2 () { return y2; } public Color getColor () { return myColor; } public boolean getFill () { return fill; } // now the gets for calculated values public int getUpperLeftX () { // defined as the minimum of x1 and x2 return Math.min (x1, x2); } public int getUpperLeftY () { // defined as the minimum of y1 and y2 return Math.min (y1, y2); } public int getWidth () { return Math.abs (x1 - x2); } public int getHeight () { return Math.abs (y1 - y2); } // draw the shape using calculated values public void draw (Graphics g) { g.setColor (myColor); // draw the right type of shape if (fill) g.fillRect(getUpperLeftX (), getUpperLeftY (), getWidth (), getHeight ()); else g.drawRect (getUpperLeftX (), getUpperLeftY (), getWidth (), getHeight ()); } // end method draw } // end class MyRectangle
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.