CHAPTER [2]
OBJECT-ORIENTED
PROGRAMMING
[COS2182]
BASICS IN JAVA
PROGRAMMING
Mr.Yadera Alemu .wogasa
Identifiers
You need to choose names for the things you
will refer to in your programs.
Identifiers are used for naming such
programming entities as variables, constants,
methods, classes, and packages.
The rules for naming identifiers:
Can be a sequence of characters that consist
of letters, digits, underscores (_) and a
Must start with a letter, an
underscore or dollar sign. It cannot
begin with a digit.
Cannot be a reserved word. (See
list of java keywords in here)
Cannot be ‘true’, ‘false’ or null
Can be of any length.
Data Types
The Java language provides for eight primitive
types of data:
Variables
Variables are locations in memory
in which values can be stored.
They have a name, a type, and a
value.
Java actually has three kinds of variables:
instance variables,
class variables, and
local variables
Instance variables: are used to define
attributes or the state for a particular
object.
Class variables: are similar to instance
variables, except their values apply to all
that class’s instances
Local variables: are declared and used
inside method definitions,
Declaring Variables
To use any variable in a Java program, you
must first declare it.
Variable declarations consist of a type and a
variable name.
Syntax: datatype variableName;
Declaring Variables
Example: a variable that stores an integer
representing the highest score on an exam
could be declared as follows:
Declaring Variables(continued)
Other examples:
double Old_Price;
double radius;
int qtty;
char grade;
int x;
Assignment Statements and
Expressions
After a variable is declared, you
can assign a value to it by using
an assignment statement (=).
The syntax as follows:
variableName = expression;
For Example: highScore = 98;
Where it is used?
Expression – represents a computation involving values,
variables, and operators that evaluates to a value.
For example:
int x = 5; //assign 5 to a variable x
double radius = 2.3; //assign 2.3 to a variable radius
char a = ‘A’; //assign ‘A’ to a variable a
x = 5 * (2/3) + 5; //assign the value of the
expression to x
area = radius * radius * 3.142; //compute area
POP QUIZ
Statements
A statement is a command that
causes something to happen.
All statements in Java are separated
by semicolons ;
Example:
System.out.println(“Hello, World”);
Introduction to Strings
Strings consist of a series of characters inside
double quotation marks.
Examples statements assign String variables:
String lemlem = "John Smith";
String dureti = "swordfish786";
Strings are not one of the primitive data types.
Strings are constant; their values cannot be
changed after they are created.
POP QUIZ
What data types would you use to store the
following types of information?:
1)Population of Ethiopia
2)Approximation of π
3)Open/closed status of a file
4)Your name
5)First letter of your name
6)$237.66
What are
Operators?
Operators are special symbols used for
mathematical functions
assignment statements
logical comparisons
Examples:
3 + 5 // uses + operator
14 + 5 – 4 * (5 – 3) // uses +, -, * operators
The Operator Groups
There are 5 different groups of
operators:
Arithmetic operators
Assignment operator
Increment/Decrement operators
Relational operators
Conditional operators
Arithmetic Operators
Java has 6 basic arithmetic operators
+ add
- subtract
* multiply
/ divide
% modulo (remainder)
Order of operations (or precedence) when evaluating
an expression is the same as PEMDAS:
1. Parentheses
2. Exponents
3. Multiplication and Division from left to right
4. Addition and Subtraction from left to right
Example: 10 + 15 / 5;
a) (10 + 15) / 5 = 5
b) 10 + (15 / 5) = 13
Reading Input from the Console
Enables the program to accept
input from the user.
Java uses:-
System. out to refer to the standard output device.
System.in to the standard input device.
To perform console output, you simply use the
println method to or a string to the console. display
a primitive value
Integer Division Example
int i = 63;
int j = 35;
System.out.println(i / j);
Output: 1
double x = 63;
double y = 35;
System.out.println(x / y);
Ouput: 1.8
Assignment Operator
The basic assignment
operator (=) assigns the
value of var to expr:
var = expr ;
Examples:
x = x + 5; is
Increment/Decrement Operators
count = count + 1;
can be written as:
++count; or count++;
++ is called the increment operator.
count = count - 1;
can be written as:
--count; or count--;
-- is called the decrement operator.
The increment/decrement operator has two forms:
The prefix form ++count, --count
first adds 1 to the variable and then
continues to any other operator in the
expression
int lemlem = 5;
int dureti = 10;
int masho;
masho= ++lemlem + duret;
Masho has value 16
The postfix form count++, count–
first evaluates the expression and then adds 1 to the
variable
int lemlem = 5;
int dureti = 10;
int masho;
masho= lemlem++ + duret;
Masho has value 15
lemlem has value 6
Relational (Comparison) Operators
Relational operators compare two values.
Examples of Relational Operations
int x = 3;
int y = 5;
boolean result;
1. result = (x > y); now result is assigned the value
false because 3 is not greater than 5
2. result = (15 == x*y); now result is assigned the
value true because the product of 3 and 5 equals
15
3. result = (x != x*y);
Conditional Operators
Conditional operators
Can be referred to as boolean operators,
because they are only used to combine
expressions that have a value of true or
false.
Truth Table for Conditional Operators
Examples of Conditional Operators
POP QUIZ
What are Control Structures?
Alter the flow of the program, the
sequence of statements that are executed
in a program.
They act as "direction signals" to control
the path a program takes.
Two types of control structures in Java:
Decision Statements
Loops
Decision Statements
An allows the code to execute a
statement or block of statements
conditionally.
Two types of decisions statements in Java:
if statements
switch statements
If Statement
if (expression) {
statement;
}
rest_of_program;
Expression must evaluate to a boolean value, either true or
false
• If expression is true, statement is executed and then
rest_of_program
• If expression is false, statement is not executed and the
program continues at rest_of_program
If Statement Flow Diagram
If-Else Flow Diagram
Chained If-Else Statements
Switch Statements
The switch statement enables you to test several cases
generated by a given expression.
• For example:
switch (expression) {
case value1:
statement1;
case value2:
statement2; ….
default:
default_statement;
This is how it is accomplished with a switch
Loops
loop allows you to execute a statement or
block of statements repeatedly.
• Three types of loops in Java:
while loops
for loops
do-while loops (not discussed in this
course)
The while Loop
while (expression){
statement
}
This while loop executes as long as the given
logical expression between parentheses is true.
When expression is false, execution continues
with the statement following the loop block.
The expression is tested at the beginning of the
loop, so if it is initially false, the loop will not be
executed at all
For example:
The for Loop
for (init_expr; loop_condition; increment_expr)
{ statement; }
for loop appear in parentheses and is made up of three parts:
1. The first part, the init_expression, sets the initial
conditions for the loop and is executed before the loop
starts.
2. Loop executes so long as the loop_condition is true and
exits otherwise.
3. The third part of the control information, the
increment_expr, is usually used to increment the loop
counter. This is executed at the end of each loop iteration.
What will this for loop do?
THANK YOU
HAVE A NICE DAY