Business Programming Lecture Note

Embed Size (px)

Citation preview

  • 8/7/2019 Business Programming Lecture Note

    1/57

    MUIT 414: Business Programming

    Lecturer

    Emmanuel BuabinBSc (Ghana), MEng (Deggendorf)

  • 8/7/2019 Business Programming Lecture Note

    2/57

    Commercial Programming 2

    Tel: 0543 233275

    Email: [email protected]

  • 8/7/2019 Business Programming Lecture Note

    3/57

    Commercial Programming 3

    How Good are You? (3 hours)

    Programming Revision

    Expression

    Conditions (Single and Compound)

    Operators (Arithmetic, Logical, Relational)

    Loops (Pre-test Loop, Post-Test Loop)

    Control Structures

    IF, IFELSE, IFELSE IF., SWITC

    H statement Data Structures

    Arrays, ArrayList, HashMap etc

  • 8/7/2019 Business Programming Lecture Note

    4/57

    Commercial Programming 4

    Inheritance, Aggregation, Method

    Overloading, Polymorphism, etc

  • 8/7/2019 Business Programming Lecture Note

    5/57

    Commercial Programming 5

    Object Oriented Programming

    with JAVA

  • 8/7/2019 Business Programming Lecture Note

    6/57

    Commercial Programming 6

    Basic Concepts of Object

    Orientation An object has

    state: defined by the set of fields or attributes.

    behavior: defined by the set of methods or operation

    that can be applied to the object.

    Class

    A template for creating objects.

    Objects of the same class exhibit the same behavior.

    They may have different states.

  • 8/7/2019 Business Programming Lecture Note

    7/57

    Commercial Programming 7

    Objects and Classes

    An object represents an individual entity or thing.

    A class represents a group of objects that exhibit

    some common characteristics or behavior.

  • 8/7/2019 Business Programming Lecture Note

    8/57

    Commercial Programming 8

    Classification

    Classes are resulted from classification.

    Examples of classes in real world:

    Students Graduate students

    MS students

    Ph.D. students

    Undergraduate students BSc/BA/BPharm etc

  • 8/7/2019 Business Programming Lecture Note

    9/57

    Commercial Programming 9

    Objects and Classes

    A class defines a

    self-contained software component thatrepresents a real world class.

    The design of classes MUST follow the basicprinciples of object-orientation:

    modularity

    encapsulation

  • 8/7/2019 Business Programming Lecture Note

    10/57

    Commercial Programming 10

    Each OBJECT is an instance of a class.

    A class defines the common characteristics of its

    instances: the behaviors and

    the templates of the fields.

    Each OBJECT has its own state

  • 8/7/2019 Business Programming Lecture Note

    11/57

    Commercial Programming 11

    Organization ofClass

    A mechanism to organize classes bycommonalities.

    From subclasses its generalization

    From superclass its specialization

  • 8/7/2019 Business Programming Lecture Note

    12/57

    Commercial Programming 12

    Inheritance

    is-a relation

    Example A graduate student is a student

    A Master student is a graduate student

    A Ph.D. student is a graduate student An undergraduate student is a student

  • 8/7/2019 Business Programming Lecture Note

    13/57

    Commercial Programming 13

    Class Diagram: Inheritance

    S tudent

    GraduateS tudent UndergraduateS tudent

    MasterS tudent PhDS tudent

  • 8/7/2019 Business Programming Lecture Note

    14/57

    Commercial Programming 14

    Composition

    has-a relation

    Example: a student has

    has an address (type: Address)

    has a faculty advisor (type: Faculty)

  • 8/7/2019 Business Programming Lecture Note

    15/57

    Commercial Programming 15

    Class Diagram: Composition

    name : tring

    gpa : loat

    StudentFaculty

    street ress : tring

    cit : tring

    state : tring

    ipCo e : tring

    Address

    *

    1

    *1

  • 8/7/2019 Business Programming Lecture Note

    16/57

    Commercial Programming 16

    Class Declaration

    Class: Attribute and Method declarations

    Class Level: Variables can be used by allmethods in that class

    Method Level: Variables can only be used inthat method declaration

    A method declaration specifies the code thatwill be executed when the method is invoked

    (or called)

  • 8/7/2019 Business Programming Lecture Note

    17/57

    Commercial Programming 17

    Class Declaration Syntax

    [ ClassModifiers ] class ClassName

    [extends SuperClass ][implements interface1, interface2 ...]

    {

    ClassMemberDeclarations}

  • 8/7/2019 Business Programming Lecture Note

    18/57

    Commercial Programming 18

    Class Visibility

    Public Accessible everywhere.

    NB: One public class allowed per file

    Default Accessible within the current package.

    Other class modifiers: abstract

    A class that contains abstract methods cannot beinstantiated

    finalsubclasses CANNOT be instantiated.

  • 8/7/2019 Business Programming Lecture Note

    19/57

    Commercial Programming 19

    Field and Method Declaration

    [MethodModifiers ] ReturnType Name ( [ ParameterList ] )

    {

    //Statements}

    [ FieldModifiers ] DataType

    FieldName1 [= initializer1 ] ,FieldName2 [= initializer2 ] ...;

    Attributes arefields

  • 8/7/2019 Business Programming Lecture Note

    20/57

    Commercial Programming 20

    Encapsulation

    external view: for the users of the class, clientview

    internal view: for the developers of the class,

    implementer view visibility:

    public,

    protected,

    package (default), private

    In short, expose only

    constructs which are

    supposed to be exposed to

    the outside world and

    restrict those which are

    meant to be within.

  • 8/7/2019 Business Programming Lecture Note

    21/57

    Commercial Programming 21

    Visibility

    public protectedpackag

    eprivate

    The class itself Yes Yes Yes Yes

    Classes in the

    same package Yes Yes Yes No

    Subclasses in a

    different packageYes Yes No No

    Non-subclasses in

    a differentpackage

    YesNo No No

  • 8/7/2019 Business Programming Lecture Note

    22/57

    Commercial Programming 22

    Example: Employee.java

    publicclass Employee

    { //Beginningofclass

    private StringstrFName; //firstattribute

    private StringstrLName;

    //secondattribute

    public Employee()

    {

    //emptyconstructor

    }

    public String joinNames(Stringfn, Stringln)

    {

    returnfn + + ln;

    }

  • 8/7/2019 Business Programming Lecture Note

    23/57

    Commercial Programming 23

    Example: Employee.java contd

    public StringgetFirstName ()

    {

    returnstrFName;

    }

    public StringgetLastName()

    {

    returnstrLName;

    }

  • 8/7/2019 Business Programming Lecture Note

    24/57

    C

    ommercial Programming 24

    publicvoidsetFirstName(Stringfn)

    {

    strFName=fn;

    }

    publicvoidsetLastName(Stringln){

    strLName=ln;

    }

    } //endofclass Employee

    Example: Employee.java contd

  • 8/7/2019 Business Programming Lecture Note

    25/57

    Commercial Programming 25

    Test Program using EmployeeC

    lasspublicclass Test{

    publicstaticvoidmain (String[]args)

    {

    Employee

    objEmp=

    newEmployee

    ();

    //creatinganobjectusingtheempty

    //constructor

    StringF_Name=Kojo;

    StringL_Name=Mensah;

    StringfullName;

    fullName=objEmp.joinNames(F_Name,L_Name);System.out.println(fullName);

    }

    } Output

    Kojo Mensah

  • 8/7/2019 Business Programming Lecture Note

    26/57

    Commercial Programming 26

    Parameterized Constructorse.g.s .

    public Employee(String fn,String ln)

    {

    strFName = fn;strLName = ln;

    }

    public Employee(String fn)

    {strFName = fn;

    strLName = Baidoo;

    }

    Constructors are

    the first, to be

    called when

    object is created

  • 8/7/2019 Business Programming Lecture Note

    27/57

    Commercial Programming 27

    Example: Constructorse.g.s

    Employee objEmp1 = new Employee(Kojo, Mensah);

    objEmp1.getFirstName() // Kojo

    objEmp1.getLastName() // Mensah

    Employee objEmp2 = new Employee(William);

    objEmp2.getFirstName() // William

    objEmp2.getLastName() // Baidoo

  • 8/7/2019 Business Programming Lecture Note

    28/57

    Commercial Programming 28

    Method Overloading

    Two or more methods/constructors with

    the same name but different numbers or

    different types of parameters:

    voidmethodA(inti)

    voidmethodA(inti,int j)

    voidmethod

    B(int

    i)

    voidmethodB(floatf)

    NB: Pass the right parameters to methods

  • 8/7/2019 Business Programming Lecture Note

    29/57

    Commercial Programming 29

    E.g. Overloaded Methods

    void BankAccount(string b_no)

    Accepts bank number

    void BankAccount(string b_no, string cn) Accepts bank number and customer name

    void BankAccount(string b_no, float dep)

    Accepts bank number and deposit amount

  • 8/7/2019 Business Programming Lecture Note

    30/57

    Commercial Programming 30

    Special Methods

    obj1.equals(obj2)

    Same as o1 == o2

    strObj.to

    String

    ()

    returns a string representation of the state

    of the object

    finalize()

    invoked by the Java runtime just beforethe object is garbage-collected

  • 8/7/2019 Business Programming Lecture Note

    31/57

    Commercial Programming 31

    Polymorphism

    Making Objects behave in different ways

    Illustrate this with a java program

    See next page for Parameter Passing

  • 8/7/2019 Business Programming Lecture Note

    32/57

  • 8/7/2019 Business Programming Lecture Note

    33/57

    Commercial Programming 33

    Compulsory Assignment:

    Hotel Room Modeling

    Using ideas in inheritance,polymorphism, Parameterizedconstructors, MODEL and IMPLEMENT

    the ff rooms Executive Room (Air condition, Study &

    Office Desk, Wardrobe, Living room,Jacuzzi, King Size Bed)

    Deluxe Room (Air condition, Wardrobe,Study Desk )

    Standard Room (Wardrobe)

  • 8/7/2019 Business Programming Lecture Note

    34/57

    System Analysis & Design 34

    Testing & Debugging

    Testing is basically a process to detect errors in

    the software product

    Errors: In day-to-day life we say whenever

    something goes wrong there is an error.

    In Software Concept: Difference between what is

    expected out of software and what is beingachieved is ERROR

    Error Tracking Tools

  • 8/7/2019 Business Programming Lecture Note

    35/57

    System Analysis & Design 35

    Testing Types (2 of them)

    A software product can be tested in 2

    ways.

    Black Box Approach: Overall functioning of

    the product is tested. Inputs are given and

    outputs are checked.

    NB: DOES NOT CARE about the internalfunctioning of the product.

  • 8/7/2019 Business Programming Lecture Note

    36/57

    System Analysis & Design 36

    Testing Types (2 ) contd

    White box Approach:

    Here the internal functioning of the product is

    tested.

    Each procedure is tested for its accuracy.

    It is more intensive than black box testing.

    But for the overall product both approaches

    are crucial.

  • 8/7/2019 Business Programming Lecture Note

    37/57

    System Analysis & Design 37

    Verification & Validation

    Verification is a process to check thedeviation of results from the required ones.

    Validation refers to the process of using

    software in a live environment in order tofind errors. The feedback from the validation phase

    generally produces changes in the software todeal with bugs and failures that areuncovered.

    Validation may continue for several months.

  • 8/7/2019 Business Programming Lecture Note

    38/57

    System Analysis & Design 38

    Java Documentation

    Documentation Style

    In-source

    @author Emmanuel Buabin

    @date September 1, 2010

    Etc

    Comment

    Documentation Generation Tools JAVA: Javadoc

  • 8/7/2019 Business Programming Lecture Note

    39/57

    Commercial Programming 39

    Compulsory Assignment:

    Hotel Reservation System

    Using a Data structure of your choice

    and OOP ideas, write a sample Java

    Program to perform the following

    business processes

    Add Booking

    Update Booking

    Delete Booking

    Book Listings

  • 8/7/2019 Business Programming Lecture Note

    40/57

    Commercial Programming 40

    Java Database Connectivity

    (JDBC)

  • 8/7/2019 Business Programming Lecture Note

    41/57

    Commercial Programming 41

    Java DataBase Connectivity

    Java Database Connectivity

    Steps:

    1. Load a JDBC

    driver2. Open a connection to a data base

    3. Send queries or update statements to DB

    4. Process the results

    5. Close connection

  • 8/7/2019 Business Programming Lecture Note

    42/57

    Commercial Programming 42

    Example:

    BuildUserDB_Access.javaimport java.sql.*;

    import java.io.*;

    import java.util.*;

    publicclass BuildUserDB_Access{

    publicstaticfinal Stringdatabase=

    "Access 2000";

    publicstaticfinal String jdbcDriver=

    "sun.jdbc.odbc.JdbcOdbcDriver";

    publicstaticfinal StringdataSource=

    "jdbc:odbc:";

    publicstaticvoidmain(Stringargs[]) {

    StringdbName= "Users";

    StringtableName= "UserPass";

    Connectionconn=null;

    Statementstmt=null;

  • 8/7/2019 Business Programming Lecture Note

    43/57

    Commercial Programming 43

    Example:

    BuildUserDB_Access.javatry

    {

    Class.forName(jdbcDriver);

    }catch(ClassNotFoundExceptione)

    {

    System.exit(1);}

    try{

    Stringurl=dataSource + dbName;

    conn=DriverManager.getConnection(url);stmt=conn.createStatement();

    }catch (SQLExceptionse) {

    System.exit(1);

    }

  • 8/7/2019 Business Programming Lecture Note

    44/57

    Commercial Programming 44

    Example:

    BuildUserDB_Access.java

    try{

    StringdropString= "DROP TABLE " +

    tableName;

    stmt.executeUpdate(dropString);

    }catch

    (SQLException

    se){}

    try{

    StringcreateString=

    "CREATE TABLE " + tableName +

    " (username VARCHAR(128) NOT NULLPRiMARY

    KEY," +

    " passwordVARCHAR(128))";stmt.executeUpdate(createString);

  • 8/7/2019 Business Programming Lecture Note

    45/57

    Commercial Programming 45

    Example:

    BuildUserDB_Access.javaStringinsertString=

    "iNSERT iNTO " + tableName +

    " VALUES ('ScottMcNealy', 'lavender')";

    stmt.executeUpdate(insertString);

    insertString=

    "iNSERT iNTO " + tableName +" VALUES ('SteveJobs', 'aqua')";

    stmt.executeUpdate(insertString);

    insertString=

    "iNSERT iNTO " + tableName +

    " VALUES ('Bill Gates', 'blue')";

    stmt.executeUpdate(insertString);

  • 8/7/2019 Business Programming Lecture Note

    46/57

    Commercial Programming 46

    Example:

    BuildUserDB_Access.java

    ResultSetrset=

    stmt.executeQuery("SELECT *FROM" +

    tableName);

    while( rset.next() ){

    System.out.println(rset.getString("username") +

    ":" + rset.getString("password"));

    }

    stmt.close();

    rset.close();conn.close();

    }catch (SQLExceptionse) {}

    }

    }

  • 8/7/2019 Business Programming Lecture Note

    47/57

    Commercial Programming 47

    The UserPass Table

    username password

    Bill Gates blue

    Scott McNealy lavender

    Steve Jobs aqua

  • 8/7/2019 Business Programming Lecture Note

    48/57

    Commercial Programming 48

    Introduction to

    HyperText Markup Language(HTML Basics)

  • 8/7/2019 Business Programming Lecture Note

    49/57

    Commercial Programming 49

    HTML (contd)

    Structure of HTML page

    Heading styles H1, H2, H3, H4 etc

    Paragraph tag

    , 2 of

  • 8/7/2019 Business Programming Lecture Note

    50/57

    Commercial Programming 50

    HTML (contd)

    Lists

    Unordered List (UL),

    Ordered List (OL)

    Horizontal rule

    (HR)

    Font Coloring

  • 8/7/2019 Business Programming Lecture Note

    51/57

    Commercial Programming 51

    HTML (contd)

    Tables

    Cell 1Cell 2

    .

  • 8/7/2019 Business Programming Lecture Note

    52/57

    Commercial Programming 52

    HTML (contd)

    Forms (Link Submit to other pages)

    Hypertext Anchors (A)

    Link to Web-pages (HREF)

    others

  • 8/7/2019 Business Programming Lecture Note

    53/57

    Commercial Programming 53

    Class Assignment

    Create an HTML page in

  • 8/7/2019 Business Programming Lecture Note

    54/57

    Commercial Programming 54

    Java Server Faces

    (JSF)

    (tutorial will be given during Labs)

  • 8/7/2019 Business Programming Lecture Note

    55/57

    Commercial Programming 55

    Tools to be used

    Eclipse IDE

    Dreamweaver

  • 8/7/2019 Business Programming Lecture Note

    56/57

    Commercial Programming 56

    Compulsory Assignment:

    Online Hotel Booking System

    Using JSF/JSP and JDBC ideas, upgrade

    the Hotel Reservation System to an Online

    Application

  • 8/7/2019 Business Programming Lecture Note

    57/57

    Commercial Programming 57

    END of LECTURE