0% found this document useful (0 votes)
26 views30 pages

Lecture-02 Introduction To Java Programming: by Dr. Bharati Mishra

Uploaded by

Johny Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views30 pages

Lecture-02 Introduction To Java Programming: by Dr. Bharati Mishra

Uploaded by

Johny Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Lecture-02

Introduction to Java Programming


By
Dr. Bharati Mishra
Programming Constructs
Program Anatomy

• Comments
• Reserved words
• Modifiers
• Statements
• Blocks
• Classes
• Methods
• The main method
Comments

• 3 types of comments in Java


•Line comment //
• Example
• // This is a line comment!
•Paragraph comment /* */
• Example
• /* This is a paragraph comment.
It includes multiple lines. */
• JavaDoc comments (automatic
documentation) /** */
JavaDoc
• JavaDoc comments (automatic
documentation) /** */
• Tags starting with @
Reserved Words

• Reserved words (or keywords)


• Specific meaning to the compiler
• Cannot be used for other purposes
• Keywords in our previous examples
• class
• public
• static
• void
Modifiers

• 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

• Represents an action or a sequence of


actions
• Every statement in Java ends with a
semicolon ;
• Examples

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

• You can use a variable in an expression.


• A variable can also be used in both sides of the = operator.
For example,
• x = x + 1;
Assignment Statements and Assignment
Expressions
• To assign a value to a variable, you must place the variable name to the left of the assignment
operator. Thus, the following statement is wrong:
• 1 = x; // Wrong

• Identify and Fix the error


1 public class Test {
2 public static void main(String[] args) {
3 int i = j = k = 2;
4 System.out.println(i + " " + j + " " + k);
5}
6}
Blocks

• A pair of braces in a program grouping


components of a program
Class

• The essential Java construct


• A template or blueprint for objects
• A program is defined by using one or
more classes.
Method

• A collection of statements that


performs a sequence of operations
• Might accept input
• Might provide output

Input
Input 2
1
Input
3

Output
Method

• What does it look like?


• Example
public returnType name(input1, input2, …)
{
// Statements;
}
Method

• What does it look like?


• Example

Return
Modifier Value name Input

public int addTwoNumbers(int a, int b)


{
int c = a + b;
return c;
}
Method

• What does it look like?


• Example

Return
Modifier Modifier Value name Input

public static void main(String[] args)


{
// Statements;
}
Named Constants
• A named constant is an identifier that represents a
permanent value.
• Value is constant, doesn’t change
• A constant must be declared and initialized in the
same statement
• Use “final” keyword to declare a value as constant

final datatype CONSTANTNAME = VALUE;

Example:

final double PI = 3.14159;


final int SIZE = 3;
Benefits of using Named constants
•There are three benefits of using constants:
(1) you don’t have to repeatedly type the same value if it is
used multiple times;
(2) if you have to change the constant value (e.g., from 3.14
to 3.14159 for PI), you need to change it only in a single
location in the source code; and
(3) a descriptive name for a constant makes the program
easy to read.
Conventions for naming variables, methods, and
classes.
• Use lowercase for variables and methods
• If a name consists of several words, concatenate them into one, making the first
word lowercase and capitalizing the first letter of each subsequent word.
• Capitalize the first letter of each word in a class name—for example, the class
names
ComputeArea and System.
• Capitalize every letter in a constant, and use underscores between words—for
example,
the constants PI and MAX_VALUE.
Numbers in More Detail ..
Numeric Data Types

• Different types of numeric data


Numeric Data Types
Sign bit

byte= 8 bits

short

int

long

float

double
Floating Point Numbers

• Integer calculations are precise.


• Calculations involving floating-point numbers are
approximated
• E.g. below displays 0.5000000000000001, not 0.5

System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);

• E.g. below displays 0.09999999999999998, not 0.1

System.out.println(1.0 – 0.9);
Literal

• A constant value appearing directly in the


program
• E.g.
int i = 34;
long x = 10000;
double d = 5.0;

• You can use append letters to indicate type

1000000L; //long
5.0f; //float
Literal

• A literal should fit in its variable


• The following example is wrong.

byte myVar = 10000;


• myVar can not hold this number.
• Byte range = [-128, 127] > 1000
Scientific Notation

• Floating point literals can be specified in


scientific notation, e.g.
• 1.23456e2
• 123.456 = 1.23456 * (10^2)
• Pitfall:
• Here “e” or “E” is NOT the natural logarithm’s base
(2.718). It is 10.
Numeric Operators

• Different types of numeric operators


Numeric Operators

• 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy