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: 3734757 • 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 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 that the length of the edge of each square is between 7 and 17 inclusively. Display 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. Redisplay the information for the 10 Square objects, i.e., the lengths of the edges, diameters, perimeters, and areas on the screen.

Explanation / Answer

Please find the below Square and Client classes. Square class implements FigureGeometry to override required methods.

Square class also implements Comparable inteface which will override compareTo method that will be used to sort the Square class based on the edge.

Please find the below output. Revert back in case of any concerns.

****** Square ******


class Square implements FigureGeometry, Comparable<Square> {
private double edge;// edge is the length of one edge in a square.
// the default constructor that sets 1 to edge.
Square() {
edge = 1;
}
// a constructor that sets r to edge.
Square(double r) {
edge = r;
}
// the getter method that returns edge.
public double getEdge() {
return edge;
}
public double diameter() {
return edge * squareroot2;
}
public double perimeter() {
return 4 * edge;
}
public double area() {
return (edge * edge);
}
public int compareTo(Square square) {
if (this.edge > square.getEdge()) {
return 1;
} else if (this.edge < square.getEdge()) {
return -1;
} else {
return 0;
}
}
}

****** Client Class ******


import java.util.Arrays;
public class Client {
public static void main(String[] args) {
Square squares[] = new Square[10];
int min = 7;
int max = 17;
for (int i = 0; i < 10; i++) {
double edge = (Math.random() * (max - min) + min);
Square square = new Square(edge);
squares[i] = square;
}
System.out.println("************** Before Sorting ************** ");
display(squares);
Arrays.sort(squares);
System.out.println(" ************** After Sorting ************** ");
display(squares);
}
public static void display(Square squares[]) {
for (int i = 0; i < 10; i++) {
Square square = squares[i];
System.out.println("Lengths of the Edges: "+square.getEdge());
System.out.println("Diameter: "+square.diameter());
System.out.println("Perimeter: "+square.perimeter());
System.out.println("Area: "+square.area());
System.out.println();
}
}
}

****** Output *****

************** Before Sorting **************

Lengths of the Edges: 8.44806373566879
Diameter: 11.945562122235668
Perimeter: 33.79225494267516
Area: 71.3697808819221

Lengths of the Edges: 8.079311574953092
Diameter: 11.424146566983671
Perimeter: 32.31724629981237
Area: 65.27527552517101

Lengths of the Edges: 10.585692489024321
Diameter: 14.96816917948039
Perimeter: 42.342769956097285
Area: 112.05688547218593

Lengths of the Edges: 11.766307010290326
Diameter: 16.637558112550522
Perimeter: 47.065228041161305
Area: 138.4459806604073

Lengths of the Edges: 7.750563695781695
Diameter: 10.959297065835317
Perimeter: 31.00225478312678
Area: 60.07123760236921

Lengths of the Edges: 11.074423777883661
Diameter: 15.659235221927496
Perimeter: 44.297695111534644
Area: 122.64286201215502

Lengths of the Edges: 8.798070734866856
Diameter: 12.440472019101733
Perimeter: 35.192282939467425
Area: 77.40604865572062

Lengths of the Edges: 14.277790219619693
Diameter: 20.188795370542245
Perimeter: 57.11116087847877
Area: 203.85529355546774

Lengths of the Edges: 16.8788785696512
Diameter: 23.866734297486794
Perimeter: 67.5155142786048
Area: 284.89654176903053

Lengths of the Edges: 16.141236560653937
Diameter: 22.823708496764667
Perimeter: 64.56494624261575
Area: 260.5395177069913


************** After Sorting **************

Lengths of the Edges: 7.750563695781695
Diameter: 10.959297065835317
Perimeter: 31.00225478312678
Area: 60.07123760236921

Lengths of the Edges: 8.079311574953092
Diameter: 11.424146566983671
Perimeter: 32.31724629981237
Area: 65.27527552517101

Lengths of the Edges: 8.44806373566879
Diameter: 11.945562122235668
Perimeter: 33.79225494267516
Area: 71.3697808819221

Lengths of the Edges: 8.798070734866856
Diameter: 12.440472019101733
Perimeter: 35.192282939467425
Area: 77.40604865572062

Lengths of the Edges: 10.585692489024321
Diameter: 14.96816917948039
Perimeter: 42.342769956097285
Area: 112.05688547218593

Lengths of the Edges: 11.074423777883661
Diameter: 15.659235221927496
Perimeter: 44.297695111534644
Area: 122.64286201215502

Lengths of the Edges: 11.766307010290326
Diameter: 16.637558112550522
Perimeter: 47.065228041161305
Area: 138.4459806604073

Lengths of the Edges: 14.277790219619693
Diameter: 20.188795370542245
Perimeter: 57.11116087847877
Area: 203.85529355546774

Lengths of the Edges: 16.141236560653937
Diameter: 22.823708496764667
Perimeter: 64.56494624261575
Area: 260.5395177069913

Lengths of the Edges: 16.8788785696512
Diameter: 23.866734297486794
Perimeter: 67.5155142786048
Area: 284.89654176903053