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

1. Member
Join Date
Sep 2014
Posts
5
Rep Power
0

## 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.
Last edited by jlcarrillo; 09-19-2014 at 06:30 AM.

2. Nub
Join Date
Sep 2014
Location
Houston
Posts
8
Rep Power
0

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

What have you written out thus far?
There are many ways to solve these.
Don't forget the code tags. =)

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

There's only one math class, and it's named Math; it has a bunch of static methods; e.g. Math.sin(x) returns the sine of angle x (measured in radians). Read its API documentation for the details.

kind regards,

Jos

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

jlcarrillo, you didn't bother to return to the earlier thread you started. Is history going to repeat itself here?

db

5. Member
Join Date
Sep 2014
Posts
5
Rep Power
0

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

Java Code:
```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.

6. Member
Join Date
Sep 2014
Posts
5
Rep Power
0

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

I was able to figure it out here is my result:
Java Code:
```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();
}
}```

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

Does your program produce the correct results? As you have figured out already, the Math methods assume angles measured in radians ...

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•