Docstoc

M255 Object-oriented programming with Java - PowerPoint

Document Sample
M255 Object-oriented programming with Java - PowerPoint Powered By Docstoc
					                         M255
                  UNDERGRADUATE COMPUTING



               Object-oriented
            programming with Java

Prepared By: Prof. Dr. Shehab Gamalel-Din



                                            1
              Course structure
• 8 credits / 1 semester.

• Four blocks of text comprising 14 units of study
  material.

• Units are of unequal lengths. Study Calendar will
  indicate the time required for each unit.

• Two TMAs (20%), one quiz (30%) and a final(50%).

                                                      2
             Course materials
• Course Guide, Software Guide and Study
  Calendar.

• Fourteen units of study texts, incorporating
  study instructions, SAQs and practical
  activities.

• Course software that will include the IDE.

                                                 3
             Course materials
• Glossary - a description of all the technical and
  programming terms used in the course.

• Index, which provides references to the main
  topics in the course.

• TMAs, Specimen Examination Paper and
  Solutions.

                                                      4
             Course software
• A Java 1.5 development kit and a Java IDE
  (BlueJ 2.0.5) are provided on CD-ROM.

• Practical activities are to be carried out in
  order to consolidate the understanding of the
  material.

• Some of the files needed for practical
  activities are supplied on CD-ROM.
                                                  5
        Prerequisite knowledge
• Basic computer skills:
  – Basic components of a computer.
  – Accessing the internet.
  – Word-processing and installing software.


• Basic programming concepts:
  – Sequence, selection, iteration.
  – Data types and assignment statements.

                                               6
      What the course is about
• Constructing computer systems out of units of
  software (objects).

• How objects interact (to form a program) by
  sending messages to each other.

• How objects can be reused and interchanged
  between programs, so increasing programming
  productivity.
                                                  7
       What the course is about
• The programming language we will use is Java.

• Students will certainly learn quite a lot of
  Java, and write lots of program code.

• But the focus is on the fundamentals of OOP
  rather than the ins and outs of the Java
  language.

                                                  8
                    Aims
• The course aims to provide an understanding
  of the fundamental concepts involved in OOP.
  – Object
  – Class
  – Protocol
  – Inheritance
  – Encapsulation
  – Polymorphism
  – Collaboration
                                                 9
             Block I (units 1-4)
• Fundamental ideas of object-oriented
  programming.

• Understand and write simple object-oriented
  code.

• An introduction to basic object-oriented
  concepts (attribute, state, protocol, class and
  subclass).
                                                    10
            Block I (units 1-4)
• Extending existing classes and writing code for
  simple methods using the Java IDE BlueJ.

• Test the code using the OUWorkspace, a
  programming tool integrated into the BlueJ
  IDE.

• OUWorkspace allows us to write and test
  snippets of Java code in a quick and convenient
  way.                                              11
            Block II (units 5-8)
• Continues the fundamental object-oriented
  ideas, using the BlueJ IDE and the
  OUWorkspace.

• Investigate inheritance hierarchies, overriding,
  abstract classes, interfaces, polymorphism,
  reuse of code, static methods and variables.

• Creating our own classes, and find out more
  about the library classes provided by Java.        12
            Block II (units 5-8)
• Learn about different kinds of errors and also
  about debugging using the IDE.

• Understand more about the need to design
  code, and to implement a class according to a
  specification.




                                                   13
          Block III (units 9-11)
• Introduces the concept of collections (objects
  that store other objects) and related issues.

• Carry out increasingly complex programming
  exercises.

• The importance of reuse and how to select an
  appropriate collection class from Java’s
  Collections Framework.
                                                   14
         Block IV (units 12-14)
• Discussing how to get information into a Java
  program from an external source (such as a
  file) and how to write information to an
  external source.

• How an object can be saved to a file from a
  running program, and at a later stage be read
  back into the program.

                                                  15
          Block IV (units 12-14)
• Testing code in an object-oriented context.

