Java Programming I\'m having hard time with the following assignment and I\'m un
ID: 3755819 • Letter: J
Question
Java Programming
I'm having hard time with the following assignment and I'm unsure how to do it.
Write a program that produces the following example output...
Your program must meet the following specifications:
Write a method static void reportPoint(double x, double y) that prints the details for a single point. For example it would print, without a newline: Point(1.0, 1.0)
Write a method static void reportCircle(double x, double y, double r) that prints the details for a single circle. For example it would print, without a newline: Circle(0.0, 0.0) Radius: 3.0
Write a method static void reportRectangle(double left, double top, double width, double height) that prints the details for a single rectangle.
The report of the rectangle should show the left, top, right, and bottom values, rather than the left, top, width, and height.
Write a method static boolean isPointInsideCircle(double ptX, double ptY, double circleX, double circleY, double circleRadius) that tests if a point is inside a circle.
Write a method static boolean isPointInsideRectangle(double ptX, double ptY, double rLeft, double rTop, double rWidth, double rHeight) that tests if a point is inside a rectangle.
In the main method use the following statements to initialize the point, circle, and rectangle data.
In the main method, loop over the circles, then the points, reporting on each as appropriate.
In the main method, loop over the rectangles, then the points, reporting on each as appropriate.
The first point is ptX[0] and ptY[0], the first circle is circleX[0], circleY[0], and circleRadius[0], and so on.
The right hand side of the rectangle is left + width, and the bottom of the rectangle is top - height. The following diagram illustrates the coordinate system for this assignment...
Explanation / Answer
Here you go:
It includes all the code needed as per instructions mentioned above:
public class Geometry {
// This method prints the details of a point
static void reportPoint(double x, double y) {
System.out.print("Point(" + x + "," + y + ")");
}
// This method prints the details of a circle
static void reportCircle(double x, double y, double r) {
System.out.print("Circle(" + x + ", " + y + ") Radius: " + r);
}
// This method prints the details of a rectangle
static void reportRectangle(double left, double top, double width, double height) {
double right = left + width;
double bottom = top = height;
System.out.print("Rectangle(" + left + "," + top + "," + right + "," + bottom + ")");
}
// This method checks if a point is inside circle or not using the formula
// d=((x2x1)^2+(y2y1)^2)
static boolean isPointInsideCircle(double ptX, double ptY, double circleX, double circleY, double circleRadius) {
boolean isInsideCircle = true; // Setting a variable true by default
double x = circleX - ptX; // Calculating the value of (x2-x1)
double y = circleY - ptY; // Calculating the value of (y2-y1)
double xSqr = Math.pow(x, 2); // Calculating x square
double ySqr = Math.pow(y, 2); // Calculating y square
double distanceBetweenPoints = Math.sqrt(xSqr + ySqr); // Calculating the distance between points
// Condition check if distance between points is lesser than radius then the
// point is inside circle
// else if greater it is outside
if (distanceBetweenPoints >= circleRadius) {
isInsideCircle = false;
}
return isInsideCircle;
}
// This method will calculate the area of each triangle
public static double areaOfEachTriangle(double xa,double ya, double xb, double yb, double px, double py)
{
return (float)Math.abs((xa * (yb - py) +
xb * (py - ya) + px * (ya - yb)) / 2.0);
}
// This method checks if a point is inside a rectangle or not
static boolean isPointInsideRectangle(double ptX, double ptY, double rLeft, double rTop, double rWidth,
double rHeight) {
boolean isInsideRectangle = true; // Setting a variable true by default
// Calculating area of rectangle
double rRight = rLeft + rWidth;
double rBottom = rTop - rHeight;
double trinagle1Area = areaOfEachTriangle(rLeft,rTop,rRight, rTop, ptX, ptY);
double trinagle2Area = areaOfEachTriangle(rRight,rTop,rRight,rBottom, ptX, ptY);
double trinagle3Area = areaOfEachTriangle(rRight, rBottom,rLeft, rBottom, ptX, ptY);
double trinagle4Area = areaOfEachTriangle(rLeft, rBottom, rLeft, rTop, ptX, ptY);
double rectArea = rWidth * rHeight;
double triangleAreaSum = (trinagle1Area + trinagle2Area + trinagle3Area + trinagle4Area);
if (triangleAreaSum == rectArea)
return isInsideRectangle;
else
isInsideRectangle = false;
return isInsideRectangle;
}
public static void main(String[] args) {
double[] ptX = { 1, 2, 3, 4 };
double[] ptY = { 1, 2, 3, 4 };
double[] circleX = { 0, 5 };
double[] circleY = { 0, 5 };
double[] circleRadius = { 3, 3 };
double[] rectLeft = { -2.5, -2.5 };
double[] rectTop = { 2.5, 5.0 };
double[] rectWidth = { 6.0, 5.0 };
double[] rectHeight = { 5.0, 2.5 };
String checkValue = "";
for (int x = 0; x < circleX.length; x++) {
for (int y = 0; y < ptX.length; y++) {
if (Geometry.isPointInsideCircle(ptX[y], ptY[y], circleX[x], circleY[x], circleRadius[x])) {
checkValue = " is inside ";
} else {
checkValue = " is outside ";
}
Geometry.reportPoint(ptX[y], ptY[y]);
System.out.print(checkValue);
Geometry.reportCircle(circleX[x], circleY[x], circleRadius[x]);
System.out.println("");
}
}
for (int recX = 0; recX < rectLeft.length; recX++) {
for (int y = 0; y < ptX.length; y++) {
if (Geometry.isPointInsideRectangle(ptX[y], ptY[y], rectLeft[recX], rectTop[recX], rectWidth[recX],
rectHeight[recX])) {
checkValue = " is inside ";
} else {
checkValue = " is outside ";
}
Geometry.reportPoint(ptX[y], ptY[y]);
System.out.print(checkValue);
Geometry.reportRectangle(rectLeft[recX], rectTop[recX], rectWidth[recX], rectHeight[recX]);
System.out.println("");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.