Create a NetBeans project named CPS150_Lab13 . Copy-and-paste the highlighted co
ID: 3825085 • Letter: C
Question
Create a NetBeans project named CPS150_Lab13.
Copy-and-paste the highlighted code below into the main method of your new CPS150_Lab13 class.
Complete the following code to test whether two circles, each having a user-defined radius and a fixed center point lying along the same horizontal line, are disjoint, overlapping, or mutually contained.
public class CPS150_Lab13
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// red circle variables
int red_radius = 0;
int red_centerX = 0;
// no red_centerY since circles are aligned on the x-axis
int red_left_point = 0; // point on x-axis intersected by left edge
int red_right_point = 0; // point on x-axis intersected by right edge
// blue circle variables
int blue_radius = 0;
int blue_centerX = 0;
// no blue_centerY since circles are aligned on the x-axis
int blue_left_point = 0; // point on x-axis intersected by left edge
int blue_right_point = 0; // point on x-axis intersected by right edge
/*
* get the radius and center point x-coordinate
* for the red circle
*/
System.out.print("Input the radius of the red circle: ");
red_radius = in.nextInt();
System.out.print("Input the x-coordinate of the red circle's center point: ");
red_centerX = in.nextInt();
/*
* get the radius and center point x-coordinate
* for the blue circle
*/
System.out.print("Input the radius of the blue circle: ");
blue_radius = in.nextInt();
System.out.print("Input the x-coordinate of the blue circle's center point: ");
blue_centerX = in.nextInt();
/*
* for each circle, calculate the points where the left
* and right edges intersect the x-axis
*
* *** ADD CODE HERE ***
*/
/*
* determine and output the relationship between
* the red and blue circles as one of:
* i. circles are disjoint
* ii. red circle is contained in blue circle
* iii. blue circle is contained in red circle
* iv. circles are overlapping
*
* *** ADD CODE HERE ***
*/
} // end main method
} // end class CPS150_Lab13
Sample Runs (user input in red):
Input the radius of the red circle: 10
Input the x-coordinate of the red circle's center point: 0
Input the radius of the blue circle: 15
Input the x-coordinate of the blue circle's center point: 40
circles are disjoint
Input the radius of the red circle: 15
Input the x-coordinate of the red circle's center point: 50
Input the radius of the blue circle: 20
Input the x-coordinate of the blue circle's center point: 0
circles are disjoint
Input the radius of the red circle: 10
Input the x-coordinate of the red circle's center point: 20
Input the radius of the blue circle: 40
Input the x-coordinate of the blue circle's center point: 30
red circle is contained in blue circle
Input the radius of the red circle: 10
Input the x-coordinate of the red circle's center point: 20
Input the radius of the blue circle: 20
Input the x-coordinate of the blue circle's center point: 30
red circle is contained in blue circle
Input the radius of the red circle: 40
Input the x-coordinate of the red circle's center point: 20
Input the radius of the blue circle: 10
Input the x-coordinate of the blue circle's center point: 30
blue circle is contained in red circle
Input the radius of the red circle: 20
Input the x-coordinate of the red circle's center point: 20
Input the radius of the blue circle: 10
Input the x-coordinate of the blue circle's center point: 30
blue circle is contained in red circle
Input the radius of the red circle: 10
Input the x-coordinate of the red circle's center point: 0
Input the radius of the blue circle: 15
Input the x-coordinate of the blue circle's center point: 10
circles are overlapping
Input the radius of the red circle: 10
Input the x-coordinate of the red circle's center point: 10
Input the radius of the blue circle: 15
Input the x-coordinate of the blue circle's center point: 0
circles are overlapping
Explanation / Answer
import java.util.*;
public class CPS150_Lab13
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// red circle variables
int red_radius = 0;
int red_centerX = 0;
// no red_centerY since circles are aligned on the x-axis
int red_left_point = 0; // point on x-axis intersected by left edge
int red_right_point = 0; // point on x-axis intersected by right edge
// blue circle variables
int blue_radius = 0;
int blue_centerX = 0;
// no blue_centerY since circles are aligned on the x-axis
int blue_left_point = 0; // point on x-axis intersected by left edge
int blue_right_point = 0; // point on x-axis intersected by right edge
/*
* get the radius and center point x-coordinate
* for the red circle
*/
System.out.print("Input the radius of the red circle: ");
red_radius = in.nextInt();
System.out.print("Input the x-coordinate of the red circle's center point: ");
red_centerX = in.nextInt();
/*
* get the radius and center point x-coordinate
* for the blue circle
*/
System.out.print("Input the radius of the blue circle: ");
blue_radius = in.nextInt();
System.out.print("Input the x-coordinate of the blue circle's center point: ");
blue_centerX = in.nextInt();
/*
* for each circle, calculate the points where the left
* and right edges intersect the x-axis
*
* *** ADD CODE HERE ***
*/
/*
* determine and output the relationship between
* the red and blue circles as one of:
* i. circles are disjoint
* ii. red circle is contained in blue circle
* iii. blue circle is contained in red circle
* iv. circles are overlapping
*
* *** ADD CODE HERE ***
*/
//Creating Red circle
//Creating object for two circles for given input
Circle red=new Circle(red_radius,red_centerX);
Circle blue=new Circle(blue_radius,blue_centerX);
//to check disjoint
if(red.isDisjoint(blue)||blue.isDisjoint(red)){
System.out.println("circles are disjoint");
}
//to check red in blue or not
if(blue.isCircleContainsOther(red)){
System.out.println("red circle is contained in blue circle");
}
//to check blue in red or not
if(red.isCircleContainsOther(blue)){
System.out.println("blue circle is contained in red circle");
}
//to check overlaping
if((red.isOverlap(blue)||blue.isOverlap(red))){
System.out.println("circles are overlapping");
}
} // end main method
}
//Circle class
class Circle{
//instantanious variables
private int radius,centerX;
private int leftX,rightX;
//constructer taking radius and centerX of circle
public Circle(int radius,int centerX){
this.radius=radius;
this.centerX=centerX;
calculateXCordinates();//calculating leftX,rightX
}
//function to calculate leftx=center-radius,rightx=center+radius
private void calculateXCordinates(){
this.leftX=this.centerX-this.radius;
this.rightX=this.centerX+this.radius;
}
//to check a circle inside the present circle Ex:[10 30] [20 25]
public boolean isCircleContainsOther(Circle other){
return (this.leftX<=other.leftX && other.rightX<=this.rightX);
}
//to check disjoint Example [10 30] [40 50]
public boolean isDisjoint(Circle other){
return (this.rightX<other.leftX);
}
//to check overlapign Ex:[10 30] [20 50]
public boolean isOverlap(Circle other){
return !isCircleContainsOther(other) && ((this.rightX>=other.leftX) &&
this.leftX<other.leftX && this.rightX<other.rightX);
}
}