Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need help using the MATH class for an assignment

1. ## Need help using the MATH class for an assignment

Here is the assignment:

Prompt the user to input two sides (a and b) of a right triangle. Calculate, using the
formulas below, the hypotenuse and the other two angles of the triangle. Calculate the
perimeter and the area.

Here are the formulas:

a^2 + b^2 = h^2
sinθ = a/h
A + B + C = 180 degrees

I really just need help on how to start this program out, i know which math classes i need to use. I need help creating the formulas.

3. ## Re: Need help using the MATH class for an assignment

I need help creating the formulas.
Do you have any buddies that know math? Can you get one of them to write you the equations that are needed.
Once you have the equations and need help coding them, post the equations and the code you are writing to solve that equation and ask some questions about the problems you are having.

4. ## Re: Need help using the MATH class for an assignment

```import java.util.Scanner;
import java.text.DecimalFormat;

public class RightTriangle {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.000");
double side_A, side_B, angle_A, angle_B, hypotenuse;
final double right_Angle = 90.0;
System.out.print("Please enter length of side a: ");
side_A = input.nextDouble();
System.out.print("Please enter length of side b: ");
side_B = input.nextDouble();
System.out.printf("Side A     = %6s\n",df.format(side_A));
System.out.printf("Side B     = %6s\n",df.format(side_B));
hypotenuse = Math.sqrt(side_A*side_A+side_B*side_B);
angle_A = Math.asin(side_B/hypotenuse)*180/Math.PI;
angle_B = right_Angle-angle_A;
System.out.printf("Hypotenuse = %6s\n",df.format(hypotenuse));
System.out.printf("Angle A    = %6s\n",df.format(angle_A));
System.out.printf("Angle B    = %6s\n",df.format(angle_B));
System.out.printf("Angle C    = %6s\n",df.format(right_Angle));
}

}```

This is what i have so far, now i just need the area and perimeter. stuck at this point.

--- Update ---

I was able to figure it out here is my final result:
```import java.util.Scanner;
import java.text.DecimalFormat;

public class RightTriangle {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.000");

double side_A, side_B, angle_A, angle_B, hypotenuse, triangle_Area, triangle_Perimeter;
final double right_Angle = 90.0;

System.out.print("Please enter length of side a: ");
side_A = input.nextDouble();
System.out.print("Please enter length of side b: ");
side_B = input.nextDouble();
System.out.printf("Side a     = %6s\n",df.format(side_A));
System.out.printf("Side b     = %6s\n",df.format(side_B));

hypotenuse = Math.sqrt(side_A*side_A+side_B*side_B);
angle_A = Math.asin(side_B/hypotenuse)*180/Math.PI;
angle_B = right_Angle-angle_A;
System.out.printf("Hypotenuse = %6s\n",df.format(hypotenuse));
System.out.printf("Angle A    = %6s\n",df.format(angle_A));
System.out.printf("Angle B    = %6s\n",df.format(angle_B));
System.out.printf("Angle C    = %6s\n",df.format(right_Angle));

triangle_Area = side_A*side_B/2;
System.out.printf("Area       = %6s\n",df.format(triangle_Area));
triangle_Perimeter = side_A+side_B+Math.sqrt(side_A*side_A+side_B*side_B);
System.out.printf("Perimeter  = %6s\n",df.format(triangle_Perimeter));

input.close();
}
}```