Question

[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

Semicircles

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:

center contained: false
Expected: false
E contained: false
Expected: false
W contained: false
Expected: false
N contained: false
Expected: false
false
Expected: true
true
Expected: false
black & green intersect: true
Expected: true
green & black intersect: false
Expected: true
black & red intersect : false
Expected: true
red & black intersect : true
Expected: true
red & green intersect: true
Expected: true
green & red intersect: true
Expected: true
red & blue intersect : true
Expected: false
blue & red intersect : false
Expected: false
black & blue intersect : false
Expected: false
blue & black intersect : false
Expected: false

Help me please. I'm stuck in this problem like about 10hrs

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
  
}
  
}

Add a comment
Know the answer?
Add Answer to:
[Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...

    please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{    private Node current;    private Node parent;    private Node grandparent;    private Node header;    private Node...

  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** *...

    Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** * A class that models a bounding ball */ public class Ball {    // Minimum and maximum x and y values for the screen    private static double minX;    private static double minY;    private static double maxX;    private static double maxY;    private Point center;    private double radius;    // Every time the ball moves, its x position changes by...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • Java : I keep getting something wrong in my loadfile method, how do I fix my...

    Java : I keep getting something wrong in my loadfile method, how do I fix my code? Thank you. here is what is contained in the text file (WorldSeriesWinners.txt) 112 1903 Boston Americans 1904 No World Series 1905 New York Giants 1906 Chicago White Sox 1907 Chicago Cubs 1908 Chicago Cubs 1909 Pittsburgh Pirates 1910 Philadelphia Athletics 1911 Philadelphia Athletics 1912 Boston Red Sox 1913 Philadelphia Athletics 1914 Boston Braves 1915 Boston Red Sox 1916 Boston Red Sox 1917 Chicago...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT