java Lecture3-Classes and Methods

  • Upload
    jatt543

  • View
    227

  • Download
    0

Embed Size (px)

Citation preview

  • 8/2/2019 java Lecture3-Classes and Methods

    1/37

    Classes and Methods

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    DmWaemk

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    2/37

    Classes

    data field 1

    method n

    data field n

    method 1

    An object

    ...

    ...

    State

    Behavior

    Data Field

    radius = 5

    Method

    findArea

    A Circle object

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    DmWaemk

    PP

    http://www.eepdf.com/http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    3/37

    Class Definition Data Fields

    Variables to store data items

    Differentiate multiple objects of a class

    They are called data members

    Methods

    Operations of a class

    They operate on variables of the class

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    DmWaemk

    PP

    http://www.eepdf.com/http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    4/37

    ExampleCircle

    radius

    findArea

    circle1: Circle

    radius = 2

    new Circle()

    circlen: Circle

    radius = 5

    new Circle()

    ...

    Graphical

    notation for

    classes

    Graphical

    notation for

    ob ects

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http:/

    /www.eepdf.com/'toremoveth

    iswatermark.

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http:/

    /www.eepdf.com/'toremoveth

    iswatermark.

    DmWaemk

    PP

    http://www.eepdf.com/http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    5/37

    Fields in Class Definition Class variables

    Instance variables

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http:/

    /www.eepdf.com/'toremoveth

    iswatermark.

    Pleasepurchase'eePDFOfficetoPDFConverter'on'http:/

    /www.eepdf.com/'toremoveth

    iswatermark.

    DmWaemk

    Pl

    Pl

    http://www.eepdf.com/http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    6/37

    Fields in Class Definition Class Variable

    Only one copy is shared with all the objects ofthe class

    Shared among all objects of a class

    Exists even if no object has been created

    Also called as static fields

    Declared using the keyword static If value is changed, it is reflected for all

    objects.

    leasepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    leasepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    DmWaemk

    Ple

    Ple

    http://www.eepdf.com/http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    7/37

    Instance variables Variables associated with each object

    A separate value for each instance of aclass

    For example, name , address etc.

    easepurchase'eePDFOffi

    cetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    easepurchase'eePDFOffi

    cetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    DmWaemk

    Ple

    Ple

    http://www.eepdf.com/http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    8/37

    ExampleClass Sphere Definition

    public class Sphere {// class variable

    static double PI=3.14;

    // instance variables

    double xCenter;

    double yCenter;

    double zCenter;double radius;

    }

    easepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    easepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    DmWaemk

    Ple

    PleD

    http://www.eepdf.com/http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    9/37

    Methods in Class Definition Instance Methods

    Class Methods

    easepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    easepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    mWaemk

    Plea

    PleaD

    http://www.eepdf.com/http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    10/37

    Class Methods Execute class methods even when no

    objects of a class exist Declared using the keyword static.

    Static methods Can not refer to an instance variable

    Example: main method

    Builtin methods in standard class Math

    asepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    asepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    mWaemk

    Plea

    PleaD

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    11/37

    Instance Methods Can execute when objects exist.

    Example: calculate area()

    asepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    asepurchase'eePDFOfficetoPDFConverter'on'http://www.eepdf.com/'toremoveth

    iswatermark.

    mWaemk

    Plea

    PleaD

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    12/37

    Accessing Variables and Methods

    Accessing Class Methods

    double rootPi = Math.sqrt(Math.PI);

    Objects can also access in the same way

    Instance variables and methods double ballVolume = ball.volume();

    asepurchase'eePDFOfficetoPDFConverter'on'http://

    www.eepdf.com/'toremoveth

    iswatermark.

    asepurchase'eePDFOfficetoPDFConverter'on'http://

    www.eepdf.com/'toremoveth

    iswatermark.

    mWaemk

    Pleas

    PleasD

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    13/37

    Defining Classesclass Sphere {

    static final double PI = 3.14; //class variable with fixed valuestatic int count = 0; // Class variable to count objects

    // Instance variables

    double radius; // Radius of a spheredouble xCenter; // 3D coordinates

    double yCenter; // of the center

    double zCenter; // of a sphere// Plus the rest of the class definition...

    }

    sepurchase'eePDFOfficetoPDFConverter'on'http://

    www.eepdf.com/'toremovethiswatermark.

    sepurchase'eePDFOfficetoPDFConverter'on'http://

    www.eepdf.com/'toremovethiswatermark.

    mWaemk

    Pleas

    PleasD

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    14/37

    Defining Methods Self contained block of codes

    Reusable

    Can be executed from anywhere in the

    program Calling a method

    sepurchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    sepurchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    mWaemk

    Pleas

    Pleas

    D

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    15/37

    Basic Structure of a method

    return_type methodName( arg1, arg2, ...,argn )

    { code here}

    Return Statement

    return return_value; // To return a valuefrom a method

    epurchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    epurchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    mWaemk

    Please

    Please

    D

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    16/37

    Example

    public static int max(int num1, int num2)

    {

    int result = 0;

    if (num1 > num2)

    result = num1;

    else

    result = num2;

    return result;

    }

    modifier

    returnValueType

    methodName

    parameters

    return value

    methodbody

    method

    heading

    epurchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    epurchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    mWaemk

    Please

    Please

    D

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    17/37

    Parameters and Arguments Parameter

    has a name and a type Appear in definition of the method

    Defines type of value to be passed to method when

    called Arguments

    Actual value that is passed to a method whenexecuted

    Must be consistent with the type specified inparameter definition

    epurchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    epurchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    mWaemk

    Please

    Please

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    18/37

    Examplepublic static void main(String[] args){

    ...x = obj.mean( 3.0 , 5.0 );

    }

    double mean( double value1 , double value2 ){

    double result = ( value1 + value2 )/ 2.0;

    return result;}

    purchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    purchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethiswatermark.

    Waemk

    Please

    Please

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    19/37

    Passed by Valuedouble d = 2.0;

    changeMe(d);System.out.println(d);

    public void changeMe(double d){

    //this has no effect on d outside of this method!

    d = 345.0;}

    purchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethi

    swatermark.

    purchase'eePDFOffic

    etoPDFConverter'on'http://www.eepdf.com/'toremovethi

    swatermark.

    Waemk

    Pleasep

    Pleasep

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    20/37

    Class Method Definitionclass Sphere {

    // Class definition as before...// Static method to report the number of

    objects created

    static int getCount() {

    return count; // Return current object count

    }}

    purchase'eePDFOffic

    etoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    purchase'eePDFOffic

    etoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    Waemk

    Pleasep

    Pleasep

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    21/37

    Example Classclass Sphere {static final double PI = 3.14; // Class variable that has a fixed valuestatic int count = 0; // Class variable to count objects

    // Instance variablesdouble radius;double xCenter; // 3D coordinatesdouble yCenter; // of the centerdouble zCenter; // of a sphere

    // Static method to report the number of objects createdstatic int getCount(){return count; // Return current object count}

    // Instance method to calculate volumedouble volume() {

    return 4.0/3.0*PI*radius*radius*radius;}

    // Plus the rest of the class definition...}

    purchase'eePDFOffic

    etoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    purchase'eePDFOffic

    etoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    Waemk

    Pleasep

    Pleasep

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    22/37

    class Sphere {static final double PI = 3.14;static int count = 0;

    // Instance variablesdouble radius = 5.0;double xCenter = 10.0;double yCenter = 10.0;double zCenter = 10.0;

    // Static method to report the number of objects createdstatic int getCount(){

    return count; // Return current object count

    }

    // Instance method to calculate volume

    double volume() {return 4.0/3.0*PI*radius*radius*radius;

    }// Plus the rest of the class definition...

    public static void main (String[] args){int count;double volume;count=Sphere.getCount();System.out.println(count);Sphere s1=new Sphere();volume=s1.volume();System.out.println(volume);

    }

    urchase'eePDFOffic

    etoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    urchase'eePDFOffic

    etoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    Waemk

    Pleasepu

    Pleasepu

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    23/37

    The Variable this Every instance method has a variable with

    the name this that refers to the currentobject for which the method is being called

    void changeRadius(double radius) {

    // Change the instance variable to the

    argument value

    this.radius = radius;}

    urchase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    urchase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    Waemk

    Pleasepu

    Pleasepu

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    24/37

    Initializing data members: The

    ordinary way

    class Sphere {static final double PI = 3.14; // Class variable that hasa fixed value

    static int count = 0; // Class variable to count objects// Instance variablesdouble radius = 5.0; // Radius of a spheredouble xCenter = 10.0; // 3D coordinates

    double yCenter = 10.0; // of the centerdouble zCenter = 10.0; // of a sphere// Rest of the class...

    }

    urchase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    urchase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    Waemk

    Pleasepu

    Pleasepu

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    25/37

    Using Initialization Blocks There are two kinds of initialization blocks:

    static initialization blocknon-static initialization block

    rchase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    rchase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethiswatermark.

    Waemk

    Pleasepur

    Pleasepur

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    26/37

    static initialization block A static initialization block is a block defined

    using the keyword static and is executed oncewhen the class is loaded.

    A static initialization block can initialize onlystatic data members of the class.

    A static initialization blockis a normal block ofcode enclosed in braces, { }, and preceded bythe static keyword.

    Here is an example:static {// whatever code is needed for initialization goes here}

    rchase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    rchase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    Waemk

    t ti

    Pleasepurc

    Pleasepurc

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    27/37

    static

    initializationblock

    class TryInitialization {

    static int[] values = new int[10];

    static {

    System.out.println("Running initializationblock.");

    for(int i=0; i

  • 8/2/2019 java Lecture3-Classes and Methods

    28/37

    Non-static initialization blocknon-static initialization block is executed for

    each object that is created and thus caninitialize instance variables in a class.

    This block appears without the static keyword.

    {

    // whatever code is needed for initialization goes here

    }Running initialization block.

    First object:66 17 98 59 99 18 40 96 40 21Running initialization block.Second object:57 86 79 31 75 99 51 5 31 44

    chase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    chase'eePDFOfficetoPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    Waemk

    Pleasepurc

    Pleasepurc

    Dm

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    29/37

    LAB PRACTICE Compile & execute

    TryInitialization class with staticinitialization and non-static

    initialization block. Examine its output.

    chase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    chase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    Waemk

    Pleasepurch

    Pleasepurch

    DmW

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    30/37

    LAB PRACTICE Make a class Bicycle, identify its data members for

    example speed=0, gear=1 etc.

    Add methods like

    changeGear(int), speedUp(int) , applyBrakes(),printStates() etc.

    Make a main method to create 2 bicycle objects andcall functions of the class in the following sequence.1. Change speed

    2. Change gear

    3. Print states

    4. Apply brakes

    5. Print states

    hase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    hase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    aemk

    P

    leasepurch

    P

    leasepurch

    DmW

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    31/37

    Solution

    class Bicycle{

    int speed = 0;int gear = 1;void changeGear(int newValue){ gear = newValue;

    }void speedUp(int increment){ speed = speed + increment;}void applyBrakes(){ speed = 0;}

    void printStates(){ System.out.println(" speed:"+speed+" gear:"+gear);}}

    hase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    hase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    aemk

    P

    leasepurch

    P

    leasepurch

    DmW

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    32/37

    Solution

    class BicycleDemo{

    public static void main(String[] args){ // Create two different Bicycle objectsBicycle bike1 = new Bicycle();Bicycle bike2 = new Bicycle(); // Invoke methods on those objects

    bike1.speedUp(10);bike1.changeGear(2);bike1.printStates();bike1.applyBrakes();bike1.printStates();

    bike2.speedUp(10);bike2.changeGear(2);bike2.speedUp(10);bike2.changeGear(3);

    bike2.printStates();}}

    ase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    ase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    aemk

    P

    leasepurcha

    P

    leasepurcha

    DmW

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    33/37

    Solution

    The output of this test prints speed,and gear for the two bicycles:

    speed:10 gear:2

    speed:20 gear:3

    ase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    ase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    aemk

    P

    leasepurcha

    P

    leasepurcha

    DmW

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    34/37

    LAB PRACTICE1. Create a class Box that uses a parameterized

    method to initialize the dimensions of abox.(dimensions are width, height, depth ofdouble type).

    2. The class should have a method that canreturn volume.

    3. Obtain an object and print the corresponding

    volume in main() function.

    ase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    ase'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    aemk

    P

    leasepurcha

    P

    leasepurcha

    DmW

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    35/37

    Solution

    se'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    se'eePDFOffice

    toPDFConverter'on'http://w

    ww.eepdf.com/'toremovethis

    watermark.

    aemk

    P

    leasepurchas

    P

    leasepurchas

    DmW

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    36/37

    Solution

    se'eePDFOffice

    toPDFConverter'on'http://www.eepdf.com/'toremovethis

    watermark.

    se'eePDFOffice

    toPDFConverter'on'http://www.eepdf.com/'toremovethis

    watermark.

    aemk

    P

    leasepurchas

    P

    leasepurchas

    DmWa

    http://www.eepdf.com/
  • 8/2/2019 java Lecture3-Classes and Methods

    37/37

    Output

    Volume is 3000.0Volume is 162.0

    se'eePDFOffice

    toPDFConverter'on'http://www.eepdf.com/'toremovethis

    watermark.

    se'eePDFOffice

    toPDFConverter'on'http://www.eepdf.com/'toremovethis

    watermark.

    emk