Performing Floating-Point Arithmetic
Next, you will modify the ArithmeticDemo application to work with floating-point values instead of integers.
1. Within the ArithmeticDemo application, change the class name to ArithmeticDemo2, and immediately save the file as ArithmeticDemo2.java. Change all the variables' data types to double. Change the two prompts to request double values, and change the two calls to the nextInt() method to nextDouble(). Save, compile, and execute the program again. Figure 2-39 shows a typical execution. Notice that the average calculation now includes decimal places.
Figure 2-39 Typical execution of the ArithmeticDemo2 application see image.
2. Rerun the program, experimenting with various input values. Some of your output might appear with imprecisions similar to those shown in Figure 2-40. If you are not satisfied with the slight imprecisions created when using floating-point arithmetic, you can round or change the display of the values, as discussed in Appendix C.
Figure 2-40 Another typical execution of the ArithmeticDemo2 application see image.