// Convert.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2004-10-03 File created. // 2005-05-26 String method contains() used instead of indexOf() // 2008-02-03 Last modification. // The best way to execute this program is to run it in a // command prompt window where command line parameters can // be given. It may be difficult, and even impossible, to // supply the command line parameters if this program is // executed with a modern development tool such as JCreator // or Eclipse. // This program will, though, ask for data from the user // if no command line parameters are given. // More notes at the end of this file: import java.util.* ; class Conversion { String first_unit ; String second_unit ; double conversion_constant ; public Conversion( String given_first_unit, String given_second_unit, double given_conversion_constant ) { first_unit = given_first_unit ; second_unit = given_second_unit ; conversion_constant = given_conversion_constant ; } public void convert( String given_unit, double amount_to_convert ) { if ( first_unit.contains( given_unit ) ) { System.out.print( "\n " + amount_to_convert + " " + first_unit + " is " + amount_to_convert * conversion_constant + " " + second_unit ) ; } if ( second_unit.contains( given_unit ) ) { System.out.print( "\n " + amount_to_convert + " " + second_unit + " is " + amount_to_convert / conversion_constant + " " + first_unit ) ; } } } class Convert { public static void main( String[] command_line_parameters ) { Scanner keyboard = new Scanner( System.in ) ; Conversion[] conversion_table = new Conversion[ 13 ] ; conversion_table[ 0 ] = new Conversion("meters", "yards", 1.093613 ); conversion_table[ 1 ] = new Conversion("meters", "feet", 3.280840 ); conversion_table[ 2 ] = new Conversion("miles", "kilometers",1.609344); conversion_table[ 3 ] = new Conversion("inches", "centimeters", 2.54 ); conversion_table[ 4 ] = new Conversion("acres", "hectares", 0.4046873); conversion_table[ 5 ] = new Conversion("pounds", "kilograms",0.4535924); conversion_table[ 6 ] = new Conversion("ounces", "grams", 28.35 ); conversion_table[ 7 ] = new Conversion("gallons (U.S.)","liters", 3.785); conversion_table[ 8 ] = new Conversion("gallons (Br.)", "liters", 4.546); conversion_table[ 9 ] = new Conversion("pints (U.S.)", "liters", 0.473); conversion_table[ 10 ]= new Conversion("pints (Br.)", "liters", 0.568); conversion_table[ 11 ]= new Conversion("joules", "calories",4.187); conversion_table[ 12 ]= new Conversion("lightyears", "kilometers", 9.461e12 ) ; String unit_from_user ; int amount_to_convert ; if ( command_line_parameters.length == 2 ) { unit_from_user = command_line_parameters[ 0 ] ; amount_to_convert = Integer.parseInt( command_line_parameters[ 1 ] ) ; } else { System.out.print( "\n Give the unit to convert from: " ) ; unit_from_user = keyboard.nextLine() ; System.out.print( " Give the amount to convert: " ) ; amount_to_convert = Integer.parseInt( keyboard.nextLine() ) ; } for ( int conversion_index = 0 ; conversion_index < conversion_table.length ; conversion_index ++ ) { conversion_table[ conversion_index ].convert( unit_from_user, amount_to_convert ) ; } } } /* // NOTE: // The Scanner method nextInt() does not work properly // after the use of method nextLine(). For this reason it is // best to input everything with the nextLine() method in programs // where nextLine() must be used. // The statement // amount_to_convert = Integer.parseInt( keyboard.nextLine() ) ; // means the same as // amount_to_convert = keyboard.nextInt() ; // NOTE: // Another possibility to build the conversion table in this program // is to create an initialized array of objects in the following way: Conversion[] conversion_table = { new Conversion("meters", "yards", 1.093613 ), new Conversion("meters", "feet", 3.280840 ), new Conversion("miles", "kilometers",1.609344), new Conversion("inches", "centimeters", 2.54 ), new Conversion("acres", "hectares", 0.4046873), new Conversion("pounds", "kilograms",0.4535924), new Conversion("ounces", "grams", 28.35 ), new Conversion("gallons (U.S.)","liters", 3.785), new Conversion("gallons (Br.)", "liters", 4.546), new Conversion("pints (U.S.)", "liters", 0.473), new Conversion("pints (Br.)", "liters", 0.568), new Conversion("joules", "calories",4.187), new Conversion("lightyears", "kilometers", 9.461e12 ) } ; */