april 20023csg11 electronic commerce java (1) john wordsworth department of computer science the...
TRANSCRIPT
April 2002 3CSG1 1
Electronic CommerceJava (1)
John WordsworthDepartment of Computer ScienceThe University of [email protected] 129, Ext 6544
April 2002 3CSG1 2
Lecture objectives
Outline the place of the Java language in E-commerce
Revise the concepts of object-oriented programming
Describe the structure of a Java class and its parts
Describe the structure of a Java application, and the way it uses objects
Explain how Java implements inheritance
April 2002 3CSG1 3
What is JavaAn object-oriented programming language invented by Sun Microsystems
A collection of classes that model useful objects: windows, buttons, text areas, files, databases …
A language for developing applications that can be executed on many platforms:
Write Java source code
Compile to Java bytecodes (javac)
Execute under the control of the Java Virtual Machine (java)
April 2002 3CSG1 4
Java in e-commerce
Client programming with applets:
Classes for setting up and controlling the graphical user interface
Server programming with servlets:
Classes for managing the interface with tier 1 (JSPs)
Classes for managing the interface with tier 3 (EJBs)
April 2002 3CSG1 5
An object-oriented languageObject - an instance of a class:
variablesmethods
Message - executing a method: destination object (class)parameters
Class - a blueprint for objects:class variable (static) and class method (static)instance variable and instance methodconstructorinheritance
Interface - a collection of methods
April 2002 3CSG1 6
An interface for a simple bank account
public interface AccountIf {void deposit (int amt); void withdraw (int amt); void showBal; }
April 2002 3CSG1 7
A class for a simple bank account public class Account implements AccountIf {private String sortcode, acctno;private int balance;Account (String sc, String an){ sortcode = new String (sc); acctno = new String (an);balance = 0;}public void deposit (int amt) { balance = balance + amt; }public void withdraw (int amt) { balance = balance – amt;}public void showBal { System.out.println ( “Sort code “+sortcode+” Acct number “+acctno=“ Balance “+balance); }
April 2002 3CSG1 8
A program to use a bank account
public class AccountMain { public static void main (String [] args) { Account ac1 = new Account ("304050", "01234567"); ac1.showBal(); ac1.deposit(500); ac1.showBal(); ac1.withdraw(575); ac1.showBal(); }}
April 2002 3CSG1 9
A bit of inheritancepublic class DAccount extends Account{ DAccount (String sc, String an, int opening) { super (sc, an); balance = opening;} public void withdraw (int amt) { if (amt < balance) { balance = balance - amt; } else { System.out.println ( "Withdrawal not allowed"); } }}
April 2002 3CSG1 10
A program to use a deposit account
public class DAccountMain { public static void main (String [] args) { DAccount ac2 = new DAccount ("304050", "01234567“, 1000); ac1.showBal(); ac1.withdraw(500); ac1.showBal(); ac1.withdraw(500); ac1.showBal(); }}
April 2002 3CSG1 11
Sample output
Sort code 405060 Account number 98765432 Balance 1000
Sort code 405060 Account number 98765432 Balance 500
Withdrawal not allowed
Sort code 405060 Account number 98765432 Balance 500
April 2002 3CSG1 12
Key points
Java is an object-oriented language for writing portable applications.
Java has classes that support the construction of programs to run in Tier 1 (applets) and Tier 2 (servlets) of an e-commerce system.
Java classes have a regular structure that can be adapted to writing object-oriented applications, classes, and objects.