Java Reference
In-Depth Information
In general, you invoke Java methods by typing the class name followed by
a period followed by the method name. As you have learned, classes must be
imported before they can be used. Because the Math class is part of the java.lang
default package, it needs no previous import statement.
Program Output
After all calculations are performed, the program should display appropriate
output to the user. The output for the Body Mass Index Calculator program is
a message indicating the user's calculated BMI.
Using Variables in Output
The println() method commonly displays a string of characters, but it also
can display values from variable locations. Table 3-10 displays some forms of the
println() method.
Table 3-10
The println() Method
General form:
1. System.out.println(“literal”);
2. System.out.println(String variable);
3. System.out.println(numeric variable);
4. System.out.print(“literal”);
System.out.println(variable);
5. System.out.println(“literal” + variable);
6. System.out.println(Class.method(arguments));
7. System.out.println(“”);
Purpose:
To display output on the standard output device
Examples:
1.System.out.println("Anita's Antiques");
2.System.out.println(firstName);
3.System.out.println(dSalesTax);
4.System.out.print("The answer is ");
System.out.println(answer);
5.System.out.println("The answer is " + answer);
6.System.out.println(Math.round(answer));
7.System.out.println(""); //prints a blank line
Remember that System.out refers to the default output device, usually the
monitor. The println() method can display a literal string of characters, using
code such as that in example 1. For Java to display the values from variable
locations to users, the program code must use a variable identifier as the argu-
ment of the println() method, as shown in examples 2 and 3.
If you want to combine strings of characters and variables on the same
line, the code can take one of two forms. First, you can use the print() method
followed by println() method, as in example 4. The print() method does not
force a new line after displaying, so any output following a print() method will
display in the same line.
 
Search WWH ::




Custom Search