954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How format the output of a "Point" for triangle class

Hi everybody, I have been having trouble with the output of a set of points for my triangle program. When I run my program, it outputs in the end "triangle1 coordinates: Vertex A is java.awt.Point[x=12,y=13]" and I have no idea how to format it to hide the "java.awt.Point" part. Any advice would be greatly appreciated! Thanks in advance.

public class TriangleTester 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter the x- and y- coordinates of the first vertex: ");
        int a1 = scan.nextInt();
        int a2 = scan.nextInt();
        
        System.out.println("Enter the x- and y- coordinates of the second vertex: ");
        int b1 = scan.nextInt();
        int b2 = scan.nextInt();
                
        System.out.println("Enter the x- and y- coordinates of the third vertex: ");
        int c1 = scan.nextInt();
        int c2 = scan.nextInt();
        
        Triangle triangle1 = new Triangle(a1, a2, b1, b2, c1, c2);
        
        System.out.println(triangle1.toString());
    }
}
public class Triangle 
{
    private int xA;
    private int yA;
    private int xB;
    private int yB;
    private int xC;
    private int yC;
    
    Point vertexA;
    Point vertexB;
    Point vertexC;

    public Triangle(Point vertexA, Point vertexB, Point vertexC) 
    {
        vertexA = vertexA;
        vertexB = vertexB;
        vertexC = vertexC;
    }
    
    public Triangle(int xA, int yA, int xB, int yB, int xC, int yC)
    {
        vertexA = new Point(xA, yA);
        vertexB = new Point(xB, yB);
        vertexC = new Point(xC, yC);
    }
   
    public String toString()
    {
       return "Vertex A is " + vertexA.toString();
    }
}
konman795
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

You are calling default "toString()" method of "Point" class in your toString() method.

return "Vertex A is " + vertexA.toString();

If you want your own format, you must not use the toString() of Point class. You can simply format it whatever way you want. i.e. vertexA.x for "x" value.

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

Awesome!! Thanks, that's exactly what I needed!

konman795
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: