1,105,386 Community Members

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

Member Avatar
konman795
Newbie Poster
11 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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();
    }
}
Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
1
 

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.

Member Avatar
konman795
Newbie Poster
11 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: