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

I am in the process of converting my classes into a superclass and subclass. I h

ID: 3835723 • Letter: I

Question

I am in the process of converting my classes into a superclass and subclass. I have already done the portion where i can move the constructors to the superclass, and now I need a way to move the methods (if any) to the superclass as well. they are the same for all MyLine, MyRectangle, and MyOval, which if possible, move the methods to the superclass. I also need help with my toString and helping it run because i am not getting any output in the console. Im doing this to shorten the classes a little and move them to myshape and it would be highly appreciated.

this is the question.

Create another class named MyShape. Make this a superclass. Its instance variables should be the x1, y1, x2, y2, and color values that are shared by all subclasses. MyShape should include the following methods:

A no-argument constructor.

A constructor that takes the arguments necessary to initialize its instance variables.

set and get methods for all instance variables.

A ‘toString ()’ method that generates and returns a String representation of a MyShape object.

Make MyLine, MyRectangle, and MyOval subclasses of MyShape. The constructors of these classes will call the superclass constructor either implicitly or explicitly. The fill flag of MyRectangle and MyOval as well as the authors ‘special gets’ (i.e. getUpperLeftX (), etc) will remain in those subclasses. Delete all other instance variables and methods possible that are in those subclasses (i.e. because they have been “moved up” to the superclass).

DRAW PANEL;

public class DrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
  
   private SecureRandom randomNumbers = new SecureRandom ();
   private MyLine lines [];
   private MyRectangle rectangles [];
   private MyOval ovals[];
   JFrame app = new JFrame ();
   // the constructor
   public DrawPanel () {
       final int NUMBER_COLORS = 256; // (0-255 COLOR VALUES)
       final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
       int x1, y1, x2, y2;
       Color color;
      
       // set background color
       setBackground (Color.MAGENTA);
       setBackground (Color.WHITE);
      
       // allocate the array
       lines = new MyLine [5 + randomNumbers.nextInt (5)];
       rectangles = new MyRectangle[5 + randomNumbers.nextInt (5)];
       ovals = new MyOval[5 + randomNumbers.nextInt (5)];
      
       // create the MyLine objects
       for (int count = 0; count < lines.length; count++) {
           // generate random coordinates (0 to 299)
           x1 = randomNumbers.nextInt (WINDOW_WIDTH);
           y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
           x2 = randomNumbers.nextInt (WINDOW_WIDTH);
           y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
          
           //display some output intended as a 'diagnostic' output
           // System.out.printf ("x1=%d, y1 = %d ", x1, y1);
          
           // generate a random color
           color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
           boolean flag = randomNumbers.nextBoolean ();
           // Construct a MyLine object and add it reference to the array
           lines [count] = new MyLine (x1, y1, x2, y2, color, flag);
       } // end for loop to create MyLine objects
      
       for (int count = 0; count < rectangles.length; count ++) {
           x1 = randomNumbers.nextInt (WINDOW_WIDTH);
           y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
           x2 = randomNumbers.nextInt (WINDOW_WIDTH);
           y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
          
           color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
           boolean flag = randomNumbers.nextBoolean ();
          
           rectangles [count] = new MyRectangle (x1, y1, x2, y2, color, flag);
       } // end for loop rectangles
      
       for (int count = 0; count < ovals.length; count ++) {
           x1 = randomNumbers.nextInt (WINDOW_WIDTH);
           y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
           x2 = randomNumbers.nextInt (WINDOW_WIDTH);
           y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
          
           color = new Color (randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
           boolean flag = randomNumbers.nextBoolean ();
          
           ovals [count] = new MyOval (x1, y1, x2, y2, color, flag);
       } // end for loop ovals
      
   } // end drawPanel constructor
  
   // when time to paint the panel, draw the lines, rectangles, and ovals
   public void paintComponent (Graphics g) {
       // paint the parent first
       super.paintComponent (g);
      
       // draw the lines
       for (MyLine line : lines)
           line.draw (g);
       for (MyRectangle rect : rectangles)
           rect.draw(g);
       for (MyOval oval :ovals)
           oval.draw(g);
   } // end method paintComponent
  
   public String getStatusBar(){
       String Status=" Line: "+lines.length+" Rectangle: "+rectangles.length+" Oval: "+ovals.length;
           return Status;
         
   }

  
} // end class DrawPanel

MYLINE;

public class MyLine extends MyShape{


       // the constructors.
       public MyLine() {
           // the default
           this.x1 = 0;
           this.y1 = 0;
           this.x2 = 0;
           this.y2 = 0;
           this.myColor = Color.BLACK;
           this.flag = false;
       } // end default constructor

