Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write two java files for this assignment: MyCircle.java, and MyCircleTe

ID: 3585579 • Letter: Y

Question

You will write two java files for this assignment: MyCircle.java, and MyCircleTester.java. The MyCircle.java file is the primary interest. It will create an actual class, the first one that we have written this semester. The MyCircle class is similar to the MyRectangle class that we discussed in the lecture. The MyCircle class should meet these criteria: Three instance variables double radius o double x double y Eight methods: Six of the methods are simple: getter's and setter's for x, y, and radius. » There should also be a getArea method that returns the area (derived from the radius) ·A doesOverlap method. This method should accept a MyCircle as an argument, and return true if this circle overlaps the circle that the method was invoked on. [Note: two circles overlap if the sum of their radius' is greater than the distance between their centers. Sometimes requirements like these are displayed in a simple two box diagram line the one below:

Explanation / Answer

MyCircleTester..java

import java.util.Scanner;

public class MyCircleTester {

public static void main(String arg[]){

MyCircle c[] = new MyCircle[3];

Scanner scan = new Scanner(System.in);

for(int i=0; i<c.length; i++){

System.out.println("Enter circle "+(i+1)+" radius: ");

double radius = scan.nextDouble();

System.out.println("Enter circle "+(i+1)+" x and y co-ordinates: ");

int x = scan.nextInt();

int y = scan.nextInt();

MyCircle m = new MyCircle();

m.setX(x);

m.setY(y);

m.setRadius(radius);

c[i] = m;

}

for(int i=0; i<c.length ; i++){

System.out.println("Circle "+(i+1)+" area: "+c[i].getArea());

}

System.out.println("-------------------------------------");

System.out.println("Circle 2 overlaps Circle 1: "+c[1].doesOverlap(c[0]));

System.out.println("Circle 3 overlaps Circle 2: "+c[2].doesOverlap(c[1]));

System.out.println("Circle 3 overlaps Circle 1: "+c[2].doesOverlap(c[0]));

}

}

MyCircle.java

public class MyCircle {

double radius;

double x;

double y;

public MyCircle(){

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

public double getX() {

return x;

}

public void setX(double x) {

this.x = x;

}

public double getY() {

return y;

}

public void setY(double y) {

this.y = y;

}

public double getArea(){

return Math.PI * radius * radius;

}

public boolean doesOverlap(MyCircle otherCircle){

double distance = Math.pow((x - otherCircle.x) * (x - otherCircle.x) + (y - otherCircle.y) * (y - otherCircle.y), 0.5);

if (distance > (radius + otherCircle.radius)){

return false;

}

else {

return true;

}

}

public String toString() {

return "x = "+x+" y = "+y+" radius: "+radius;

}

public boolean equals(MyCircle c){

if(x == c.x && y == c.y && radius==c.radius){

return true;

}

else{

return false;

}

}

}

Output:

Enter circle 1 radius:
1
Enter circle 1 x and y co-ordinates:
2
3
Enter circle 2 radius:
4
Enter circle 2 x and y co-ordinates:
5
6
Enter circle 3 radius:
7
Enter circle 3 x and y co-ordinates:
8
9
Circle 1 area: 3.141592653589793
Circle 2 area: 50.26548245743669
Circle 3 area: 153.93804002589985
-------------------------------------
Circle 2 overlaps Circle 1: true
Circle 3 overlaps Circle 2: true
Circle 3 overlaps Circle 1: false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote