For my HW assignment we\'re working with composition, and I\'ve never used it be
ID: 3786032 • Letter: F
Question
For my HW assignment we're working with composition, and I've never used it before. I know how to use inheritance kind of, but I'm having trouble figuring out how to impliment composition. I'll paste my class Point underneath what I need help doing.
So for the section I need help on, it says "Using the class Point, define another class Circle to model a circle. A circle should have a center and a radius. The circle contains:
-a method that returns a Point instance
-a method that returns the circle's area
-a method that moves the circle's center(X,Y) to another position, the method heading should be like "public void move (Point newCenter){}"
-a method that resizes the circle radius according to the parameter, the method heading should be like "public void resize (double resizeRate){}"
-at least one alternate constructor which takes arguments (parameters) to initialize instance variables
Thank you in advance for the help!
---Point.java---
public class Point {
private int X;
private int Y;
public Point(int Xcoord, int Ycoord) {
X = Xcoord;
Y = Ycoord;
}
public int getX() {
return X;
}
public void setX() {
this.X = X;
}
public int getY() {
return Y;
}
public void setY() {
this.Y = Y;
}
public String toString() {
return "(" + X + "," + Y + ")";
}
}
Explanation / Answer
public class Point {
private int X;
private int Y;
public Point(int Xcoord, int Ycoord) {
X = Xcoord;
Y = Ycoord;
}
public int getX() {
return X;
}
public void setX(int X) {
this.X = X;
}
public int getY() {
return Y;
}
public void setY(int Y) {
this.Y = Y;
}
public String toString() {
return "(" + X + "," + Y + ")";
}
}
public class Circle
{
private int radius;
private Point p;
public Circle(int radius,int X,int Y){
this.radius=radius;
this.p=new Point(X,Y);
}
public Point getPointInstance()
{
return this.p;
}
public double area(){
double area= (3.14)*(radius)*(radius);
return area;
}
public void move(Point newCenter){
p.setX(newCenter.getX());
p.setY(newCenter.getY());
}
public void resize (int resizeRate ){
radius= radius*(resizeRate);
}
public static void main(String args[]){
Circle c=new Circle(2,4,5);
Point p=c.getPointInstance();
System.out.println(p);
double area=c.area();
System.out.println(area);
Point newCenter=new Point(5,10);
c.move(newCenter);
c.resize(10);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.