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

The following FigureGeometry is an interface. You are asked to define a class ca

ID: 3887778 • Letter: T

Question

The following FigureGeometry is an interface. You are asked to define a class called Square to implement the interface. The skeleton of the class Square is given below.

public interface FigureGeometry

{

public static final double squareroot2 = 1.414;

            public abstract double perimeter( );

            pubic abstract double area( );

}

class Square implements FigureGeometry

{

    private double edge; //edge is the length of one edge in a square.

   

    Square( ) { } //the default constructor that sets 1.0 to edge.

    Square(double r) { } //a constructor that sets r to edge.

    public double getEdge( ) { } //the getter method that returns edge.

    public double diameter( ){ } //the method that returns the diameter of a square.

                                                  //consider using squareroot2 when defining this method.

    public double perimeter( ) { } //the method that returns the perimeter of a square.

    public double area( ){ } //the method that returns the area of a square.

}

Moreover, you are asked to write a client program using the Square class. Within your client program you are asked to define a Square array of size 100. Using Math.random( )

and for … loop to create 100 Square objects to be stored in the array such that the edge of each square is between 107 and 507 inclusively. Among the 100 Square objects, find the location of the square that has the largest perimeter and display its information, i.e., length of the edge, diameter, perimeter, and area, on the console. Also, find the location of the square that has the smallest perimeter and display its information, i.e., length of the edge, diameter, perimeter, and area, on the console.

heres my code:

public interface FigureGeometry {

   public static final double squareroot2 = 1.414;

   public abstract double perimeter();

   public abstract double area();

}

public class Square implements FigureGeometry{

   private double edge;

   Square() {

   this.edge = 1;

   }

   Square(double r) {

   this.edge = r;

   }

   public double getEdge() {

   return this.edge;

   }

   public double diameter() {

   return squareroot2 * getEdge();

   }

  

   public double perimeter() {

   return 4 * getEdge();

   }

  

   public double area() {

   return getEdge() * getEdge();

   }

}

import java.util.Random;

public class SquareClient {

   public static void main(String[] args) {

   int sA[] = new int[100];

   Random r=new Random();

   for(int i=0;i<100;i++){

       sA[i] = r.nextInt((507-107) + 1)+107;

  

   }

  

   int max=0;

   for(int i=0;i<100;i++) { // not working

       if(sA[i].perimeter()>sA[max].perimeter()){

   max=i;

     

   }

   }

   }

   }

Explanation / Answer

Tested on Eclipse

/****************************FigureGeometry.java***************************/

/**

* The Interface FigureGeometry.

*/

public interface FigureGeometry {

/** The Constant squareroot2. */

public static final double squareroot2 = 1.414;

/**

* Perimeter.

*

* @return the double

*/

public abstract double perimeter();

/**

* Area.

*

* @return the double

*/

public abstract double area();

}

/*************************Square.java**********************/

/**

* The Class Square.

*/

public class Square implements FigureGeometry {

/** The edge. */

private double edge;

/**

* Instantiates a new square.

*/

Square() {

this.edge = 1;

}

/**

* Instantiates a new square.

*

* @param r the r

*/

Square(double r) {

this.edge = r;

}

/**

* Gets the edge.

*

* @return the edge

*/

public double getEdge() {

return this.edge;

}

/**

* Diameter.

*

* @return the double

*/

public double diameter() {

return squareroot2 * getEdge();

}

/* (non-Javadoc)

* @see geo.FigureGeometry#perimeter()

*/

public double perimeter() {

return 4 * getEdge();

}

/* (non-Javadoc)

* @see geo.FigureGeometry#area()

*/

public double area() {

return getEdge() * getEdge();

}

}

/********************************SquareClient.java*****************************/

import java.util.Random;

/**

* The Class SquareClient.

*/

public class SquareClient {

/**

* The main method.

*

* @param args

* the arguments

*/

public static void main(String[] args) {

// creating square type array of 100

Square sA[] = new Square[100];

Random r = new Random();

for (int i = 0; i < 100; i++) {

int randomNum = r.nextInt((507 - 107) + 1) + 107;

sA[i] = new Square(randomNum);

}

int max = 0;

int min = 0;

for (int i = 0; i < 100; i++) { // not working

if (sA[i].perimeter() > sA[max].perimeter()) {

max = i;

}

if (sA[i].perimeter() < sA[min].perimeter()) {

min = i;

}

}

System.out.println("Largest perimeter information ");

Square maxSquare = sA[max];

System.out.println("Edge: " + maxSquare.getEdge() + " Diameter: " + maxSquare.diameter() + " Perimeter: "

+ maxSquare.perimeter() + " Area: " + maxSquare.area());

System.out.println("Smallest perimeter information ");

Square minSquare = sA[min];

System.out.println("Edge: " + minSquare.getEdge() + " Diameter: " + minSquare.diameter() + " Perimeter: "

+ minSquare.perimeter() + " Area: " + minSquare.area());

}

}

/*****************************output*************************/

Largest perimeter information
Edge: 498.0 Diameter: 704.1719999999999 Perimeter: 1992.0 Area: 248004.0
Smallest perimeter information
Edge: 113.0 Diameter: 159.78199999999998 Perimeter: 452.0 Area: 12769.0

Thanks a lot. Please let me know if you have any doubt.

Note: Output may differ