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: Trouble with law of Cosine program

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Trouble with law of Cosine program

    Hello all you Java programmers out there. I have a tiny problem. The code is not allowing me to use the cos(double x) and acos(double x) methods even though I imported the math class. So....Whats wrong with my code???


    /*
    * Andre Taylor
    * 10/26/2010
    * This program is a prototype that is designed to use the law of cosines to solve oblique triangles
    * The user inputs the given values and the program computes and displays
    * solved triangle
    */

    package obliquetrianglescosine;
    import java.lang.Math; //*Imported to use cosine and arccosin methods
    //Its not working for some reason. it Says unused port even though I
    //call for the cos and arccos method
    import java.util.Scanner;
    /**
    *
    * @author owner
    */
    public class cosineLaw {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    double a; //Declare variables for values given
    double b;
    double c;
    double A;
    double B;
    double C;

    System.out.println("So what info was given to you? If nothing is input put 0 where value is."); //User inputs info given if nothing given user inputs 0
    System.out.print("a:");
    Scanner input = new Scanner (System.in);
    a= input.nextDouble();

    System.out.print("b:");
    b= input.nextDouble();

    System.out.print("c:");
    c= input.nextDouble();

    System.out.print("A:");
    A= input.nextDouble();

    System.out.print("B:");
    B= input.nextDouble();

    System.out.print("C:");
    C= input.nextDouble();

    computeLastSide( a, b, C, c);
    computeSecondAngle( a, b, c,A);
    computeLastAngle( A, B, C);

    System.out.println("Side c= "+c);
    System.out.println("Angle A= "+A);
    System.out.println("Angle B= "+B);


    }
    public static double computeLastSide(double a,double b,double C, double c) //computes the last side of triangle
    {

    c =(a*a+b*b)-2*a*b*cos(C);
    return c;

    }
    public static double computeSecondAngle(double a, double b, double c, double A) //computes second angle
    {
    A = acos(((b*b+c*c)-a*a)/2*b*c);
    return A;
    }
    public static double computeLastAngle(double A, double B, double C) //computes last angle
    {
    B= 180-A-C;
    return B;
    }
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: Trouble with law of Cosine program

    Quote Originally Posted by Delstateprogramer View Post
    Hello all you Java programmers out there. I have a tiny problem. The code is not allowing me to use the cos(double x) and acos(double x) methods even though I imported the math class. So....Whats wrong with my code???


    /*
    * Andre Taylor
    * 10/26/2010
    * This program is a prototype that is designed to use the law of cosines to solve oblique triangles
    * The user inputs the given values and the program computes and displays
    * solved triangle
    */

    package obliquetrianglescosine;
    import java.lang.Math; //*Imported to use cosine and arccosin methods
    //Its not working for some reason. it Says unused port even though I
    //call for the cos and arccos method
    import java.util.Scanner;
    /**
    *
    * @author owner
    */
    public class cosineLaw {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    double a; //Declare variables for values given
    double b;
    double c;
    double A;
    double B;
    double C;

    System.out.println("So what info was given to you? If nothing is input put 0 where value is."); //User inputs info given if nothing given user inputs 0
    System.out.print("a:");
    Scanner input = new Scanner (System.in);
    a= input.nextDouble();

    System.out.print("b:");
    b= input.nextDouble();

    System.out.print("c:");
    c= input.nextDouble();

    System.out.print("A:");
    A= input.nextDouble();

    System.out.print("B:");
    B= input.nextDouble();

    System.out.print("C:");
    C= input.nextDouble();

    computeLastSide( a, b, C, c);
    computeSecondAngle( a, b, c,A);
    computeLastAngle( A, B, C);

    System.out.println("Side c= "+c);
    System.out.println("Angle A= "+A);
    System.out.println("Angle B= "+B);


    }
    public static double computeLastSide(double a,double b,double C, double c) //computes the last side of triangle
    {

    c =(a*a+b*b)-2*a*b*cos(C);
    return c;

    }
    public static double computeSecondAngle(double a, double b, double c, double A) //computes second angle
    {
    A = acos(((b*b+c*c)-a*a)/2*b*c);
    return A;
    }
    public static double computeLastAngle(double A, double B, double C) //computes last angle
    {
    B= 180-A-C;
    return B;
    }
    }
    Use Math.acos. You can't just say acos() as that's not a method of your class.

    Also, be careful to make sure when checking for sines and cosines that it's returning degrees or radians like you want it to.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with law of Cosine program

    thanks and how would I be sure that its returning degrees(I dont want Radians)?and what of the regular cos(double x) that one is giving me an error too?

  4. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Trouble with law of Cosine program

    Look at the javadoc for the math class: Math (Java Platform SE 6)

    You should be able to figure out if it is using radians or degrees....... then if you need to convert it, you can use toDegrees, or toRadians, which are both part of the Math class

Similar Threads

  1. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM
  2. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 12:37 PM
  3. cosine in java
    By etidd in forum Java Theory & Questions
    Replies: 4
    Last Post: January 22nd, 2010, 09:33 AM
  4. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM
  5. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM