You are on page 1of 32

Object Oriented Programming

Department of Computing and Information Systems


References
• B.Meyer Object-Oriented Software Construction(latest
edition)
• J.Rumblaugh,et al.Object-Oriented Modeling and
Design(latest edition)
• G.Booch,Object-Oriented Analysis & Design with
application (latest edition)
• Advanced Java 2 How to program ,by Paul J.deitel,et al.

Department of Computing and Information


Systems
Why Programming Languages?
• A computer cannot understand the words
spoken by us, and this necessitate the need of
artificial languages, which can instruct
machines to carry out functions we desire.
• A programming language can be considered as
a tool which can be used in problem solving.
Object-oriented programming (OOP)
• is a programming paradigm that uses
"objects" – data structures consisting of data
fields and methods together with their
interactions – to design applications and
computer programs. Programming techniques
may include features such as information
hiding, data abstraction, encapsulation,
modularity, polymorphism, and inheritance.

Department of Computing and Information


Systems
OOP languages
• Languages called "pure" OO languages
• Examples: Smalltalk, Eiffel, Ruby, JADE
• Languages designed mainly for OO programming, but
with some procedural elements.
• Examples: C++, Java, Python.
• Languages that are historically procedural languages,
but have been extended with some OO features.
• Examples: Fortran 2003, Perl, COBOL 2002.
• Languages with abstract data type support, but not all
features of object-orientation, sometimes called
object-based languages
• Examples: Modula-2 (with excellent encapsulation and information
hiding), Pliant, CLU.
Department of Computing and Information
Systems
Introduction to Java technology
• The Java programming language is a high-level language that
can be characterized by all of the following buzzwords:
– Object Oriented
– Platform independent
– Simple
– Secure
– Architectural- neutral :
– Portable
– Robust
– Multi-threaded :
– Interpreted
– High Performance
– Distributed
– Dynamic
Department of Computing and Information
Systems
• Programming:
• all source code is first written in plain text files ending
with the .java extension Ex:-Myprogram.java
• Your program will

Because the Java VM is available on many different operating systems, the same .class files
are capable of running on Microsoft Windows, the Solaris TM Operating System (Solaris OS),
Linux, or Mac OS. Department of Computing and Information
Systems
class Myprogram{
public static void main(String[] args){
System.out.println(" Object oriented programming with
Java");
}
} String[] args or String args[].

Save the file as Myprogram.java.


Compile the file to Java bytecode: javac Myprogram.java
Run the program: java Hello

Department of Computing and Information


Systems
The Java platform has two
components:
– The Java Virtual Machine
– The Java Application Programming Interface (API)
• You've already been introduced to the Java Virtual Machine; it's the
base for the Java platform and is ported onto various hardware-
based platforms.
• The API is a large collection of ready-made software components that
provide many useful capabilities. It is grouped into libraries of related
classes and interfaces; these libraries are known as package

Department of Computing and Information


Systems
Primitive data types
Key word Purpose Possible entries
byte 8-bit integer from -128 to 127 (inclusive).

short 16-bit integer From -32,768 to 32,767


(inclusive).

Int 32-bit integer From -2,147,483,648 to


2,147,483,647 (inclusive).

long 64-bit integer From


-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
(inclusive)
float 32-bit floating
double 64-bit floating
boolean simple flags that track True & false
true/false conditions
char single of
Department 16-bit Unicode
Computing and Information All characters ,numbers
character Systems symbols
• Character Literals
– \b backspace
– \n new line
– \t horizontal tab
– \’ single quote
• Check for \f ,\r ,\\ ,\
Constants
Constants are variables whose value does not
change during the life of the object.
Java does not have a constant keyword.
can define a constant like this:
static final String name = "John”
static final float pi = 3.14159;
Department of Computing and Information
Systems
Naming Variables
• Variable names are case-sensitive.
• A variable's name can be any legal identifier — an unlimited-
length sequence of Unicode letters and digits, beginning with a
letter, the dollar sign "$", or the underscore character "_".
– For better programming practice
• always begin your variable names with a letter, not "$" or "_".

• White space is not permitted.


• If the name you choose consists of only one word, spell that
word in all lowercase letters.
• If it consists of more than one word, capitalize the first letter of
each subsequent word.
Ex: gearRatio , currentGear

Department of Computing and Information


Systems
Most common imports
• There are 166 packages containing 3279 classes and interfaces
in Java 5. However, only a few packages are used in most
programming. GUI programs typically use at least the first
three imports.

• import java.awt.*; Common GUI elements.


• import java.awt.event.*; The most common GUI event
listeners.
• import java.util.*; Data structures (Collections), time,
Scanner, etc.
• import java.io.*; Input-output classes.
• import java.text.*; Some formatting classes.
• import java.util.regex.*; Regular expression classes
Department of Computing and Information
Systems
The Arithmetic Operators
• The Java programming language provides operators
that perform addition, subtraction, multiplication,
and division.
• + additive operator (also used for String concatenation)
• - subtraction operator
• * multiplication operator
• / division operator
• % remainder operator

• Let a=10 ,b=20and result are integer variables ,and print


addition , subtraction , multiplication , division and remainder
of a and b . write a program to implement this
Department of Computing and Information
Systems
Unary operators
• The unary operators require only one operand; they
perform various operations such as
incrementing/decrementing a value by one,
negating an expression, or inverting the value of a
boolean.
• + Unary plus operator; indicates positive value (numbers are
positive without this, however)
• - Unary minus operator; negates an expression
• ++ Increment operator; increments a value by 1
• -- Decrement operator; decrements a value by 1
• ! Logical complement operator; inverts the value of a boolean
Department of Computing and Information
Systems
The Equality and Relational Operators
• The equality and relational operators determine if
one operand is greater than, less than, equal to, or
not equal to another operand.
• == equal to
• != not equal to
• > greater than
• >= greater than or equal to
• < less than
• <= less than or equal to

Department of Computing and Information


Systems
The Conditional Operators

• The && and || operators perform Conditional-


AND and Conditional-OR operations on two
boolean expressions.
• && Conditional-AND
• || Conditional-OR

Department of Computing and Information


Systems
In addition to basic arithmetic
operations
The Math class in the java.lang package provides methods and
constants for doing more advanced mathematical computation.
• The methods in the Math class are all static, so you call them
directly from the class, like this:
Math.cos(angle);
Note : Using the static import language feature, you don't have to
write Math in front of every math function:
import static java.lang.Math.*;
This allows you to invoke the Math class methods by their
simple names.
For example: cos(angle);
Find out other possible Math methods and their usage.
Department of Computing and Information
Systems
If –Then statement
• The if-then statement is the most basic of all
the control flow statements.
It tells your program to execute a certain
section of code only if a particular test
evaluates to true.

Department of Computing and Information


Systems
Syntax of If –then statement
1) if( condition)
statement;
else
statement;
2) if( condition)
statement;
else if (condition)
statement;
3) if( condition 1)
statement;
if( condition 2)
statement ;
else
statement;
else
Department of Computing and Information
statement; Systems
Compound statements
• For compound statement, it is necessary to
enclosed with curly braces .
if( condition) {
statement 1;
statement 2;

………..
statement n;}
• For compound statements of nested if also
used Department of Computing and Information
Systems
What will be the output
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A'; }
else if (testscore >= 80) {
grade = 'B'; }
else if (testscore >= 70) {
grade = 'C'; }
else if (testscore >= 60) {
grade = 'D'; }
else { grade = 'F'; }
System.out.println("Grade = " + grade); } }
Department of Computing and Information
Systems
The switch Statement

