Home > database >  Java - How can I use a 3 argument constructor to set 2 instance variables
Java - How can I use a 3 argument constructor to set 2 instance variables

Time:10-29

I'm new to java and I have to use a 3 argument Circle() constructor to set 2 instance variables radius and center . I have to use x and y for the center. Bellow is the UML diagram I'm working with. Thanks in advance. enter image description here

public class Circle extends Point
{
  private double radius;
  private Point center;

  public Circle(double x, double y, double radius)
  {
    super(x, y);
    this.radius = radius;
  }

CodePudding user response:

Okay so I figured it out. The constructor should be like this:

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

CodePudding user response:

That diagram indicates that the relationship between Circle and Point is a Composition.

So the relevant java code is:

public class Circle
{
  private double radius;
  private Point center;

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