• Putting the ideas of testing into practice using a
  testing framework built into BlueJ called JUnit.

• Placing programming within the wider context
  of the software development process.


                                                       16
                         M255

                   Object-oriented
                programming with Java


                      Unit 1
                    Introduction


Prepared By: Prof. Dr. Shehab Gamalel-Din
                                            17
           Unit 1

Introduction and Basic Concepts:
   Fundamental hardware and
       software concepts


                                   18
                    Unit 1
                Introduction
• The emphasis of the course is on object-
  oriented programming.

• Programming in an object-oriented language
  is more than just learning new syntax rules.

• It requires a new way of thinking.


                                                 19
 Fundamental hardware and software
             concepts
• Differences between hardware and software.

• Categorize software as:
  – Systems
     •   Computer system
     •   Software system
     •   Systems software
     •   Operating system
  – Applications
  – Programs
     • input data – process data – output data
                                                 20
                 Computer Systems
Hardware is the physical devices but Software is the collection of
Programs that allows the hardware to do its job.




                                                                     21
               Computer Hardware




-Memory is the storage area where data and programs are stored
-ALU is where calculations and logical operations take place.
-CU controls the operations of memory, ALU and I/O devices
-Input subsystem accepts input and the program from outside.
-Output subsystem sends the result of processing to outside.
- Is the hard disk an input or output device?
                                                                 22
Computer Hardware




     peripherals


                    23
                       Software
• Software
  – A general term for software objects; the term can refer to
    a single object or to a collection of integrated,
    interdependent objects (which together make an
    application), or to collections of more or less unrelated
    objects.
• Software System
  – A logical representation of a physical system.
  – Software that simulates some human enterprise and is
    intended to run for ever, responding to events in often
    complex ways.
  – the heart of the reservation system is intended to run for
    24 hours a day, forever, and to provide real-time access to
    the database that identifies the available seats on relevant
    flights.
                                                                   24
                Software Systems
• Software Component
   – A software component is a piece of software that can be
     combined with other pieces to construct a program or
     system. An object is a component.
• Program
   – A collection of integrated, interdependent software
     components that have been put together for a specific
     purpose.
• Application
   – A program or system designed to help in some human
     task.
• A System
   – Software may be composed of many applications.            25
             Programs & Applications
• Program
   – A collection of integrated, interdependent software
     components that have been put together for a specific
     purpose.
• a program assumes a pattern of: input data – process
  data – output data.
   – the program has a starting point at which it takes some input,
     it then performs whatever computation is needed, and it has
     an end point at which output is given and the software ceases
     to run. This contrasts with systems, which run forever.
• a system might well call upon the services of a program
  (via the operating system) to accomplish some simple
  task
   – For example, one (very simple) program might display the
     numbers 1 to 10 on your computer screen in quick succession.     26
                Applications
• Applications differ from systems in that they
  are not designed to run forever and they
  generally run on a single computer and
  perform a single task for a single user.
  – For example, a word processor that resides on a
    personal computer can only be used by the user
    of that computer and its sole purpose is the
    production of text documents.
• Note that the terms program and application
  are often used synonymously.

                                                      27
              Systems Software
• System software is defined as ‘software that helps
  the computer carry out its basic operating tasks’.

• It implies that system software is at a very low level:
  this means ‘very close to the machine’, in that it is
  intimately linked to the precise components and
  structure of the computer.
• So, system software is concerned with locations in
  memory, registers in chips, and so on.
• The most important system software is the
  Operating System

                                                            28
            Systems Software
• The operating system that controls the
  execution of other programs;
• The user interface software such as graphical
  windows and menus systems or text based
  command line interpreters.
• Development tools, such as compilers, for
  building other programs.
• Utility programs (involved for example in
  sending data to a printer or communicating
  with peripheral devices).
                                                  29
            Operating System
An operating system is an
  interface between the
 hardware of a computer
            and
   the user (program or
human) that facilitates the
  execution of the other
 programs and the access
to hardware and software
   resources efficiently.
                               30
            Operating Systems
