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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help using the MATH class for an assignment

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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; September 18th, 2014 at 11:28 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

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


  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();
    	}
    }

Similar Threads

  1. Hello, I have a class assignment and I need help.
    By Wings in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 17th, 2013, 11:49 PM
  2. class assignment
    By shanem in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 2nd, 2012, 01:23 PM
  3. class assignment
    By shanem in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2012, 04:09 PM
  4. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  5. Magic square class (modular math problem)
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2010, 10:56 AM