Create a test program to create an array of shapes


Discuss the below:

Q: Create a test program that creates an array of shapes consisting of several instances of Circ, Rect, and Octagon and displays their area and perimeter.

* Create a new Octagon object by using the clone() method and compare the two objects using the compareTo() method.

* Include in your test program the method that returns the largest object in an array of objects. The method header is
public static Object max(Object[] a)

All the objects are assumed to be the instances of the Comparable interface.

* Apply the method max to your array of shapes to find and display the maximum one.

The project should include the following classes: Circ, Rect, Shape, Octagon, and Test (the main class containing the main() method). Redesign the first two classes to implement the Comparable and Cloneable interfaces.

1 public class Rect extends Shape
{
private int width;
private int height;

public Rect()
{
super();
width = height = 0;
}

public Rect(int xLoc, int yLoc, int width, int height)
{
super(xLoc, yLoc);
this.width = width;
this.height = height;
}

public double getArea()
{
return width * height;
}

public void stretchBy(double factor)
{
width = (int)Math.round(width * factor);
height = (int)Math.round(height * factor);
}

public String toString()
{
String str = "RECTANGLE \n"
+ super.toString()
+ "Width & Height: " + width + " " + height + "\n"
+ "Area: " + getArea();
return str;
}
}

abstract public class Shape
{
  private int xPos;
  private int yPos;

  public Shape()
  {
     xPos = 0;
     yPos = 0;
  }

  public Shape(int xPos, int yLoc)
  {
     this.xPos = xPos;
     yPos = yLoc;
  }

  public final int getX()
  {
     return xPos;
  }

  public final int getY()
  {
     return yPos;
  }

  public void moveTo(int newX, int newY)
  {
     xPos += newX;
     yPos += newY;
  }

  abstract public double getArea();

  abstract public void stretchBy(double factor);
  
  public String toString()
  {
     return "(X,Y) Position: (" + xPos + "," + yPos + ") \n";
  }
}
abstract public class Shape
{
private int xPos;
private int yPos;

public Shape()
{
xPos = 0;
yPos = 0;
}

public Shape(int xPos, int yLoc)
{
this.xPos = xPos;
yPos = yLoc;
}

public final int getX()
{
return xPos;
}

public final int getY()
{
return yPos;
}

public void moveTo(int newX, int newY)
{
xPos += newX;
yPos += newY;
}

abstract public double getArea();

abstract public void stretchBy(double factor);

public String toString()
{
return "(X,Y) Position: (" + xPos + "," + yPos + ") \n";
}
}

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Create a test program to create an array of shapes
Reference No:- TGS01931860

Now Priced at $30 (50% Discount)

Recommended (98%)

Rated (4.3/5)