Object Oriented Programming: No. of Slides: 85
Object Oriented Programming: No. of Slides: 85
No. of slides: 85
INDEX
UNIT 2 PPT SLIDES
S.NO. TOPIC LECTURE NO. PPTSLIDES
L 1.2
Before Java: C++
L 1.3
Java: History
• In 1990, Sun Microsystems started a project called
Green.
• Objective: to develop software for consumer electronics.
• Project was assigned to James Gosling, a veteran of
classic network software design. Others included Patrick
Naughton, ChrisWarth, Ed Frank, and Mike Sheridan.
• The team started writing programs in C++ for embedding
into
– toasters
– washing machines
– VCR’s
• Aim was to make these appliances more “intelligent”.
L 1.4
Java: History (contd.)
• C++ is powerful, but also dangerous. The power and popularity of C
derived from the extensive use of pointers. However, any incorrect
use of pointers can cause memory leaks, leading the program to
crash.
• In a complex program, such memory leaks are often hard to detect.
• Robustness is essential. Users have come to expect that Windows
may crash or that a program running under Windows may crash.
(“This program has performed an illegal operation and will be shut
down”)
• However, users do not expect toasters to crash, or washing
machines to crash.
• A design for consumer electronics has to be robust.
• Replacing pointers by references, and automating memory
management was the proposed solution.
L 1.5
Java: History (contd.)
L 1.6
Java: History (contd)
L 1.10
• Architecture-neutral: Java Virtual Machine
provides a platform independent environment for the
execution of Java byte code
• Interpreted and high-performance: Java
programs are compiled into an intermediate
representation – byte code:
a) can be later interpreted by any JVM
b) can be also translated into the native machine
code for efficiency.
L 1.11
• Distributed: Java handles TCP/IP protocols,
accessing a resource through its URL much like
accessing a local file.
• Dynamic: substantial amounts of run-time type
information to verify and resolve access to
objects at run-time.
• Secure: programs are confined to the Java
execution environment and cannot access other
parts of the computer.
L 1.12
• Portability: Many types of computers and
operating systems are in use throughout the
world—and many are connected to the Internet.
• For programs to be dynamically downloaded to
all the various types of platforms connected to
the Internet, some means of generating portable
executable code is needed. The same
mechanism that helps ensure security also helps
create portability.
• Indeed, Java's solution to these two problems is
both elegant and efficient.
L 1.13
Data Types
L 1.14
• byte: 8-bit integer type.
Range: -128 to 127.
Example: byte b = -15;
Usage: particularly when working with data
streams.
• short: 16-bit integer type.
Range: -32768 to 32767.
Example: short c = 1000;
Usage: probably the least used simple type.
L 1.15
• int: 32-bit integer type.
Range: -2147483648 to 2147483647.
Example: int b = -50000;
Usage:
1) Most common integer type.
2) Typically used to control loops and to index
arrays.
3) Expressions involving the byte, short and int
values are promoted to int before calculation.
L 1.16
• long: 64-bit integer type.
Range: -9223372036854775808 to
9223372036854775807.
Example: long l = 10000000000000000;
Usage: 1) useful when int type is not large enough to hold the
desired value
• float: 32-bit floating-point number.
Range: 1.4e-045 to 3.4e+038.
Example: float f = 1.5;
Usage:
1) fractional part is needed
2) large degree of precision is not required
L 1.17
• double: 64-bit floating-point number.
Range: 4.9e-324 to 1.8e+308.
Example: double pi = 3.1416;
Usage:
1) accuracy over many iterative calculations
2) manipulation of large-valued numbers
L 1.18
char: 16-bit data type used to store characters.
Range: 0 to 65536.
Example: char c = ‘a’;
Usage:
1) Represents both ASCII and Unicode character
sets; Unicode defines a
character set with characters found in (almost) all
human languages.
2) Not the same as in C/C++ where char is 8-bit and
represents ASCII only.
L 1.19
• boolean: Two-valued type of logical values.
Range: values true and false.
Example: boolean b = (1<2);
Usage:
1) returned by relational operators, such as
1<2
2) required by branching expressions such
as if or for
L 1.20
Variables
L 2.3
Variable Declaration
L 2.4
Variable Scope
L 2.7
Array Declaration
L 2.8
Array Creation
• After declaration, no array actually exists.
• In order to create an array, we use the new
operator:
type array-variable[];
array-variable = new type[size];
• This creates a new array to hold size elements
of type type, which reference will be kept in the
variable array-variable.
L 2.9
Array Indexing
L 2.10
Array Initialization
L 2.11
Multidimensional Arrays
L 2.12
Operators Types
L 2.13
Arithmetic assignments
+= v += expr; v = v + expr ;
-= v -=expr; v = v - expr ;
*= v *= expr; v = v * expr ;
/= v /= expr; v = v / expr ;
%= v %= expr; v = v % expr ;
L 2.14
Basic Arithmetic Operators
+ op1 + op2 ADD
L 2.15
Relational operator
== Equals to Apply to any type
L 2.16
Logical operators
& op1 & op2 Logical AND
! ! op Logical NOT
>> op1 >> op2 Shifts all bits in op1 right by the value of
op2
<< op1 << op2 Shifts all bits in op1 left by the value of
op2
L 2.18
Expressions
• An expression is a construct made up of variables,
operators, and method invocations, which are
constructed according to the syntax of the language, that
evaluates to a single value.
• Examples of expressions are in bold below:
int number = 0;
anArray[0] = 100;
System.out.println ("Element 1 at index 0: " +
anArray[0]);
int result = 1 + 2; // result is now 3 if(value1 ==
value2)
System.out.println("value1 == value2");
L 2.19
Expressions
• The data type of the value returned by an expression depends on the
elements used in the expression.
• The expression number = 0 returns an int because the assignment
operator returns a value of the same data type as its left-hand operand;
in this case, number is an int.
• As you can see from the other expressions, an expression can return
other types of values as well, such as boolean or String.
• The Java programming language allows you to construct compound
expressions from various smaller expressions as long as the data type
required by one part of the expression matches the data type of the
other.
• Here's an example of a compound expression: 1*2*3
L 2.20
Control Statements
L 3.2
Iteration Statements
L 3.3
Jump Statements
byte
byte ->
-> short,
short, int,
int, long,
long, float,
float, double
double
short
short -> -> int,
int, long,
long, float,
float, double
double
char
char -> -> int,
int, long,
long, float,
float, double
double
int
int ->
-> long,
long, float,
float, double
double
long
long ->-> float,
float, double
double
float
float ->-> double
double
L 3.6
Type Conversion
L 3.8
Type Casting
L 4.2
What is a Class?
L 4.4
Object Destruction
L 4.6
Class Definition
class Box {
double width;
double height;
double depth;
}
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println ("Volume is " + vol);
} }
L 4.8
Constructor
L 5.1
Example: Constructor
class Box {
double width;
double height;
double depth;
Box() {
System.out.println("Constructing Box");
width = 10; height = 10; depth = 10;
}
double volume() {
return width * height * depth;
}
}
L 5.2
Parameterized Constructor
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
double volume()
{ return width * height * depth;
}
}
L 5.3
Methods
L 6.1
Access Modifiers: Public, Private,
Protected
• Public: keyword applied to a class, makes it
available/visible everywhere. Applied to a
method or variable, completely visible.
• Default(No visibility modifier is specified): it
behaves like public in its package and
private in other packages.
• Default Public keyword applied to a class,
makes it available/visible everywhere.
Applied to a method or variable, completely
visible.
L 6.2
• Private fields or methods for a class only
visible within that class. Private members
are not visible within subclasses, and are
not inherited.
• Protected members of a class are visible
within the class, subclasses and also
within all classes that are in the same
package as that class.
L 6.3
Visibility
public class Circle {
private double x,y,r;
// Constructor
public Circle (double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
//Methods to return circumference and area
public double circumference() { return 2*3.14*r;}
public double area() { return 3.14 * r * r; }
}
L 6.4
Keyword this
L 6.5
Keyword this
L 6.7
finalize() Method
L 6.8
Method Overloading
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
void test(int a) {
System.out.println("a: " + a);
}
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
double test(double a) {
System.out.println("double a: " + a); return a*a;
}
}
L 7.2
Constructor Overloading
class Box {
double width, height, depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
Box() {
width = -1; height = -1; depth = -1;
}
Box(double len) {
width = height = depth = len;
}
double volume() { return width * height * depth; }
}
L 7.3
Parameter Passing
L 7.4
Call by value
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.print("a and b before call: “);
System.out.println(a + " " + b);
ob.meth(a, b);
System.out.print("a and b after call: ");
System.out.println(a + " " + b);
}
}
L 7.5
Call by refference
• As the parameter hold the same address as the argument,
changes to the object inside the method do affect the object
used by the argument:
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.print("ob.a and ob.b before call: “);
System.out.println(ob.a + " " + ob.b);
ob.meth(ob);
System.out.print("ob.a and ob.b after call: ");
System.out.println(ob.a + " " + ob.b);
}
}
L 7.6
Recursion
class Factorial {
int fact(int n) {
if (n==1) return 1;
return fact(n-1) * n;
}
}
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.print("Factorial of 5 is ");
System.out.println(f.fact(5));
} }
L 8.2
String Handling
L 8.3
• Java defines one operator for String objects:
+.
• It is used to concatenate two strings. For
example, this statement
• String myString = "I" + " like " + "Java.";
results in myString containing
"I like Java."
L 8.4
• The String class contains several methods that you can
use. Here are a few. You can
• test two strings for equality by using
equals( ). You can obtain the length of a string by calling
the length( ) method. You can obtain the character at a
specified index within a string by calling charAt( ). The
general forms of these three methods are shown here:
• // Demonstrating some String methods.
class StringDemo2 {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
System.out.println("Length of strOb1: " +
strOb1.length());
L 8.5
System.out.println ("Char at index 3 in strOb1: " +
strOb1.charAt(3));
if(strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}}
L 8.6