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

In Exercise 10.1, you created a MyShape hierarchy in which classes MyLine, MyOva

ID: 3657912 • Letter: I

Question

In Exercise 10.1, you created a MyShape hierarchy in which classes MyLine, MyOval and MyRectangle extend MyShape directly. If your hierarchy was properly designed, you should be able to see the similarities between the MyOval and MyRectangle classes. Redesign and reimplement the code for the MyOval and MyRectangle classes to ?factor out? the common features into the abstract class MyBoundedShape to produce the hierarchy in Fig. 10.18. Class MyBoundedShape should declare two constructors that mimic those of class MyShape, only with an added parameter to set whether the shape is filled. Class MyBoundedShape should also declare get and set methods for manipulating the filled flag and methods that calculate the upper-left x-coordinate, upper-left y-coordinate, width and height. Remember, the values needed to draw an oval or a rectangle can be calculated from two (x, y) coordinates. If designed properly, the new MyOval and MyRectangle classes should each have two constructors and a draw method. Below is the code I am working with //main class //TestDraw.java import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLabel; public class TestDraw { public static void main(String[] args) { final int WINDOW_WIDTH = 400, WINDOW_HEIGHT = 400; JFrame application = new JFrame (); // the window and its components DrawPanel panel = new DrawPanel (); // call constructor creating MyLine objects JLabel southLabel = new JLabel(panel.status()); application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); application.add (panel); application.setLocationByPlatform(true); application.setSize (WINDOW_WIDTH, WINDOW_HEIGHT); application.setVisible (true); // show the window application.add(southLabel, BorderLayout.SOUTH); } } //DrawPanel.java import java.awt.Color; import java.awt.Graphics; import java.util.Random; import javax.swing.JPanel; import javax.swing.JOptionPane; public class DrawPanel extends JPanel { private Random randomNumbers = new Random(); private MyShape shapes[]; //array of shapes private int numOfShapes=0; // constructor, creates a panel with random shapes public DrawPanel() { setBackground( Color.WHITE ); String value=JOptionPane.showInputDialog(this,"Enter Number of Shapes?","Number of shapes",JOptionPane.QUESTION_MESSAGE); numOfShapes=Integer.parseInt(value); shapes = new MyShape[ numOfShapes*3 ]; // create lines int lines=numOfShapes; for ( int count = 0; count < lines; count++ ) { // generate random coordinates int x1 = randomNumbers.nextInt( 450 ); int y1 = randomNumbers.nextInt( 450 ); int x2 = randomNumbers.nextInt( 450 ); int y2 = randomNumbers.nextInt( 450 ); // generate a random color Color color = new Color( randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) ); // add the line to the list of shapes to be displayed shapes[ count ] = new MyLine( x1, y1, x2, y2, color ); } // end for int ovals=numOfShapes*2; // create ovals for ( int count = lines; count < ovals; count++ ) { // generate random coordinates int x1 = randomNumbers.nextInt( 450 ); int y1 = randomNumbers.nextInt( 450 ); int x2 = randomNumbers.nextInt( 450 ); int y2 = randomNumbers.nextInt( 450 ); // generate a random color Color color = new Color( randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) ); // get filled property boolean filled = randomNumbers.nextBoolean(); // add the line to the oval of ovals to be displayed shapes[ count ] = new MyOval( x1, y1, x2, y2, color, filled ); } // end for //int rectangles=numOfShapes*3; // create rectangles for ( int count = ovals; count < numOfShapes*3; count++ ) { // generate random coordinates int x1 = randomNumbers.nextInt( 450 ); int y1 = randomNumbers.nextInt( 450 ); int x2 = randomNumbers.nextInt( 450 ); int y2 = randomNumbers.nextInt( 450 ); // generate a random color Color color = new Color( randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) ); // get filled property boolean filled = randomNumbers.nextBoolean(); // add the rectangle to the list of shapes to be displayed shapes[ count ] = new MyRectangle( x1, y1, x2, y2, color, filled ); } // end for } // end DrawPanel constructor // for each shape array, draw the individual shapes public void paintComponent( Graphics g ) { super.paintComponent( g ); // draw the shapes for ( MyShape shape : shapes ) shape.draw( g ); } // end method paintComponent public String status() { // returns number of lines,ovals,rectangles which are drawn in the panel in a string format return String.format("%s: %d, %s: %d, %s:%d", "lines",numOfShapes,"ovals",numOfShapes,"rectangles",numOfShapes); } } // end class DrawPanel //MyShape.java import java.awt.Color; import java.awt.Graphics; public abstract class MyShape { private int x1, y1, x2, y2; private Color myColor; public MyShape() { this.setX1(0); this.setY1(0); this.setX2(0); this. setY2(0); setMyColor(Color.BLACK); } public MyShape(int x1, int y1, int x2, int y2, Color myColor) { setX1(x1); setY1(y1); setX2(x2); setY2(y2); setMyColor(myColor); } public void setX1(int x1) { if(x1 >= 0 && x1 <= 300) { this.x1 = x1;} else {this.x1 = 0;} } public int getX1() { return x1;} public void setY1(int y1) { if(y1 >= 0 && y1 <= 300) { this.y1 = y1; } else { this.y1 = 0; } } public int getY1() { return y1;} public void setX2(int x2) { if(x2 >= 0 && x2 <= 300) { this.x2 = x2; } else { this.x2 = 0; } } public int getX2() { return x2; } public void setY2(int y2) { if(y2 >= 0 && y2 <= 300) { this.y2 = y2; } else { this.y2 = 0; } } public int getY2() { return y2; } public void setMyColor(Color myColor) { this.myColor = myColor; } public Color getMyColor() { return myColor; } public abstract void draw(Graphics g); public String toString() {return String.format("Lines: %d, Ovals: %d, Rectangles: %d" ,this.getX1(), this.getX2(), this.getY1(), this.getY2(), this.getMyColor()); } } //MyRectangle.java import java.awt.Color; import java.awt.Graphics; public class MyRectangle extends MyShape { private boolean filled; // whether this shape is filled // constructor initializes private vars with default values public MyRectangle() { this( 0, 0, 0, 0, Color.BLACK, false ); // call constructor } // end MyRect no-argument constructor // constructor with input values public MyRectangle( int x1, int y1, int x2, int y2,Color color, boolean isFilled ) { setX1( x1 ); // set x coordinate of first endpoint setY1( y1 ); // set y coordinate of first endpoint setX2( x2 ); // set x coordinate of second endpoint setY2( y2 ); // set y coordinate of second endpoint setMyColor( color ); // set the color setFilled( isFilled ); } // end MyRect constructor public int getUpperLeftX() { return Math.min( getX1(), getX2() ); } // end method getUpperLeftX // get upper left y coordinate public int getUpperLeftY() { return Math.min( getY1(), getY2() ); } // end method getUpperLeftY // get shape width public int getWidth() { return Math.abs( getX2() - getX1() ); } // end method getWidth // get shape height public int getHeight() { return Math.abs( getY2() - getY1() ); } // end method getHeight // determines whether this shape is filled public boolean isFilled() { return filled; } // end method is filled // sets whether this shape is filled public void setFilled( boolean isFilled ) { filled = isFilled; } // end method setFilled // draws a rectangle in the specified color public void draw( Graphics g ) { g.setColor( getMyColor() ); if ( isFilled() ) g.fillRect( getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() ); else g.drawRect( getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() ); } // end method draw } // end class MyRect //MyOval.java import java.awt.Color; import java.awt.Graphics; public class MyOval extends MyShape { private boolean filled; // whether this shape is filled // constructor initializes private vars with default values public MyOval() { this( 0, 0, 0, 0, Color.BLACK, false ); // call constructor } // end MyOval no-argument constructor // constructor with input values public MyOval( int x1, int y1, int x2, int y2, Color color, boolean isFilled ) { setX1( x1 ); // set x coordinate of first endpoint setY1( y1 ); // set y coordinate of first endpoint setX2( x2 ); // set x coordinate of second endpoint setY2( y2 ); // set y coordinate of second endpoint setMyColor( color ); // set the color setFilled( isFilled ); } // end MyOval constructor // get upper left x coordinate public int getUpperLeftX() { return Math.min( getX1(), getX2() ); } // end method getUpperLeftX // get upper left y coordinate public int getUpperLeftY() { return Math.min( getY1(), getY2() ); } // end method getUpperLeftY // get shape width public int getWidth() { return Math.abs( getX2() - getX1() ); } // end method getWidth // get shape height public int getHeight() { return Math.abs( getY2() - getY1() ); } // end method getHeight // determines whether this shape is filled public boolean isFilled() { return filled; } // end method is filled // sets whether this shape is filled public void setFilled( boolean isFilled ) { filled = isFilled; } // end method setFilled // draws an oval in the specified color public void draw( Graphics g ) { g.setColor( getMyColor() ); if ( isFilled() ) g.fillOval( getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() ); else g.drawOval( getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() ); } // end method draw } // end class MyOval

Explanation / Answer

hey refer this...................:):):) http://vapvarun.com/study/java_books/java,%20how%20to%20program,%202004/0131483986/ch10lev1sec8.html