OOEDP_wk1_L1

download OOEDP_wk1_L1

of 31

Transcript of OOEDP_wk1_L1

  • 8/2/2019 OOEDP_wk1_L1

    1/31

    OOEDP Week 1 Lecture 1 1April 12

    Object Oriented Programming in Java

    Object Oriented and Event Driven Programming

    Week 1 Lecture 1

  • 8/2/2019 OOEDP_wk1_L1

    2/31

    OOEDP Week 1 Lecture 1 2April 12

    Objectives

    In this lecture, we will

    Discuss the module organisation

    Discover why object-orientation is desirable

    Review the basic object oriented concepts Define classes, create and use instances of classes

    Examine the structure and syntax of a simple Javaprogram using classes

  • 8/2/2019 OOEDP_wk1_L1

    3/31

    OOEDP Week 1 Lecture 1 3April 12

    Module introduction

    this module follows on from the first Semester module,Introduction to Software Development (ISD)

    we will build on the Java basics we learnt last semester

    this module introduces object-orientation writing classes

    creating and using instances of classes

    object-oriented concepts

    developing graphical user interfaces (GUIs) using classes

    from the Java Swing and awt packages (libraries)

  • 8/2/2019 OOEDP_wk1_L1

    4/31

    OOEDP Week 1 Lecture 1 4April 12

    Module organisation

    see Blackboard site for details

    you must attend 2 lectures and 2 practical sessions perweek

    see your timetable for details

    most of you will have the same tutor as ISD Semester 1

    you will keep a portfolio of your tutorial exercisesthroughout the course

    test based on the portfolio in Week 7

    30% of module mark assignment, based on portfolio work, worth 70%

  • 8/2/2019 OOEDP_wk1_L1

    5/31

    OOEDP Week 1 Lecture 1 5April 12

    Introduction to Objects and Classes

    If you want to build a wall you find an object, a mancalled Bob, who knows how to build walls

    Then you ask Bob to build the wall

    You know what you would like done but not how to do it The object Bob is a specific type, or class, of object

    He is an instance of a Builder

    All instances of Builder know how to build walls

    Object Oriented programming adopts the same approach!!

  • 8/2/2019 OOEDP_wk1_L1

    6/31

    OOEDP Week 1 Lecture 1 6April 12

    Objects and Classes in Java

    Java is an object-oriented language

    all Java code belongs to a class

    you have already written many Java classes

    in most cases you have not constructed any instances of theclasses you wrote

    instead you wrote static methods

    public static void main(String[] args)

    public static int addNums(int num1, int num2)

    the keyword static means the method belongs to the class

    as a whole, not to an individual instance (object)

  • 8/2/2019 OOEDP_wk1_L1

    7/31

  • 8/2/2019 OOEDP_wk1_L1

    8/31

    OOEDP Week 1 Lecture 1 8April 12

    Objects and Classes in Java

    Java provides the class Scanner that knows how to readinput from an input stream

    you construct two objects of this class one object called kybd to read from System.in

    one object called inFile to read from the file payroll.txt the Scanner class defines methods that can be used to read

    the input

    next()

    nextInt()

    nextDouble()

    these methods can be called on any Scanner object

    inFile.next() reads the next integer in payroll.txt

    kybd.nextInt() reads the next integer from the keyboard

  • 8/2/2019 OOEDP_wk1_L1

    9/31

    OOEDP Week 1 Lecture 1 9April 12

    Objects and Classes

    This is analogous to the builder situation

    Builder is a class that knows how to do something

    Build walls

    Scanner is a class that knows how to do something get input from an input stream

    Bob is an instance of builder

    We can ask Bob to build a wall

    kybd is an instance ofScanner

    We can askkybd to find the next integer typed in on the

    keyboard

  • 8/2/2019 OOEDP_wk1_L1

    10/31

    OOEDP Week 1 Lecture 1 10April 12

    Objects and Classes

    Using classes makes performing complex tasks simple

    In order to use a class you need to know:

    Where the class is located

    How to build an instance of the class an object What instances of the class can do

    How to ask the instance to do something

    A class has a set of methods that define the functionalitythat it can provide

    For example nextInt is a method of the Scanner class

    A method is invoked by sending a message to an object

    num1 = kybd.nextInt();

  • 8/2/2019 OOEDP_wk1_L1

    11/31

    OOEDP Week 1 Lecture 1 11April 12

    When is it useful to write a class?

    consider the payroll example from the ISD lecture on tables

    of data we wanted to store information about a list of employees

    where each employee has a name, employee number,department, and salary

    Name Employee

    number

    Department Salary

    Pete 437 6 25000

    Cathy 525 4 60000

    Graham 214 3 40000

    Claude 639 4 35000

    Dave 309 5 15000

  • 8/2/2019 OOEDP_wk1_L1

    12/31

    OOEDP Week 1 Lecture 1 12April 12

    When is it useful to write a class?

    we store this information as

    a one-dimensional array of type String, to store the names a two-dimensional array of type int

    what are the drawbacks of this approach?

    Name Employee

    number

    Department Salary

    Pete 437 6 25000

    Cathy 525 4 60000

    Graham 214 3 40000

    Claude 639 4 35000

    Dave 309 5 15000

  • 8/2/2019 OOEDP_wk1_L1

    13/31

    OOEDP Week 1 Lecture 1 13April 12

    The object-oriented approach to thepayroll problem

    define a class Employee

    an employee has an name, department, employee number andsalary

    create objects of the Employee class

    one object for each employee on the payroll

    store the Employee objects in a 1-D array of type Employee

    to access Employee information, loop through the array and callan appropriate method on each Employee object

    to find the total salary, call a getSalary() method on each Employeeobject

    add to a running total

    we will learn how to do this over the next few weeks

  • 8/2/2019 OOEDP_wk1_L1

    14/31

    OOEDP Week 1 Lecture 1 14April 12

    What is Object Oriented Programming?

    Object oriented Programming an approach to programDesign and Implementation that focuses upon:

    Organisation of data

    Access of data

    In contrast traditional programming focuses upon theorganisation of Algorithms

    The three main characteristics of OOP are: Encapsulation

    Inheritance

    Polymorphism

  • 8/2/2019 OOEDP_wk1_L1

    15/31

    OOEDP Week 1 Lecture 1 15April 12

    Characteristics of O.O.P.

    Encapsulation

    combining data structures with the functions that act upon them

    hiding implementation

    Member Variables

    Member Functions (also called Member Methods)

    CLASS is the unit of encapsulation

    An Object is an instance of a class

  • 8/2/2019 OOEDP_wk1_L1

    16/31

    OOEDP Week 1 Lecture 1 16April 12

    Characteristics of O.O.P.

    Inheritance

    the ability to construct DERIVED classes from existingBASE classes

    the derived class inherits the member variables andmember methods of the base class

    the derived class can

    add additional member variables

    add new member methods or override or extend the

    functionality of existing base class methods

  • 8/2/2019 OOEDP_wk1_L1

    17/31

    OOEDP Week 1 Lecture 1 17April 12

    Characteristics of O.O.P.

    Polymorphism

    one method can have a different function associated withit for each class in a hierarchy

    which function to call can be determined at run timebased upon the class of the object against which themethod is called

  • 8/2/2019 OOEDP_wk1_L1

    18/31

    OOEDP Week 1 Lecture 1 18April 12

    Designing Object Oriented Programs

    The general approach for designing and developing

    object oriented programs is:

    determine the set of classes required

    establish the essential instances of the classes specify the exchange of messages between instances

    Create CLASSES

    Create INSTANCES of classes (OBJECTS)

    Specify the MESSAGES to be exchanged by objects

  • 8/2/2019 OOEDP_wk1_L1

    19/31

    OOEDP Week 1 Lecture 1 19April 12

    Classes

    ACLASS is a user defined type which encapsulates

    the representation of the type

    the operations which access or update objects of that type

    operations for creating and deleting objects

    A class is a structure that can have functions as membersas well as data elements

    The functions are called:

    member functions

    or member methods

  • 8/2/2019 OOEDP_wk1_L1

    20/31

  • 8/2/2019 OOEDP_wk1_L1

    21/31

  • 8/2/2019 OOEDP_wk1_L1

    22/31

    OOEDP Week 1 Lecture 1 22April 12

    Person

    Person

    name

    age

    setName

    setAge

    getName

    getAge

    displayDetails

    attributes

    member variables

    member methods

    member functions

  • 8/2/2019 OOEDP_wk1_L1

    23/31

    OOEDP Week 1 Lecture 1 23April 12

    Person

    setName setAge

    getName getAge

    displayDetails

    name

    age

  • 8/2/2019 OOEDP_wk1_L1

    24/31

    OOEDP Week 1 Lecture 1 24April 12

    Classes in Java

    public class Person {

    String name;

    int age;

    public void setName (String theName) {name = theName;}

    public void setAge (int theAge) {age = theAge;}

    public String getName () {return name;}

    public int getAge () {return age;}

    public void displayDetails ()

    { System.out.println("Name is : " + name );System.out.println("Age is : " + age );

    }

    }

  • 8/2/2019 OOEDP_wk1_L1

    25/31

    OOEDP Week 1 Lecture 1 25April 12

    Creating an Object

    An object is an instance of a class

    The tutor is an example of a person

    has a name

    has an age

    In Java an object is constructed by instantiating a class:

    construct an object

    an instance of the class

    Person theTutor = new Person( );

    the class

    a reference to

    the object

  • 8/2/2019 OOEDP_wk1_L1

    26/31

    OOEDP Week 1 Lecture 1 26April 12

    Using the Methods of an Object

    The member functions of a class can be called against aninstance of the class:

    theTutor.setName (Pete) ;

    theTutor.setAge (53) ;

    theTutor.displayDetails ( ) ;

    This is often described as sending a message to the object

    asking the object to do something

    the object itself knows how to do what is asked

  • 8/2/2019 OOEDP_wk1_L1

    27/31

    OOEDP Week 1 Lecture 1 27April 12

    The Heap and Garbage Collection

    All objects in Java are created on the heap

    Space is allocated dynamically using the keyword new

    Consider the following code:

    Person thePerson; The variable thePerson can hold a reference to an object

    of type Person

    Initially it is a null reference

    An object can be constructed on the heap using new

    thePerson = new Person ( );

    A constructor is invoked when new is used

    Constructs the object

  • 8/2/2019 OOEDP_wk1_L1

    28/31

    OOEDP Week 1 Lecture 1 28April 12

    Testing a Class

    One way to test a class is to build a simple driver class

    The driver contains a main

    In the main method the class to be tested is instantiated

    Then each of the methods of the class are invoked

    Why can the driver class method main be used withoutinstantiating the driver class?

    In the driver main is:

    public static void main (String [] args)

    Methods that are static are at the class level and can beinvoked without instantiating the class

  • 8/2/2019 OOEDP_wk1_L1

    29/31

    OOEDP Week 1 Lecture 1 29April 12

    Testing the class

    public class TestPerson {

    public static void main(String []args)

    {

    System.out.println("Testing Person class");

    System.out.println("---------------------------------------");

    // instantiating the class

    // constructing an object of type Person

    Person theTutor = new Person();

    // using the set methodstheTutor.setName ("Pete") ;

    theTutor.setAge (53) ;

  • 8/2/2019 OOEDP_wk1_L1

    30/31

    OOEDP Week 1 Lecture 1 30April 12

    Testing the class (continued)

    System.out.println("Testing displayDetails "+

    " name Pete age 53 expected");

    theTutor.displayDetails () ;

    System.out.println("Testing getName - Pete expected");

    System.out.println(theTutor.getName( ));

    System.out.println("Testing getAge - 53 expected");

    System.out.println(theTutor.getAge( ));

    }

    }

  • 8/2/2019 OOEDP_wk1_L1

    31/31

    OOEDP Week 1 Lecture 1 31April 12

    Summary

    In this lecture we have:

    Discussed the module organisation

    Introduced why object-orientation is desirable

    Reviewed the basic object oriented concepts Defined classes, created and used instances of classes

    Examined the structure and syntax of a simple Javaprogram using classes