# Related to Point

• 10-13-2012, 08:39 PM
jwl
Related to Point
I need to make a method like so

Code:

`public String TriangleType(Point a, Point b, Point c)`
which will return the values either Equilateral, Isosceles, or Scalar.

so far this is what I have:

Code:

```    length1 = Math.sqrt(Math.pow((y2 - y1),2) + Math.pow((x2 - x1),2));         length2 = Math.sqrt(Math.pow((y3 - y2),2) + Math.pow((x3 - x2),2));         length3 = Math.sqrt(Math.pow((y1 - y3),2) + Math.pow((x1 - x3),2));         System.out.printf("(" + x1 + ", " + y1 + ")" + "(" + x2 + ", " + y2                           + ")" + "(" + x3 + ", " + y3 + ")");         System.out.printf("|(%.3f, %.3f, %.3f)",length1, length2, length3);         System.out.println();         if ((length1 != length2) && (length2 != length3))           { System.out.println("SC"); return "SC"; }         else if ((length1 == length2) && (length1 == length3))           { System.out.println("EQ"); return "EQ"; }         else { System.out.println("IS"); return "IS"; }```
One thing I don't understand is where to use point a, b, and c.

I also have a subclass of Point which I don't quite understand either. If someone could help me out with this please let me know. I can add information as needed.
• 10-14-2012, 04:06 PM
Re: Related to Point
Well, your code isn't in a method, so how is it even runnable?

Your code block is looking at the x,y coordinates of the points. You need three points to make a triangle right? So what don't you understand?
• 10-14-2012, 08:31 PM
jwl
Re: Related to Point
Quote:

Originally Posted by quad64bit
Well, your code isn't in a method, so how is it even runnable?

Your code block is looking at the x,y coordinates of the points. You need three points to make a triangle right? So what don't you understand?

The code is actually in the TriangleType () {} method. My problem is that I have another method that generates three points x1, y1, x2, y2, x3, y3 and also puts it in a file.

Code:

```public void GenerateRandomTriangles(int numTriangles) {         int max = 150, x1, y1, x2, y2, x3, y3;         String str = "(%3d, %3d) (%3d, %3d) (%3d, %3d)";         Random rand = new Random(0);         try {             PrintWriter outFile = new PrintWriter("Triangle.txt");             Scanner scan = new Scanner(new File("Triangle.txt"));             //try {             //numTriangles = scan.nextInt(); scan.nextLine();             //} catch (NoSuchElementException nse) {             //    System.out.print(" ");             //}             outFile.println("5 //numTriangles; each line below gives integer"                   + "coordinates (x, y) of verticies of a triangle ");             for (int i = 0; i < numTriangles; i++) {                 x1 = rand.nextInt(max); y1 = rand.nextInt(max);                 x2 = rand.nextInt(max);                 do { y2 = rand.nextInt(max);                 } while ((y1 == y2));                 do {x3 = rand.nextInt(max);                 } while ((x3 == x1));                 do {y3 = rand.nextInt(max);                 } while (y3 == y2 || (y2 - y1)*(x3 - x1) ==                         (y3 - y1)*(x2 - x1));                    outFile.printf(str, x1, y1, x2, y2, x3, y3);                 outFile.println();             }         outFile.close();                    } catch (FileNotFoundException e) {             System.out.println("cannot create Triangle.txt");             System.exit(0);} }```
As you can see the generation of the points occurs in a for loop which I have numTriangles set to five. What I need it to do is communicate with the method TriangleType() { } as each iteration of the loop occurs to calculate the kind of triangle that is generated 5 times. A big thing is I need to create a subclass called Point which I believe is where I should do this. I may have jumped the gun with defining the variables in GenerateRandomTriangles() {} method but I need each definition of each x and y variable to transfer from one method to another and eventually come together in a TestTriangle() {} mehtod that with print the points and the kind of triangles for five different set of points(for five different triangles) with a couple of other prompts. Any kind of directional help will do. I have a couple a read file things there too but I really need to focus on distributing the points around effectively and more important; efficiently.
• 10-14-2012, 08:46 PM
Re: Related to Point
So if the method needs to return a special type of point (a subclass of point), thats easy enough to do. If it needs to return a collection of points, you can do that too with lists or arrays, or even a custom data type.

Are you unsure how to subclass a class?
• 10-14-2012, 08:54 PM
jwl
Re: Related to Point
Are you unsure how to subclass a class?[/QUOTE]

That seems to be my main problem. I do not have a lot of experience with subclasses. if someone can help me understand how to implement it maybe with an example of some sort closely related to what I described in the above post it would help me out a great deal.
• 10-15-2012, 04:51 AM
Junky
Re: Related to Point
Why do you need a subclass called Point? The Java API already has a class called Point which holds the x and y co-ords. If you really do need a subclass, what is it a subclass of?

BTW, back to basics. To write a subclass it needs to extend the superclass.
• 10-15-2012, 05:06 PM