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

Question 2 Write a class Square, which inherits from class Rectangle, to represe

ID: 3760229 • Letter: Q

Question

Question 2 Write a class Square, which inherits from class Rectangle, to represent a square with a given width and center point. Class Square should have two constructors: • A constructor to make a square with a given width, centered at a given point. • A constructor with no arguments that makes the unit square – this constructor must call the first constructor. Write a main program that creates the unit square, and prints its width and area by calling the getWidth and getArea methods. Carefully explain in detail how the unit square is constructed, and how the getWidth and getArea methods are called. using java

Explanation / Answer

class Rectangle
{
   double wid, cen;
}
class Square extends Rectangle
{
   Square(double w, double c)
   {
       wid=w;
       cen=c;
   }

   Square()
   {
       this(12,12);
   }
  
   double getWidth()
   {
       return wid;
   }

   double getArea()
   {
       double a=wid*wid;
       return a;
   }
}

class rect_sq
{
   public static void main(String args[])
   {
       Square unit = new Square();

       System.out.println("Width is = " +

unit.getWidth());
       System.out.println("Area is = " +

unit.getArea());
   }
}

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