java - Setting up a toString() method for an array list -
sorry probley simple question how go setting tostring() method array list?
is simple
points = new arraylist<point>(); public string tostring() { return points.tostring();}
which not seem working me, or more complex since array list? because reason when execute mine printing first value or object.
p.s i'm trying return values have added list.
more in detail constructer
public cloud() { points = new arraylist<point>(); }
add point
public void addpoint(point p) { // done if (points.contains(p)) { // if p in list nothing } else { points.add(p); // if p not in list adds end } }
tostring
public string tostring() { return points.tostring(); }
main
public static void main(string[] args) { cloud cloud = new cloud(); cloud.setdebug(false); system.out.println("cloud.debug off"); system.out.println("initial cloud: " + cloud.tostring()); point p1 = new point(3.0, 1.0); cloud.addpoint(p1); point p2 = new point(2.0, 2.0); cloud.addpoint(p2); point p3 = new point(1.5, 1.5); cloud.addpoint(p3); point p4 = new point(3.0, 0.0); cloud.addpoint(p4); system.out.println("final cloud: " + cloud);
this printing final cloud: (3.0,1.0) while should printing final cloud: [(3.0,1.0), (2.0,2.0), (1.5,1.5), (3.0,0.0)]
edit: points class
public class point { private double x; private double y; public static final double epsilon = 1e-5; public static boolean debug = false; public point(double x, double y) { this.x = x; this.y = y; // done sets x,y private types x,y type provided // in () } public point() { this(0.0, 0.0); // calls point (double x,double) constructer // given arguments } // inturn setting x , y == 0.0 public double getx() { return x; // returns private value of x when called in main // method } // can't changed user public double gety() { return y; // return private value of y when called in main // method can't changed } // user public string tostring() { return "(" + x + "," + y + ")"; // done teacher sets tostring // method , implemetns } public boolean equals(point p) { if (math.abs(this.getx()) - math.abs(p.x) < epsilon) { return true; // checks if x - p.x less epsilon covers // round off } if (math.abs(this.gety()) - math.abs(p.y) < epsilon) { return true; // checks if y-p.y less epsilon covers // round off } return false; // both these methods test equality using epsilon, // becuae dealing } // doubles, roundof can occur public boolean equals(object obj) { // given if (obj instanceof point) { point p = (point) obj; // method overrides object equals // method , calls return equals(p); // clas's equals(point) method } return false; } // todo implement point.eucliddist /** * * @param p * @return euclidean distance of point point p */ public double eucliddist(point p) { double distance = 0; double firstvalue; double secondvalue; distance = math.sqrt(((this.getx() - p.x) * (this.getx() - p.x)) // calculate // // distance + ((this.gety() - p.y) * (this.gety() - p.y))); // between // 2 points // firstvalue= math.pow(this.getx()-p.x, 2); // secondvalue= math.pow(this.gety()-p.y, 2); // distance = math.sqrt(firstvalue + secondvalue); return distance; } /** * @param args * : no args */ public static void main(string[] args) { // test methods if (debug) system.out.println("debug on"); else system.out.println("debug off"); system.out.println("epsilon: " + point.epsilon); point origin = new point(); point p1 = new point(0.0, 4.0); point p2 = new point(3.0000001, 3.9999999); point p3 = new point(3.0, 4.0); point p4 = new point(0.0, 5.0); point p5 = new point(12.0, 0.0); system.out.println("origin: " + origin); system.out.println("p1: " + p1); system.out.println("p2: " + p2); system.out.println("p3: " + p3); system.out.println("p4: " + p4); system.out.println("p5: " + p5); if (p2.equals(p3)) system.out.println(p2 + " equals " + p3); else system.out.println(p2 + " not equal " + p3); system.out.println("euclidean distance between " + origin + " , " + p1 + ": " + origin.eucliddist(p1)); system.out.println("euclidean distance between " + p1 + " , " + p3 + ": " + p1.eucliddist(p3)); system.out.println("euclidean distance between " + p3 + " , " + origin + ": " + p3.eucliddist(origin)); system.out.println("euclidean distance between " + p4 + " , " + p5 + ": " + p4.eucliddist(p5)); }
}
you can create tostring()
method overrides in classes of yours, not in other classes you're not overriding. arraylist has valid tostring()
method useful. need make sure items held list class has valid tostring() method.
note state:
which not seem working me... because reason when execute mine printing first value or object.
this suggests don't have tostring() problem, have different problem program, you're not adding objects list correctly. need more debugging , show more pertinent code.
edit
i'm guessing point class's contains(...)
method erroneous, returning true
when should returning false. please show point class.
edit 3 (deleted edit 2) equals wrong:
this ok:
public boolean equals(object obj) { // given if (obj instanceof point) { point p = (point) obj; // method overrides object equals // method , calls return equals(p); // clas's equals(point) method } return false; }
but here, return equals if either x's or y's closely match , shouldn't be. should return true if both closely match:
public boolean equals(point p) { if (math.abs(this.getx()) - math.abs(p.x) < epsilon) { return true; // checks if x - p.x less epsilon covers // round off } if (math.abs(this.gety()) - math.abs(p.y) < epsilon) { return true; // checks if y-p.y less epsilon covers // round off } return false; // both these methods test equality using epsilon, // becuae dealing } // doubles, roundof can occur
also you're using math.abs(...)
incorrectly. should go around subtraction statement, not around each variable.
Comments
Post a Comment