Java Reference
In-Depth Information
stack . push (( stack . pop ()). divide ( tmp ,
BigDecimal . ROUND_HALF_UP ));
break
break ;
default
default :
throw
throw new
new IllegalStateException ( "Unknown OPERATOR popped" );
}
} else
else {
throw
throw new
new IllegalArgumentException ( "Syntax error in input" );
}
}
return
return stack . pop ();
}
}
Running this produces the expected (very large) value:
> javac -d . numbers/BigNumCalc.java
> java numbers.BigNumCalc
6838458446744073709551614.466860
>
The current version has its inputs hardcoded, as does the JUnit test program, but in real life
you can use regular expressions to extract words or operators from an input stream (as in
Printing All Occurrences of a Pattern ), or you can use the StreamTokenizer approach of the
simple calculator (see Scanning Input with StreamTokenizer ). The stack of numbers is main-
tained using a java.util.Stack (see Stack ) .
BigInteger is mainly useful in cryptographic and security applications. Its method isProb-
ablyPrime() can create prime pairs for public key cryptography. BigDecimal might also be
useful in computing the size of the universe.
Program: TempConverter
The program shown in Example 5-9 prints a table of Fahrenheit temperatures (still used in
daily weather reporting in the United States) and the corresponding Celsius temperatures
(used in science everywhere, and in daily life in most of the world).
Example 5-9. TempConverter.java
public
public class
class TempConverter
TempConverter {
Search WWH ::




Custom Search