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

How to write two java files for this assignment: MyCircle.java, and MyCircleTest

ID: 3861092 • Letter: H

Question

How to 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

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:

MyCircle Class

double x

double y

double radius

void setX(double value)

double getX()

void setY(double value)

double getY()

void setRadius(double value)

double getRadius()

double getArea()

boolean doesOverlap(MyCircle otherCircle)

MyCircleTester

The MyCircleTester.java code can be inside a static main method. Use this as a place to test your class. I encourage you to spend a significant amount of time testing out the MyCircle class. This is a great way to learn the nuances of java syntax. Feel free to write and erase as much code as you want. (Naturally.)

After you have tested the code to your own satisfaction, submit code that meets these minimum criteria

Allocate and initialize at least three MyCircle objects. Two of them should overlap, and two should not.

Display the areas of the three circles

Invoke doesOverlap on MyCircles to show which circles overlap.

And; How to refine the Circle class in order to practice the following advanced class writing techniques:

Aggregation

Multiple Constructors

Copy Constructors

The toString method

The equals method

The final goal is to create a class with the following design. Class uses doubles, instead of the floats in the first assignment. The new members and methods are listed in green.

Class Circle

Point center

double radius

Circle(Point o, double r)

Circle(double xValue, double yValue, double r)

Circle()

Circle(Circle c)

Point getCenter()

void setCenter(Point p)

void setX(double value)

double getX()

void setY(double value)

double getY()

void setRadius(double value)

double getRadius()

double getArea()

String toString()

boolean equals(Circle c)

boolean doesOverlap(Circle c)

Step 1: Aggregation

Begin by downloading Point.java. Take some time to familiarize yourself with the class. It is a simple class for storing an x and y coordinate.

In the previous circle class assignment the class was given three instance members: x, y, and radius. Create a new Circle class that uses has only two members: a member named center of type Point, and member radius of type double.

Rewrite the existing setters and getters for x and y so that they use the center member. Add setters and getters for the center, so that users of the class can access the point directly.

Rewrite the doesOverlap method so that it uses the center member.

Step 2: Constructors

Create four constructors. Each constructor is described below:

The two argument constructor. This constructor should accept a Point object and double. It should initialize the new objects center member so that it refers to a copy of the point argument, and the radius member to the double value.

The three argument constructor. This constructor should accept three double values, for x, y, and radius respectively. It should create a new Point object to store the x and y values, and store this Point object in the center member. It should store the third double value into the radius member. Note that you can use the two argument constructor to write the three argument constructor.

The no-argument constructor. This constructor should create a new Point object with an x and y coordinate of 0, and use this point object to initialize the center member. It should set the radius to 1.

The copy constructor. This constructor should accept a Circle as an argument. It should set the center member to a copy of the point argument’s center member. It should set the radius member to the point argument’s radius value;

Step 3: toString

Write a toString method for your Circle class. You format the string however you like, as long as it includes the objects x, y, and radius values.

Step 4: equals

Write an equals method that returns true if the argument Circle has the same x, y and radius values as the receiving circle.

double x

double y

double radius

void setX(double value)

double getX()

void setY(double value)

double getY()

void setRadius(double value)

double getRadius()

double getArea()

boolean doesOverlap(MyCircle otherCircle)

Explanation / Answer

class Point {

private int x = 0;
private int y = 0;

public Point() {}

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return this.x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return this.y;
}

public void setY(int y) {
this.y = y;
}

public void setXY(int x, int y) {
this.x = x;
this.y = y;
}

public double distance(int x, int y) {
int xDiff = this.x - x;
int yDiff = this.y - y;
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}

public double distance(Point another) {
int xDiff = this.x - another.getX();
int yDiff = this.y - another.getY();
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}

public String toString() {
return "(" + this.x + ", " + this.y + ")";
}

}   

class Circle {

private Point center;
private int radius = 1;

public Circle(int x, int y, int radius) {
this.center = new Point(x, y);
this.radius = radius;
}

public Circle(Point center, int radius) {
this.center = center;
this.radius = radius;
}

public int getRadius() {
return this.radius;
}

public void setRadius(int radius) {
this.radius = radius;
}

public Point getCenter() {
return this.center;
}

public void setCenter(Point center) {
this.center = center;
}

public int getCenterX() {
return this.center.getX();
}

public int getCenterY() {
return this.center.getY();
}

public void setCenterXY(int x, int y) {
this.center.setXY(x, y);
}

public double getArea() {
return Math.PI * radius * radius;
}

public String toString() {
return "Circle "+this.center+" radius="+this.radius;
}
   public boolean equals(Circle c)
   {
   if (this.getCenterX()==c.getCenterX() && getCenterY()==c.getCenterY() && this.radius==c.radius){return true;}return 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