Thanks to visit codestin.com
Credit goes to www.slideshare.net

WEEK 1
Instructor:
Nazish Basir
OBJECT
ORIENTED
PROGRAMMIN
G
IN
JAVA
TOPICS
• I N T R O D U C T I O N TO O B J E C T
O R I E N T E D P R O G R A M M I N G
• I N T R O D U C T I O N TO J AVA
• I N S TA L L AT I O N O F J AVA
• G E N E R A L S T RU C T U R E O F
P R O G R A M
• RU N N I N G YO U R F I R S T P R O G R A M
• VA R I A B L E S A N D DATA T Y P E S
• L I T E R A L S
OBJECT ORIENTED PROGRAMMING
• Programming paradigm based on the concept of objects,
which may contain data, in the form of fields, often known
as attributes; and code, in the form of procedures, often
known as methods.
• Object-Oriented Programming (OOP) is a programming
language model, which uses Objects and data as core
components.
• The main idea behind OOP is to represent the data and
logic with objects instead of actions or functions.
CLASS
• The class defines the shape and nature of an object.
• A class is the template or blueprint from which objects are
actually made.
• A class is also defined as a new data type, a user defined
type which contain two things:
– Data Member
– Methods
• A class defines the properties and behaviors for objects.
• The description of a number of similar object is also called
as a class.
• Classes is logical in nature.
OBJECT
• An object is an instance of a class.
• An object represents an entity in the real world that can
be distinctly identified. For example, a student, a desk, a
circle, a button, and even a loan can all be viewed as
objects.
• Object to object communication is done via methods.
OBJECT
• Software objects also have a state and a behavior:
– The state of an object (also known as its properties or attributes):
– is represented by data fields with their current values.
• A circle object, for example, has a data field radius , which is the property that
characterizes a circle.
• A dog has states -- color, name, breed
– The behavior of an object (also known as its actions):
– is defined by methods.
• To invoke a method on an object is to ask the object to perform an action.
• For example, you may define methods named getArea() and getPerimeter()
for circle objects.
• A dog has behaviors -- wagging the tail, barking, eating.
JAVA
• Object-oriented programming language
• Resembles C++ in many respects.
• Architecture-neutral means Java program should be able to
run on a any Operating System without recompilation.
• Java programs are compiled into machine-independent byte
code which is then run within a Java interpreter, which is
responsible for executing the byte code instructions.
• The Java interpreter is typically referred to as the JavaVirtual
Machine (JVM), and it must be present on each computer that
runs the program.
Source Code
(.java)
Byte Code
(.class)
JAVAC JAVA
Compiler Interpreter
Execution
COMPILING JAVA
ASSIGNMENT
•Write a 2 page report about Java
History.
•Include how java changed over time by
including versions and releases.
INSTALLING JAVA
AND WRITING AND
COMPILING YOUR
FIRST PROGRAM
VARIABLES
•Named locations in memory where you can
store values.
•Can store one data value at a time
•Can change the values
•Variable Names (Identifiers) can be short
names (like x and y) or more descriptive
names (age, sum, totalVolume)
DECLARING VARIABLES
•Declaring means giving a name and a data
type to a variable before it can be used.
•Syntax:
datatype identifier;
datatype identifier1,
identifier2, ..;
INTEGER DATA TYPES
Type
Size
(Bytes)
Min Value Max Value
byte 1 -128 127
short 2 -32,768 32,767
int 4 - 2,147,483,648 2,147,483,647
long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
FLOATING-POINT DATA TYPES
Type
Size
(Bytes
)
Max Positive Non Zero
Value
MaxValue
float 4 1.40239846 x 10-45
3.40282347 x 1038
double 8 4.9406564584124654 x 10-324
1.7976931348623157 x 10308
CHARACTER DATA TYPE
char
•Size: 2 Bytes
•MinValue:The character encoded as
0000, the null character
•MaxValue:The value FFFF which is a
special code for “not a character”
BOOLEAN DATA TYPE
boolean
•Can store only two values, which are
expressed using the Java reserved words
true and false.
THE ASSIGNMENT OPERATOR
AND INITIAL VALUES
• When you declare a variable, you can also assign an initial
value to the databy using the assignment operator (=)
datatype variableName = initialValue;
datatype var1=initialValue1, var2=initialValue2 ;
• Notice that assignment is right to left.The initial value is
assigned to the variable.
IDENTIFIERS RULES
• Names can contain letters, digits, underscores ( _ ), and dollar ( $ )
signs
• Names should begin with a letter
• Names can also begin with $ and _
• Names are case sensitive ("myVar" and "myvar" are different
variables)
• Names should start with a lowercase letter and it cannot contain
whitespace. If the variable name consists of more than one word,
then each word after the first should begin with a capital letter. For
example, these identifiers are conventional Java variable names:
number1, highScore, booksToRead, ageInYears, and xAxis.
• Reserved words (like Java keywords, such as int or String) cannot be
LITERALS
• INTEGER LITRALS:
• Can be expressed in decimal(base 10)
• Hexadecimal(base 16) : Prefix 0 is used to indicate octal
• Octal(base 8) : prefix 0x indicates hexadecimal
• Examples:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
• Values of type long that exceed the range of int can be created
from long literals by specifying L or l:
long var = 1234567654321L
LITERALS
• FLOATING-POINT LITERALS
• A floating-point literal is of type float if it ends with the
letter F or f; otherwise its type is double and it can optionally
end with the letter D or d.
• The floating point types (float and double) can also be expressed
using E or e (for scientific notation),
double d1 = 123.4;
• same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1 = 123.4f;
LITERALS
• CHAR LITERALS
• Single quote :
char ch = ‘N';
• Integral literal: Unicode value in Decimal, Octal and
Hexadecimal.
char ch = 78; //range is 0 to 65535
char ch = 0116;//range 000000 to 177777
char ch = 'u004E'; // range u0000 to uFFFF
• Escape Sequence :
char ch = 'n';
JAVA ESCAPE SEQUENCE
Escape
Sequence
Description
t Insert a tab in the text at this point.
b Insert a backspace in the text at this point.
n Insert a newline in the text at this point.
r Insert a carriage return in the text at this point.
f Insert a formfeed in the text at this point.
' Insert a single quote character in the text at this point.
" Insert a double quote character in the text at this point.
 Insert a backslash character in the text at this point.
