Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner; // Import Scanner class (Used for Input)
- class Circle {
- public void run() {
- // Implement program for the assignment
- Scanner input = new Scanner(System. in );
- //Circle 1 inputs from user
- double coordinatex1; // x1 (midpoint 1st circle)
- double coordinatey1; //y1 (midpoint 1st circle)
- double radius1; //r1 (radius 1st circle)
- //Circle 2 inputs from user
- double coordinatex2; // x2 (midpoint 2nd circle)
- double coordinatey2; //y2 (midpoint 2nd circle)
- double radius2; //r2 (radius 2nd circle)
- double pointx; // x2 (midpoint 2nd circle)
- double pointy; //y2 (midpoint 2nd circle)
- // Output Varibles
- boolean isInCirlce1 = false;
- boolean isInCirlce2 = false;
- boolean isOnCirlce1 = false;
- boolean isOnCirlce2 = false;
- boolean isPointInBothCircles = false;
- boolean isPointOnBothCircles = false;
- // Interface for Circle 1 (Enter midpoint coordinates and radius of circle 1)
- System.out.println("Enter x-coordinate of 1st circle's midpoint ");
- coordinatex1 = input.nextDouble(); // Storing the x1 input
- System.out.println("Enter y-coordinate of 1st circle's midpoint ");
- coordinatey1 = input.nextDouble(); // Storing the y1 input
- System.out.println("Enter radius of 1st circle ");
- radius1 = input.nextDouble(); // Storing the y1 input
- if (radius1 < 0) {
- System.out.println("input error");
- System.exit(0);
- }
- // Interface for Circle 1 (Enter midpoint coordinates and radius of circle 1)
- System.out.println("Enter x-coordinate of 2nd circle's midpoint ");
- coordinatex2 = input.nextDouble(); // Storing the x2 input
- System.out.println("Enter y-coordinate of 2nd circle's midpoint ");
- coordinatey2 = input.nextDouble(); // Storing the y2 input
- System.out.println("Enter radius of 2nd circle ");
- radius2 = input.nextDouble(); // Storing the r2 input
- // Negative Radius Error system
- if (radius2 < 0) {
- System.out.println("input error");
- System.exit(0);
- }
- // Interface for Point (Enter coordinates of the point)
- System.out.println("Enter x-coordinate of the point ");
- pointx = input.nextDouble(); // Storing the x-coordinate of the point
- System.out.println("Enter y-coordinate of the point ");
- pointy = input.nextDouble(); // Storing the y-coordine of the point
- double distanceC1andP = (radius1 * radius1) - (((coordinatex1-pointx) * (coordinatex1-pointx)) + (((coordinatey1-pointy) * (coordinatey1-pointy))));
- System.out.println("Distance between the 1st circle and the point is (from midpoint) " + distanceC1andP);
- double distanceC2andP = (radius2 * radius2) - (((coordinatex2-pointx) * (coordinatex2-pointx)) + (((coordinatey2-pointy) * (coordinatey2-pointy))));
- System.out.println("Distance between the 2nd circle and the point is (from midpoint) " + distanceC2andP);
- // Do Check
- isInCirlce1 = distanceC1andP > radius1
- isInCirlce2 = distanceC2andP > radius2
- isOnCirlce1 = distanceC1andP == 0;
- isOnCirlce2 = distanceC2andP == 0;
- // Update Both Output Variable based on previous calcs
- isPointInBothCircles = isInCirlce1 && isInCirlce2;
- isPointOnBothCircles = isOnCirlce1 && isOnCirlce2;
- // Do Display
- if(isPointInBothCircles || isPointOnBothCircles){
- if(isPointInBothCircles){
- System.out.println("Point is inside of both circles ");
- }
- if(isPointOnBothCircles){
- System.out.println("Point is on of both circles ");
- }
- } else if(isInCirlce1 || isOnCirlce1){
- if(isInCirlce1){
- System.out.println("Point is inside of circle 1 ");
- }
- if(isOnCirlce1){
- System.out.println("Point is on circle 1 ");
- }
- } else if(isInCirlce2 || isOnCirlce2){
- if(isInCirlce2){
- System.out.println("Point is inside of circle 2 ");
- }
- if(isOnCirlce2){
- System.out.println("Point is on circle 2 ");
- }
- } else {
- System.out.println("Point is outside of both circles ");
- }
- }
- // Execute Code
- public static void main(String[] args) {
- (new Circle()).run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment