Docstoc

Programming

Document Sample
Programming Powered By Docstoc
					                                                       Manufacturing
Aalborg University – Department of Production            Information
                                                             Systems
                                                              Group




                         Kursus:
               Programmering

                     Kaj A. Jørgensen
       Aalborg Universitet - Institut for Produktion




2006.09.04            Kursus: Programmering               1
                                                                     Manufacturing
Aalborg University – Department of Production                          Information
                                                                           Systems
                                                                            Group


Kursus: Programmering - Introduktion
 • Mål og indhold i kurset
     •   Kursusbeskrivelse: http://www.iprod.aau.dk/~kaj
     •   Programmeringssproget Java
     •   Lærebog: Objektorienteret programmering i Java
     •   Forelæsninger og opgaveregning - deltagerindsatsen er den
         vigtigste
 • Evalueringsmetode
     • Mundtlig evaluering gruppevis på basis af afleverede
       opgaveløsninger
 • Java Software Development Kit (SDK) fra Sun Microsystems
 • Java udviklingsværktøjer
     • JDeveloper, NetBeans, Jbuilder
     • Andre: RealJ, Jcreator,

2006.09.04                 Kursus: Programmering                        2
                                                   Manufacturing
Aalborg University – Department of Production        Information
                                                         Systems
                                                          Group


Eksamensopgave - indholdsskitse
• Grundlæggende programmering (ca. 20%)
     • Forklar kort …
•   Objekt og klasse (ca. 30%)
     • Erklæring af objekter …
     • Objekt- og klassediagrammer …
•   Klassifikation, arv (ca. 30%)
     • Extends, super, …
     • Interface, implements …
•   Hændelsesprogrammering (ca. 20%)
     • Hændelser og lyttere …

2006.09.04                 Kursus: Programmering      3
                                                              Manufacturing
Aalborg University – Department of Production                   Information
                                                                    Systems
                                                                     Group


Hvad er et program
• Eksempler
     • Vejvisning
     • Madopskrifter
     • Strikkeopskrifter
     • Programinstallering
     • Computeropsætning
     • Beskrivelse af en boreproces
•   Definition (uformel)
     • Et program er en identifikation af et antal handlinger og
       en præcis anvisning af en rækkefølge for deres udførelse


2006.09.04                 Kursus: Programmering                   4
                                                                                              Manufacturing
Aalborg University – Department of Production
 Binary Choice
                                        End-Controlled                                          Information
                                        OPERATION          Loop
                                                                              PI1                   Systems
                                        STRUCTURES
                B
                                                                                                     Group
                                                                              PI2
                                       Simple Sequence
       True          False

                                             O1

    O1                       O2                                               PIM

                                             O2
                                                                  False
                                                                              BI
                                                                                    True


M ulti Choice                                              Begin-Controlled
                W                                          Loop                              False
                                             Oi                                 B

                                                                                      True

                                                                               PI1


                                            On                                PI2
  O1            O2            On



                                                                              PIM



2006.09.04                         Kursus: Programmering                                             5
                                                     Manufacturing
Aalborg University – Department of Production          Information
                                                           Systems
                                                            Group


Operation Structures
Simple sequence:

             O1                                 O1

             O2                                 O2

                                    Simple
             O3                     Sequence    O3




             On                                 On



2006.09.04            Kursus: Programmering             6
                                                          Manufacturing
Aalborg University – Department of Production               Information
                                                                Systems
                                                                 Group


Operation Structures
Binary choice:



             Logical
              Expr.                                  O1
                                            Binary
                                            Choice
     O1                O2                            O2




2006.09.04                  Kursus: Programmering            7
                                                        Manufacturing
Aalborg University – Department of Production             Information
                                                              Systems
                                                               Group


Operation Structures
Multi choice:


                                                       O1
                 Multi
                Choice                                 O2

                                              Multi
                                              Choice
     O1      O2      On          O0                    On



                                                       O0


2006.09.04                Kursus: Programmering             8
                                                     Manufacturing
Aalborg University – Department of Production          Information
                                                           Systems
                                                            Group


Operation Structures
Begin-controlled loop:

             Logical                            O1
              expr.

              O1
                               Begin-
                                                O2
              O2               Controled
                               Loop             O3
              O3




              On                                On



2006.09.04             Kursus: Programmering            9
                                                     Manufacturing
Aalborg University – Department of Production          Information
                                                           Systems
                                                            Group


Operation Structures
End-controlled loop:

                                                O1
              O1

              O2                End-
                                                O2
                                Controled
              O3                Loop            O3



              On
                                                On
             Logical
              expr.




2006.09.04             Kursus: Programmering           10
                                                     Manufacturing
Aalborg University – Department of Production          Information
                                                           Systems
                                                            Group


Operation Structures
Middle-out controlled loop:

                                                O1
              O1
                                                O2
                               Middle-Out

                               Controled Loop
                                                O3
             Logical
              expr.




                                                On

              On




2006.09.04             Kursus: Programmering           11
                                                     Manufacturing
Aalborg University – Department of Production          Information
                                                           Systems
                                                            Group


Operation Structures
Subrange-controlled loop:

             Subr.                              O1
             expr.

             O1
                               Subrange
                                                O2
             O2                Controled
                               Loop             O3
             O3




             On                                 On




2006.09.04            Kursus: Programmering            12
                                                                Manufacturing
Aalborg University – Department of Production                     Information
                                                                      Systems
                                                                       Group


Operation Structures – Overview
                                              Begin -
                                              Controlled Loop




             Binary                           End -
             Choice                           Controlled Loop



                            Simple
                            Sequence


             Multi                            Middle - Out
             Choice
                                              Controlled Loop




                                              Subrange -
                                              Controlled
                                              Loop



2006.09.04            Kursus: Programmering                       13
                                                                             Manufacturing
Aalborg University – Department of Production                                  Information
                                                                                   Systems
                                                                                    Group


Operation Structures – Diagram Example
                            Compute
                Read         n_ok?
                size n
                                                      Write
                                                      "n is
                                                       even"
                                         Even ?
                             Not
                             n_ok?                    Write
       Magic    Check                                 "out of
       Square     n                                    range"



                                        Initialise
                                                        Init.
                                                      variable
                n_ok?
                            Legal       Compute                     New
                            Square      Square                      field
                                                      Key<max
                                                     Go through
                                                      all fields
                                         Write
                                         Square                    Compute
                                                                    field




2006.09.04               Kursus: Programmering                                 14
                                                             Manufacturing
Aalborg University – Department of Production                  Information
                                                                   Systems
                                                                    Group

Operation Structures – Identification of Methods

                       Read
                       size n




             Magic     Check
             Square      n
                                                Initialise




                                      Legal     Compute
                                      Square    Square
                       n_ok?



                                                 Write
                                                 Square




2006.09.04            Kursus: Programmering                    15
                                                           Manufacturing
Aalborg University – Department of Production                Information
                                                                 Systems
                                                                  Group

Operation Structures – Two Separate Methods
                         Compute
                          n_ok?

                                                 Write
                                                 "n is
               Check                              even"
                 n
                                       Even ?
                           Not
                           n_ok?                 Write
                                                 "out of
                                                  range"




                           Init.
                         variable


              Compute                   New
                                        field
              Square
                         Key<max
                         Go through
                          all fields

                                       Compute
                                        field




2006.09.04              Kursus: Programmering                16
                                                                                Manufacturing
Aalborg University – Department of Production                                     Information
                                                                                      Systems
                                                                                       Group

Operation Structures – Improved Example
                  Read
                  size n


                                           Initialise

                                                           Init.
                                                         variable
                 Compute
                  n_ok?        Legal                                   New
                                           Compute
                               Square      Square                      field

        Magic                                            Key<max
        Square                                          Go through
                                                         all fields
                                            Write
                                            Square                    Compute
                  n_ok?                                                field




                                             Write
                                             "n is
                                              even"
                                Even ?

                                             Write
                                             "out of
                                              range"



2006.09.04                 Kursus: Programmering                                  17
                                                             Manufacturing
    Aalborg University – Department of Production              Information
                                                                   Systems
                                                                    Group


    Data Modelling Fundamentals