EXERCISE 1:
Write a program which displays information about you like:
Your Name, Fathers Name, Class Roll No., age, and Cell Phone
number.
EXERCISE 2:
Write down a program which has 2 variable ‘a’ and ‘b’ assign
them any value and evaluate the following equation:
x = a2
+ 2ab + b2
Create variable x and Display the result of ‘x’
EXERCISE 3:
 Declare two short variable and try to assign their sum to byte variable and write
down what happened.
short a=10, b=20;
short c = a+b;
 Declare a long variable with value with int range then declare another long
variable with value outside the range of int. Write down what happened.
 Declare a float variable like this and write down what happened.
float a = 12.54;
 Declare all types of integers and floating point variables and try to give those
values outside of their range and then write down the errors.
 Create 5 character variable by giving it different hexadecimal codes and write
down what characters are shown on screen.
 Declare and initialize two character variable with any alphabet value. Then use
this statement to print the result and write down what happened.
System.out.println (ch1 + ch2);
END

Object oriented programming1 Week 1.pptx

  • 1.
  • 2.
    TOPICS • I NT R O D U C T I O N TO O B J E C T O R I E N T E D P R O G R A M M I N G • I N T R O D U C T I O N TO J AVA • I N S TA L L AT I O N O F J AVA • G E N E R A L S T RU C T U R E O F P R O G R A M • RU N N I N G YO U R F I R S T P R O G R A M • VA R I A B L E S A N D DATA T Y P E S • L I T E R A L S
  • 3.
    OBJECT ORIENTED PROGRAMMING •Programming paradigm based on the concept of objects, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. • Object-Oriented Programming (OOP) is a programming language model, which uses Objects and data as core components. • The main idea behind OOP is to represent the data and logic with objects instead of actions or functions.
  • 4.
    CLASS • The classdefines the shape and nature of an object. • A class is the template or blueprint from which objects are actually made. • A class is also defined as a new data type, a user defined type which contain two things: – Data Member – Methods • A class defines the properties and behaviors for objects. • The description of a number of similar object is also called as a class. • Classes is logical in nature.
  • 6.
    OBJECT • An objectis an instance of a class. • An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. • Object to object communication is done via methods.
  • 7.
    OBJECT • Software objectsalso have a state and a behavior: – The state of an object (also known as its properties or attributes): – is represented by data fields with their current values. • A circle object, for example, has a data field radius , which is the property that characterizes a circle. • A dog has states -- color, name, breed – The behavior of an object (also known as its actions): – is defined by methods. • To invoke a method on an object is to ask the object to perform an action. • For example, you may define methods named getArea() and getPerimeter() for circle objects. • A dog has behaviors -- wagging the tail, barking, eating.
  • 9.
    JAVA • Object-oriented programminglanguage • Resembles C++ in many respects. • Architecture-neutral means Java program should be able to run on a any Operating System without recompilation. • Java programs are compiled into machine-independent byte code which is then run within a Java interpreter, which is responsible for executing the byte code instructions. • The Java interpreter is typically referred to as the JavaVirtual Machine (JVM), and it must be present on each computer that runs the program.
  • 10.
    Source Code (.java) Byte Code (.class) JAVACJAVA Compiler Interpreter Execution COMPILING JAVA
  • 11.
    ASSIGNMENT •Write a 2page report about Java History. •Include how java changed over time by including versions and releases.
  • 12.
    INSTALLING JAVA AND WRITINGAND COMPILING YOUR FIRST PROGRAM
  • 13.
    VARIABLES •Named locations inmemory where you can store values. •Can store one data value at a time •Can change the values •Variable Names (Identifiers) can be short names (like x and y) or more descriptive names (age, sum, totalVolume)
  • 14.
    DECLARING VARIABLES •Declaring meansgiving a name and a data type to a variable before it can be used. •Syntax: datatype identifier; datatype identifier1, identifier2, ..;
  • 15.
    INTEGER DATA TYPES Type Size (Bytes) MinValue Max Value byte 1 -128 127 short 2 -32,768 32,767 int 4 - 2,147,483,648 2,147,483,647 long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
  • 16.
    FLOATING-POINT DATA TYPES Type Size (Bytes ) MaxPositive Non Zero Value MaxValue float 4 1.40239846 x 10-45 3.40282347 x 1038 double 8 4.9406564584124654 x 10-324 1.7976931348623157 x 10308
  • 17.
    CHARACTER DATA TYPE char •Size:2 Bytes •MinValue:The character encoded as 0000, the null character •MaxValue:The value FFFF which is a special code for “not a character”
  • 18.
    BOOLEAN DATA TYPE boolean •Canstore only two values, which are expressed using the Java reserved words true and false.
  • 19.
    THE ASSIGNMENT OPERATOR ANDINITIAL VALUES • When you declare a variable, you can also assign an initial value to the databy using the assignment operator (=) datatype variableName = initialValue; datatype var1=initialValue1, var2=initialValue2 ; • Notice that assignment is right to left.The initial value is assigned to the variable.
  • 20.
    IDENTIFIERS RULES • Namescan contain letters, digits, underscores ( _ ), and dollar ( $ ) signs • Names should begin with a letter • Names can also begin with $ and _ • Names are case sensitive ("myVar" and "myvar" are different variables) • Names should start with a lowercase letter and it cannot contain whitespace. If the variable name consists of more than one word, then each word after the first should begin with a capital letter. For example, these identifiers are conventional Java variable names: number1, highScore, booksToRead, ageInYears, and xAxis. • Reserved words (like Java keywords, such as int or String) cannot be
  • 21.
    LITERALS • INTEGER LITRALS: •Can be expressed in decimal(base 10) • Hexadecimal(base 16) : Prefix 0 is used to indicate octal • Octal(base 8) : prefix 0x indicates hexadecimal • Examples: int decimal = 100; int octal = 0144; int hexa = 0x64; • Values of type long that exceed the range of int can be created from long literals by specifying L or l: long var = 1234567654321L
  • 22.
    LITERALS • FLOATING-POINT LITERALS •A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d. • The floating point types (float and double) can also be expressed using E or e (for scientific notation), double d1 = 123.4; • same value as d1, but in scientific notation double d2 = 1.234e2; float f1 = 123.4f;
  • 23.
    LITERALS • CHAR LITERALS •Single quote : char ch = ‘N'; • Integral literal: Unicode value in Decimal, Octal and Hexadecimal. char ch = 78; //range is 0 to 65535 char ch = 0116;//range 000000 to 177777 char ch = 'u004E'; // range u0000 to uFFFF • Escape Sequence : char ch = 'n';
  • 24.
    JAVA ESCAPE SEQUENCE Escape Sequence Description tInsert a tab in the text at this point. b Insert a backspace in the text at this point. n Insert a newline in the text at this point. r Insert a carriage return in the text at this point. f Insert a formfeed in the text at this point. ' Insert a single quote character in the text at this point. " Insert a double quote character in the text at this point. Insert a backslash character in the text at this point.
  • 25.
    EXERCISE 1: Write aprogram which displays information about you like: Your Name, Fathers Name, Class Roll No., age, and Cell Phone number. EXERCISE 2: Write down a program which has 2 variable ‘a’ and ‘b’ assign them any value and evaluate the following equation: x = a2 + 2ab + b2 Create variable x and Display the result of ‘x’
  • 26.
    EXERCISE 3:  Declaretwo short variable and try to assign their sum to byte variable and write down what happened. short a=10, b=20; short c = a+b;  Declare a long variable with value with int range then declare another long variable with value outside the range of int. Write down what happened.  Declare a float variable like this and write down what happened. float a = 12.54;  Declare all types of integers and floating point variables and try to give those values outside of their range and then write down the errors.  Create 5 character variable by giving it different hexadecimal codes and write down what characters are shown on screen.  Declare and initialize two character variable with any alphabet value. Then use this statement to print the result and write down what happened. System.out.println (ch1 + ch2);
  • 27.