We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,610 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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();
    }
}
2
Contributors
2
Replies
59 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
konman795
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 376
Skill Endorsements: 17

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

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

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0571 seconds using 2.69MB