Exploring the cos() Method in Java: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-04 04:15:13   6272  Share
Subject Syllabus DetailsSubject Details 1 Program
☰ TContent
☰Fullscreen

Table of Content:

Description

On this tutorial, we will be showing a java example on how to use the cos(double a) method of Math Class. The cos(double a) returns the cosine 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.

 

Most of the methods of the Math class is static and the cos() 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.cos(a).

The method returns the cosine of the specified double value.

Syntax

double cos(double d)

Parameters

Here is the detail of parameters ?

  • d ? This method accepts a value of double data type. An angle, in radians.

Return Value

  • This method returns the cosine of the specified double value.

Compatibility

Requires Java 1.0 and up

Example

public class MarhCos {

   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 cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians));
   }
}

Output

The value of pi is 3.1416
The cosine of 45.0 degrees is 0.7071
Press any key to continue . . .

Example

Below is a java code demonstrates the use of cos() method of Math class. The example presented might be simple however it shows the behavior of the cos() method.

 
import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * cos() method of Math class
 */

public class MathCosineExample {

	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 cosine of the angle
		double cosValue = Math.cos(valueRadians);
		System.out.println("cosine of " + s + " is " + cosValue);
	}
}

Output

Enter an angle in degrees:45
cosine of 45 is 0.7071067811865476
Press any key to continue . . .

The above java example source code demonstrates the use of cos() 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 asin() method accepts double method argument. After transforming into double we have also used Math.toRadians() to convert the input to radians which is the required method argument.