       public MyLine(int x1, int y1, int x2, int y2, Color color, boolean flag) {
           this.x1 = x1;
           this.y1 = y1;
           this.x2 = x2;
           this.y2 = y2;
           this.myColor = color;
           this.flag = flag;
       } // 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 int getX1() {
           return x1;
       }

       public int getY1() {
           return y1;
       }

       public int getX2() {
           return x2;
       }

       public int getY2() {
           return y2;
       }

       public int getUpperLeftX() {
           return Math.min(getX1(), getX2());
       }

       public int getUpperLeftY() {
           return Math.min(getY1(), getY2());
       }

       public int getWidth() {
           return Math.abs(getX1() - getX2());
       }

       public int getHeight() {
           return Math.abs(getY1() - getY2());
       }

       public Color getColor() {
           return myColor;
       }

       // draw the shape. the color object parameter is the 'graphics context'
       public void draw(Graphics g) {
           if (flag) {
               g.fillRect(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());

           } else {
               g.setColor(Color.BLACK);
           }
           g.drawRect(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());


       } // end method draw

   } // end class MyLine

myline, my oval, myrectangle are all the same.

MYSHAPE; (SUPERCLASS)

public class MyShape {
   // instance variables
   public int x1, y1, x2, y2;
   public Color myColor;
   public boolean flag;
  
   //constructors
   public MyShape() {
       // the default
       this.x1 = 0;
       this.y1 = 0;
       this.x2 = 0;
       this.y2 = 0;
       this.myColor = Color.BLACK;
       this.flag = false;
   } // end default constructor

   public MyShape(int x1, int y1, int x2, int y2, Color color, boolean flag) {
       this.x1 = x1;
       this.y1 = y1;
       this.x2 = x2;
       this.y2 = y2;
       this.myColor = color;
       this.flag = flag;
   } // 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 String toString () {
       return "Shape with x1="+this.x1+" y2="+this.y2+" x2="+this.x2+" y2="+this.y2;
   }
  


} // end method MyShape

TEST DRAW(MAIN)

public class TestDraw {
  
   public static void main(String[] args) {
       // declare local variable/objects, using constants for the window dimensions
       MyShape Shape = new MyShape();
               final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
               DrawPanel panel = new DrawPanel (); // call the constructor creating MyLine objects
               JFrame app = new JFrame (); // the window and its components
               JLabel label = new JLabel (panel.getStatusBar());
               app.setLayout(new BorderLayout());
              
               // specify what is done when the user 'presses' the terminate icon in the title bar      
               app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
              
              
               // panel.add( label, BorderLayout.SOUTH );
               // add the panel to the 'container' for the JFrame
               app.add(panel);
               app.add(label, BorderLayout.SOUTH);
      

              
               // set the size of the drawing area
               app.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
               // finally, set the flag that tells the Java Virtual Machine to call the 'paintComponent'
               app.setVisible (true); // show the window
              
           } // end method main

   } // end class TestDraw

Explanation / Answer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.security.SecureRandom;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


class DrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
  
    private SecureRandom randomNumbers = new SecureRandom ();
    private MyLine lines [];
    private MyRectangle rectangles [];
    private MyOval ovals[];
    JFrame app = new JFrame ();
    // the constructor
    public DrawPanel () {
        final int NUMBER_COLORS = 256; // (0-255 COLOR VALUES)
        final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
        int x1, y1, x2, y2;
        Color color;
      
        // set background color
        setBackground (Color.MAGENTA);
        setBackground (Color.WHITE);
      
        // allocate the array
        lines = new MyLine [5 + randomNumbers.nextInt (5)];
        rectangles = new MyRectangle[5 + randomNumbers.nextInt (5)];
        ovals = new MyOval[5 + randomNumbers.nextInt (5)];
      
        // create the MyLine objects
        for (int count = 0; count < lines.length; count++) {
            // generate random coordinates (0 to 299)
            x1 = randomNumbers.nextInt (WINDOW_WIDTH);
            y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
            x2 = randomNumbers.nextInt (WINDOW_WIDTH);
            y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
          
            //display some output intended as a 'diagnostic' output
            // System.out.printf ("x1=%d, y1 = %d ", x1, y1);
          
            // generate a random color
            color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
            boolean flag = randomNumbers.nextBoolean ();
            // Construct a MyLine object and add it reference to the array
            lines [count] = new MyLine (x1, y1, x2, y2, color, flag);
        } // end for loop to create MyLine objects
      
        for (int count = 0; count < rectangles.length; count ++) {
            x1 = randomNumbers.nextInt (WINDOW_WIDTH);
            y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
            x2 = randomNumbers.nextInt (WINDOW_WIDTH);
            y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
          
            color = new Color (randomNumbers.nextInt (NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
            boolean flag = randomNumbers.nextBoolean ();
          
            rectangles [count] = new MyRectangle (x1, y1, x2, y2, color, flag);
        } // end for loop rectangles
      
        for (int count = 0; count < ovals.length; count ++) {
            x1 = randomNumbers.nextInt (WINDOW_WIDTH);
            y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
            x2 = randomNumbers.nextInt (WINDOW_WIDTH);
            y2 = randomNumbers.nextInt(WINDOW_HEIGHT);
          
            color = new Color (randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS), randomNumbers.nextInt(NUMBER_COLORS));
            boolean flag = randomNumbers.nextBoolean ();
          
            ovals [count] = new MyOval (x1, y1, x2, y2, color, flag);
        } // end for loop ovals
      
    } // end drawPanel constructor
  
    // when time to paint the panel, draw the lines, rectangles, and ovals
    public void paintComponent (Graphics g) {
        // paint the parent first
        super.paintComponent (g);
      
        // draw the lines
        for (MyLine line : lines)
            line.draw (g);
        for (MyRectangle rect : rectangles)
            rect.draw(g);
        for (MyOval oval :ovals)
            oval.draw(g);
    } // end method paintComponent
  
    public String getStatusBar(){
        String Status=" Line: "+lines.length+" Rectangle: "+rectangles.length+" Oval: "+ovals.length;
               return Status;
       
    }

  
} // end class DrawPanel

