Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Answer question in end EXAMPLE 3-5 //Program to illustrate how to format the outputting of //decimal numbers. //Line 1 //Line 2 //Line 3 public class

Answer question in end image text in transcribed
image text in transcribed
image text in transcribed
EXAMPLE 3-5 //Program to illustrate how to format the outputting of //decimal numbers. //Line 1 //Line 2 //Line 3 public class FormattingDecimalNumNew static final double PI 3.14159265 //Line //Line 5 //Line 6 //Line 7 public static void main (Stringt] args) double radius 12.67 double height 12.00 InputlOutput 133 System.out.println ("Two decimal places: //Line 8 System.out.printf("Line 9: "height-1.2f, volume-%.2f, + "PI-1.2finh", radius, height, PI radiusradius height, PI) radius %.2f, " - //Line 9 System.out.println ("Three decimal places:) System.out.printf("Line 11: radus-%.3f, " 8.3f, //Line 10 3 "height volume %.3f%n%n", 8.3f,%n radius, + - - PI height, PI radiusradius - height, PI) I/Line 11 //Line 12 System.out.println ("Four decimal places: System.out.printf("Line 13: radius-%.4f, " + "height .4f, volume-.42,n PI %.4ftnkn", radius, - height, PI radiusradius height, PI): I/Line 13 System.out.printf("Line 14: radius-%.3f, " "height radius, height, PI) + 8.2f, P1-8.5f%n", - //Line 14 //Line 15 //Line 16 Sample Run: Two decimal places: Line 9: radius-12.67, height 12.00, volume 6051.80, PI 3.14 Three decimal places: Line 11: radius 12.670, height 12.000, volume- 6051.797 PI 3.142 Four decimal places: Line 13: radius 12.6700, height 12.0000, volume 6051.7969 PI 3.1416 Line 14: radius 12.670, height 12.00, PI 3.14159 In this program, the statement in Line 9 outputs the values of radius, height, the volume, and PI to two decimal places. The statement in Line 11 outputs the values of radius, height, the volume, and PI to three decimal places. The statement in Line 13 outputs the values of radius, height, the volume, and PI to four decimal places. The statement in Line 14 outputs the value of radius to three decimal places, the value of height to two decimal places, and the value of PI to five decimal places. Notice how the values of radius are printed at Lines 11, 13, and 14. The value of radius printed in Line 11 contains a trailing OThis is because the stored value of radius has only two decimal places, a 0 is printed at the third decimal place. In a similar manner, the value of height is printed in Lines 11, 13, and 14. 134 Chapter 3: Introduction to Objects and Input/Output Also, notice how the statements in Lines 9, 11, and 13, calculate and output the volume to two, three, and four decimal places. Note that the value of PI printed in Lines 9, 11, 13, and 14 is rounded. EXAMPLE 3-5 //Program to illustrate how to format the outputting of //decimal numbers. //Line 1 //Line 2 //Line 3 public class FormattingDecimalNumNew static final double PI 3.14159265 //Line //Line 5 //Line 6 //Line 7 public static void main (Stringt] args) double radius 12.67 double height 12.00 InputlOutput 133 System.out.println ("Two decimal places: //Line 8 System.out.printf("Line 9: "height-1.2f, volume-%.2f, + "PI-1.2finh", radius, height, PI radiusradius height, PI) radius %.2f, " - //Line 9 System.out.println ("Three decimal places:) System.out.printf("Line 11: radus-%.3f, " 8.3f, //Line 10 3 "height volume %.3f%n%n", 8.3f,%n radius, + - - PI height, PI radiusradius - height, PI) I/Line 11 //Line 12 System.out.println ("Four decimal places: System.out.printf("Line 13: radius-%.4f, " + "height .4f, volume-.42,n PI %.4ftnkn", radius, - height, PI radiusradius height, PI): I/Line 13 System.out.printf("Line 14: radius-%.3f, " "height radius, height, PI) + 8.2f, P1-8.5f%n", - //Line 14 //Line 15 //Line 16 Sample Run: Two decimal places: Line 9: radius-12.67, height 12.00, volume 6051.80, PI 3.14 Three decimal places: Line 11: radius 12.670, height 12.000, volume- 6051.797 PI 3.142 Four decimal places: Line 13: radius 12.6700, height 12.0000, volume 6051.7969 PI 3.1416 Line 14: radius 12.670, height 12.00, PI 3.14159 In this program, the statement in Line 9 outputs the values of radius, height, the volume, and PI to two decimal places. The statement in Line 11 outputs the values of radius, height, the volume, and PI to three decimal places. The statement in Line 13 outputs the values of radius, height, the volume, and PI to four decimal places. The statement in Line 14 outputs the value of radius to three decimal places, the value of height to two decimal places, and the value of PI to five decimal places. Notice how the values of radius are printed at Lines 11, 13, and 14. The value of radius printed in Line 11 contains a trailing OThis is because the stored value of radius has only two decimal places, a 0 is printed at the third decimal place. In a similar manner, the value of height is printed in Lines 11, 13, and 14. 134 Chapter 3: Introduction to Objects and Input/Output Also, notice how the statements in Lines 9, 11, and 13, calculate and output the volume to two, three, and four decimal places. Note that the value of PI printed in Lines 9, 11, 13, and 14 is rounded

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

2nd Edition

1597499471, 978-1597499477

More Books

Students also viewed these Databases questions

Question

2. Outline the functions of nonverbal communication

Answered: 1 week ago