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 6 of 6

Thread: Law of cosines help to find angles URGENT!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Law of cosines help to find angles URGENT!

    Hello and thanks for stopping by to help me, ive been stumped on this for an hour and a half now. I have this program here :

    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import java.lang.Math;
     
    public class Triangle
    { 
     
     double x1 = 0;
     double x2 = 0;
     double y1 = 0;
     double y2 = 0;
     double z1 = 0;
     double z2 = 0;
     double sideA = 0;
     double sideB = 0;
     double sideC = 0;
     double area = 0;
     double perimeter = 0;
     double angleA = 0;
     double angleB = 0;
     double angleC = 0;   
     
       public Triangle(int x1, int x2, int y1, int y2, int z1, int z2)
       {
          this.x1 = x1;
          this.x2 = x2;
          this.y1 = y1;
          this.y2 = y2;
          this.z1 = z1;
          this.z2 = z2;
       }
     
       public double getX1()
       {
          return x1;
       }
     
       public double getX2()
       {
          return x2;
       }
     
       public double getY1()
       {
          return y1;
       }
     
       public double getY2()
       {
          return y2;
       }
     
       public double getZ1()
       {
          return z1;
       }
     
       public double getZ2()
       {
          return z2;
       }
     
       public double getLengthA()
       {
          sideA = Math.sqrt(Math.pow((z1-y1)+(z2-y2),2));
          return sideA;
       }
     
       public double getLengthB()
       {
          sideB = Math.sqrt(Math.pow((z1-x1),2) + Math.pow((z2-x2),2));
          return sideB;
       }
     
       public double getLengthC()
       {
          sideC = Math.sqrt(Math.pow((y1-x1),2) + Math.pow((y2-x2),2));
          return sideC;
       }
     
       public double getHeight()
       {
          return z2;
       }
     
       public double getArea()
       {
          area = (y1*z2)/2;
          return area;
       } 
     
       public double getPerimeter()
       {
          perimeter = getLengthA() + getLengthB() + getLengthC();
          return perimeter;
       }
     
       public double getAngleA()
       {
     
       }
     
       public double getAngleB()
       {
     
        }
     
       public double getAngleC()
        {
     
        }  
     
     
    }

    This program takes user input and makes some calculations such as area perimeter and angles. Assume the user enter x1=0, x2=0, y1=3, y2=0, z1=3, and z2=4. If a user inputes these numbers when they are callled to do so, all the proper calculations will be made except for angles. I have been stuck on the angle part for an hour and a half trying all different variations of the law of cosines in my code, which is, cos C = (a + b - c)/2ab. I cant get it to work at all though. If anyone could just write it out in code for me for just one of the angels, that would be great, as i could modify it accordingly to find the other ones by myself. Like i said ive been at this for hours and i cant get it to work so its becoming really frustrating. Please help if you can. Thanks!


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Law of cosines help to find angles URGENT!

    What are you trying/have tried? Is there anything in the Math class that could help?

    Another thing, you should really set all the values for your instance variables in your constructor. There is nothing stopping you from calling methods in the constructor. You're currently only setting coordinates. That's how I would do it, I'm pretty sure that's the preferred way to create an object.

    Similar to this
    public class Triangle{
     
        double x1 = 0;
        double x2 = 0;
        double y1 = 0;
        double y2 = 0;
        double z1 = 0;
        double z2 = 0;
        double sideA = 0;
     
        // Other instance variables omitted.
     
        // Constructor to set all the instance variables.
        public Triangle(int x1, int x2, int y1, int y2, int z1, int z2) {
            this.x1 = x1;
            this.x2 = x2;
            this.y1 = y1;
            this.y2 = y2;
            this.z1 = z1;
            this.z2 = z2;
            this.sideA = calcSideA(); // Notice this line
            // etc.....
        }
     
        // other methods omitted.
     
        // Create a calculation method to set the instance variable and make it
        // Private to this class
        private double calcSideA() {
            return Math.sqrt(Math.pow((z1 - y1) + (z2 - y2), 2));
        }
     
        // Return the value should it be needed in the calling Class or another
        // Class method.
        public double getSideA() {
            return sideA;
        }
     
    }

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Law of cosines help to find angles URGENT!

    Yeah i realize its messy and know how i can fix it with my next program but i am right near the end of this one so .im not gonna both going back to change anything now. As of right now i have gotten the computation of angleB to come out correctly for me here :

    public double getAngleB()
       {
          angleB = Math.acos(Math.toRadians((getLengthA() * getLengthA() + getLengthC() * getLengthC() - getLengthB() * getLengthB())/(2.0 * getLengthA() * getLengthB())));
          angleB = Math.toDegrees(angleB);
          return angleB; 
       }

    So all i need now is angleA and i can get angleC with no problem. I have tried revising the code from getAngleB to make it work with a but i have tried all the variations(i think) and cant get it to work. The correct angle for angleA would be 53.13010235415598, but i cant get that number to come out no matter what i try. Do you have anything that you think might work?

    --- Update ---

    And im not trying to be pushy or anything, but if you are still trying to help me, this assignment is due in 40 minutes, so if you could help within that time frame that would be awesome.

  4. #4
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Law of cosines help to find angles URGENT!

    I'll be honest with you, I'm not. I am at work. Unless someone swoops in I'd say you've left it too late to seek help.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Law of cosines help to find angles URGENT!

    Your time constraints are you problem. Marking your post as urgent generally means you get less help not more!
    Improving the world one idiot at a time!

  6. #6
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    234
    My Mood
    Stressed
    Thanks
    31
    Thanked 42 Times in 42 Posts

    Default Re: Law of cosines help to find angles URGENT!

    I'm guessing you have three sides. X, Y, and Z. x = x2 - x1 y = y2 - y1 z = z2 - z1 or something like that, right?

    I'm not sure which goes with which in the a, b, and c thing.

    I'll give you a hint about how to get the value of C

    C = arccos ((a^2 + b^2 - c ^2 ) / (2*a*b))

    Use

    Math.acos(double value);

    Sorry I can't more but you're not very clear on what exactly you need help with.

Similar Threads

  1. Find maximum working time [Urgent]
    By loong424 in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 22nd, 2013, 03:18 AM
  2. [SOLVED] Drawing lines using angles in Java HELP
    By shadysback in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2012, 02:10 PM
  3. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  4. Drawing images with different angles
    By gargamel7 in forum AWT / Java Swing
    Replies: 24
    Last Post: September 13th, 2011, 06:04 AM
  5. Law
    By Law in forum Member Introductions
    Replies: 1
    Last Post: January 21st, 2011, 10:46 AM