Mastering the sin() Method in Java: A Complete Tutorial
Table of Content:
Description
The method returns the sine of the specified double value.
On this tutorial, we will be showing a java example on how to use the sin(double a) method of Math Class. The sin(double a) returns the trigonometric sine of the method argument value. It must be noted that the parameter is in radians, however, if your source value is a degree which is always the case for real world applications the handy Math.toRadians() method will be helpful for the conversion.
- If the argument is NaN or an infinity, then the result is NaN.
- If the argument is zero, then the result is a zero with the same sign as the argument.
Most of the methods of the Math class is static and the sin() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object instead call it using Math.sin(a).
Syntax
double sin(double d)
Parameters
Here is the detail of parameters ?
-
d ? A double data type. An angle, in radians.
Return Value
-
This method returns the sine of the specified double value.
Compatibility
Requires Java 1.0 and up
Example
public class MathSin { public static void main(String args[]) { double degrees = 45.0; double radians = Math.toRadians(degrees); System.out.format("The value of pi is %.4f%n", Math.PI); System.out.format("The sine of %.1f degrees is %.4f%n", degrees, Math.sin(radians)); } }
Output
The value of pi is 3.1416 The sine of 45.0 degrees is 0.7071 Press any key to continue . . .
Example
Below is a java code demonstrates the use of sin() method of Math class. The example presented might be simple however it shows the behavior of the sin() method.
import java.util.Scanner; /* * This example source code demonstrates the use of * sin() method of Math class */ public class MathSineExample { public static void main(String[] args) { // Ask for user input System.out.print("Enter an angle in degrees:"); // use scanner to read the console input Scanner scan = new Scanner(System.in); // Assign the user to String variable String s = scan.nextLine(); // close the scanner object scan.close(); // convert the string input to double double value = Double.parseDouble(s); // convert the value to radians double valueRadians = Math.toRadians(value); // get the sine of the angle double sineValue = Math.sin(valueRadians); System.out.println("sine of " + s + " is " + sineValue); } }
Output
Enter an angle in degrees:90 sine of 90 is 1.0 Press any key to continue . . .
The above java example source code demonstrates the use of sin() method of Math class. We simply ask for user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value, and the return data type is String thus we have used the Double.parseDouble() to transform it into double. We have to convert it first to double because the sin() method accepts double method argument. After transforming into double we have also used Math.toRadians() to convert the input to radians which are the required method argument.
Java sin() method Example
The Java Math.sin Function allows you to find the trigonometry Sine for the numeric values. In this Java program, We are going to find the Sine values of both positive and negative values and display the output
// Java sin Function public class SinMethod { public static void main(String[] args) { double x = Math.sin(1.96 - 4.65 + 2.98); System.out.println("Sine value = " + x); System.out.println("\nSine value of Positive Number = " + Math.sin(10.25)); System.out.println("Sine value of Positive Number= " + Math.sin(2.28)); System.out.println("\nSine value of Negative Number = " + Math.sin(-4.85)); System.out.println("Sine value of Negative Number = " + Math.sin(-7.95)); double m = Math.toRadians(30); System.out.println("\nSine value of Negative Number = " + Math.sin(m)); } }
Output
Sine value = 0.28595222510483514 Sine value of Positive Number = -0.7346984304047954 Sine value of Positive Number= 0.758880708180922 Sine value of Negative Number = 0.9905465359667133 Sine value of Negative Number = -0.9953937772576199 Sine value of Negative Number = 0.49999999999999994 Press any key to continue . . .
Example
The Java Math.sin Function allows you to find the trigonometry Sine for the numeric values. In this Java program, We are going to find the Sine values of both positive and negative values and display the output
// Java sin Function public class SinMethod { public static void main(String[] args) { double x = Math.sin(1.96 - 4.65 + 2.98); System.out.println("Sine value = " + x); System.out.println("\nSine value of Positive Number = " + Math.sin(10.25)); System.out.println("Sine value of Positive Number= " + Math.sin(2.28)); System.out.println("\nSine value of Negative Number = " + Math.sin(-4.85)); System.out.println("Sine value of Negative Number = " + Math.sin(-7.95)); double m = Math.toRadians(30); System.out.println("\nSine value of Negative Number = " + Math.sin(m)); } }
Output
Sine value = 0.28595222510483514 Sine value of Positive Number = -0.7346984304047954 Sine value of Positive Number= 0.758880708180922 Sine value of Negative Number = 0.9905465359667133 Sine value of Negative Number = -0.9953937772576199 Sine value of Negative Number = 0.49999999999999994 Press any key to continue . . .
Java sin on Array example
In this Java program we will show you, How to find the sine values of bulk data. Here, we are going to declare an array of double type and find the sine values of an array elements.
public class SinMethodOnArray { public static void main(String[] args) { double [] myArray = {0, 1, 30, 45, 60, 75, 90, 120, 180, 240, 360}; for (int i = 0; i < myArray.length; i++) { System.out.format("Sine value of Array Element %.2f = %.4f\n", myArray[i], Math.sin(myArray[i])); } } }
Output
Sine value of Array Element 0.00 = 0.0000 Sine value of Array Element 1.00 = 0.8415 Sine value of Array Element 30.00 = -0.9880 Sine value of Array Element 45.00 = 0.8509 Sine value of Array Element 60.00 = -0.3048 Sine value of Array Element 75.00 = -0.3878 Sine value of Array Element 90.00 = 0.8940 Sine value of Array Element 120.00 = 0.5806 Sine value of Array Element 180.00 = -0.8012 Sine value of Array Element 240.00 = 0.9454 Sine value of Array Element 360.00 = 0.9589 Press any key to continue . . .
Java sin Function on Arraylist example
In this Java program we are going to declare an arraylist of double type and find the sine values of list elements.
import java.util.ArrayList; public class SinMethodOnArrayList { public static void main(String[] args) { ArrayListmyList = new ArrayList (5); myList.add(30.45); myList.add(45.64); myList.add(120.60); myList.add(180.75); myList.add(90.68); myList.add(14.98); for (double x : myList) { System.out.format("Sine value of ArrayList Item %.2f = %.4f \n", x, Math.sin(x)); } } }
Output
Sine value of ArrayList Item 30.45 = -0.8226 Sine value of ArrayList Item 45.64 = 0.9962 Sine value of ArrayList Item 120.60 = 0.9389 Sine value of ArrayList Item 180.75 = -0.9941 Sine value of ArrayList Item 90.68 = 0.4134 Sine value of ArrayList Item 14.98 = 0.6654 Press any key to continue . . .
ANALYSIS
First, We declared an ArrayList of integer type and assigned some random values.ArrayListNext, We used the For Loop to iterate the double values in ArrayListmyList = new ArrayList (5); myList.add(30.45); myList.add(45.64); myList.add(120.60); myList.add(180.75); myList.add(90.68); myList.add(14.98);
for (double x : myList) {Following statements will print the output. If you observe the code snippet, we used the Math.sin Function directly inside the System.out.format statement. Here, compiler will call the java math.sin method ( static double sin(double x) ) to find the corresponding Sine values.
System.out.format("Sine value of ArrayList Item %.2f = %.4f \n", x, Math.sin(x));