# Geometry: Display angles of a triangle

Page 1 of 1

## 5 Replies - 4279 Views - Last Post: 02 March 2015 - 02:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=330803&amp;s=7d391d9a46f9cd869da2c5a770eb9299&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 coxxie

Reputation: 0
• Posts: 17
• Joined: 29-October 11

# Geometry: Display angles of a triangle

Posted 05 October 2013 - 11:02 AM

(Geometry: display angles) Write a program that prompts the user to enter three
points of a triangle and displays the angles in degrees. Round the value to keep
two digits after the decimal point. The formula to compute angles A, B, and C are
as follows:

A = arccos((a * a - b * b - c * c) / (-2 * b * c))
B = arccos((b * b - a * a - c * c) / (-2 * a * c))
C = arccos((c * c - b * b - a * a) / (-2 * a * />/>)

Here is a sample run of the program:

Enter three points: 6.5 6.5 2.5
The three angles are 15.26 90.0 74.74

Here is my code so far: I am having trouble trying to take in three variables, pass them to the method and then return them.
```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pkg5.pkg23;

/**
*
* @author Coxxie
*/
import java.util.Scanner;

public class Main
{

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter 3 sides of a triangle: \n");
System.out.print("Enter side A of a triangle: \n");
double sideA = input.nextInt();
System.out.print("Enter side B of a triangle: \n");
double sideB = input.nextInt();
System.out.print("Enter side C of a triangle: \n");
double sideC = input.nextInt();

double anglel = angleA(sideA, sideB, sideC);
double angle2 = angleB(sideA, sideB, sideC);
double angle3 = angleC(sideA, sideB, sideC);

System.out.println
("The three angles are: " + angleA(anglel) + " "+ angleB(angle2) + " " + angleC(angle3));

}

public static double angleA(double sideA, double sideB, double sideC)
{
//figA = equation in book. Imput it in and copy in sides.
double angA = Math.acos((sideA * sideA - sideB * sideB - sideC * sideC) / (-2 * sideB * sideC));

return angA;
}

public static double angleB(double sideA, double sideB, double sideC)
{
//figA = equation in book. Imput it in and copy in sides.
double angB = Math.acos((sideA * sideA - sideB * sideB - sideC * sideC) / (-2 * sideB * sideC));

return angB;
}

public static double angleC(double sideA, double sideB, double sideC)
{
//figA = equation in book. Imput it in and copy in sides.
double angC = Math.acos((sideA * sideA - sideB * sideB - sideC * sideC) / (-2 * sideB * sideC));

return angC;
}
}
```

This post has been edited by jon.kiparsky: 05 October 2013 - 11:15 AM
Reason for edit:: [code] tags

Is This A Good Question/Topic? 0

## Replies To: Geometry: Display angles of a triangle

### #2 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Geometry: Display angles of a triangle

Posted 05 October 2013 - 11:32 AM

Why are you making your code about sides? Aren't you supposed to be entering points?

Quote

I am having trouble trying to take in three variables, pass them to the method and then return them.

You seem to have done all of these already. Apart from here:
``` System.out.println("The three angles are: " + angleA(anglel) + " "+ angleB(angle2) + " " + angleC(angle3));
```

in which case, this is what was probably intended:
``` System.out.println("The three angles are: " + anglel + " "+ angle2 + " " + angle3);
```

This post has been edited by Mylo: 05 October 2013 - 11:43 AM

### #3 schutzzz

• D.I.C Regular

Reputation: 141
• Posts: 342
• Joined: 22-April 13

## Re: Geometry: Display angles of a triangle

Posted 05 October 2013 - 12:43 PM

Your methods angleA, angleB, and angleC each require 3 parameters. You're only giving them each one.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Geometry: Display angles of a triangle

Posted 05 October 2013 - 06:04 PM

why 3 methods angleA(), angleB() and angleC() that do exactly the same thing ?
You need only one named angle()

### #5 coxxie

Reputation: 0
• Posts: 17
• Joined: 29-October 11

## Re: Geometry: Display angles of a triangle

Posted 06 October 2013 - 10:44 AM

I was focusing to much on the diagram provided for the and didn't read the problem though all the way. I was focusing on sides and not points of the triangle. When I get my code updated I will up load it.

Thanks for the pointers so far.

### #6 Neyroman

Reputation: 2
• Posts: 5
• Joined: 02-March 15

## Re: Geometry: Display angles of a triangle

Posted 02 March 2015 - 02:12 PM

Good day!

If you don't mind i would like to continue this discussion. As the code wasn't updated i decided to place my variant here. Well.. and i also have trouble with it as i am receiving incorrect output.

The program's output:

Enter three points: 1 1 6.5 1 6.5 2.5
The three angles are 89.04 90.00 89.74

but it has to be

Enter three points: 1 1 6.5 1 6.5 2.5
The three angles are 15.26 90.00 74.74

Would be very grateful for any help

```import java.util.Scanner;
public class Exercise_5_23
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter three points: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();

System.out.print("The three angles are ");
getAngle(x1, y1, x2, y2, x3, y3);
}

public static void getAngle(double x1, double y1, double x2, double y2,
double x3, double y3)
{
double sideA = Math.sqrt(Math.pow((x2 - x3), 2) + Math.pow((y2 - y3), 2));
double sideB = Math.sqrt(Math.pow((x3 - x1), 2) + Math.pow((y3 - y1), 2));
double sideC = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));

double angleA = Math.toDegrees(Math.acos(Math.toRadians((sideA * sideA - sideB * sideB -
sideC * sideC) / ((-2) * sideB * sideC))));
double angleB = Math.toDegrees(Math.acos(Math.toRadians((sideB * sideB - sideA * sideA -
sideC * sideC) / ((-2) * sideA * sideC))));
double angleC = Math.toDegrees(Math.acos(Math.toRadians((sideC * sideC - sideB * sideB -
sideA * sideA) / ((-2) * sideA * sideB))));

System.out.printf("%5.2f %5.2f %5.2f", angleA, angleB, angleC);
}

}
```