// // Formulae program // // Author: Kenneth Chan // email : kjc@liverpool.ac.uk // Date : October 2011 // Description: program that solves some easy formula // import java.util.Scanner ; class formula { static Scanner input = new Scanner( System.in ) ; //////////////////////////////////////////////////// // c2f: method to convert centigrade (celcius) to fahrenheit //////////////////////////////////////////////////// static void c2f() { double celcius ; double fahrenheit ; System.out.println( "CENTIGRADE TO FAHRENHEIT CONVERSION" ) ; System.out.println( "Please enter degrees celcius: " ) ; celcius = input.nextDouble() ; fahrenheit = ((celcius*9)/5)+32 ; System.out.println() ; System.out.printf( "%12.5f celcius is %12.3f fahrenheit\n", celcius, fahrenheit ) ; } // // f2c: method to convert fahrenheit to centigrade // static void f2c() { double celcius ; double fahrenheit ; System.out.println( "FAHRENHEIT TO CENTIGRADE CONVERSION" ) ; System.out.print( "Please enter degrees fahrenheit: " ) ; fahrenheit = input.nextDouble() ; celcius = 5 * ( fahrenheit - 32 ) / 9 ; System.out.println() ; System.out.printf( "%8.3f fahrenheit is %6.3f celcius\n", fahrenheit, celcius ) ; } // // c1: method to calculate the circumference of circle // static void c1() { double radius ; double circumference ; final double PI = 3.14159 ; System.out.println( "CIRCUMFERENCE CALCULATION" ) ; System.out.print( "Please enter radius: " ) ; radius = input.nextDouble() ; circumference = 2*PI*radius ; System.out.println() ; System.out.printf( "radius = %8.4f, circumference = %8.4f\n", radius, circumference ) ; } // // c2: method to calculate the area of circle using Math.PI // static void c2() { double radius ; System.out.println( "CIRCLE AREA CALCULATION" ) ; System.out.print( "Please enter radius: " ) ; radius = input.nextDouble() ; System.out.println() ; System.out.printf( "radius = %8.4f, area = %8.4f\n", radius, Math.PI*radius*radius ) ; } // // MAIN // public static void main( String args[] ) { if ( args.length > 0 ) { if ( args[0].indexOf( 'a' ) >= 0 ) c2f() ; if ( args[0].indexOf( 'b' ) >= 0 ) f2c() ; if ( args[0].indexOf( 'c' ) >= 0 ) c1() ; if ( args[0].indexOf( 'd' ) >= 0 ) c2() ; } else { System.out.println( "Usage: java formula [a][b][c][d]" ) ; } } }