• Generally, operating systems are designed for one
  particular platform and cannot be moved to another
  platform.
• This, of course, raises the question of portability.
• Operating Systems execute not as a high-level
  language source code but as a low-level machine
  code compiled for a specific computer architecture
  or platform.
• Much systems software (including operating
  systems) is written in either a very low-level
  language, or a high-level language which uses
  facilities that are low level.

                                                         31
       Operating System’s Tasks
• Management of memory
• Coordination and control of peripheral devices
• Scheduling of access to the processor
• Provision of an interface between
  applications/programs and hardware
• Provision of basic utilities
• Provision of a user interface


                                                   32
                 Memory Management
• Memory
   – Both data and programs are stored in RAM before a
     program can execute (run).
   – Both the data and programs should have the same format
     because they are stored in memory as binary pattern (
     sequence of 0s & 1’s).
• Physical memory, or RAM, has a very fast
  access time compared with a hard disk. So, in
  the execution of software, the more RAM a
  computer has, generally the faster it can
  process data and, hence, run applications.

• It is the job of the operating system to allocate
  an appropriately sized area of memory to each
  program (or application), and to ensure that
  program instructions and data do not interfere
  with each other, or with the data and
  instructions of other programs.
                                                              33
   Coordination and control of peripheral
                  devices
• A peripheral device: is any
  component of the computer that is
  not part of the essential computer
  (i.e. the CPU and main memory) and
  it is type of the hardware system
  example: input , output devices

• In order to carry out its tasks a
  computer may need to
  communicate with one or more
  peripheral devices. The operating
  system coordinates these
  operations, ensuring that data is
  moved safely and efficiently
  between the different components
  of the system.                            34
 Scheduling of access to the processor
• The operating system manages access to the
  processor, by prioritizing jobs to be run and
  ensuring that the processor is used efficiently.
  – Round Robin strategy
  – For example, if the currently running program
    finishes, or is interrupted in order to wait for data
    from the hard disk, the operating system will
    ensure, if possible, that another program is given
    access to the processor.

                                                            35
   Provision of an interface between
  applications/programs and hardware
• provides a stable, consistent way for software to
  communicate with the computer’s hardware without having
  to access that hardware directly, or know about the details of
  the hardware.
• provides an application programming interface (API), which is
  a set of high-level instructions (a protocol) through which an
  application can ‘talk’ to the operating system to request
  services such as printing a file or saving a file to disk.
   – E.g., to print a file, a program asks the operating system (through the
     API) to print the file. The operating system then communicates with
     the printer to carry out the request.
   – This abstraction allows a software developer to write an application
     that is more portable and device independent.

                                                                               36
         Operating System’s Tasks
• Provision of basic utilities
   – it only acts on the disks, files or printers (or on some other
     hardware devices) without providing further output
   – E.g., disk formatting facilities, file management systems
     and software installation wizards.
• Provision of a user interface
   – Enables users to communicate with the computer. It
     provides a means of inputting data and instructions, and
     presents output in an understandable way.
   – Command lines: DOS shell or GUI interfaces


                                                                      37
         Booting your Program
• The Boot Program is a firmware that is stored
  into the ROM memory chip during
  manufacture and is permanent.
• The boot process runs in the following order:
     • Apply power to computer system.
     • Activates the instructions stored in ROM (Read Only
       Memory):
        – Performing some self-tests on the computer.
        – Loading the operating system from hard disk into RAM.
     • Operating system takes over.

                                                                  38
How programs execute on a computer
• When you write a piece of software you express your
  ideas as text in a high-level programming language.
  This text is called source code.
• For your ideas to be implemented as software, your
  text – words and symbols – is translated into
  executable bits (machine language).
• In practice, you write your software source code with
  the assumption that it will be translated and
  executed, and you think in the language in which you
  are writing, not in terms of the executable bits.

                                                          39
                  The Run-Time System
• When writing software, it is necessary to express a solution to a
  problem in a programming language resembling a limited natural
  language (high-level language) that can be understood and
  interpreted by human beings.

