Results 1 to 7 of 7
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Related to Point

    I need to make a method like so

    Java 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:

    Java 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.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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?

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: Related to Point

    Quote Originally Posted by quad64bit View Post
    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.

    Java 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.
    Last edited by jwl; 10-14-2012 at 09:44 PM.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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?

  5. #5
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default 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.
    Last edited by jwl; 10-15-2012 at 02:42 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Related to Point

    Junky is correct on both counts. If you must subclass Point (instead of just using an instance of it) then you need to make a new class. This might help: Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

Similar Threads

  1. Replies: 1
    Last Post: 04-27-2012, 08:14 AM
  2. Related to ...
    By siva_vs_siva in forum New To Java
    Replies: 2
    Last Post: 09-09-2010, 06:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •