structural programming course content and data structures
play

Structural Programming Course Content and Data Structures - PDF document

Structural Programming Course Content and Data Structures Introduction Vectors Objects Testing/Debugging Winter 2000 Methods Arrays Tracing Programs Searching CMPUT 102: Methods Object State Files


  1. Structural Programming Course Content and Data Structures • Introduction • Vectors • Objects • Testing/Debugging Winter 2000 • Methods • Arrays • Tracing Programs • Searching CMPUT 102: Methods • Object State • Files I/O • Sharing resources • Sorting Dr. Osmar R. Zaïane • Selection • Inheritance • Repetition • Recursion Lecture 9 – Lecture 10 University of Alberta  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 1  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 2 2 Objectives of Lecture 9 Outline of Lecture 9 The structure of a Java Program The structure of a Java Program • Understand the structure of a Java program • Program and the different classes that form a program. • Classes • Get an introduction to methods and invocation • Methods of methods by sending message expressions. • Method dispatch • Comprehend the relationship between • Launching an application program, classes and methods. • Launching an applet • Find out how applications and applets are launched.  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 3  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 4 The Structure of a Java Program A Java Program - a Set of Classes • There are four major structural components • A Java program consists of one or more of Java programs classes . – the program itself … – classes – methods One Java Class One Java Class A Java Program One Java Class – statements A Java Class  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 5 Structural Programming and Data Structures University of Alberta 6 1

  2. Syntax for a Java Class Outline of Lecture 9 visibility modifier class keyword class name • Program public class Adventure { class start delimiter • Classes /* start comment delimiter An instance of this class is an arithmetic adventure game • Methods where an adventurer navigates rooms that contain treasure chests that are opened by correctly answering • Method dispatch arithmetic problems. class comment */ • Launching an application end comment delimiter • Launching an applet body of the class goes here } class end delimiter  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 7  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 8 Outline of Lecture 9 A Java Class - a Set of Methods • Program • The body of each Java class includes a set of methods . • Classes • A method is some code that performs a • Methods single task. • Method dispatch … • Launching an application One Java Class • Launching an applet One Java Class A Java Class One Java Class A Java Method  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 9  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 10 Two Kinds of Methods Syntax for a Java Method visibility modifier return type static keyword • There are two kinds of methods in Java. method name • An instance method implements a message public static void main(String args[]) { method start delimiter that is sent to an instance of the class. /* parameter list Starting point for a program. • A static method implements a task that is method comment */ independent of any particular object. body of the method goes here • In either case, some code is run and } (optionally) a result is returned. method end delimiter  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 11 Structural Programming and Data Structures University of Alberta 12 2

  3. A Java Method - Statements Java Statements • The body of a method includes a sequence • There are many kinds of Java statements. of statements . • Each statement ends with a semi-colon. • We have already seen four kinds of … statements: – variable declaration A Java Statement A Java Statement A Java Statement A Java Method – import A Java Statement – message expression – assignment statement  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 13  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 14 Method Dispatch Outline of Lecture 9 • The association of messages to instance • Program methods is called method dispatch . • Classes • The class of the receiver object must contain an instance method with the same • Methods name as the message name. • Method dispatch • The class of each parameter in the • Launching an application parameter list of the method must match the • Launching an applet class of each corresponding argument in the argument list of the message.  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 15  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 16 Method Dispatch Example 1 Method Dispatch Example 2 System.out.print("Hello"); “HELLO” “Hello”.toUpperCase(); one argument class String empty argument list message name is print one parameter class String message name is toUpperCase empty parameter list class of receiver is PrintStream class of receiver is String public void print(String aString) PrintStream public String toUpperCase() { String Class /* /* Class  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 17 Structural Programming and Data Structures University of Alberta 18 3

  4. Kinds of Java Programs Outline of Lecture 9 • Recall there are three kinds of programs: – Applications • Program – Applets – Libraries • Classes • The structure of all three kinds of programs • Methods are the same. • Method dispatch • However, each kind of program is launched • Launching an application differently. • Launching an applet • Libraries are never launched, they are just called by other programs.  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 19  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 20 Java Applications - launching Java Applications - main Protocol • In a Java application, one class is marked as the special “starting” class. • The start class must contain a static method • When the Java application is launched by for main with protocol: the interpreter, it invokes a static method public static void main(String args[]) called “main” in the start class. One Java Class One Java Class A Java Program One Java Class Program Start Class Java main() Interpreter  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 21  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 22 Java Applets - launching Outline of Lecture 9 • When the web browser reads a document that tells it to load an applet, it creates an • Program instance of your applet subclass and sends it the instance message init(). • Classes • Methods Other Java Classes Other Java Classes • Method dispatch A Java Applet Other Java Classes • Launching an application Web anApplet init() • Launching an applet Applet Subclass Browser  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 23 Structural Programming and Data Structures University of Alberta 24 4

Recommend


More recommend