next up previous
Next: 1.2.8 Defining Static Methods Up: 1.2.7 Java Programs Previous: 1.2.7.1 A Sample Java

1.2.7.2 Variations on the Sample Program

Any valid Java expression can be substituted for the string "Java rules!" in the class Motto above. The resulting program will print the value of the specified expression. For example, the program

class Motto {
  public static void main(String[] args) {
    System.out.println("98 F = " + (98.-32.)*(5./9.) + " C");
  }
}
prints the text
98 F = 36.66666666666667 C
(converting Fahrenheit to centigrade). The static method System.out.println takes a single argument and prints it. You can print the value of more than one expression on a line by constructing the desired String using the String concatenation operator +. This operator coerces any value that is not a String to a String suitable for printing.


Finger Exercise: Modify the Motto class from the previous finger exercise to convert your favorite Fahrenheit temperature f to centigrade using the formula

(f - 32.)*(5./9.)

Execute it as a program.



Corky Cartwright
2000-01-07