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 program’s 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:n”);

        System.out.printf(“A string——————————-||n”, word);

        System.out.printf(“An upper-case string——————-||n”, word);

        System.out.printf(“A string in 15 spaces——————||n”, word);

        System.out.printf(“A string left justified in 15 spaces—||n”, word);

        System.out.printf(“A string within 5 characters———–||n”, word);

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

        System.out.printf(“n”);

        /* 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:n”); System.out.printf(“A double——————————-||n”, real); System.out.printf(“A double in scientific notation——–||n”, real); System.out.printf(“A double with precision of 2———–||n”, real); System.out.printf(“A double with precision of 10———-||n”, real); System.out.printf(“n”); /* 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 n for greater compatibility

"Get 15% discount on your first 3 orders with us"
Use the following coupon
"FIRST15"

Order Now