• Unlike if-then and if-then-else, the switch


statement allows for any number of possible
execution paths.
• A switch works with the
• Byte
• short
• char
• int
• It also works with enumerated types (discussed in
Classes and Inheritance)

Department of Computing and Information


Systems
Syntax of switch statement
switch(variable){
case value 1 :
statement;
break;
case value 2:
statement;
break;
…………….
case value n:
statement;
break;
default :
statement;
break;
}
Department of Computing and Information
Systems
What will be the output
{
int month = 8;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
Department of Computing and Information
default: System.out.println("InvalidSystems
month.");break; } }
The while Statement
• The while statement continually executes a block of
statements while a particular condition is true.
Its syntax can be expressed as:
while (expression) {
statement(s) }

• The while statement evaluates expression, which must


return a boolean value.
• If the expression evaluates to true, the while statement
executes the statement(s) in the while block.
• The while statement continues testing the expression
and executing its block until the expression
Department of Computing and Information
evaluates to
false. Systems
What will be the output
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++; } } }

Department of Computing and Information


Systems
Break statements:

The break statement is a branching statement that
contains two forms: labeled and unlabeled. The break
statement is used for breaking the execution of a loop
(while, do-while and for) . It also terminates the switch
statements.
Syntax:
        break;    // breaks the innermost loop or switch
statement.
        break label;   // breaks the outermost loop in a
series of nested loops.
Department of Computing and Information
Systems
Continue statements

This is a branching statement that are used in the


looping statements (while, do-while and for) to skip
the  current iteration of the loop and  resume the
next iteration .

Syntax:
        continue;

Department of Computing and Information


Systems
Do-while statement
• The Java programming language also provides a do-
while statement, which can be expressed as follows:
do {
statement(s)
} while (expression);
• The difference between do-while and while is that do-
while evaluates its expression at the bottom of the
loop instead of the top.
• Therefore, the statements within the do block are
always executed at least once.

Department of Computing and Information


Systems
The for Statement
• The for statement provides a compact way to iterate
over a range of values.
The general form of the for statement can be
expressed as follows:
for (initialization; termination; increment) {
statement(s) }
When the termination expression evaluates to
false, the loop terminates.
The initialization expression initializes the loop;
• When the termination expression evaluates to false,
the loop terminates.
• The increment expression is invoked after each
Department of Computing and Information
iteration through the loop; Systems
What will be the output
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i); } }
}

Department of Computing and Information


Systems

You might also like