•   Objects (the software view)
     • a software component containing
         • attributes
                 – variables
                 – methods – procedures and functions
         • structures of sub objects (not shown)
     • encapsulation - attributes can be
         • public – visible for other objects
         • private – invisible for other objects
     • each object is allocated in memory
         • with start address and some amount of space
     • an object containing a method with name “main” is a program

    2006.09.04                   Kursus: Programmering         18
                                                                Manufacturing
    Aalborg University – Department of Production                 Information
                                                                      Systems
                                                                       Group


    Data Modelling Fundamentals, cont.
•   Object Types (Java: classes)
      • Object type = template for objects
      • Object = instance of an object type
      • Constructor methods can be defined for initialisation
      • Standard object types - libraries, e.g. string, array
      • Special object types can be designed: class { ... }
      • The new operator
          •   creates a new object of a specified type
          •   start address is defined
          •   necessary space is allocated
          •   constructor method is invoked

    2006.09.04                  Kursus: Programmering             19
                                                                  Manufacturing
    Aalborg University – Department of Production                   Information
                                                                        Systems
                                                                         Group


    Data Modelling Fundamentals, cont.
•   References
     • References are declared <class name> <reference name>
         • Java example: Random R;
     • A reference can contain an address value, i.e. of an object
     • Usually initialised when new objects are created
         • Java statement: <reference> = new <class name>(...);
                 – example: R = new Random();
     • Java dot notation: <reference name>.<attribute name>
         • example: int i = R.nextInt();
     • Reference values can be compared and copied
     • Special Java value: null – referring to nothing
         • Java boolean expression: R == null (yes or no?)
    2006.09.04                Kursus: Programmering                  20
                                                                    Manufacturing
  Aalborg University – Department of Production                       Information
                                                                          Systems
                                                                           Group


 Data Modelling Fundamentals, cont.
• References to Java strings and arrays
   • String reference, e.g.              String hey;
   • Assign address value of string constant to reference:
       hey = ”Hi there.”;

                Reference variable                  String object

                                                      String
              Hey

                                                      Hi there.



   • Array reference, e.g.              int[][] ChessBoard;
   • Assign address value of new object to reference:
        ChessBoard = new int[8][8];
 2006.09.04                          Kursus: Programmering            21
                                                                Manufacturing
Aalborg University – Department of Production                     Information
                                                                      Systems
                                                                       Group


Data Modelling Fundamentals, cont.
      • Object type definition in Java (only data attributes)
         class SimpleCDdata
         {
             String Title;
             int Year;
         }
      • Instance of Object type
          SimpleCDdata CD = new SimpleCDdata();
      • Attribute assignment
         CD.Title = ”I love you”;
         CD.Year = 1970;



2006.09.04               Kursus: Programmering                    22
                                                     Manufacturing
Aalborg University – Department of Production          Information
                                                           Systems
                                                            Group


Data Modelling Fundamentals, cont.
    Definition of object type with methods:
    public class CDdata
    {
       private String Title;         private int Year;
       public CDdata (String t, int y)
       {
            Title = t;      Year = y;
       }
       public void setTitle (String t) { Title = t; }
       public String getTitle () { return Title; }
       . . .
    }
2006.09.04            Kursus: Programmering              23
                                                                                Manufacturing
Aalborg University – Department of Production                                     Information
                                                                                      Systems
                                                                                       Group


Data Modelling Fundamentals, cont.
 • Collections
     • Oriented relationship between two object types
     • Symbol in type diagrams: double-headed arrow
     • Anchor type and body type
     • Identified by a special attribute in the anchor type
     • One-to-many relationships
     • Internal organisation - information structure:
             • static structure - array
             • dynamic/linked structures – standard structures: lists, trees,
               networks


        CDset                         CDdata
                                                                 ??

                    Object types                           Objects

2006.09.04                         Kursus: Programmering                          24
                                                    Manufacturing
Aalborg University – Department of Production         Information
                                                          Systems
                                                           Group


Linked Data Structures
•   Characteristics - Efficiency
     • Insert and remove operations
     • Retrieval operations – Search trees
•   Standard structures
     • List structures
        • Single-linked
        • Double-linked
     • Tree structures
        • Binary trees
        • Multi-way trees
     • Networks

2006.09.04                  Kursus: Programmering     25
                                                                  Manufacturing
Aalborg University – Department of Production                       Information
                                                                        Systems
                                                                         Group


