2 Replies - 3379 Views - Last Post: 26 January 2010 - 02:24 PM Rate Topic: -----

#1 nicktrite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-January 10

NaN problem with calculating angles of a triangle.

Posted 26 January 2010 - 01:23 PM

Hey, I'm here a again with another problem.

I'm not quite sure whether there is something wrong with my code or with my math, but we are having to calculate the angles of a triangle when the user inputs the three sides. Once we have the angles, we are to determine the sides of a triangle that will have double the area of the first triangle.

I do know that the formula I am using here will work, because I have worked it out on paper.

The problem I encountered was that the area of the first triangle was coming up NaN. I traced this back to the actual problem being the angles were coming up NaN.

Here is what I have:
public class Triangle{
	private double sideA1;//this is a data field
	private double sideB1;
	private double sideC1;
	private double sidea2;
	private double sideb2;
	private double sidec2;
	private double angleA;
	private double angleB;
	private double angleC;
	private double area1;
	private double area2;
	private double perimeter;
	public Triangle(double a, double b, double c){//this is a constructor for an object rectangle of the class rectangle
		sideA1 = a;
		sideB1 = b;
		sideC1 = c;
		area2 = (sideA1*sideB1*Math.sin(angleC));
		perimeter = a+b+c;
	public double getAngleA() { 		
		angleA = Math.acos((sideA1*sideA1 + sideB1*sideB1 + sideC1*sideC1)/(-2*sideB1*sideC1));
		return angleA;
	public double getAngleB(){
		double angleB = Math.acos((sideA1*sideA1 + sideB1*sideB1 + sideC1*sideC1)/(-2*sideA1*sideC1));
		return angleB;
	public double getAngleC() {
		angleC = Math.PI - angleA - angleB;
		return angleC; 
	public double findPerimeter(){
		return perimeter;
	public double findArea(){
		area1 = (sideA1*sideB1*Math.sin(angleC))/2;
		return area1;

Then the test class is:
import javax.swing.JOptionPane;
public class Project2 {
	public static void main(String[] args){
		String ainput, binput, cinput;
		double a = 0;
		double b = 0;
		double c = 0;
		ainput = JOptionPane.showInputDialog("What is the value of a side of the triangle?");
		a = Double.parseDouble(ainput);
		binput = JOptionPane.showInputDialog("What is the value of the next side?");
		b = Double.parseDouble(binput);
		cinput = JOptionPane.showInputDialog("What is the value of the last side?");
		c = Double.parseDouble(cinput);
		Triangle t = new Triangle(a, b, c);
		//double area = t.findArea();
		double p = t.findPerimeter();
		System.out.println("Angle A = " + t.getAngleA());
		System.out.println("Angle B = " + t.getAngleB());
		System.out.println("Angle C = " + t.getAngleC());
		//System.out.println("area = " + area);
		System.out.println("Perimeter = " + p);

Is This A Good Question/Topic? 0
  • +

Replies To: NaN problem with calculating angles of a triangle.

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon

Reputation: 10364
  • View blog
  • Posts: 38,392
  • Joined: 27-December 08

Re: NaN problem with calculating angles of a triangle.

Posted 26 January 2010 - 02:12 PM

Are you sure you're getting NaN as the output for your test class? When I try to compile, it fails b/c you call a print() method from the Triangle object, but it isn't defined in the Triangle class. Also, in the future, please include any test input as well as the desired output vs. the expected output so that we have something to test against. Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

#3 dukky  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 111
  • Joined: 28-November 09

Re: NaN problem with calculating angles of a triangle.

Posted 26 January 2010 - 02:24 PM

i have a suggestion for this, i am not positive of it though, whenever you use the tan cos or sin methods, you have to give them degrees in radians. i dont know what the difference is normally but i know that its supposed to go in as radians, so if you haven't figured it out yet, use the "Math.toRadians()" method to enter the degrees
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1