// Muunna.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2006-04-03 File created. // 2006-04-06 Last modification. // 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 // muunnettava_maara = Integer.parseInt( nappaimisto.nextLine() ) ; // means the same as // muunnettava_maara = nappaimisto.nextInt() ; // 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. import java.util.* ; class Muunnos { String ensimmainen_yksikko ; String toinen_yksikko ; double muunnoskerroin ; public Muunnos( String annettu_ensimmainen_yksikko, String annettu_toinen_yksikko, double annettu_muunnoskerroin ) { ensimmainen_yksikko = annettu_ensimmainen_yksikko ; toinen_yksikko = annettu_toinen_yksikko ; muunnoskerroin = annettu_muunnoskerroin ; } public void muunna( String annettu_yksikko, double muunnettava_maara ) { if ( ensimmainen_yksikko.contains( annettu_yksikko ) ) { System.out.print( "\n " + muunnettava_maara + " " + ensimmainen_yksikko + "a vastaa " + muunnettava_maara * muunnoskerroin + " " + toinen_yksikko + "a" ) ; } if ( toinen_yksikko.contains( annettu_yksikko ) ) { System.out.print( "\n " + muunnettava_maara + " " + toinen_yksikko + "a vastaa " + muunnettava_maara / muunnoskerroin + " " + ensimmainen_yksikko + "a" ) ; } } } class Muunna { public static void main( String[] komentoriviparametrit ) { Scanner nappaimisto = new Scanner( System.in ) ; Muunnos[] muunnostaulukko = new Muunnos[ 13 ] ; muunnostaulukko[ 0 ] = new Muunnos("metri", "jaardi", 1.093613 ); muunnostaulukko[ 1 ] = new Muunnos("metri", "jalka", 3.280840 ); muunnostaulukko[ 2 ] = new Muunnos("maili", "kilometri",1.609344); muunnostaulukko[ 3 ] = new Muunnos("tuuma", "senttimetri", 2.54 ); muunnostaulukko[ 4 ] = new Muunnos("eekkeri","hehtaari", 0.4046873); muunnostaulukko[ 5 ] = new Muunnos("pauna", "kilogramma",0.4535924); muunnostaulukko[ 6 ] = new Muunnos("unssi", "gramma", 28.35 ); muunnostaulukko[ 7 ] = new Muunnos("U.S. gallona", "litra", 3.785); muunnostaulukko[ 8 ] = new Muunnos("U.K. gallona", "litra", 4.546); muunnostaulukko[ 9 ] = new Muunnos("U.S. pintti", "litra", 0.473); muunnostaulukko[ 10 ]= new Muunnos("U.K. pintti", "litra", 0.568); muunnostaulukko[ 11 ]= new Muunnos("joule", "kalori",4.187); muunnostaulukko[ 12 ]= new Muunnos("valovuosi", "kilometri", 9.461e12 ) ; String yksikko_kayttajalta ; int muunnettava_maara ; if ( komentoriviparametrit.length == 2 ) { yksikko_kayttajalta = komentoriviparametrit[ 0 ] ; muunnettava_maara = Integer.parseInt( komentoriviparametrit[ 1 ] ) ; } else { System.out.print( "\n Anna yksikko josta muunnos tehdaan: " ) ; yksikko_kayttajalta = nappaimisto.nextLine() ; System.out.print( " Anna muunnettava maara: " ) ; muunnettava_maara = Integer.parseInt( nappaimisto.nextLine() ) ; } for ( int muunnoksen_indeksi = 0 ; muunnoksen_indeksi < muunnostaulukko.length ; muunnoksen_indeksi ++ ) { muunnostaulukko[ muunnoksen_indeksi ].muunna( yksikko_kayttajalta, muunnettava_maara ) ; } } }