Java Reference
In-Depth Information
public
public static
void main ( String [] args ) {
TempConverter t = new
static void
new TempConverter ();
t . start ();
t . data ();
t . end ();
}
protected
protected void
void start () {
}
protected
protected void
void data () {
for
for ( int
int i =- 40 ; i <= 120 ; i += 10 ) {
float
float c = ( i - 32 )*( 5 f / 9 );
print ( i , c );
}
}
protected
protected void
float c ) {
System . out . println ( f + " " + c );
void print ( float
float f , float
}
protected
protected void
void end () {
}
}
This works, but these numbers print with about 15 digits of (useless) decimal fractions! The
second version of this program subclasses the first and uses printf (see Printing with For-
matter and printf ) to control the formatting of the converted temperatures (see
Example 5-10 ) . It will now look right, assuming you're printing in a monospaced font.
Example 5-10. TempConverter2.java
public
public class
class TempConverter2
TempConverter2 extends
extends TempConverter {
public
public static
void main ( String [] args ) {
TempConverter t = new
static void
new TempConverter2 ();
t . start ();
t . data ();
t . end ();
}
protected
protected void
float c ) {
System . out . printf ( "%6.2f %6.2f%n" , f , c );
void print ( float
float f , float
}
protected
protected void
void start () {
Search WWH ::




Custom Search