Results 1 to 9 of 9
  1. #1
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Question triangle program

    Hello everyone!

    I have to write a program using these instructions. I am not asking you to program the assignment, I just need some help on the design proces.

    Instructions:
    Write a Class to store information about Points.

    Have a second class for a Triangle.

    The instance variables for the Triangle class should be the three Points (yes, inside one class it is legal to have instance variables from another class)the lengths of the three sides, the Area and the Perimeter of the triangle.

    To find the lengths of sides use the distance formula for the distance between two Points(in the point class there should be a method to find this distance).

    To find the area you should use Hero's formula. Look up this formula on the Internet. It uses the lengths of the three sides and the semi-perimeter. This formula can be used to find the area of any triangle (it does not have to be a right triangle).

    Allow the user to input (from the keyboard) the coordinates of the three Points.
    Once these coordinates are input, declare three Points.

    I would then have a constructor for the Triangle class that expects three Points.
    Call this constructor with the three Points.

    Have a method that could be used to call all other methods for computing the values of the other instance variables.

    Have a toString to output all of the data about a Triangle. There should also be a toString method inside the Point class for the output of any Point object.
    Ok, I understand that my Points class will need the methods to compute the distance, area, and perimeter, like so:

    Pseudocode for points class:

    Java Code:
    class Points()
    
    // I need to call the instance variables inside each method from the triangle class 
    
    findTheDistance
    
    findTheArea
    
    findThePerimeter
    But I am confused on the Triangle class. Am I supposed to create the variables a, b, c, perimeter, and the area inside the triangle class?

    Also, will the main method be inside the triangle class? Or is there going to be a third class for the user with a main method?

    confused here:confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by computerbum View Post
    But I am confused on the Triangle class. Am I supposed to create the variables a, b, c, perimeter, and the area inside the triangle class?
    Triangle will most definitely have 3 Point variables (or fields). It may or may not have perimeter and area fields, or you may elect to just have methods for the latter two and calculate the results when needed.

    Also, will the main method be inside the triangle class? Or is there going to be a third class for the user with a main method?
    It could possibly be in Triangle or elsewhere as this doesn't really matter since your program should function well regardless of where main is located.

  3. #3
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Ok, reading this line of the instructions, it says:

    I would then have a constructor for the Triangle class that expects three Points.
    Call this constructor with the three Points.
    Am I supposed to get the input of sides a, b, and c and then pass it to a constuctor like so:

    Java Code:
            System.out.print(" Enter sides a, b, c: "); // get three points
            double a = in.nextDouble();
            double b = in.nextDouble();
            double c = in.nextDouble();
    
           Triangle A = new Triangle(a, b, c);          // pass to new triangle
    Normally I would construct a new object, then set the variables by a mutator get method or use a method to set all variables inside the class.

    This seems strange to me, could some one please tell me if I am reading this correctly?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you know that a triangle has to have 3 points (not doubles, points), that it really doesn't make sense without 3 points, then you can create a constructor that accepts 3 points, and thereby prevent a Triangle from being constructed without 3 points. Think of it like a default constructor (what your used to) and a couple of setter methods combined.

    Having said that, this:
    Java Code:
            System.out.print(" Enter sides a, b, c: "); // get three points
            double a = in.nextDouble();
            double b = in.nextDouble();
            double c = in.nextDouble();
    
           Triangle A = new Triangle(a, b, c);
    is close, but won't work since you need Triangle to accept three Points, not three doubles.

  5. #5
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    ok, so it should be:

    Java Code:
    Triangle A = new Triangle();
    
    System.out.print( " Enter coordinates for side A: );
    double x = in.nextDouble();
    double y = in.netxDouble();
    
    A.setA(x, y);
    
    
    // then do the same and construct two more objects of sides B and C

  6. #6
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Oh shoot I am still wrong,

    I need to have variables to store the points of a triangle in the triangle class and have the coordinates x and y in the points class, right?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by computerbum View Post
    ok, so it should be:

    Java Code:
    Triangle A = new Triangle();
    
    System.out.print( " Enter coordinates for side A: );
    double x = in.nextDouble();
    double y = in.netxDouble();
    
    A.setA(x, y);
    
    
    // then do the same and construct two more objects of sides B and C
    No, at least not based on my interpretation of the requirements for your Triangle's constructor. You are passing two doubles into the Triangle via a setter and don't you instead need to construct the Triangle with 3 points? I think you'll need to do something like:
    <pseudocode>
    Java Code:
    get two doubles for point a
    get two doubles for point b
    get two doubles for point c
    
    Construct a new triangle passing points a, b, and c into its constructor
    </pseudocode>

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by computerbum View Post
    Oh shoot I am still wrong,

    I need to have variables to store the points of a triangle in the triangle class and have the coordinates x and y in the points class, right?
    YES!! That's it!

  9. #9
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Alright, now I can get to work, I sure do appreciate it. This assignment is due by tomorrow night along with my midterm exams due by March 3.

    Strapped for time has got my thinking cap on backwards, lol!

    Hopefully I will be good from here if I need anything else I will let you know, thanks again.

Similar Threads

  1. Help with calculating triangle program
    By ALH813 in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 05:08 AM
  2. Errors in Program (Right Triangle)
    By SupaStudy in forum New To Java
    Replies: 3
    Last Post: 03-26-2009, 10:42 AM
  3. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 02:08 PM
  4. Triangle Sides program
    By jamesov89 in forum New To Java
    Replies: 6
    Last Post: 10-06-2008, 03:36 AM
  5. Making triangle
    By banie in forum New To Java
    Replies: 4
    Last Post: 02-02-2008, 11:23 AM

Posting Permissions

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