1. Member
Join Date
Sep 2010
Posts
4
Rep Power
0

## Angle to Radians and Calculate the Cosine

I need to finish this code, to prompt an angle in radians and calculate its cosine.

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. 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. Member
Join Date
Sep 2010
Posts
4
Rep Power
0
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. Originally Posted by coldplay7588
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. Member
Join Date
Sep 2010
Posts
4
Rep Power
0
How do i convert the string?

6. Originally Posted by coldplay7588
How do i convert the string?
To what type?

kind regards,

Jos

7. Member
Join Date
Sep 2010
Posts
4
Rep Power
0
the same data type as the angle, so i would say double?

8. 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);```