Radians

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

radians

Radians

As is normal for most calculators (and most programming languages), the arguments for Java's trigonometric functions are in radians. Unlike most calculators, you can't push a button and switch to degrees. Here is an example program:

import java.io.*;
import java.util.*;

class CosineCalc
{
  public static void main (String[] args)  
  {
    double value;
    Scanner scan = new Scanner( System.in );
 
    System.out.print("Enter radians:");
    value = scan.nextDouble();
    
    // calculate its cosine
    double result = Math.cos( value );
    
    // write out the result
    System.out.println("cosine: " + result );
  }
}

Here is an example run:

C:/chap11>java CosineCalc
Enter radians:1.5
cosine: 0.0707372016677029

QUESTION 19:

What do you suspect are the types of the argument and return value for Math.cos()?