Java Reference
In-Depth Information
In J2SE version 5.0, the printf() method may be used to format numeric
output. After importing the java.io.* package, programmers may embed one of
several special conversion characters that will insert data into a string, as shown
in Table 4-12.
Table 4-12
The printf() Method
Conversion Character
sample code
General form:
System.out.printf(String format, Object arguments)
Purpose:
To write a formatted string to an output stream using the specified format string
and arguments
Examples:
%b - Boolean value
System.out.printf("The Boolean value is %b\n", true);
%c - character
System.out.printf("The character %c\n", 'A');
%d - decimal
System.out.printf("An integer %d displays from a declared
integer
variable, num\n", num);
%f - floating-
System.out.printf("%f is a floating-point number
point number
representation of 3.1415929\n", 3.1415929);
%e - number in
System.out.printf("The scientific notation is %e\n",
standard scientific
3.1415929);
notation
%s - String
System.out.printf("The string %s displays\n", "Hi There");
Moving to the Web
Creating the applet version of the Commission program involves using the same
techniques of building the applet using modularity, in which a larger program's
source code is broken down into smaller sections of source code. First, an HTML
host document to call the applet must be created; then the applet stub is created,
compiled, and tested. Next, components must be added to the applet and selec-
tion structures must be included in the applet code. Finally, the applet should
be tested, using sample data to test its error and exception-handling capabilities.
Creating the Host Document
Recall from previous chapters that because an applet is initiated and
executed from within another language or run as part of a Web page, you must
identify a host, or reference program, to execute the applet. The host document
often is a Web page created as an HTML file with an applet tag.
The code to create an HTML file to access the CommissionApplet program
is shown in Figure 4-35.
1 <HTML>
2 <APPLET CODE = "CommissionApplet.class" WIDTH = "350" HEIGHT = "200" >
3 </APPLET>
4 </HTML>
FIGURE 4-35
 
Search WWH ::




Custom Search