简体   繁体   中英

convert object to String in sysout output, java

I've been trying to figure out how to put toString() method to make system println look meaningful, but I dont know where should I put this method exactly to make the code look right. So the class point where I added all the constructors + getters and settes is:

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

public double getX() {
    return x;
}
public void setX(double x) {
    this.x = x;
}
public double getY() {
    return y;
}
public void setY(double y) {
    this.y = y;
}
void setXY(double x, double y) {
    if ( x == y) {
        return;
    }
    this.x = x;
    this.y = y;
  }
    public double odlegloscDo(Point p) {
    return Math.sqrt(Math.pow(x, x) + Math.pow(y, y));
    }
    }

Then in main method I put all the date with new objects and what I exactly want to do. And added also a method to simple formula execute the fields of the objects I created. As the below:

public class Odleglosc {

public static  void main(String[] args) {

Point p1 = new Point();
Point p2 = new Point(2);
Point p3 = new Point(1,5);

System.out.println("p1: " + p1);
System.out.println("p2: " + p2);
System.out.println("p3: " + p3);
System.out.println("p3 equals p1 = " + p3.equals(p1));

double odleglosc = 0.0;
if (!p3.equals(p1)) {
odleglosc = p1.odlegloscDo(p3);
System.out.println("Odleglosc  p1 -> p3  = " + odleglosc);


}

p3.setX(-9);
p2.setY(p3.getY());
double sum = p3.getX() + p2.getX();
p1.setX(sum);

System.out.println("================================");
System.out.println("p1: " + p1);
System.out.println("p2: " + p2);
System.out.println("p3: " + p3);

    }
}

So, the main problem is that I get this output:

p1: zajecia4.Point@139a55
p2: zajecia4.Point@1db9742
p3: zajecia4.Point@106d69c
p3 equals p1 = false
Odleglosc  p1 -> p3  = 1.4142135623730951
================================
p1: zajecia4.Point@139a55
p2: zajecia4.Point@1db9742
p3: zajecia4.Point@106d69c

I know how toString() method works and stuff, I just simply don't understand where to put it and how to make the printout be meaningful from my code. Please help me with it, otherwise it will be tough to move forward at the course for java, where my mentors actually cannot explain properly. Other thing is that my mentor said that in my code I should also put equals method somewhere, but I didn't understand him either with this. I already put it in my if instruction, but where else should I put equals method so it compares fields properly?

The toString method returns the string representation of the object. You need to override it this way:

public class Point {
    double x, y;
    ...

    @Override
    public String toString() {
        return x + " - " + y;
    }
}

The output will be:

p1: 0 - 0
p2: 2 - 0
p3: 1 - 5

Concerning the Equals method, it could also be overriden the same way:

public class Point {
    double x, y;
    ...

    @Override
    public String toString() {
        return x + " - " + y;
    }

    @Override
    public boolean equals(Point point) {
        return this.x == point.x && this.y = point.y;
    }
}

The utility of the .equals method is that it allows you to test for values' equality , not reference equality (which is normally achieved using '==').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM