java_unit-i+(2)

download java_unit-i+(2)

of 32

Transcript of java_unit-i+(2)

  • 8/3/2019 java_unit-i+(2)

    1/32

    OOP

    TEXT BOOKS :

    Java; the complete reference, 7th edition, Herbert schildt, TMH.

    UnderstandingOOP with Java, updated edition, T. Budd, pearson

    education.

  • 8/3/2019 java_unit-i+(2)

    2/32

    Unit-I

  • 8/3/2019 java_unit-i+(2)

    3/32

    Object-Oriented thinking

    Need of oop paradigm / model:-

    Object-oriented programming is a newprogramming

    model.A model helps you understandhow the system works.

    For example, the Newton model of physics explains whyapples fall to the ground.

    In computer science , a model explains how theelements that go into a program are organizedand howthey interactwith one another.

  • 8/3/2019 java_unit-i+(2)

    4/32

    A Way of Viewing/Understanding the World

    To understand the major ideas in object

    oriented programming

    Let us see how we handle a real worldsituation.

    Then we will see how can we make computerto

    model the techniques used in the situation.

  • 8/3/2019 java_unit-i+(2)

    5/32

    Real world situation

    I want to send some flowers to my friend sally

    who lives in a city many miles away.

    Because of the distance, I cannot pick up the

    flowers and carry them to sallys door myself.I will go to Flora, my local florist, tell her the type

    and quantity of flowers I wish to send and sallys

    address.

    I can be assured that the flowers will bedelivered automatically.

  • 8/3/2019 java_unit-i+(2)

    6/32

    Contd..

    Me

    Flora

    Sallys Florist

    Delivery person

    Sally

    FlowerArranger

    Wholesaler

    Grower

    Gardeners

    The community of agents helping me

  • 8/3/2019 java_unit-i+(2)

    7/32

    Agents and communities

    I find an appropriate agent(Flora) and pass to her amessage containing a request.

    It is the responsibilityofFlora to satisfymy request.

    There is some method-- some algorithm or a set of

    operations -- used by Flora to do this.I do not need to know theparticular methodthat Flora will

    use, this information is hidden from me.

    The General Principle

    An object oriented program is structuredas acommunityof interacting agents called objects. Each

    object provides a service that is used by other members

    of the community.

  • 8/3/2019 java_unit-i+(2)

    8/32

    Messages andMethods

    Action is initiated in object oriented programming by the

    transmission of a message to an agent(an object).

    The message contains the request foraction and is

    accompaniedby additional information (arguments)

    needed to carry out the request.

    The receiveris the agent to which the message is sent.

    If the receiveraccepts the message it accepts the

    responsibilityto carry out the indicated action.

    In response to a message the receiverwill perform

    some methodto satisfy the request.

  • 8/3/2019 java_unit-i+(2)

    9/32

    In what sense is a message different from a

    procedure call?

    In both cases there is a set of well-defined steps that will

    be executed following the request. But, there are two

    important differences.

    The first is that a message has a specific receiver. In a

    procedure call, there is no specificreceiver.

    The second is that the interpretation of the message ( that is, the

    method used to respond to the message ) is dependenton thereceiverand can vary with differentreceivers.

  • 8/3/2019 java_unit-i+(2)

    10/32

    Contd..

    The specific receiverfor any given message will not be known untilrun time. There is a late bidingbetween the message and code

    fragment( method ) used to respond to the message.

    In a function or procedure call, binding name to code fragment will

    be done at compile time only. There is an early binding.

  • 8/3/2019 java_unit-i+(2)

    11/32

    Classes andInstances

    A class is the template or blueprintfrom which objects

    are actually created.

    All objects are instances of a class.

    All objects of thesame class use the same methodin response to the

    same message.

  • 8/3/2019 java_unit-i+(2)

    12/32

    Class Hierarchies-Inheritance

    The principle thatknowledge of more general category is

    also applicable to a more specificcategory is called

    inheritance.

    Employee

    name

    salary

    hire_date

    Professor

    bonus

    Programmer

    contract_period

  • 8/3/2019 java_unit-i+(2)

    13/32

    Classes can be organized into a hierarchicalinheritance

    structure. Achildclass (or subclass) will inheritattributes

    from a parent class higher in the tree.

    An abstractclass is a class for which there are no direct

    instances; it is used only to create subclass.

  • 8/3/2019 java_unit-i+(2)

    14/32

    Flash Flora Elizabeth Kenneth Phyl Sallys flowers

    Florist Potter

    Shopkeeper Artist Dentist

    HumanDog Platypus

    Mammal

    Animal

    Carnation

    Flower

    Plant

    Material Objects

    A class hierarchy for various material objects

  • 8/3/2019 java_unit-i+(2)

    15/32

    Contd..

    Flora

    Material Object

    Animal

    Mammal

    Human

    Shopkeeper

    Florist

    Categories surrounding Flora

    Flora is a florist, but Florist is a

    specialized form of the category

    Shopkeeper.

  • 8/3/2019 java_unit-i+(2)

    16/32

    Method Binding, Overriding and Exceptions

    Phyl the platypus presents aproblem for our simplestructure.

    We know that mammals give birth to live babies.

    But Phyl lays eggs.

    To accommodate this variation, we need to find atechnique to convert exceptions to a general rule.

    We do this by overridingthe information inherited fromparent class in subclass.

    Name of the method in the subclass is same as in theparent class.

  • 8/3/2019 java_unit-i+(2)

    17/32

    The search for a method to invoke in response to a given

    message starts with the class of the receiver.

    Ifno appropriate method is found, the search continues

    up theparentclass chain until either a method is found

    or the parent class chain is exhausted.

  • 8/3/2019 java_unit-i+(2)

    18/32

    Summary of oop concepts Everything is an object.

    Computation is performed by communicatingobjectswith each other.

    Objects communicate by sending and receivingmessages. A message is a request for action bundled

    with whatever arguments may be necessary to completethe task.

    Each object has its own memory.

    Every object is an instance of a class.

  • 8/3/2019 java_unit-i+(2)

    19/32

    All objects that are instances of the same class canperform the same actions.

    Classes are organized into a single rooted tree structure,called the inheritance hierarchy.

    Properties ofsuper class are automatically available tosubclasses.

  • 8/3/2019 java_unit-i+(2)

    20/32

    Dealing /Coping with Complexity

    Complexity of programming projects is non-linear Two programmers cannot do in one month what one

    programmer can do in 2 months

    Adding more men/women to a project often lengthens,not shortens, the schedule.

    Interconnectedness, the dependence of oneportion of code on another portion, is responsiblefor this situation.

    Abstraction mechanisms are used to deal with thiscomplexity and abstraction layers in objectoriented software design provides someassistance to reduce complexity.

  • 8/3/2019 java_unit-i+(2)

    21/32

    Abstraction

    Abstraction is thepurposefulsuppression orhidingof some details of a process in order to

    bring out more clearly other aspects.

    For example, data is stored in 512B blocks on a disk but

    users are presented with the higher level file abstraction.

    We are given variables to enter the data, but actually data

    will be stored in the form of 1s and 0s.

    Similarly header files, ex- math.h in c

  • 8/3/2019 java_unit-i+(2)

    22/32

    Abstraction mechanisms in

    programming languages.

    Procedures and Functions

    It increases readability of the code

    Easy to maintain the code.

    Easy to find errors.

    Header files and Packages

    Easy to write the code.

    It reduces the code size.

    It reduces the time.

  • 8/3/2019 java_unit-i+(2)

    23/32

    Contd..

    Abstract Data Types ( classes )Separates interface and implementation

    Easy to understand the functionality .

    Provides security.

    polymorphismIncreases readability of the code.

    InheritanceIt reduces the code size .

  • 8/3/2019 java_unit-i+(2)

    24/32

    Abstraction layers in Object-OrientedSoftware

    Design

    At the highest levela program is viewed as a communityofcooperatingobjects. At this highest level the importantfeatures to stress are the services provided by eachobject and the lines of communication between them.

    The next level of abstraction places a group of objectsworking together in a single unit.

  • 8/3/2019 java_unit-i+(2)

    25/32

    Contd..

  • 8/3/2019 java_unit-i+(2)

    26/32

    Contd..

    The next level of abstraction deals with the interaction

    between 2 individualobjects where one uses services from

    the other.

    The last level of abstraction considers a single task inisolation i.e. what are the steps required to implement a

    single method.

  • 8/3/2019 java_unit-i+(2)

    27/32

    Programming in the Small versus

    Programming in the Large

    Code developed by singleprogrammer or very smallset of programmers.

    Every individual

    understands all aspectsof the project.

    Major effort is the designand development ofalgorithms .

    Code developed by alarge team.

    No single individual isresponsible for the entire

    project or understands allaspects of it

    Major effort is themanagement of detailsand the communication ofinformation between

    different portions of theproject.

  • 8/3/2019 java_unit-i+(2)

    28/32

    Week 2 :

    a)Write a Java program that prompts the user foran integer and then prints out all prime numbers

    up to that integer.b)Write a Java program to multiply two given

    matrices.

    c)Write a Java Program that reads a line of

    integers, and then displays each integer, and thesum of all the integers (Use StringTokenizerclass of java.util)

  • 8/3/2019 java_unit-i+(2)

    29/32

    Reading data from keyboard using JOptionPane

    import java.lang.*;

    import java.io.*;

    import javax.swing.*;

    public class InputFromKeyboardJOptionPane

    {

    public static void main(String[] args)

    {

    String str=JOptionPane.showInputDialog("enter data");

    System.out.println(str);

    }

    }

  • 8/3/2019 java_unit-i+(2)

    30/32

    StringTokenizerStringTokenizeris a class .

    It converts the input stringinto a set oftokens.

    To use StringTokenizer, you should specify an input stringand a

    string that contains delimiters.

    Delimiters are characters that separate tokens.

    The defaultset ofdelimiters consists of the whitespace characters:

    The StringTokenizerconstructors are shown below:

    StringTokenizer(String str)

    StringTokenizer(String str, String delimiters)

  • 8/3/2019 java_unit-i+(2)

    31/32

    Method Description

    boolean hasMoreTokens( ) Returns true if one ormore tokens remain in

    the string and returns

    false if there are none.

    String nextToken( ) Returns the next token

    as a String.

  • 8/3/2019 java_unit-i+(2)

    32/32

    Arrays

    One-Dimensional Arrays :-

    General form

    type array-var[ ]= new type[size];

    Ex:- int a=new int[10];

    Two-Dimensional Arrays :-

    General form

    type array-var[ ][ ]= new type[row_size][col_size];

    Ex:- int a[ ][ ]=new int[3][3];