You are on page 1of 57

MUIT 414: Business Programming

Lecturer

Emmanuel Buabin
BSc (Ghana), MEng (Deggendorf)
• Tel: 0543 233275
• Email: jojobuabin@yahoo.com

Commercial Programming 2
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, IF…ELSE, IF…ELSE IF…., SWITCH statement
– Data Structures
• Arrays, ArrayList, HashMap etc

Commercial Programming 3
• Inheritance, Aggregation, Method
Overloading, Polymorphism, etc

Commercial Programming 4
Object Oriented Programming
with JAVA

Commercial Programming 5
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.

Commercial Programming 6
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.

Commercial Programming 7
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

Commercial Programming 8
Objects and Classes
• A class defines a
– self-contained software component that
represents a real world class.

• The design of classes MUST follow the basic


principles of object-orientation:
– modularity
– encapsulation

Commercial Programming 9
• 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

Commercial Programming 10
Organization of Class
• A mechanism to organize classes by
commonalities.

– From subclasses its generalization

– From superclass its specialization

Commercial Programming 11
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

Commercial Programming 12
Class Diagram: Inheritance
Student

GraduateStudent UndergraduateStudent

MasterStudent PhDStudent

Commercial Programming 13
Composition

• “has-a” relation
• Example:
• a student has
– has an address (type: Address)
– has a faculty advisor (type: Faculty)

Commercial Programming 14
Class Diagram: Composition
Student
Faculty
-name : String *
-gpa : float 1
* 1

Address
-streetAddress : String
-city : String
-state : String
-zipCode : String

Commercial Programming 15
Class Declaration
• Class: Attribute and Method declarations
– Class Level: Variables can be used by all
methods in that class
– Method Level: Variables can only be used in
that method declaration
– A method declaration specifies the code that
will be executed when the method is invoked
(or called)

Commercial Programming 16
Class Declaration Syntax

[ ClassModifiers ] class ClassName


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

{
  ClassMemberDeclarations
}
Commercial Programming 17
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 be
instantiated
– final
subclasses CANNOT be instantiated.
Commercial Programming 18
Field and Method Declaration
[ MethodModifiers ] ReturnType Name ( [ ParameterList ]  )
{
  //Statements
}
Attributes are
fields
[ FieldModifiers ] DataType 

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

Commercial Programming 19
Encapsulation
• external view: for the users of the class, client
view
• internal view: for the developers of the class,
implementer view
• visibility: In short, expose only
– public, constructs which are
– protected, supposed to be exposed to
– the outside world and
package (default),
restrict those which are
– private
meant to be within.

Commercial Programming 20
Visibility
publi protecte packag privat
c d e e
The class itself Yes Yes Yes Yes
Classes in the
Yes Yes Yes No
 same package 
Subclasses in a
Yes Yes No No
 different package 
Non-subclasses in
 a different Yes No No No
package

Commercial Programming 21
Example: Employee.java
public class Employee
{ //Beginning of class

private String strFName; //first attribute


private String strLName; //second attribute

public Employee()
{
//empty constructor
}

public String joinNames(String fn, String ln)


{
return fn +” ”+ ln;
}
Commercial Programming 22
Example: Employee.java cont’d
public String getFirstName ()
{
return strFName;
}

public String getLastName()


{
return strLName;
}

Commercial Programming 23
Example: Employee.java cont’d
public void setFirstName(String fn)
{
strFName = fn;
}
public void setLastName(String ln)
{
strLName = ln;
}

} //end of class Employee

Commercial Programming 24
Test Program using Employee
Class
public class Test
{
public static void main (String[] args)
{
Employee objEmp = new Employee();
//creating an object using the empty
//constructor
String F_Name=“Kojo”;
String L_Name=“Mensah”;
String fullName;
fullName=objEmp.joinNames(F_Name,L_Name);
System.out.println(fullName);
}
} Output

Commercial Programming
Kojo Mensah 25
Parameterized Constructors
e.g.s ….
Constructors are
public Employee(String fn,String ln)
the first, to be
{
called when
strFName = fn;
object is created
strLName = ln;
}

public Employee(String fn)


{
strFName = fn;
strLName = “Baidoo”;
}
… Commercial Programming 26
Example: Constructors
e.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

Commercial Programming 27
Method Overloading
• Two or more methods/constructors with
the same name but different numbers or
different types of parameters:
void methodA(int i)
void methodA(int i, int j)

void methodB(int i)
void methodB(float f)

NB: Pass the right parameters to methods


Commercial Programming 28
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

Commercial Programming 29
Special Methods
• obj1.equals(obj2)
– Same as o1 == o2
• strObj.toString()
– returns a string representation of the state
of the object
• finalize()
– invoked by the Java runtime just before
the object is garbage-collected

Commercial Programming 30
Polymorphism
• Making Objects behave in different ways
– Illustrate this with a java program

• See next page for Parameter Passing