Example: The Stack Data Structure
             CDstack


                                Stack
                               top   size

                                        2



                 Node                              Node
                data   link                       data   link




               CD-Data                           CD-Data
             Title: Kiss me                   Title: I love you
             Year: 1970                       Year: 1996

2006.09.04                    Kursus: Programmering                 26
                                                                                                 Manufacturing
Aalborg University – Department of Production                                                      Information
                                                                                                       Systems
                                                                                                        Group


Example: The Queue Data Structure

                            CDQueue                Queue

                                           front    size     rear




                     Node                           Node                         Node

             data              link        data              link       data              link




                    CD-Data                        CD-Data                     CD-Data

         Title: Ace Of Base            Title: Bob Marley           Title: Elton John
         Year: 1996                    Year: 1993                  Year: 2000




2006.09.04                            Kursus: Programmering                                        27
                                                                                                                    Manufacturing
         Aalborg University – Department of Production                                                                Information
                                                                                                                          Systems
                                                                                                                           Group


         Example: The Binary Tree Data Structure
CDTree
           BinaryTree

           top   now                                                                                    CD-Data

                                                   BinTreeNode                                Title: Ace Of Base
                                                                                              Year: 1996
                                              LeftChild      RightChild
                                                          Data

                                                                                                        CD-Data

                                                                                              Title: Bob Marley
                                                                                              Year: 1993



                        BinTreeNode                                   BinTreeNode
                                                                                                        CD-Data
                  LeftChild      RightChild                      LeftChild      RightChild
                              Data                                           Data             Title: Elton John
                                                                                              Year: 2000




         2006.09.04                                Kursus: Programmering                                              28
                                                       Manufacturing
Aalborg University – Department of Production            Information
                                                             Systems
                                                              Group


Classification of Object Types

•   Sample taxonomy: Object Types with Attributes
     • CompactDisc (Owner)
        • MusicCD (Artist, AlbumTitle, RecordedYear)
        • FilmCD (FilmTitle, Director, PremiereYear)
        • DataCD (SpaceUsed)
             – SystemCD (SystemName, SoftwareMaker)
             – UserCD (Content, CreatedDate)




2006.09.04               Kursus: Programmering           29
                                                         Manufacturing
 Aalborg University – Department of Production             Information
                                                               Systems
                                                                Group

Classification of Object Types, cont.
     • Objects - object types - attributes - values
     • Identification - definition - specification
     • Classification
         • generalisation
         • specialisation
     • Attributes: name - type / data - method
     • Taxonomy: super-types / sub-types - inheritance
     • [Multiple inheritance]




 2006.09.04                 Kursus: Programmering          30
                                                   Manufacturing
Aalborg University – Department of Production        Information
                                                         Systems
                                                          Group


Classification of Object Types, cont.
• Definition of Object Types based on Supertypes
    class CompactDisc extends Object
    {
        String Owner;
    }


    class MusicCD extends CompactDisc
    {
        String Artist, AlbumTitle;
        int   RecordedYear;
    }
2006.09.04              Kursus: Programmering        31
                                                 Manufacturing
Aalborg University – Department of Production      Information
                                                       Systems
                                                        Group


Classification of Object Types, cont.
 •   Definition of Object Types based on
     Supertypes, cont.
      class DataCD extends CompactDisc
      {
             long SpacesUsed;
      }


      class SystemCD extends DataCD
      {
             String SystemName, SoftwareMaker;
      }
2006.09.04               Kursus: Programmering     32
                                                  Manufacturing
Aalborg University – Department of Production       Information
                                                        Systems
                                                         Group


Classification of Object Types, cont.
• Definition of Constructor
    class CompactDisc extends Object
    {
        String Owner;
        CompactDisc( String o )
        {
             Owner = o;
        }
    }



2006.09.04                Kursus: Programmering     33
                                                   Manufacturing
 Aalborg University – Department of Production       Information
                                                         Systems
                                                          Group


Classification of Object Types, cont.
• Superconstructing
   class FilmCD extends CompactDisc
   {
      String FilmTitle, Director;
      int    PremiereYear;
      FilmCD ( String o, String f, String d, int p )
      {
         super(o);
         FilmTitle = f;
         Director = d;
         PremiereYear = p;
      }
   }
