user4617245
user4617245

Reputation: 21

Cannot set up constructor correctly

Here is my task at hand: (using netbeans)

a.Does not inherit from any other class

b.Contains four private instance, all of type “Point” ,variables called

point1
point2
point3
point4

c. Create a constructor that takes the following values:

i. double x1, double y1,double x2, double y2,double x3, double y3, double x4,double y4

ii. The constructor should create and set the four Point instance variables

d.The class should contain the following getters:

i. getPoint1( ) ii. getPoint2( ) iii. getPoint3( ) iv. getPoint4( )

e.The class should contain a method called getCoordinatesAsString() that returns the formatted String "%s, %s, %s, %s\n", point1, point2, point3, point4

Here is what i have so far:

public class Quadrilateral {

private Point point1;
private Point point2;
private Point point3;
private Point point4;

public Quadrilateral(double x1, double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
    point1 = x1,y1;
    point2 = x2,y2;
    point3 = x3,y3;
    point4 = x4,y4;
}

public Point getPoint1()
{
    return point1;
}

public Point getPoint2()
{
    return point2;
}
public Point getPoint3()
{
    return point3;
}
public Point getPoint4()
{
    return point4;
}

    public String getCoordinatesAsString()
{
    return String.format("%s, %s, %s, %s\n", point1,point2,point3,point4);
}

public String toString()
{
    return String.format("%s:\n%s", "Coordinates of Quadrilateral are", getCoordinatesAsString());
}



}

I cannot figure out how to set up the constructor correctly. I get an error saying incompatible types, double cannot be converted to Point.

Upvotes: 0

Views: 76

Answers (4)

Razib
Razib

Reputation: 11163

You also may send point to the Quadrilateral constructor. Make an overloaded version of the Quadrilateral constructor -

public Quadrilateral(Point point1, Point point2, Point point3, Point point4)
{
    this.point1 = point1;
    this.point2 = point2;
    this.point3 = point3;
    this.point4 = point4;
}  

Then from the client of Quadrilateral you may construct a Quadrilateral like this way-

Quadrilateral aQuadrilateral = new Quadrilateral(new Point(x1, y1), new Point(x2, y2),new Point(x3, y3), new Point(x4, y4) );

Upvotes: 0

Samarth
Samarth

Reputation: 61

Change the constructor of your Quadrilateral class to

point1 = new Point(x1,y1);
point2 = new Point(x2,y2);
point3 = new Point(x3,y3);
point4 = new Point(x4,y4);

And your Point class should look something like

Class Point {
    private double x;
    private double y;

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

    public String getCoordiantes () {
        return String.format("("+ %f +","+ %f +")" , this.x, this.y);
    }

    (...) //other methods
}

Upvotes: 0

Jite
Jite

Reputation: 5847

When you create a new instance of a class (an object), you invoke its constructor.
This is done by using the new keyword.

The Point structure is a class, and you wish to create an instance of it.
You are instead trying to asign it to a double value, while you should create a new Point object and pass the two double values in as parameters in to the Point constructor.

So create the points in the Quadrilateral constructor the same way you create the Quadrilateral object, by using the new keyword!

Upvotes: 1

Eran
Eran

Reputation: 393831

You are not constructing the Points properly.

Try :

public Quadrilateral(double x1, double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
    point1 = new Point(x1,y1);
    point2 = new Point(x2,y2);
    point3 = new Point(x3,y3);
    point4 = new Point(x4,y4);
}

Upvotes: 1

Related Questions