Question: The program below contains several printf statements where the format specifier (which should appear between the | characters) has been omitted. The comments below each

The program below contains several printf statements where the format specifier (which should appear between the | characters) has been omitted. The comments below each block of output show what the output should look like with a correct format specifier inserted between the | characters. Modified the format specifiers so that the programs output matches the output in the comments speak.

public class OutputTest { 

public static void main(String[] args)

{

String word = "Information";

double real = 12*Math.PI;

//a series of output statements to show some of the functionality of the printf method.

//The comments below indicate the output from each section:

System.out.printf("String output tests: ");

System.out.printf("A string-------------------------------|| ", word);

System.out.printf("An upper-case string-------------------|| ", word);

System.out.printf("A string in 15 spaces------------------|| ", word);

System.out.printf("A string left justified in 15 spaces---|| ", word);

System.out.printf("A string within 5 characters-----------|| ", word);

System.out.printf("A string in a space of 5 to 7 chars----|| ", word);

System.out.printf(" ");

/* OUTPUT:

String output tests:

A string-------------------------------|Information|

An upper-case string-------------------|INFORMATION|

A string in 15 spaces------------------| Information|

A string left justified in 15 spaces---|Information |

A string within 5 characters-----------|Infor|

A string in a space of 5 to 7 chars----|Informa|

*/

System.out.printf("Double output tests: "); 
 System.out.printf("A double-------------------------------|| ", real); 
 System.out.printf("A double in scientific notation--------|| ", real); 
 System.out.printf("A double with precision of 2-----------|| ", real); 
 System.out.printf("A double with precision of 10----------|| ", real); 
 System.out.printf(" "); 
 /* OUTPUT: 
 Double output tests: 
 A double-------------------------------|37.699112| 
 A double in scientific notation--------|3.769911e+01| 
 A double with precision of 2-----------|37.70| 
 A double with precision of 10----------|37.6991118431| 
 A double as a hex number---------------|0x1.2d97c7f3321d2p5| 
 */ 
 } 
 
} 

Java printf( ) Method Quick Reference

System.out.printf( format-string *, arg1, arg2, + );

Format String:

Composed of literals and format specifiers. Arguments are required only if there are format specifiers in the format string. Format specifiers include: flags, width, precision, and conversion characters in the following sequence:

Flags:% [flags] [width] [.precision] conversion-character ( square brackets denote optional parameters )

-

:

left-justify ( default is to right-justify )

+

:

output a plus ( + ) or minus ( - ) sign for a numerical value

0

:

forces numerical values to be zero-padded ( default is blank padding )

,

:

comma grouping separator (for numbers > 1000)

:

space will display a minus sign if the number is negative or a space if it is positive

Width:

Specifies the field width for outputting the argument and represents the minimum number of characters to be written to the output. Includespace for expected commas and a decimal point in the determination of the width for numerical values.

Precision:

Used to restrict the output depending on the conversion. It specifies the number of digits of precision when outputting floating-point values or the length of a substring to extract from a String. Numbers are rounded to the specified precision.

Conversion-Characters:

d :

decimal integer

[byte, short, int, long]

f :

floating-pointnumber

[float, double]

c :

character

Capital C will uppercase the letter

s :

String

Capital S will uppercase all the letters in the string

h :

hashcode

A hashcode is like an address. This is useful for printing are ference

n :

newline

Platform specific newline character- use %n instead of for greater compatibility

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!