0
package polygongeneric;

import java.util.ArrayList;

public class Polygon {

private ArrayList <Point2d> p = null;
private int points = 0;

public Polygon() { }

public Polygon(int numPoints) {
    p = new ArrayList<>();
}

public boolean addPoint(Point2d point) {
   p.add(points, point);
    points++;
    return true;
}

public boolean addPoint(double x, double y) {
   Point2d a = new Point2d(x,y);
   p.add(points, a);
   return true;
}

@Override
public String toString() {
    String s = "";
    for (int i=0; i<points; i++)
        s += p.get(i).toString() + "\n";
    return s;
}

}

I'm trying to convert a class from using an array of references to Point2d objects as type Point2d. This is what I have so far but it's not outputting the answer that it's supposed to.

This is what my code outputs

(0.1,0.9)

(0.5,0.5)

(0.2,0.5)

This is what it's supposed to output

(0.1,0.9)

(0.3,0.7)

(0.5,0.5)

(0.4,0.8)

(0.2,0.5)

Do you guys have any idea. What I'm doing wrong?

This is my Point2d class

package polygongeneric;

public class Point2d {

private double x = 0, y = 0;

public Point2d() { }

public Point2d(double x, double y) {
    setX(x);
    setY(y);
}

public void setX(double initX) {
    if (initX >= 0 && initX <= 1)
        x = initX;
}

public void setY(double y) {
    if (y >= 0 && y <= 1)
        this.y = y;
}

public double getX() { return x; }

public double getY() { return y; }

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

}

This is my main method

package polygongeneric;

public class PolygonGeneric {

public static void main(String[] args) {

    Polygon p = new Polygon(5);

    p.addPoint(new Point2d(.1, .9));
    p.addPoint(.3, .7);
    p.addPoint(new Point2d(.5, .5));
    p.addPoint(.4, .8);
    p.addPoint(new Point2d(.2, .5));

    System.out.println(p);
}

 }
  • 2
    There is in important difference between your two add() methods. But most importantly, your points variable is useless. Just use the add() method taking a single argument. You could also avoid duplication by using `return add(new Point2d(x, y));` Also, what's the point of returning a boolean, which is always true, and which you ignore anyway, from these methods? – JB Nizet Mar 29 '17 at 21:23
  • Good point. Thanks –  Mar 29 '17 at 21:28

3 Answers3

2

You are not incrementing the position in your addPoint(double x, double y), so basically, you are replacing the existing point with a new point, so you are missing few point values and you need to correct the correct the code as shown below:

public boolean addPoint(double x, double y) {
        Point2d a = new Point2d(x, y);
        p.add(points, a);
        points++;
        return true;
    }

Because you are simply adding the point at the end of the list, I suggest you can directly use arraylist.add(point); so that you will not get into these increment/other issues.

Also, you can change your constructor of Polygon class (which accepts int) as follows because you are not using the numPoints variable or else use an array with numPoints as the size instead of ArrayList.

public Polygon() {
    p = new ArrayList<>();
}
Vasu
  • 21,832
  • 11
  • 51
  • 67
1

You did not increment points in the addPoint(double x, double y) function.

András Tóth
  • 605
  • 4
  • 12
  • Both of you guys answered it correctly Idk whom should I mark it right –  Mar 29 '17 at 21:26
0

Why not reuse the same method? and call the overloaded function public boolean addPoint(Point2d point); instead of writing the same logic again and again.

public boolean addPoint(double x, double y) {
   Point2d a = new Point2d(x,y);
   return addPoint(a);
}
Samarth
  • 773
  • 1
  • 6
  • 14