• You write your solution (program) using a piece of text according to
  the high level language (e.g., Java). This text is called source code.

• That source code must be translated into a primitive language
  called machine code (low-level language.)
   – in effect, the bits that can be understood and executed by the hardware of a
     computer.

• Translation of the high-level language source code to a low-level
  machine code is carried out by a piece of software called a
  compiler.
                                                                                40
              The Run-Time System




• During compilation a compiler must first check that the text
  conforms to the syntax rules of the language, that is, it is
  properly formed.
• Only if this check does not show up problems, does the
  compiler proceed to produce the machine code which will be
  executed.
                                                                 41
           The Run-Time System
• Typically, compilers will also generate some
  additional information not given in the source code.

• This extra code is called the run-time system. It
  enables the machine code to be executed at run
  time, that is, at the time that a request for execution
  is made.

• This run-time system is usually specific to a particular
  computer system, that is to a particular combination
  of hardware and software.
                                                             42
                The Run-Time System
  • The major problem with this simple model of compilation is that the
    compiled code is not portable to other machine architectures, as different
    machine types employ different machine codes.




• If you wish to move your software onto another architecture, for example from
  a PC to a Macintosh, you would have to recompile the high-level language
  source code with a Macintosh-specific compiler to produce Macintosh
  machine code.                                                                   43
             The Run-Time System
• Another more portable model of compilation makes use of a
  special layer of software (on each real machine architecture)
  called a Virtual Machine (VM).

• In this model of compilation, the high-level language source
  code is compiled not to some architecture-dependent machine
  code, but to an intermediate code; that is, to the machine
  code of the notional (virtual) machine.

• In some programming environments virtual machine code is
  usually called bytecode.

• Using this intermediate code approach allows low-level code
  to be moved unchanged between different computer systems
                                                                  44
The Run-Time System




Compiling for Virtual Machine   45
              The Run-Time System
•   Once the intermediate virtual machine code has been
    produced, there are three options for execution of the
    software (now in intermediate code) on a real computer:
    1. The first is to include an interpreter (a piece of software) within the
       virtual machine. Every time an application is run, the interpreter
       takes the intermediate virtual machine code and translates each
       notional instruction, one at a time, into the real instructions for the
       real computer hardware to execute (Slow but Portable).

    2. The second option is for the virtual machine to include another phase
       of code generation in which all the intermediate code for a piece of
       software is translated into the real machine code in one go, so that it
       is ready for the real machine hardware to execute. (Very Slow)




                                                                                 46
          The Run-Time System
3. The third option is a combination of the first two and is
   now called dynamic compilation. When a request is made
   for a method to be accepted, the environment’s built-in
   compiler produces an intermediate code (bytecode) for
   that method. This is then compiled into machine code by
   the virtual machine software when the method is first
   executed, and this real machine code is stored for
   subsequent executions. Therefore subsequent execution
   of that method has all the speed that results from simple
   compilation. (Of course, any method that is never
   executed will never be translated into machine code.)

– This is the option used by the Java Virtual machine (JVM)
  and in BlueJ environment used in this course.
                                                               47
The computer as a layered device




                                   48
                  Object Technology
• Procedural programming.

• Procedural programming is
so called because the program
code gives a step-by-step
procedure

• Designing a procedural
program the programmer will
usually break down the problem
in a top-down manner

•The main program usually calls
subprograms (usually called
functions or procedures) which
in turn might call other
subprograms                           49
              Object Technology
• Procedural programming
  – Often data is placed into separate structures (called data
    structures) which are global to the whole program so it is
    visible and accessible to every function or procedure in the
    program, each of which will be able to change that data.




                                                                   50
            Object Technology
• Object-oriented programming:




                                 51
      Classes , objects and protocol
• Classes.

• Instances.
  – Understand the same messages.
  – Respond in the same way to each message.
  – Have the same attributes.


• Exploring objects in microworld.
  – Activities. (Amphibian worlds: two frogs)
                                                52
            The Object Technology
