Define a utility class for displaying values of type double. Call the class DoubleOut. Include all the methods from the class DollarFormat in Listing 6.14, all the methods from the class OutputFormat of Self-Test Question 30, and a method called scienceWrite that displays a value of type double using e notation, such as 2.13e?12. (This e notation is also called scientific notation, which explains the method name.) When displayed in e notation, the number should appear with exactly one nonzero digit before the decimal point—unless the number is exactly zero. The method scienceWrite will not advance to the next line. Also add a method called scienceWriteln that is the same as scienceWrite except that it does advance to the next line. All but the last two method definitions can simply be copi

The full solution to Project 1, DoubleOut.java, requires a little more thought than some of the previous projects. For example, the possibility that the floating-point number may be less than 0 must be taken into account. And, after carefully thinking about how to convert to scientific notation and looking at the code in OutputFormat, it should be apparent that scienceWriteln()can be written by making a few changes to the method write(double number, int digitsAfterPoint). It is helpful to use step-wise refinement and develop one piece at a time. For example, develop a solution for numbers greater than one first, then make the modifications for values less than one, where, as it happens, a little pitfall is encountered: The pow method does not work with negative exponents, so special provision must be made. One of the difficulties is deciding how to obtain just the digits to print to the right of the decimal place. With a little thought, guidance, or trial and error, students should be able to figure out that the code used in writePositive will work if the value in the allWhole equation is divided by 10e (where e is the exponent of 10) if e is positive. If e is negative, the inverse operation is required, so multiply by 10-e (the code is pow(10, -e)to make the exponent positive).

An intermediate version of the code is given in DoubleOutWithStub.java and DoubleOutWithStubDriver.java.

The final version of the code is given in DoubleOut.java and DoubleOutDriver.java.

Computer Science & Information Technology

You might also like to view...

The Apple iPod is a media player

Indicate whether the statement is true or false

Computer Science & Information Technology

An instance method or constructor may be overloaded by providing the same name and ____ argument list.

A. a different B. the same C. a larger D. a smaller

Computer Science & Information Technology