Commercial Programming 31
Class Assignment
• Write a Java program using the ff
– class
– attributes
– method
– inheritance
– polymorphism
– parameterized constructor
– method (overloaded)

Commercial Programming 32
Compulsory Assignment:
Hotel Room Modeling
• Using ideas in inheritance,
polymorphism, Parameterized
constructors, 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)
Commercial Programming 33
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 being
achieved is ERROR
• Error Tracking Tools

System Analysis & Design 34


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 internal


functioning of the product.

System Analysis & Design 35


Testing Types (2 ) cont’d
• 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.

System Analysis & Design 36


Verification & Validation
• Verification is a process to check the
deviation of results from the required ones.
• Validation refers to the process of using
software in a live environment in order to
find errors.
– The feedback from the validation phase
generally produces changes in the software to
deal with bugs and failures that are
uncovered.
– Validation may continue for several months.
System Analysis & Design 37
Java Documentation
• Documentation Style
– In-source
• @author ‘Emmanuel Buabin’
• @date ‘September 1, 2010’
• Etc
– Comment
• Documentation Generation Tools
– JAVA: Javadoc

System Analysis & Design 38


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

Commercial Programming 39
Java Database Connectivity
(JDBC)

Commercial Programming 40
Java DataBase Connectivity
• Java Database Connectivity
• Steps:
1. Load a JDBC driver
2. Open a connection to a data base
3. Send queries or update statements to DB
4. Process the results
5. Close connection

Commercial Programming 41
Example:
BuildUserDB_Access.java
import java.sql.*;
import java.io.*;
import java.util.*;
public class BuildUserDB_Access {
public static final String database =
"Access 2000";
public static final String jdbcDriver =
"sun.jdbc.odbc.JdbcOdbcDriver";
public static final String dataSource =
"jdbc:odbc:";

public static void main(String args[]) {


String dbName = "Users";
String tableName = "UserPass";
Connection conn = null;
Statement stmt = null;
Commercial Programming 42
Example:
BuildUserDB_Access.java
try
{
Class.forName(jdbcDriver);
} catch(ClassNotFoundException e)
{
System.exit(1);
}

try {
String url = dataSource + dbName;
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
} catch (SQLException se) {
System.exit(1);
}
Commercial Programming 43
Example:
BuildUserDB_Access.java
try {
String dropString = "DROP TABLE " +
tableName;
stmt.executeUpdate(dropString);
} catch (SQLException se) {}
try {
String createString =
"CREATE TABLE " + tableName +
" (username VARCHAR(128) NOT NULL PRiMARY
KEY," +
" password VARCHAR(128))";
stmt.executeUpdate(createString);

Commercial Programming 44
Example:
BuildUserDB_Access.java
String insertString =
"iNSERT iNTO " + tableName +
" VALUES ('Scott McNealy', 'lavender')";
stmt.executeUpdate(insertString);
insertString =
"iNSERT iNTO " + tableName +
" VALUES ('Steve Jobs', 'aqua')";
stmt.executeUpdate(insertString);
insertString =
"iNSERT iNTO " + tableName +
" VALUES ('Bill Gates', 'blue')";
stmt.executeUpdate(insertString);

Commercial Programming 45
Example:
BuildUserDB_Access.java
ResultSet rset =
stmt.executeQuery("SELECT * FROM " +
tableName);

while( rset.next() )
{
System.out.println(rset.getString("username") +
":" + rset.getString("password"));
}
stmt.close();
rset.close();
conn.close();
} catch (SQLException se) {}
}
}
Commercial Programming 46
The UserPass Table

username password
Bill Gates blue

Scott McNealy lavender

Steve Jobs aqua

Commercial Programming 47
Introduction to
HyperText Markup Language
(HTML Basics)

Commercial Programming 48
HTML (cont’d)
• Structure of HTML page
– <html>
• <head><title> </title></head>
• <Body></Body>
– </html>

• Heading styles
– H1, H2, H3, H4 etc

• Paragraph tag
– <P>, 2 of <br>

Commercial Programming 49
HTML (cont’d)
• Lists
– Unordered List (UL),
– Ordered List (OL)

• Horizontal rule
– (HR)

• Font Coloring
Commercial Programming 50
HTML (cont’d)
• Tables
– <table>
• <tr>
– <td>Cell 1</td><td>Cell 2</td>
• </tr>
• ….
– </table>

Commercial Programming 51
HTML (cont’d)
• Forms (Link “Submit” to other pages)

• Hypertext Anchors (A)

• Link to Web-pages (HREF)

• others

Commercial Programming 52
Class Assignment
• Create an HTML page in

Commercial Programming 53
Java Server Faces
(JSF)

(tutorial will be given during “Labs”)

Commercial Programming 54
Tools to be used
• Eclipse IDE
• Dreamweaver

Commercial Programming 55
Compulsory Assignment:
Online Hotel Booking System
• Using JSF/JSP and JDBC ideas, upgrade
the Hotel Reservation System to an Online
Application

Commercial Programming 56
END of LECTURE

Commercial Programming 57

You might also like