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: 3678564 • 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( );

public 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 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 10. Using Math.random() and for ... loop to create 10 Square objects to be stored in the array such thatt he length of the edge of each square is between 7 and 17 inclusively. Dispaly the information for the 10 Square objects, i.e., the lengths of the edges, diameters, perimeters, and areas on the screen. Sort the 10 Square objects in increasing order based on the lengths of the edges of the 10 Square objects. Redispaly the information for the 10 Square objects, i.e., the lengths of the edges, diameters, perimeters, and areas on the screen.

Explanation / Answer

interface

FigureGeometry.Java

package com.test.chegg;

/**
* Interface for Square program
*
* @author yourName
*
*/
public interface FigureGeometry {
   public static final double squareroot2 = 1.414;

   public abstract double perimeter();

   public abstract double area();
}

Classes

Square.java

package com.test.chegg;

/**
* Square class to find diameter,area,perimeter of a square
*
* @author yourName
*
*/
public class Square implements FigureGeometry, Comparable<Square> {
   // edge is the length of one edge in a square.
   private double edge;

   // the default constructor that sets 1 to edge.
   Square() {
       this.edge = 1;
   }

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

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

   // the method that returns the diameter of a square.
   // consider using squareroot2 when defining this method.
   public double diameter() {
       return squareroot2 * getEdge();
   }

   // the method that returns the perimeter of a square.
   public double perimeter() {
       return 4 * getEdge();
   }

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

   public String toString() {
       return "Length Of the Edge=" + String.format("%.2f", getEdge())
               + " Diameter=" + String.format("%.2f", diameter())
               + " Perimeter=" + String.format("%.2f", perimeter()) + " Area="
               + String.format("%.2f", area());
   }

   public int compareTo(Square square) {
       int compareLength = (int) ((Square) square).getEdge();

       // ascending order
       return (int) (this.edge - compareLength);

   }
}

SquareClient.java

package com.test.chegg;

import java.util.Arrays;

public class SquareClient {
   public static void main(String[] args) {
       Square[] sqareArray = new Square[10];

       int randomNum = 0;
       for (int i = 0; i < 10; i++) {
           randomNum = (int) (Math.random() * (17 - 7) + 7);
           Square square = new Square(randomNum);
           sqareArray[i] = square;
       }
       for (int i = 0; i < 10; i++) {
           System.out.println(sqareArray[i].toString());
       }

       System.out.println(" The Square Objects in increasing order:");
       Arrays.sort(sqareArray);
       for (int i = 0; i < 10; i++) {
           System.out.println(sqareArray[i].toString());
       }
   }
}

Output:

Length Of the Edge=10.00 Diameter=14.14 Perimeter=40.00 Area=100.00
Length Of the Edge=12.00 Diameter=16.97 Perimeter=48.00 Area=144.00
Length Of the Edge=7.00 Diameter=9.90 Perimeter=28.00 Area=49.00
Length Of the Edge=11.00 Diameter=15.55 Perimeter=44.00 Area=121.00
Length Of the Edge=15.00 Diameter=21.21 Perimeter=60.00 Area=225.00
Length Of the Edge=11.00 Diameter=15.55 Perimeter=44.00 Area=121.00
Length Of the Edge=11.00 Diameter=15.55 Perimeter=44.00 Area=121.00
Length Of the Edge=14.00 Diameter=19.80 Perimeter=56.00 Area=196.00
Length Of the Edge=11.00 Diameter=15.55 Perimeter=44.00 Area=121.00
Length Of the Edge=9.00 Diameter=12.73 Perimeter=36.00 Area=81.00


The Square Objects in increasing order:
Length Of the Edge=7.00 Diameter=9.90 Perimeter=28.00 Area=49.00
Length Of the Edge=9.00 Diameter=12.73 Perimeter=36.00 Area=81.00
Length Of the Edge=10.00 Diameter=14.14 Perimeter=40.00 Area=100.00
Length Of the Edge=11.00 Diameter=15.55 Perimeter=44.00 Area=121.00
Length Of the Edge=11.00 Diameter=15.55 Perimeter=44.00 Area=121.00
Length Of the Edge=11.00 Diameter=15.55 Perimeter=44.00 Area=121.00
Length Of the Edge=11.00 Diameter=15.55 Perimeter=44.00 Area=121.00
Length Of the Edge=12.00 Diameter=16.97 Perimeter=48.00 Area=144.00
Length Of the Edge=14.00 Diameter=19.80 Perimeter=56.00 Area=196.00
Length Of the Edge=15.00 Diameter=21.21 Perimeter=60.00 Area=225.00