The following FigureGeometry is an interface. You are asked to define a class ca
ID: 3737085 • 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. (In Java language, please).
Explanation / Answer
Explanation:
Please find the below FigureGeometry Interface and Square Class which implements FigureGeometry interface. Square class also implements Comparable interface to overrides compareTo method. This method will be used to sort Square objects based on its edge instance variable.
Also find the client Driver class which has main and display methods. display(Square squares[]) method will display details of 10 square object before and after sorting.
Please check and revert in case anything needs to be change.
Program:
FigureGeometry Interface
public interface FigureGeometry {
public static final double squareroot2 = 1.414;
public abstract double perimeter( );
public abstract double area( );
}
Square Class
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;
}
}
}
Driver Class
import java.util.Arrays;
public class Driver {
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: 15.79195992726913
Diameter: 22.32983133715855
Perimeter: 63.16783970907652
Area: 249.38599834447402
Lengths of the Edges: 16.41805335289557
Diameter: 23.215127440994333
Perimeter: 65.67221341158228
Area: 269.5524758985254
Lengths of the Edges: 14.500453853075657
Diameter: 20.503641748248977
Perimeter: 58.001815412302626
Area: 210.26316194517665
Lengths of the Edges: 8.41041770027971
Diameter: 11.89233062819551
Perimeter: 33.64167080111884
Area: 70.73512589317825
Lengths of the Edges: 16.071566226095836
Diameter: 22.72519464369951
Perimeter: 64.28626490438334
Area: 258.29524095978434
Lengths of the Edges: 14.483120739865782
Diameter: 20.479132726170214
Perimeter: 57.93248295946313
Area: 209.76078636553038
Lengths of the Edges: 14.981506713683004
Diameter: 21.183850493147766
Perimeter: 59.926026854732015
Area: 224.44554341212893
Lengths of the Edges: 9.989134842766337
Diameter: 14.124636667671599
Perimeter: 39.95653937106535
Area: 99.78281490696844
Lengths of the Edges: 16.77238627419215
Diameter: 23.7161541917077
Perimeter: 67.0895450967686
Area: 281.31294133070924
Lengths of the Edges: 15.309082543862624
Diameter: 21.64704271702175
Perimeter: 61.2363301754505
Area: 234.36800833479933
************** After Sorting **************
Lengths of the Edges: 8.41041770027971
Diameter: 11.89233062819551
Perimeter: 33.64167080111884
Area: 70.73512589317825
Lengths of the Edges: 9.989134842766337
Diameter: 14.124636667671599
Perimeter: 39.95653937106535
Area: 99.78281490696844
Lengths of the Edges: 14.483120739865782
Diameter: 20.479132726170214
Perimeter: 57.93248295946313
Area: 209.76078636553038
Lengths of the Edges: 14.500453853075657
Diameter: 20.503641748248977
Perimeter: 58.001815412302626
Area: 210.26316194517665
Lengths of the Edges: 14.981506713683004
Diameter: 21.183850493147766
Perimeter: 59.926026854732015
Area: 224.44554341212893
Lengths of the Edges: 15.309082543862624
Diameter: 21.64704271702175
Perimeter: 61.2363301754505
Area: 234.36800833479933
Lengths of the Edges: 15.79195992726913
Diameter: 22.32983133715855
Perimeter: 63.16783970907652
Area: 249.38599834447402
Lengths of the Edges: 16.071566226095836
Diameter: 22.72519464369951
Perimeter: 64.28626490438334
Area: 258.29524095978434
Lengths of the Edges: 16.41805335289557
Diameter: 23.215127440994333
Perimeter: 65.67221341158228
Area: 269.5524758985254
Lengths of the Edges: 16.77238627419215
Diameter: 23.7161541917077
Perimeter: 67.0895450967686
Area: 281.31294133070924