• In object-oriented programming, all the processing
  carried out by a computer system is considered to
  be done by objects.
• These objects carry out their
  work by sending “messages”
  to each other.

• Think of each object as an imaginary (virtual)
  computer made of software with a built-in memory, a
  unique identity and the capacity to carry out some
  tasks.                                                53
              Object Technology
• Object-oriented
  programming:
  – Objects
  – Attributes
  – Behaviour
  – State
  – Message-send
  – Message answer


                                  54
            Object oriented terms
• The “Object” is used to model the real life
• “Application domain” or “Model domain” is the application
  area that needs to be simulated
• Each object send “messages” to other objects (or possibly
  to itself)
• Each message has a “message answer”
• An “attribute” is some property or characteristic of an
  object, so a patient object might have attributes such as
  condition, date admitted, medication and so on.
• The “attribute value” of ‘condition’ might be ‘malaria’ and
  the attribute value of ‘date admitted’ might be ‘3/1/2006’.
• The “behavior” of an object is the collection of actions an
  object knows how to carry out.
• To summaries: An object is an entity that has both
  attributes and behavior
                                                                55
              Attributes and state
• Attribute: characters
  that specify each object
• Each object has its own
  attribute
• All objects of the same
  class have the same
  attributes” but probably
  with different values



                                     56
                        Messages
• A Message is the only way to
  get an object to interact to
  other objects and/or do
  something
• All objects of the same class
  have the same messages
• All objects of the same class
  respond to the same set of
  messages (protocol) in the
  same way
• Objects of different classes
  may respond to the same
  message in the same or a
  different way
• Credit() ,
  debit(),setCreditLimit() are
  examples for such messages
                                   57
              Using StarOffice
 Use the StarOffice to draw a rectangular
 Change it’s color
 A rectangle’s state is changed.
 The drawing pane is sent a message saying the
  rectangle object has changed the value of one of its
  attributes.
 The drawing pane sends a message to the rectangle
  asking for details of the change.
 The drawing pane uses the information it gets back to
  redraw the rectangle showing the altered
  appearance.
 The rectangle object is responsible for knowing about
  its state                                               58
 Exploring objects in a microworld

• Microworld is the
  application that
  developed by the ou
  team
• Two objects called frog1
  and frog2 are created and
  shown in the microworld
  user interface
• You can send message to
  each object such as left(),
  right() and see the effect
  of each message


                                     59
                                          59
          Variables and messages
• Variable is the way that
  you refer to the object
   – frog1 and frog2 are the
     variable names to refer to
     the object appeared in the
     graphical user interface

• Message is the piece of
  code sent to the object to
  change its state or to get
  information from the
  object



                                   60
                   Messages
• You can send a message
  to the object either by
  clicking on the message
  button or by writing a
  piece of code in the
  code pane
• Writing the code
Frog1.left() causes the
  frog1 object to be
  moved one step to the
  left

                              61
            The Inspect message
• Sending the message
  inspect to the object
  causes the inspector
  page to appear that
  shows the attributes
  with their values




                                  62
         Grouping objects into classes
• frog1 and frog2 are            • The list of messages to
  belong to the same class         which any instance of the
• The two frog objects in          Frog class can respond is
  the microworld have              called its protocol.
  been created as instances      • The protocol of a Frog
  of the Frog class                object, as we know it so
• So two different objects         far, is left( ), right( ),
  that belong to the same          home( ), jump( ), green( ),
  class and are referenced         brown( ) and croak( ).
  by the variables frog1 and
  frog2:
    understand the same
     messages;
    respond in the same way
     to each message;
    have the same attributes.
                                                                 63
        Attributes of frog objects

• The only attributes a
  Frog object has in the
  microworld you have
  been exploring are:
  color and position.
• Some message don’t
  alter the object state
  such as jump()


                                     64
           Finally:

You need to read the summary and
   the glossary, and to solve the
             activities.


                                    65

				
DOCUMENT INFO