Lec 4 06_aug [compatibility mode]

Post on 08-Jun-2015

136 views 1 download

Tags:

Transcript of Lec 4 06_aug [compatibility mode]

ObjectObject--Oriented ProgrammingOriented Programming

Lecture 4

Naveen Kumar

ObjectObject

An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.

Numbers, texts, pictures, sound, video ….. So on

ObjectObject

An object can be something in the physical world or even just an abstract idea.

for example: An airplane, is a physical

object that can be manipulated by a

computer.

ObjectObject

A bank transaction is an example of an object that is not physical.

To a computer,

an object is simply something that can be represented by data in the computer’s memory and manipulated by computer programs

ObjectObject

The data that represent the object are organized into a set of properties.

NameName: : AI 379AI 379

OwnerOwner:: Indian AirlinesIndian Airlines

LocationLocation:: 39 52′ 06″N39 52′ 06″N 75 13′ 52″W 75 13′ 52″W

HeadingHeading:: 271271°°

AltitudeAltitude:: 19 m19 m

AirSpeedAirSpeed:: 00

MakeMake:: BoeingBoeing

ModelModel:: 737737

WeightWeight:: 32,820 kg32,820 kg

The values stored in an object’s properties, at any one time, form the stateof an object.

ObjectObject--Oriented ProgrammingOriented Programming

Computer programs implement algorithms that manipulate the data.

In object-oriented programming, the programs that manipulate the properties of an object are the object’s methods.

ObjectObject--Oriented ProgrammingOriented Programming

We can think of an object as a collection of propertiesand, the methods that are used to manipulate those properties.

Properties

Methods

ObjectObject--Oriented ProgrammingOriented Programming

A class is a group of objects with the same properties and the same methods.

ObjectObject--Oriented ProgrammingOriented Programming

Each copy of an object, from a particular class is called an instance, of the object.

The act of creating a new instance of an object is called instantiation.

ObjectObject--Oriented ProgrammingOriented Programming

A class can be thought of as a blueprint for instances of its object.

Two different instances of the same class will have the same properties, but different values stored in those properties.

ObjectObject--Oriented ProgrammingOriented Programming

The same terminology is used in most object-oriented programming languages.

Object

Instance

Property

MethodInstantiation

ClassState

Introduction to Java

12

Development tools-part of java development kit (JDK) Classes and methods-part of Java Standard Library (JSL),

also known as Application Programming Interface (API)

1. JDK: Appletviewer ( for viewing applets) Javac (Compiler) Java (Interpreter) Javah (for C header files) Javadoc ( for creating HTML description)

Java Environment

2. Application Package Interface (API)Contains hundreds of classes and methods grouped into several functional packages: Language Support Package (String, Integer, Double, etc) Utility Packages (rand. num. gen., sys. date) Input/Output Packages Networking Packages (implementing networking appl. ) AWT Package (classes for painting graphics and images) Applet Package (web page using java)

Java Environment

1. Java 1.0 (96)2. Java 1.1 (97)(Add new library, redefine applet handling and

reconfigured many features.)3. Java 2 (98)(Second generation). Version no:1.2 (Internal

version number of java library). Also known as J2SE [ Java 2 Platform Standard Edition].- Add swing, the collection framework, enhanced JVM etc.

4. J2SE 1.3 (2000)5. J2SE 1.4 (2002)6. J2SE 1.5 (2004)7. J2SE 1.6 (2006) [1.7-(2013), in queue 1.8 (exp in 2014) ]

The Evolution of Java

Comments

In Java, comments are preceded by two slashes (//) in a line, orenclosed between /* and */ in one or multiple lines

When the compiler sees //, it ignores all text after // in the same line

When it sees /*, it scans for the next */ and ignores any text between /* and */

Example

/* Traditional "Hello World!" program. */

// package pack1; // import java.lang.System; class A { public static void main (String args[]) { System.out.println("Hello World!");

} }

Save program as A.java

Java Program Structure

Package Statement Javac command compiles the source code A.java then,

generates A.class and store it under a directory which is called as name of the package

package statement if used must be the first statement in a compilation unit. Its syntax is:

package packageName; For example:

package pack1;

Import Statement

The import statements are similar to #include statements in C and C++

In the above program, System class of java.lang package is imported into all Java programs by default. The syntax of import statement is as:

import fullClassName;

For example, the following import statement imports the System class from java.lang:

import java.lang.System;import java.lang.*;

Classes and Methods

Class declarations contain a keyword class and an identifier (Ex: A) Class members are enclosed within braces. The syntax of defining a

class is shown below:

class A{ // program code

}

To execute a class, it must contain a valid main method It is the first method that automatically gets invoked when the program

executed

public static void main (String args[]){

//instructions }