Fun and Simple Trig!

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:

Of course! It's that formula you memorized in high school (and forgot five minutes later).

Fun and Simple Trig!

Actually the formula is not that bad:

radians = (Math.PI/180.0)*degrees;

Since this is so commonly done, the Java Math class has convenient methods to do it and the opposite:

public static double toRadians(double angdeg)
    Converts an angle measured in degrees to the equivalent in radians
    
public static double toDegrees(double angrad)
    Converts an angle measured in radians to the equivalent in degrees.

Here is our sample program:

import java.io.*;
class CosineCalc
{
  public static void main (String[] args) 
  {
    Scanner scan = new Scanner( System.in );
    double degrees;

    // read in the degrees
    System.out.print  ("Enter degrees:");
    degrees = scan.nextDouble();
    
    
    // calculate its cosine
    double result = Math.cos();
    
    // write out the result
    System.out.println("cosine: " + result );
  }
}

QUESTION 21:

Fill in the blank so that the program works.