Results 1 to 7 of 7
  1. #1
    jlcarrillo is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default 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 07:30 AM.

  2. #2
    Hamenopi is offline Nub
    Join Date
    Sep 2014
    Location
    Houston
    Posts
    8
    Rep Power
    0

    Default 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. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jlcarrillo is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default 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. #6
    jlcarrillo is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default 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. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Back to the math class....
    By Lionlev in forum Advanced Java
    Replies: 11
    Last Post: 10-31-2012, 09:19 PM
  2. help with math class and random numbers
    By NestorPrime in forum New To Java
    Replies: 9
    Last Post: 10-01-2011, 10:57 PM
  3. Math Class
    By ritwik07 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 05:06 PM
  4. Math class question
    By shendel in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 01:06 PM
  5. Math Class (ceil, floor, abs)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-13-2007, 10:21 AM

Posting Permissions

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