简体   繁体   中英

How can I print multiple class instances in one statement? Java

public Class Point{
    private double x;
    private double y;

    public Point() {
    super();
    }

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


    public static Point deepCopy(Point p2) {
        Point point2 = new Point(p2.x+2, p2.y+2);
        return point2;
    }

    public static Point shallowCopy(Point p4){
        return p4;
    }

    public void setPoint3X(double x3) {
        this.x = x+1;
    }
    public void setPoint3Y(double y3) {
        this.y = y+1;
    }

    public void setPoint2(double x2, double y2) {
        this.x = x2+2;
        this.y = y2+2;
    }


    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;
    }

    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }

public class PointDemo {
    public static void main(String[] args) {

        double x = 0;
        double y = 0;

        Point point1 = new Point(5, 10);
        Point point2 = Point.deepCopy(point1);

        Point point3 = Point.deepCopy(point2);
        point3.setPoint3X(x);
        point3.setPoint3Y(y);

        Point point4 = new Point();
        point4 = Point.shallowCopy(point3);

Question 4 - Write a class called Point. The class has two instance fields: x and y, both are of double type. Write two constructors: one that uses x and y values for a point, and the other uses the first point values to create a second Point object with the exact same x and y values. Write a Demo class to build the following four Point objects.

Point 1: (x=5, y=10)

Point 2: (x=7, x=12). This point needs to be built using the deep copy constructor that copies point 1 and then using only one setter method.

Point 3: (x=10, y=15). This point needs to be built using the deep copy method that uses Point 2 as the original and then using two setter methods to change the required x and y values.

Point 4: This point needs to be built using the shallow copy method and it must use Point 3 as the shallow copy template.

Finally print all four points using one statement .

Okay. So my code gives me all the values from point1-point4 however, I cannot figure out a way to print them all in one statement. Obviously a loop in the demo class can print every Point object but that would be multiple print statements which violates the one print statement requirement.

Also, I cannot use an array in the Point class because it violates the 2 fields requirement.

Can anybody help or give me a suggestion as to how I can take all the Point objects and print it in one statement? Or is that even possible and maybe I am reading the question wrong?

您可以使用PrintStream.format(format(String format,Object ... args)

System.out.format("(%f, %f), (%f, %f), (%f, %f), (%f, %f)\n", point1.x, point1.y, point2.x, point2.y, ...and so on);

I'm going to post this as an answer too since I think it might be what your instructor actually wants.

The key point here is to remember that the toString() method on your class can be used like a regular string and concatenate other strings, and that's what you normally do with + when calling println() . So just use the normal println() method like you've probably been doing already.

System.out.println( "Point 1 - " + point1.toString() + ";\n" 
    + "Point 2 - " + point2.toString() + ";\n" 
    + "Point 3 - " + point3.toString() + ";\n" 
    + "Point 4 - " + point4.toString() + ";" );

You can use streams:

Arrays.stream(new Point[] {point1, point2, point3, point4}).forEach(System.out::println);

or String.format()

System.out::println(String.format("%s %s %s %s", point1, point2, point3, point4));

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