class MyRectangle extends MyShape{
   public MyRectangle(){
       super();
   }

   public MyRectangle(int x1, int y1, int x2, int y2, Color color, boolean flag) {
            super(x1,y1,x2,y2,color,flag);
            this.flag = flag;
        }
   public int getUpperLeftX() {
            return Math.min(getX1(), getX2());
        }

        public int getUpperLeftY() {
            return Math.min(getY1(), getY2());
        }

        public int getWidth() {
            return Math.abs(getX1() - getX2());
        }

        public int getHeight() {
            return Math.abs(getY1() - getY2());
        }

          public void draw(Graphics g) {
       // TODO Auto-generated method stub
       if (flag) {
            g.fillRect(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());
            g.setColor(this.getColor());
        } else {
            g.setColor(Color.BLACK);
        }
        g.drawRect(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());
      
   }
}
class MyOval extends MyShape{
   public MyOval(){
       super();
   }
  
   public MyOval(int x1, int y1, int x2, int y2, Color color, boolean flag) {
        super(x1,y1,x2,y2,color,flag);
        this.flag = flag;
    }
      public int getUpperLeftX() {
            return Math.min(getX1(), getX2());
        }

        public int getUpperLeftY() {
            return Math.min(getY1(), getY2());
        }

        public int getWidth() {
            return Math.abs(getX1() - getX2());
        }

        public int getHeight() {
            return Math.abs(getY1() - getY2());
        }
   public void draw(Graphics g) {
       // TODO Auto-generated method stub
       if (flag) {
            g.fillOval(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());
            g.setColor(this.getColor());
        } else {
            g.setColor(Color.BLACK);
        }
        g.drawOval(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());
      
   }
}

class MyLine extends MyShape{


    // the constructors.
    public MyLine() {
        // the default
        super();
    } // end default constructor

    public MyLine(int x1, int y1, int x2, int y2, Color color, boolean flag) {
          super(x1,y1,x2,y2,color,flag);
    } // end constructor

    // sets/gets

    public int getUpperLeftX() {
        return Math.min(getX1(), getX2());
    }

    public int getUpperLeftY() {
        return Math.min(getY1(), getY2());
    }

    public int getWidth() {
        return Math.abs(getX1() - getX2());
    }

    public int getHeight() {
        return Math.abs(getY1() - getY2());
    }


    // draw the shape. the color object parameter is the 'graphics context'
    public void draw(Graphics g) {
       g.drawLine(getX1(),getY1(),getX2(),getY2());
    } // end method draw

} // end class MyLine

class MyShape {
    // instance variables
    public int x1, y1, x2, y2;
    public Color myColor;
    public boolean flag;
  
    //constructors
    public MyShape() {
        // the default
        this.x1 = 0;
        this.y1 = 0;
        this.x2 = 0;
        this.y2 = 0;
        this.myColor = Color.BLACK;
        this.flag = false;
    } // end default constructor

    public MyShape(int x1, int y1, int x2, int y2, Color color, boolean flag) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.myColor = color;
        this.flag = flag;
    } // 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 int getX1() {
        return x1;
    }

    public int getY1() {
        return y1;
    }

    public int getX2() {
        return x2;
    }

    public int getY2() {
        return y2;
    }
    public Color getColor() {
        return this.myColor;
    }

    public String toString () {
        return "Shape with x1="+this.x1+" y2="+this.y2+" x2="+this.x2+" y2="+this.y2;
    }
    public void draw(Graphics g){
      
    }
  


} // end method MyShape

public class TestDraw {
  
    public static void main(String[] args) {
        // declare local variable/objects, using constants for the window dimensions
        MyShape Shape = new MyRectangle(2,4,3,2,Color.GREEN,true);
        final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
        DrawPanel panel = new DrawPanel (); // call the constructor creating MyLine objects
        JFrame app = new JFrame (); // the window and its components
        JLabel label = new JLabel (panel.getStatusBar());
        app.setLayout(new BorderLayout());
                  
        // specify what is done when the user 'presses' the terminate icon in the title bar      
        app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
                  
                  
        // panel.add( label, BorderLayout.SOUTH );
        // add the panel to the 'container' for the JFrame
        app.add(panel);
        app.add(label, BorderLayout.SOUTH);
                     

              
                // set the size of the drawing area
          app.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                // finally, set the flag that tells the Java Virtual Machine to call the 'paintComponent'
          app.setVisible (true); // show the window
             
              
            } // end method main

    } // end class TestDraw