Write a static method named minimumArea that accepts an array of Point objects a
ID: 3580899 • Letter: W
Question
Write a static method named minimumArea that accepts an array of Point objects as a parameter, and returns a double; the area of the smallest rectangle that could contain all of the points in the array. Assume that the Point class has get tX() and getY() methods that return a double. Each point represents a coordinate. You can calculate the minimum area by find the highest and lowest x value, and the highest and value. The area is the difference between the highest and lowest x values, multiplied by the between the highest and lowest y values.Explanation / Answer
Point.java
public class Point {
// creating instance variables
private int x;
private int y;
// Parameterized constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
}
__________________________
Test.java
public abstract class Test {
public static void main(String[] args) {
//Creating an array of POint class objects
Point point[]={new Point(3,4),new Point(-3,-4),new Point(-2,4),new Point(-4,4),new Point(4,-5),new Point(-1,-5)};
//calling the method by passing the point array as input argument
double area=minimumArea(point);
//Displaying the area
System.out.println("Area is :"+area);
}
//This method will find the area of the rectangle
private static double minimumArea(Point[] point) {
//declaring and assigning the values to the variables
int minx=point[0].getX(),miny=point[0].getY(),maxx=point[0].getX(),maxy=point[0].getY();
//This for loop will find the minx,miny,maxx and maxy values
for(int i=0;i<point.length;i++)
{
if(point[i].getX()<minx)
minx=point[i].getX();
if(point[i].getY()<miny)
miny=point[i].getY();
if(point[i].getX()>maxx)
maxx=point[i].getX();
if(point[i].getY()>maxy)
maxy=point[i].getY();
}
//calculating the area
double area=(maxx-minx)*(maxy-miny);
return area;
}
}
________________________
Output:
Area is :72.0
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.