i need to create a class named MyShape(which i did ), and make it a super class. its instance variables should be the x1 x2 y1 y2 values shared by all subclasses.
the class needs to include
1. a no argument constructor
2. constructor that takes the argument necessary to initialize its instance variables
3.sets and gets for all instance variables
4. a toString() method that generates and return a string representation of my shape object
make myLine MyRectangle MyOval subclasses of MyShape delete all instance variables and method posible that are in those classes


i have all the code in the text file but i have trouble with the bold lines

Attachments
i need to create a class named MyShape, and make it a super class. its instance variables should be the x1 x2 y1 y2 values shared by all subclasses. 
the class needs to include 
1. a no argument constructor 
2. constructor that takes the argument necessary to initialize its instance variables 
3.sets and gets for all instance variables 
4. a toString() method that generates and return a string representation of my shape object 
make myLine MyRectangle MyOval subclasses of MyShape delete all instance variables and method posible that are in those classes 


===========================================================================
package Project3;
import java.awt.Color;
 import java.awt.Graphics;
import java.util.Random;
 import javax.swing.JPanel;
 
public class DrawPanel extends JPanel
{
	private Random randomNumbers = new Random();
	private MyLine lines[]; // array on lines
	private MyOval ovals[]; // array of ovals
	private MyRectangle rectangles[]; // array of rectangles
	// constructor, creates a panel with random shapes
	public DrawPanel()
	{
	setBackground( Color.WHITE );
	lines = new MyLine[ 1 + randomNumbers.nextInt( 5 ) ];
	ovals = new MyOval[ 1 + randomNumbers.nextInt( 5 ) ];
	rectangles = new MyRectangle[ 1 + randomNumbers.nextInt( 5 ) ];

	// create lines
	for ( int count = 0; count < lines.length; 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 lines to be displayed
	  lines[ count ] = new MyLine( x1, y1, x2, y2, color );
	  } // end for
	  // create ovals
	  for ( int count = 0; count < ovals.length; 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
	  ovals[ count ] = new MyOval( x1, y1, x2, y2, color, filled );
	  } // end for
	  // create rectangles
	  for ( int count = 0; count < rectangles.length; 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 rectangles to be displayed
	  rectangles[ 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 lines
	  for ( MyLine line : lines )
	  line.draw( g );
	  // draw the ovals
	  for ( MyOval oval: ovals )
	  oval.draw( g );
	    // drat the rectangles
	    for ( MyRectangle rectangle : rectangles )
	    rectangle.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",lines.length,"ovals",ovals.length,"rectangles",rectangles.length);
	  } 
	}  // end class DrawPanel
	
===========================================================================

	
	
	
		    
	package Project3;
import java.awt.Color;
import java.awt.Graphics;
public class MyLine extends MyShape
{
	 
	private int x1; // x coordinate of first endpoint
	 private int y1; // y coordinate of first endpoint
	 private int x2; // x coordinate of second endpoint
	 private int y2; // y coordinate of second endpoint
	 private Color myColor; // color of this shape
	
	// constructor initializes private vars with default values
	  public MyLine()
	  {
	  this( 0, 0, 0, 0, Color.BLACK ); // call constructor to set values
	  } // end MyLine no-argument constructor
	 
	  // constructor with input values
	  public MyLine( int x1, int y1, int x2, int y2, Color color )
	  {
	  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
	  setColor( color ); // set the color
	  } // end MyLine constructor
	 
	  // set the x-coordinate of the first point
	 public void setX1( int x1 )
	  {
		   this.x1 = ( x1 >= 0 ? x1 : 0 );
		   } // end method setX1
		  
		   // get the x-coordinate of the first point
		   public int getX1()
		   {
		   return x1;
		   } // end method getX1
		  
		   // set the x-coordinate of the second point
		   public void setX2( int x2 )
		   {
		   this.x2 = ( x2 >= 0 ? x2 : 0 );
		   } // end method setX2
		  
		   // get the x-coordinate of the second point
		   public int getX2()
		   {
		   return x2;
		   } // end method getX2
		  
		   // set the y-coordinate of the first point
		   public void setY1( int y1 )
		   {
		   this.y1 = ( y1 >= 0 ? y1 : 0 );
		   } // end method setY1
		  
		   // get the y-coordinate of the first point
		   public int getY1()
		   {
		   return y1;
		   } // end method getY1
		  
		   // set the y-coordinate of the second point
		   public void setY2( int y2 )
		   {
		   this.y2 = ( y2 >= 0 ? y2 : 0 );
		   } // end method setY2
		  
		   // get the y-coordinate of the second point
		   public int getY2()
		   {
		   return y2;
		   }
	  // end method getY2
	
	 // set the color
	 public void setColor( Color color )
	 {
	 myColor = color;
	 } // end method setColor
	
	 // get the color
	 public Color getColor()
	 {
		return myColor;
	 } // end method getColor
		 
		  // draw the line in the specified color
		  public void draw( Graphics g )
		  {
		  g.setColor( getColor() );
		  g.drawLine( getX1(), getY1(), getX2(), getY2() );
		  } // end method draw
		  } // end class MyLine

===================================================================================
===================================================================================

package Project3;
import java.awt.Color;
 import java.awt.Graphics;
public class MyOval extends MyShape {

	private int x1; // x coordinate of first endpoint
	 private int y1; // y coordinate of first endpoint
	 private int x2; // x coordinate of second endpoint
	 private int y2; // y coordinate of second endpoint
	 private Color myColor; // Color of this oval
	 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
	 setColor( color ); // set the color
	 setFilled( isFilled );
	 } // end MyOval constructor
	
	 // set the x-coordinate of the first point
	public void setX1( int x1 )
	 {
	 this.x1 = ( x1 >= 0 ? x1 : 0 );
	 } // end method setX1
	
	 // get the x-coordinate of the first point
	 public int getX1()
	 {
	 return x1;
	 } // end method getX1
	// set the x-coordinate of the second point
	  public void setX2( int x2 )
	  {
	  this.x2 = ( x2 >= 0 ? x2 : 0 );
	  } // end method setX2
	 
	  // get the x-coordinate of the second point
	  public int getX2()
	  {
	  return x2;
	  } // end method getX2
	 
	  // set the y-coordinate of the first point
	  public void setY1( int y1 )
	  {
	  this.y1 = ( y1 >= 0 ? y1 : 0 );
	  } // end method setY1
	 
	  // get the y-coordinate of the first point
	  public int getY1()
	  {
	  return y1;
	  } // end method getY1
	 
	  // set the y-coordinate of the second point
	  public void setY2( int y2 )
	  {
	  this.y2 = ( y2 >= 0 ? y2 : 0 );
	  } // end method setY2
	 
	  // get the y-coordinate of the second point
	  public int getY2()
	  {
	  return y2;
	  } // end method getY2
	 
	  // set the color
	  public void setColor( Color color )
	  {
	  myColor = color;
	  } // end method setColor
	 
	  //get the color
	   public Color getColor()
	   {
	   return myColor;
	   } // end method getColor
	  
	   // 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 )

All it's saying is look for any variables or methods that are the same in all the subclasses and move those into the superclass so they only appear once. Each of the subclasses will inherit them all from the superclass, so there's no need to duplicate them in every subclass.

well, i am only trying to get second opinion, but anyway

I deleted from each class it's instant variable and their sets and gets methods, and i left the non argument constructor and the constructor that take the x's and y's and the color
and extended each class to the MyShape class

hope i wont get any poins drop from it

OK. Don't forget to add those variables and methods to the superclass. You were right to keep the constructors in the subclasses, because constructors are not inherited.

This article has been dead for over six months. Start a new discussion instead.