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
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;
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started