Results 1 to 8 of 8
  1. #1
    coldplay7588 is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Red face Angle to Radians and Calculate the Cosine

    I need to finish this code, to prompt an angle in radians and calculate its cosine.
    Im a beginner and this is all very confusing still. Please help??? Anyone?



    import javax.swing.*;
    import java.text.*;

    public class SimpleTrig{

    public static void main(String[] args) {

    // I need this code...

    System.exit(0);
    }
    }



    1 Declare a variable angle as double
    2 Declare a variable angleCosine as double
    3 Declare a variable inputStr with data type String
    4 Write a code to allow ask a user to type in the angle, store it to inputStr. Then convert inputStr to the corresponding data type of angle, and store the result to variable angle
    5 Write a code to compute cosine of the variable angle and store the result to angleCosine. Please use Math.cos(angle) method to compute the cosine of this angle.
    3.6. Please use JOptionPane.showMessageDialog to display the value of angleCosine on the screen.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The instructions basically tell you exactly what you need to do here. If you don't understand them, you should first study (or re-study) your notes and texts and based on that information give it a try -- you've nothing really to lose by doing this and all to gain. If you still are stuck, then please post your attempt along with any errors you're getting and any specific questions you may have. Remember that the more specific the question, usually the better the answer. For instance, questions like "I have no idea how to do this..." usually can best be answered by a link to a tutorial while questions like "I don't know how to get information from the user using JOptionPane..." usually result in more direct recommendations and code corrections.

    Best of luck and welcome to the forum!

  3. #3
    coldplay7588 is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    This is my attempt.
    double angle;
    double angleCosine;

    String inputStr;
    inputStr = JOptionPane.showInputDialog(null, "Please enter the angle");


    angle = Math.cos(angle);



    im completely stuck after this point. None of my notes help me in this process.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coldplay7588 View Post
    This is my attempt.
    double angle;
    double angleCosine;

    String inputStr;
    inputStr = JOptionPane.showInputDialog(null, "Please enter the angle");


    angle = Math.cos(angle);



    im completely stuck after this point. None of my notes help me in this process.
    Lousy attempt; the cosine of an angle isn't an angle, it is a number in the range [-1, 1] (also see your point 5 in your requirements). The most daunting point is where you have to convert your String to a type appropriate for the Math.cos( ... ) method.

    kind regards,

    Jos

  5. #5
    coldplay7588 is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    How do i convert the string?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coldplay7588 View Post
    How do i convert the string?
    To what type?

    kind regards,

    Jos

  7. #7
    coldplay7588 is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    the same data type as the angle, so i would say double?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The Double class has a method called parseDouble that takes a String parameter and returns a double. e.g.,
    Java Code:
    String myString = "1.2345";
    double myDouble = Double.parseDouble(myString);

Similar Threads

  1. is Cosine Similarity the Default Similarity in Lucene?
    By sethu.iit@gmail.com in forum Lucene
    Replies: 0
    Last Post: 06-30-2010, 09:49 AM
  2. AffineTransform begin at custom angle
    By crazycat03 in forum AWT / Swing
    Replies: 3
    Last Post: 01-25-2010, 08:00 AM
  3. Java3D Vector3d.angle(Vector3d)
    By Duesentrieb in forum Advanced Java
    Replies: 5
    Last Post: 01-20-2009, 07:47 PM
  4. calculate fft
    By ram.west in forum Advanced Java
    Replies: 2
    Last Post: 08-27-2008, 03:05 AM
  5. Calculate X and Y given starting angle and distance
    By nidhirastogi in forum Java 2D
    Replies: 5
    Last Post: 08-18-2008, 10:24 PM

Tags for this Thread

Posting Permissions

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