Question: What's wrong with my code and how do I fix it? :( The last comment line near the bottom indicates that one of the case
What's wrong with my code and how do I fix it? :(
The last comment line near the bottom indicates that one of the case checks for my code is wrong.
System.out.println(c1 + " touches " + c4 + " = " + c1.touches(c4));
The above line should return true, but instead it returns false. Please take a look at my touches(Circle c) method, I believe there's something wrong with that but I don't know what.
Here is my code, thanks for taking a look!:
public class Circle{ //instance variables private double xpos, ypos, radius; //constructors; x and y for the center of the circle public Circle(){} public Circle(double xpos, double ypos, double radius){ this.xpos=xpos; this.ypos=ypos; this.radius=radius; } //setters and getters public void setX(double xpos){this.xpos=xpos;} public void setY(double ypos){this.ypos=ypos;} public void setRadius(double radius){this.radius=radius;} public double getX(){return xpos;} public double getY(){return ypos;} public double getRadius(){return radius;} public double getArea(){return Math.PI * radius * radius;} public double getCircumference(){return 2*Math.PI*radius;} //other methods: moveTo changes xpos and ypos and resize changes radius public void moveTo(double xpos, double ypos){this.xpos=xpos; this.ypos=ypos;} public void resize(double radius){this.radius=radius;} //toString method public String toString(){ return "[xpos= " +xpos+","+"ypos= " + ypos+"] radius: " + radius; } //contains method: returns true if a point is fully contained within this circle public boolean contains(double x,double y){ double d = Math.sqrt(Math.pow((x - this.xpos), 2) + Math.pow((y - this.ypos), 2)); return (d < radius); } //touches method: returns true if a point is touching this circle public boolean touches(double x,double y){ double d = Math.sqrt(Math.pow((x - this.xpos), 2) + Math.pow((y - this.ypos), 2)); return (d == radius); } //contains method: returns true if another circle is fully contained within this circle public boolean contains(Circle circle){ double d = Math.sqrt(Math.pow((circle.xpos - this.xpos), 2) + Math.pow((circle.ypos - this.ypos), 2)); return (this.radius > (d + circle.radius)); } //touches method: returns true if a circle is touching this circle public boolean touches(Circle circle){ double d = Math.sqrt(Math.pow((circle.xpos - this.xpos), 2) + Math.pow((circle.ypos - this.ypos), 2)); return (this.radius == (d + circle.radius)); } public static void main(String[] args){ Circle c1 = new Circle(1, 2, 5); Circle c2 = new Circle(1, 2, 4); Circle c3 = new Circle(2, 2, 4); Circle c4 = new Circle(11, 2, 5); Circle c5 = new Circle(4, 2, 5); Circle c6 = new Circle(11, 2, 3); System.out.println("Area of circle with center: " + c1 + " is = " + c1.getArea()); System.out.println("Circumference of circle: " + c1 + " is = " + c1.getCircumference()); System.out.println("Testing Cases:"); System.out.println("Case 1: Point is contained, not touching."); System.out.println(c1 + " contains (2, 3)" + " = " + c1.contains(2,3)); System.out.println(c1 + " touches (2, 3)" + " = " + c1.touches(2,3)); System.out.println("Case 2: Point is touching, not contained."); System.out.println(c1 + " contains (6, 2)" + " = " + c1.contains(6, 2)); System.out.println(c1 + " touches (6, 2)" + " = " + c1.touches(6, 2)); System.out.println("Case 3: Point is not touching, nor contained."); System.out.println(c1 + " contains (60, 20)" + " = " + c1.contains(60, 20)); System.out.println(c1 + " touches (60, 20)" + " = " + c1.touches(60, 20)); System.out.println("Case 4: Circle is contained, not touching."); System.out.println(c1 + " contains " + c2 + " = " + c1.contains(c2)); System.out.println(c1 + " touches " + c2 + " = " + c1.touches(c2)); System.out.println("Case 5: Circle is touching, not contained-inside."); System.out.println(c1 + " contains " + c3 + " = " + c1.contains(c3)); System.out.println(c1 + " touches " + c3 + " = " + c1.touches(c3)); //The following should return false, true; but it returns false false. System.out.println("Case 6: Circle is touching, not contained-outside."); System.out.println(c1 + " contains " + c4 + " = " + c1.contains(c4)); System.out.println(c1 + " touches " + c4 + " = " + c1.touches(c4)); System.out.println("Case 7: Circles overlap."); System.out.println(c1 + " contains " + c5 + " = " + c1.contains(c5)); System.out.println(c1 + " touches " + c5 + " = " + c1.touches(c5)); System.out.println("Case 8: Circles don't touch or contain."); System.out.println(c1 + " contains " + c6 + " = " + c1.contains(c6)); System.out.println(c1 + " touches " + c6 + " = " + c1.touches(c6)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
