PDA

View Full Version : Triangle Angles


wa2blah
10-28-2005, 02:50 PM
Here is my code for a triangle program:



/**
* This program will do different things with a triangle like calculate area, etc.
*
* @author Murray McClafferty
* @version Oct. 27th, 2005
*/
public class Triangle
{
// instance variables
private double a; //One length
private double b; //Second length
private double c; //Third length

/**
* Constructor for objects of class Triangle
*/
public Triangle(double myA, double myB, double myC)
{
// initialise instance variables
a = myA;
b = myB;
c = myC;
}//constructor

/**
* Accessor for the a value
*
*
* @return the a value
*/
public double getA()
{
return a;
}//getA
/**
* Accessor for the b value
*
*
* @return the b value
*/
public double getB()
{
return b;
}//getB
/**
* Accessor for the c value
*
*
* @return the c value
*/
public double getC()
{
return c;
}//getC

/**
* Calculate the area of the triangle
*
* @return The area of the triangle
*/
public double area()
{
double s = .5 * (a + b + c); //s=semiperimeter
double area = Math.sqrt (s*(s-a)*(s-b)*(s-c));
return area;
}//area

/**
* Calculate perimeter of the Triangle
*
* @return The perimeter of the triangle
*/
public double perimeter()
{
double perimeter = a + b + c;
return perimeter;
}//perimeter

/**
* Calculate the angle of the three sides
*
* @return The angle of the three sides
*/
public double angle()
{
return angle;
}
}


Can someone explain to me how and what libraries I would use to calculate the angle in each corner of the triangle?

TheShaner
10-28-2005, 04:28 PM
You need to make an attempt. You've just followed the same format as the Rectangle class. You're just cookie-cutting the classes but not really understanding what you've been doing. It's almost as if you've grabbed a certain format from someone else and just been throwing it into a program without completely understanding how the class actually works.

First, do you know how to find the angle of a triangle? What is the formula? If you don't know, search for it or ask your math or computer teacher. Once you know that, then you may be able to proceed with an actual programming question.

Second, you'll notice that you need to be able to store angles and such of the triangle. So, you'll have to modify the triangle class to allow the storing of angles a, b, and c and provide accessor methods of those too.

Third, to calculate an angle, have the angle method accept whatever angles and sides it needs as input. Follow the same format as your area method.

Good luck and if you need help again, please specify a certain issue or let us know how you think you should do the program and we'll guide you and help you correct your code.

-Shane

wa2blah
10-28-2005, 05:33 PM
The rectangle class was my program, however, I jsut had to modify it in order to do a triangle. I do know the formula to get the sides (cosine law, a^2=b^2+c^2-2bc cosA) but what is the line used to do the cos? i.e., math.sqrt is for square root but what is it for cosine? From there, I can finish my program,

Brandoe85
10-28-2005, 05:37 PM
Math.cos()
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

Good luck;

wa2blah
10-28-2005, 06:02 PM
Thanks, that's what I was looking for.