Exercise 4a-b: Add a method to Circle.java that returns true if this Circle obje
ID: 3785003 • Letter: E
Question
Exercise 4a-b: Add a method to Circle.java that returns true if this Circle object touches another Circle object externally, and false otherwise. Add a method to Circle.java that returns true if this Circle object touches another Circle object internally, and false otherwise.
Exercise 4: We discussed the following example ofobject interactions in the lectures. We define a Point class and a Circle class. The Circle class has a method that checks if a Point object is enclosed inside the Circle object. Study the code, run it and understand it. //Point class Defines a point with coordinates px and py public class Point private double px; private double py; //constructor public Point (double px, double py) this .px px; this py py; //get and set methods public void setX (double px) this px px public void setY (double py) (this .py py; public double getX {return px; public double gety return py; to string method pu. ring EOS ring O Cit cle class "radius py tai public class Circle instance variables private double cx private double cy; private double radius; //constructor public Circle (double cx, double cy, double radius) this Cx CX this .cy cy; this radius radius //get and set methods public void setcx (double cx) (this CX public void setCY double cy) (this cy cy; public void setRadius (double radius) this radius radius public double getcx() {return cx; public double getCY {return cy; public double getRadius fre turn. radiusExplanation / Answer
PROGRAM CODE:
Point.java
public class Point {
private double px;
private double py;
public Point(double px, double py) {
this.px = px;
this.py = py;
}
public void setX(double px)
{
this.px = px;
}
public void setY(double py)
{
this.py = py;
}
public double getX()
{
return px;
}
public double getY()
{
return py;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[px = " + px + " py = " + py + "]";
}
}
Circle.java
public class Circle {
private double cx;
private double cy;
private double radius;
public Circle(double cx, double cy, double radius) {
this.cx = cx;
this.cy = cy;
this.radius = radius;
}
public void setCX(double cx)
{
this.cx = cx;
}
public void setCY(double cy)
{
this.cy = cy;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public double getCX()
{
return cx;
}
public double getCY()
{
return cy;
}
public double getRadius()
{
return radius;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Circle with center " + cx + "," + cy + " and radius " + radius;
}
public boolean encloses(Point p)
{
double d;
d = Math.sqrt(((p.getX()-cx)*(p.getX()-cx)) + ((p.getY()-cy)*(p.getY()-cy)));
if(d<radius)
return true;
else return false;
}
// 4a) Mehtod to check whether two circles touch each other externally or not
//This is done by calculating the distance between the centers of the circles and then validating with the sum of their radii
public boolean externalTouch(Circle c)
{
double d = Math.sqrt(((c.getCX()-cx)*(c.getCX()-cx)) + ((c.getCY()-cy)*(c.getCY()-cy)));
if(d==(c.getRadius()+radius))
return true;
else return false;
}
// 4a) Mehtod to check whether two circles touch each other externally or not
//This is done by calculating the distance between the centers of the circles and then validating with the difference of their radii
public boolean internalTouch(Circle c)
{
double d = Math.sqrt(((c.getCX()-cx)*(c.getCX()-cx)) + ((c.getCY()-cy)*(c.getCY()-cy)));
if(d==Math.abs(c.getRadius()-radius))
return true;
else return false;
}
}
CircleDemo.java
import java.util.Scanner;
public class CircleDemo {
/**
* @param args
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the center coordinates (x,y) of the first circle and its radius: ");
Circle c1 = new Circle(keyboard.nextDouble(), keyboard.nextDouble(), keyboard.nextDouble());
System.out.print("Enter the center coordinates (x,y) of the first circle and its radius: ");
Circle c2 = new Circle(keyboard.nextDouble(), keyboard.nextDouble(), keyboard.nextDouble());
if(c1.externalTouch(c2))
System.out.println(c1 + " touches " + c2 + " externally");
else
System.out.println(c1 + " does not touch " + c2 + " externally");
if(c1.internalTouch(c2))
System.out.println(c1 + " touches " + c2 + " internally");
else
System.out.println(c1 + " does not touch " + c2 + " internally");
}
}
OUTPUT:
Run#1:
Enter the center coordinates (x,y) of the first circle and its radius: 10 10 10
Enter the center coordinates (x,y) of the first circle and its radius: 30 10 10
Circle with center 10.0,10.0 and radius 10.0 touches Circle with center 30.0,10.0 and radius 10.0 externally
Circle with center 10.0,10.0 and radius 10.0 does not touch Circle with center 30.0,10.0 and radius 10.0 internally
Run#2:
Enter the center coordinates (x,y) of the first circle and its radius: 10 10 10
Enter the center coordinates (x,y) of the first circle and its radius: 15 10 5
Circle with center 10.0,10.0 and radius 10.0 does not touch Circle with center 15.0,10.0 and radius 5.0 externally
Circle with center 10.0,10.0 and radius 10.0 touches Circle with center 15.0,10.0 and radius 5.0 internally
Run#3:
Enter the center coordinates (x,y) of the first circle and its radius: 10 10 10
Enter the center coordinates (x,y) of the first circle and its radius: 30 30 5
Circle with center 10.0,10.0 and radius 10.0 does not touch Circle with center 30.0,30.0 and radius 5.0 externally
Circle with center 10.0,10.0 and radius 10.0 does not touch Circle with center 30.0,30.0 and radius 5.0 internally
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.