Results 1 to 9 of 9
Thread: triangle program
 03012010, 07:55 AM #1Member
 Join Date
 Feb 2010
 Posts
 26
 Rep Power
 0
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 semiperimeter. 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.
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
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:

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?
 03022010, 05:09 AM #3Member
 Join Date
 Feb 2010
 Posts
 26
 Rep Power
 0
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.
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
This seems strange to me, could some one please tell me if I am reading this correctly?

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);
 03022010, 05:39 AM #5Member
 Join Date
 Feb 2010
 Posts
 26
 Rep Power
 0
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
 03022010, 05:45 AM #6Member
 Join Date
 Feb 2010
 Posts
 26
 Rep Power
 0
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?

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

 03022010, 05:55 AM #9Member
 Join Date
 Feb 2010
 Posts
 26
 Rep Power
 0
Similar Threads

Help with calculating triangle program
By ALH813 in forum New To JavaReplies: 5Last Post: 10192009, 05:08 AM 
Errors in Program (Right Triangle)
By SupaStudy in forum New To JavaReplies: 3Last Post: 03262009, 11:42 AM 
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01102009, 03:08 PM 
Triangle Sides program
By jamesov89 in forum New To JavaReplies: 6Last Post: 10062008, 03:36 AM 
Making triangle
By banie in forum New To JavaReplies: 4Last Post: 02022008, 12:23 PM
Bookmarks