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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.