Rad2Deg.java

Download Rad2Deg.java

 1: // Demo of defining a Java 8 Function, and applying it.
 2: // Here we simply convert Radians to the equivalent value in Degrees.
 3: //
 4: // Written 7/2016 by Wayne Pollock, Tampa Florida USA.
 5: 
 6: import java.util.function.*;
 7: 
 8: public class Rad2Deg {
 9:    public static void main ( String [] args ) {
10:       double radian = 0.0;
11: 
12:       // Convert command line argument to a double (Radians):
13:       try {
14:          radian = Double.parseDouble( args[0] );
15:       } catch ( Exception e ) {
16:          System.err.println( "Usage: java Rad2Deg <radians>,\n"
17:             + "where <radians> is a double value to convert to degrees." );
18:          System.exit( 1 );
19:       }
20: 
21:       // Define a function (using lambda notation):
22:       Function<Double,Double> toDegrees = radians -> (180 * radians)/Math.PI;
23: 
24:       // Use function:
25:       System.out.printf( "%s Radians = %4.2f Degrees.%n", args[0],
26:          toDegrees.apply(radian) );
27:    }
28: }