Lecture-02 Introduction To Java Programming: by Dr. Bharati Mishra
Lecture-02 Introduction To Java Programming: by Dr. Bharati Mishra
• Comments
• Reserved words
• Modifiers
• Statements
• Blocks
• Classes
• Methods
• The main method
Comments
• A subset of keywords
• Specify the properties of the data,
methods, and classes and how they can
be used.
• Examples we have seen
• public
• static
• Other example modifiers
• private
• final
• abstract
• protected
Statement
System.out.println("Hello World!");
a = 8 * 3.14;
Assignment Statements and Assignment
Expressions
• An assignment statement designates a value for a variable.
• An assignment statement can be used as an expression in Java.
• The equal sign (=) is used as the assignment operator.
• variable = expression;
• An expression represents a computation involving values, variables,
and operators that, taking them together, evaluates to a value.
Assignment Statements and Assignment
Expressions
int y = 1; // Assign 1 to variable y
double radius = 1.0; // Assign 1.0 to variable radius
int x = 5 * (3 / 2); // Assign the value of the expression to x
x = y + 1; // Assign the addition of y and 1 to x
double area = radius * radius * 3.14159; // Compute area
Input
Input 2
1
Input
3
Output
Method
Return
Modifier Value name Input
Return
Modifier Modifier Value name Input
Example:
byte= 8 bits
short
int
long
float
double
Floating Point Numbers
System.out.println(1.0 – 0.9);
Literal
1000000L; //long
5.0f; //float
Literal
• Be careful!
• 5 / 2 yields an integer 2
• 5.0 / 2 yields a double value 2.5
• Remainder can be very useful
• Even numbers % 2 = 0
• Today is Saturday and you and your friends are going to
meet in 10 days. What day is in 10 days?
Assignment
• Problem-01
(Compute the volume of a cylinder) Write a program that reads in the radius
and length of a cylinder and computes the area and volume using the following
formulas:
area = radius * radius * pi
volume = area * length
• Problem -02
(Sum the digits in an integer) Write a program that reads an integer between 0 and
1000 and adds all the digits in the integer. For example, if an integer is 932, the
sum of all its digits is 14.
Hint: Use the % operator to extract digits, and use the / operator to remove the
extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.