2006.09.04             Kursus: Programmering           34
                                                           Manufacturing
 Aalborg University – Department of Production               Information
                                                                 Systems
                                                                  Group


 Classification of Object Types, cont.
• Overriden Methods: use of the built-in reference super
    class UserCD extends DataCD
    {
       String Content;
       Date   CreatedDate;
       void Display()
       {
          super.Display();
          System.out.println("Content: "+Content);
          System.out.println("Created: "+CreatedDate);
       }
    }
 2006.09.04             Kursus: Programmering                35
                                                             Manufacturing
  Aalborg University – Department of Production                Information
                                                                   Systems
                                                                    Group

Classification of Object Types, cont.

  • Substitution
      • Address values can be substituted by addresses of a subtype
         CompactDisc CD = new MusicCD;
      • The opposite assignment is also possible,
        if the values are casted
           CompactDisc CD1 = new CompactDisc("Me");
           Stack S = new Stack();
           S.push(CD1);
           CompactDisc CD2 = (CompactDisc) S.pop();
           CD2.Display();



  2006.09.04             Kursus: Programmering                 36
                                                                Manufacturing
Aalborg University – Department of Production                     Information
                                                                      Systems
                                                                       Group


Classification of Object Types, cont.
•   Interfaces
     • Interface declaration contains a set of methods
     • Methods are declared with name and parameter list - ex:
       public interface Sortable
       { int CompareTo(Sortable s); }
     • Interfaces can be implemented in classes
     • In such classes, the methods must be programmed - ex:
       class DataCD implements Sortable
       { ...
            int CompareTo(Sortable s){... /*something*/ ...}
       }
     • Interfaces provides a solution to multiple inheritance

2006.09.04               Kursus: Programmering                    37
                                                                     Manufacturing
  Aalborg University – Department of Production                        Information
                                                                           Systems
                                                                            Group

Object-Orientation
      • Different interpretations of the paradigm
          • Models of real world objects
               – appearance and behaviour
          • Organisms - communicating living objects
               – clients and servers - agents
          • Inheritance of properties
      • Object-orientation can be seen from two viewpoints
          • Two abstraction mechanisms
               – classification: attributes - inheritance
               – composition: organisms - communication - services




  2006.09.04                Kursus: Programmering                      38
                                                                      Manufacturing
 Aalborg University – Department of Production                          Information
                                                                            Systems
                                                                             Group

The Data Modelling Methodology

     • Six steps in recommended order:
         •    1.   Identification of objects and object types
         •    2.   Definition of attributes of the object types
         •    3.   Building of a taxonomy through classification
         •    4.   Definition of relationships between object types
                   – references, collections and associations
         • 6. Definition of semantics
     • Composite object types - recursively defined




 2006.09.04                      Kursus: Programmering                  39
                                                      Manufacturing
  Aalborg University – Department of Production         Information
                                                            Systems
                                                             Group

Modelling as a Systems Development Approach

      • Design levels ~ abstraction levels (models)
      • Degree of invariance
      • Traditional system design levels
          • conceptual - logical - physical
      • Prototyping
          • development of prototypes
          • stepwise improvement of prototypes
      • Prototyping by modelling
      • CASE systems - repositories




  2006.09.04               Kursus: Programmering        40
                                                    Manufacturing
 Aalborg University – Department of Production        Information
                                                          Systems
                                                           Group

Software Development
     • Target system model
     • Object base and user interfaces
     • Modelling
         • data model
         • function model
     • Function modelling
         • identification of user interfaces
         • related object types
     • Functionality in object types
         • data attributes and methods - services




 2006.09.04                 Kursus: Programmering     41
                                                           Manufacturing
 Aalborg University – Department of Production               Information
                                                                 Systems
                                                                  Group

Summary and Conclusion
     • Data modelling as a development approach
         • abstraction mechanisms - abstraction levels
     • Data modelling methodology based on
         • fundamental system concepts
         • abstraction mechanisms
         • object-orientation
     • Perspectives of data modelling
         • reusable, extensible, reliable system objects
         • computer based, graphical support tools
         • generation of information systems




 2006.09.04                Kursus: Programmering             42

				
DOCUMENT INFO