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,

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: getters and setters 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 arguments center member. It should set the radius member to the point arguments 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!