Java PLEASE FIX MY CODE I think Im thinking in wrong way Im
[Java] PLEASE FIX MY CODE
I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code.
Here\'e the problem:
Write a class SemiCircle that represents the northern half of a circle in 2D space. A SemiCircle has center coordinates and a radius.
Define a constructor:
public SemiCircle(int centerX, int centerY, int theRadius)
Implement the following methods:
public boolean contains(int otherX, int otherY)
returns true if the point given by the coordinates is inside the SemiCircle. Otherwise it returns false. Note: the point will be contained in the SemiCircle if the distance from the center to the print is less than the radius and the point is above the diameter. A point on the circumference or diameter is not contained in the SemiCircle.
public boolean intersects( SemiCircle other)
returns true if the two SemiCircles intersect; otherwise it returns false. Two semicircles \"intersect\" if at least one, but not all three of the western, northern, and eastern extreme points of one semicircle are contained in the other. This means they do not intersect if one is completely contained in the other or if the extreme point only touches the other semicircle.
Look at the drawing below. The western, northern, and eastern extreme points of the top semicircle are labeled W, N, and E respectively.
In the lower drawing:
The three larger semicircles all intersect
The W point of the green one is inside the black one => green and black intersect.
The N point of the red one is inside the green one => green and red intersect.
The E point of the black one is inside the red one => black and red intersect.
The small blue semicircle does not intersect with any of the large ones.
The blue semicircle and the black one have no points in common => they do not intersect
The blue one and the green one have no points in common => they do not intersect
The blue one is completely contained in the red one => they do not intersect
Here\'e the tester code provided:
public class SemiCircleTester
{
public static void main(String[] args)
{
SemiCircle semi1 = new SemiCircle(100, 100, 60);
//test contains
//the center is not contained in the circle
System.out.println();
System.out.println(\"center contained: \" + semi1.contains(100, 100));
System.out.println(\"Expected: false\");
//the E W N points not contained
System.out.println(\"E contained: \" + semi1.contains(160, 100));
System.out.println(\"Expected: false\");
System.out.println(\"W contained: \" + semi1.contains(40, 100));
System.out.println(\"Expected: false\");
System.out.println(\"N contained: \" + semi1.contains(100, 40));
System.out.println(\"Expected: false\");
//point in semi-circle
System.out.println(semi1.contains(120, 75));
System.out.println(\"Expected: true\");
//point in whole circle but not in semi circle
System.out.println(semi1.contains(110, 120));
System.out.println(\"Expected: false\");
SemiCircle black = new SemiCircle(50, 150, 40);
SemiCircle red = new SemiCircle(100, 170, 40);
SemiCircle green = new SemiCircle(90, 140, 40);
SemiCircle blue = new SemiCircle(115, 165, 15);
System.out.println(\"black & green intersect: \" + black.intersects(green));
System.out.println(\"Expected: true\");
System.out.println(\"green & black intersect: \" + green.intersects(black));
System.out.println(\"Expected: true\");
System.out.println(\"black & red intersect : \" +black.intersects(red));
System.out.println(\"Expected: true\");
System.out.println(\"red & black intersect : \" +red.intersects(black));
System.out.println(\"Expected: true\");
System.out.println(\"red & green intersect: \" + red.intersects(green));
System.out.println(\"Expected: true\");
System.out.println(\"green & red intersect: \" + green.intersects(red));
System.out.println(\"Expected: true\");
System.out.println(\"red & blue intersect : \" + red.intersects(blue));
System.out.println(\"Expected: false\");
System.out.println(\"blue & red intersect : \" + blue.intersects(red));
System.out.println(\"Expected: false\");
System.out.println(\"black & blue intersect : \" + black.intersects(blue));
System.out.println(\"Expected: false\");
System.out.println(\"blue & black intersect : \" + blue.intersects(black));
System.out.println(\"Expected: false\");
}
}
Here\'e my code:
public class SemiCircle
{
private int xCenter;
private int yCenter;
private int radius;
public SemiCircle(int centerX, int centerY, int theRadius)
{
xCenter = centerX;
yCenter = centerY;
radius = theRadius;
}
public boolean contains(int otherX, int otherY)
{
double distance = Math.sqrt(Math.pow((otherX - xCenter), 2) + Math.pow((otherY - yCenter), 2));
if(xCenter - radius < otherX && otherX < xCenter + radius && Math.abs(distance) < radius && otherY > yCenter)
{
return true;
}
return false;
}
public boolean intersects(SemiCircle other)
{
public class SemiCircle
{
private int xCenter;
private int yCenter;
private int radius;
public SemiCircle(int centerX, int centerY, int theRadius)
{
xCenter = centerX;
yCenter = centerY;
radius = theRadius;
}
public boolean contains(int otherX, int otherY)
{
double distance = Math.sqrt(Math.pow((otherX - xCenter), 2) + Math.pow((otherY - yCenter), 2));
if(xCenter - radius < otherX && otherX < xCenter + radius && Math.abs(distance) < radius && otherY > yCenter)
{
return true;
}
return false;
}
public boolean intersects(SemiCircle other)
{
if(xCenter - radius < other.xCenter - other.radius && other.xCenter - other.radius < xCenter + radius
&& yCenter - radius < other.yCenter && other.yCenter < yCenter)
{
return true;
}
if(xCenter - radius < other.xCenter + other.radius && other.xCenter + other.radius < xCenter + radius
&& yCenter - radius < other.yCenter && other.yCenter < yCenter)
{
return true;
}
if(xCenter - radius < other.xCenter && other.xCenter < xCenter + radius
&& yCenter - radius < other.yCenter - other.radius && other.yCenter - other.radius < yCenter)
{
return true;
}
return false;
}
}
}
}
Here\'s my output:
Help me please. I\'m stuck in this problem like about 10hrs
SemicirclesSolution
public class SemiCircle
{
private int xCenter;
private int yCenter;
private int radius;
public SemiCircle(int centerX, int centerY, int theRadius){
xCenter = centerX;
yCenter = centerY;
radius = theRadius;
}
public boolean contains(int otherX, int otherY){
double distance = Math.sqrt(Math.pow((otherX - xCenter), 2) + Math.pow((otherY - yCenter), 2));
if((otherY < yCenter) && (xCenter - radius) < otherX && otherX < (xCenter + radius)
&& Math.abs(distance) < radius){
return true;
}
return false;
}
public boolean intersects(SemiCircle other) {
// These three boolean values indicate whether the Eastern, Western and Northern
// points of the other semicircle are contained in this semicircle.
boolean otherEContained = contains(other.xCenter - other.radius, other.yCenter),
otherWContained = contains(other.xCenter + other.radius, other.yCenter),
otherNContained = contains(other.xCenter, other.yCenter - other.radius); // Change made here
// These three boolean values indicate whether the Eastern, Western and Northern
// points of this semicircle are contained in the other semicircle.
boolean EContained = other.contains(xCenter - radius, yCenter),
WContained = other.contains(xCenter + radius, yCenter),
NContained = other.contains(xCenter, yCenter - radius);// Change made here
//No intersection if one semicircle is contained inside the other semicircle
if((otherEContained && otherWContained && otherNContained ) ||
(EContained && WContained && NContained) ){
return false;
}
// Now if even 1 point contained in the other semicircle then intersection
if((otherEContained || otherWContained || otherNContained ) ||
(EContained || WContained || NContained)){
return true;
}
// No point of either semicircle contained in the other semicircle, No InterSection
return false;
}
}
![[Java] PLEASE FIX MY CODE I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code. Here\'e the problem: Write a class SemiCircle that repre [Java] PLEASE FIX MY CODE I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code. Here\'e the problem: Write a class SemiCircle that repre](/WebImages/45/java-please-fix-my-code-i-think-im-thinking-in-wrong-way-im-1143382-1761613785-0.webp)
![[Java] PLEASE FIX MY CODE I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code. Here\'e the problem: Write a class SemiCircle that repre [Java] PLEASE FIX MY CODE I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code. Here\'e the problem: Write a class SemiCircle that repre](/WebImages/45/java-please-fix-my-code-i-think-im-thinking-in-wrong-way-im-1143382-1761613785-1.webp)
![[Java] PLEASE FIX MY CODE I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code. Here\'e the problem: Write a class SemiCircle that repre [Java] PLEASE FIX MY CODE I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code. Here\'e the problem: Write a class SemiCircle that repre](/WebImages/45/java-please-fix-my-code-i-think-im-thinking-in-wrong-way-im-1143382-1761613785-2.webp)
![[Java] PLEASE FIX MY CODE I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code. Here\'e the problem: Write a class SemiCircle that repre [Java] PLEASE FIX MY CODE I think I\'m thinking in wrong way. I\'m not sure what is wrong with my code. Here\'e the problem: Write a class SemiCircle that repre](/WebImages/45/java-please-fix-my-code-i-think-im-thinking-in-wrong-way-im-1143382-1761613785-3.webp)