Professional Documents
Culture Documents
Declaring a (Class line 4)
begins a class declaration for class “Welcome1”
Every Java program consists of at least one class that you (the
programmer) define.
The class keyword ((sometimes called reserved words) are reserved
for use by Java and are always spelled with all lowercase letters.) is
introduces a class declaration and is immediately followed by the
class name (Welcome1).
Prepared by: Zelalem Birhanu
3|Page
2.5 Another Application: Adding Integers
reads (or taking inputs) two integers (whole numbers, such as –22,
7, 0 and 1024) typed by a user at the keyboard, computes their sum
and displays it.
A variable is a location in the computer’s memory where a value
can be stored for use later in a program.
A Scanner enables a program to read data (e.g.,numbers and
strings) for use in a program.
Prepared by: Zelalem Birhanu
4|Page
arithmetic operators
are programs perform arithmetic calculations.
Read them carefully.
Java provides the remainder operator, % ,which yields the remainder after
division. The expression x % y yields the remainder after x is divided by y .
Thus, 7 % 4 yields 3 , and 17 % 5 yields 2.
If an expression contains nested parentheses, such as
Line 9-10 is codded in this way.
Method printf ’s first argument is a format string that
may consist of fixed text and format specifiers.
Fixed text is output by printf just as it would be by print
or println .
Each format specifier is a placeholder for a value and
specifies the type of data to output.
Format specifiers begin with a percent sign ( % )
followed by a character that represents the data type.
For example, the format specifier %s is a placeholder for a string. The
format string in line 9 specifies that printf should output two strings,
each followed by a newline character. At the first format specifier’s
position, printf substitutes the value of the first argument after the
format string. At each subsequent format specifier’s position, printf
substitutes the value of the next argument. So this example
substitutes "Welcome to" for the first %s and "Java Programming!" for
the second %s . The output shows that two lines of text are displayed.
Read this table
For example