Title: Displaying Output
1Displaying Output
- An output stream sends data to an output device.
- To process data for the output stream, Java
provides the System (this class is in the
java.lang package) class with methods for
displaying output. - To display output on the screen, use System.out
- out has 2 methods
- print() displays data and leaves the cursor at
the end of the output - println() displays data and moves cursor to
next line - both methods require arguments (data passed to a
method for processing)
2Example System.out.print(Java
Rocks) System.out.println(the
House) System.out.println(2005) Output
Java Rocksthe House 2005
String Arguments set of characters enclosed in
quotes.
3Escape Sequence (a backslash \ followed by a
symbol to represent a character)
\n newline \t tab (8 spaces) \\ backslash \ doubl
e quotes
Example System.out.print(Java
Rocks\t) System.out.println(the
House) System.out.println(2005) Output J
ava Rocks the House 2005
4Formatting Output
Format() method can be used in place of the
print() and println() methods to control output.
This is a new method for Java! Format string
specifier takes the form alignmentwidths
indicates the start of a specifier alignment s
kip for right alignment minus sign for left
alignment width the number of characters to
use for output s indicates the argument is a
string (we will look at numeric formatting
later) If width is greater than the number of
characters in the argument then spaces pad the
output. If a string is longer than width,
strings to the right are moved over. Example Sys
tem.out.format (-15s 30, Class,
Average\n) System.out.format (-15s 30,
English, 95\n) System.out.format (-15s
30, History, 86\n)
5Text can also be included in the format
string Example System.out.format (My averages
are -8s 8, 95, 86\n)
6Code Conventions (aka rules to code/live/pass
this course by)
- Begin all programs with introductory comments
which include your name, class name, date
completed and purpose of program - Package names begin with a lowercase letter and
then uppercase letter for each word within the
name. No spaces! example - firstApplication - Class names should be nouns and begin with an
uppercase letter and then uppercase letter for
each for each word within the name. No spaces!
example - GreetingMessage - A comment block should be included before each
class and method (except the main() method). - Comment should not reiterate what is obvious in
code. - Indent statements within a method.
- An open curly brace should be placed on the
same line as the class or method declaration, and
the closing curly brace should be on a separate
line and aligned with the class or method
declaration.
7- Lab AboutMe
- Parts 1 and 2, pages 64 and 65
- Follow output format guidelines carefully
- Turn in code listing and screen shot
- Good Luck!!