Question

IN JAVA Design (pseudocode) and implement (source code) a program (name it Circles) to determine if...

IN JAVA

Design (pseudocode) and implement (source code) a program (name it Circles) to determine if a circle is either completely inside, overlapping with, or completely outside another circler. The program asks the user to enter the center point (X1, Y1) and the radius (R1) for the first circle C1, and the center point (X2, Y2) and the radius (R2) for the second circle C2. The program then determines if the second circle C2 is either completely inside, or overlapping with, or completely outside the first circle C1. Hint: use the sum of R1 and R2 and the distance between the centers to solve the problem. Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs.

Sample run 1:

Circle 1 center is: (0,0)

Circle 1 radius is: 6

Circle 2 center is: (1,1)

Circle 2 radius is: 1

Judgment:            Circle 2 is completely inside circle 1

Sample run 2:

Circle 1 center is: (0,0)

Circle 1 radius is: 2

Circle 2 center is: (7,7)

Circle 2 radius is: 1

Judgment:            Circle 2 is completely outside circle 1

Sample run 3:

Circle 1 center is: (0,0)

Circle 1 radius is: 3

Circle 2 center is: (2,2)

Circle 2 radius is: 3

Judgment:            Circle 2 is overlapping with circle 1

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

Please find the code below::

Circles.java

package classes1;


public class Circles{
   public double radius;
   public double x,y;
   Circles(){
       x=y=0;
       radius=1;
   }
   public Circles(double x,double y,double radius) {
       this.radius = radius;
       this.x =x;
       this.y =y;
   }

   public void checkCollision(Circles c){
       double distanceOfCenter = Math.sqrt(Math.pow(this.x-c.x, 2) + Math.pow(this.y-c.y, 2));
       double totalRadiusLength = radius+c.radius;
       if(distanceOfCenter>totalRadiusLength){
           System.out.println("Circle 2 is completely outside circle 1");
       }else if(distanceOfCenter==totalRadiusLength){
           System.out.println("Circle 2 circumference touching circle 1 circumference");
       }else if(radius>(c.radius+distanceOfCenter)){
           System.out.println("Circle 2 is completely inside circle 1");
       }else{
           System.out.println("Circle 2 is overlapping with circle 1");
       }
   }

   public static void main(String[] args) {
       Circles c1 = new Circles(0,0,6);
       Circles c2 = new Circles(1,1,1);
       System.out.println("Circle 1 : "+c1);
       System.out.println("Circle 2 : "+c2);
       c1.checkCollision(c2);
       System.out.println();
       System.out.println();
       System.out.println();


       c1 = new Circles(0,0,2);
       c2 = new Circles(7,7,1);
       System.out.println("Circle 1 : "+c1);
       System.out.println("Circle 2 : "+c2);
       c1.checkCollision(c2);
       System.out.println();
       System.out.println();
       System.out.println();

       c1 = new Circles(0,0,3);
       c2 = new Circles(2,2,3);
       System.out.println("Circle 1 : "+c1);
       System.out.println("Circle 2 : "+c2);
       c1.checkCollision(c2);
       System.out.println();
       System.out.println();
       System.out.println();

   }
   @Override
   public String toString() {
       return "[x=" + x + ", y=" + y + " , radius=" + radius + "]";
   }


}

output:

Add a comment
Know the answer?
Add Answer to:
IN JAVA Design (pseudocode) and implement (source code) a program (name it Circles) to determine if...
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
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