Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Object-oriented Programming Java API.

Similar presentations


Presentation on theme: "Introduction to Object-oriented Programming Java API."— Presentation transcript:

1 Introduction to Object-oriented Programming Java API

2 OOP Concepts Object-oriented vs Procedure-oriented Programming Object-oriented vs Procedure-oriented Programming Objects and Classes Objects and Classes Encapsulation and Information Hiding Encapsulation and Information Hiding Java API Java API Java Programs Java Programs Example Programs Example Programs

3 Procedure-Oriented Programing In procedure-oriented programs (FORTRAN, COBOL, C, Pascal, BASIC, etc.), emphasis is on sequence of commands (procedure) to the computer. In procedure-oriented programs (FORTRAN, COBOL, C, Pascal, BASIC, etc.), emphasis is on sequence of commands (procedure) to the computer. For example, the following algorithm For example, the following algorithm reads a list of items from a file, reads a list of items from a file, sorts the list, and sorts the list, and prints the sorted list. prints the sorted list.

4 Open (aFile) cout  0 while (not isEOF (aFile)) Read (anItem from aFile) count  aList (count)  anItem end While Close (aFile) Sort (aList) Print ( elements of aList) Read, Sort, Print (procedural)

5 Object-oriented Programming As programs become complex, procedural programming becomes inadequate—too many data and actions to keep track of. As programs become complex, procedural programming becomes inadequate—too many data and actions to keep track of. Many program behaviors are not simply sequential but are concurrent. Many program behaviors are not simply sequential but are concurrent. In OOP, a program consists of a collection of objects that interact with each other. In OOP, a program consists of a collection of objects that interact with each other.

6 Emphasis on Objects aFile.open() while( aFile.EOF() is not true) anItem = aFile.read() aList.insertNewItem(anItem) end while aFile.close() aList.sort() aList.print()

7 In OOP Program, note… Objects—e.g., aList and aFile—are the focus of program, not procedures. Objects—e.g., aList and aFile—are the focus of program, not procedures. Each object possesses operations peculiar to it— e.g., aFile.open(), aFile.close(), aList.read(), aList.insertNewItem(), aList.sort() Each object possesses operations peculiar to it— e.g., aFile.open(), aFile.close(), aList.read(), aList.insertNewItem(), aList.sort() Objects are called (asked) to perform its operation to get a job done—e.g., aFile.open. Objects are called (asked) to perform its operation to get a job done—e.g., aFile.open. In OOP, the inner structure of aList is not revealed to the user of the object—user need not know whether it is an array, linked list, or something else. In OOP, the inner structure of aList is not revealed to the user of the object—user need not know whether it is an array, linked list, or something else.

8 Objects and Classes An OO program consists of a collection of objects interacting with each other. An OO program consists of a collection of objects interacting with each other. Class Class A template for creating objects A template for creating objects It consists of attributes (characteristics) and methods (operations) It consists of attributes (characteristics) and methods (operations) Object Object An instance of a class An instance of a class Many object can be created from a class Many object can be created from a class

9 Objects and Classes  Suppose you are writing a game that involves several dogs, cats, and cars. (Use your imagination.)  You need to define a Dog class.  Here are some dog objects: Fido—a big brown bulldog Fido—a big brown bulldog Lassie—a tall collie dog Lassie—a tall collie dog Taro—a small, white akita dog Taro—a small, white akita dog

10 Objects Attributes: name = Fido breed = bulldog color = brown weight = 30 kg height = 50 cm Operations: bark() display() Attributes: name = Lassie breed = collie color = brow weight = 25 kg height = 70 cm Operations bark() display() Attributes: name = Taro breed = akita color = white weight = 10kg height = 30 cm Operations bark() display() Dog object

11 Class A class A class Is an abstraction of the objects of the same type. Fido, Lassie, and Taro are all instances of the Dog class. Is an abstraction of the objects of the same type. Fido, Lassie, and Taro are all instances of the Dog class. Is a template for creating (specifying) objects of a particular type. Is a template for creating (specifying) objects of a particular type. Is like a blueprint for creating objects Is like a blueprint for creating objects Specifies attributes and methods (operations) Specifies attributes and methods (operations) Each objects has Each objects has Different attributes values Different attributes values Same operations Same operations

12 Class (cont.) A class is composed of A class is composed of Class Name Class Name Attributes Attributes Operations Operations Once a class is defined, objects can be created (instantiated) from it. Once a class is defined, objects can be created (instantiated) from it.

13 Class Diagram Dog breed color weight height Bark() display() Class name Attributes Operations Car make engineSize color maxSpeed Start() accelerate() stop() display()

14 Practice Specify some relevant attributes and operations for the following classes. Specify some relevant attributes and operations for the following classes. Bank Account Bank Account Book Book Browser window Browser window Circle (geometric figure) Circle (geometric figure) Rectangle (geometric figure) Rectangle (geometric figure)

15 Java API (Application Program Interfaces) API is a collection of predefined classes in the library. API is a collection of predefined classes in the library. You can use them to write complex programs. You can use them to write complex programs. You should bookmark it so that you can refer to it at anytime. (http://java.sun.com/j2se/1.4.2/docs/api/ ) You should bookmark it so that you can refer to it at anytime. (http://java.sun.com/j2se/1.4.2/docs/api/ )http://java.sun.com/j2se/1.4.2/docs/api/

16 Package A package is a sructure to organize a collection of related classes and other resources. A package is a sructure to organize a collection of related classes and other resources. java.io—contains classes related to File I/O java.io—contains classes related to File I/O java.util—contains classes like Date, Currency, and GregorianCalendar java.util—contains classes like Date, Currency, and GregorianCalendar javax.swing—contains classes for creating graphical interface javax.swing—contains classes for creating graphical interface

17 Java Programs Two types of Java Programs (both use the same syntax). Two types of Java Programs (both use the same syntax). Applications—general programs that executes on any computer (with a Java Virtual Machine) Applications—general programs that executes on any computer (with a Java Virtual Machine) Applets—designed to run in a Web page. Applets—designed to run in a Web page. In Java, every program unit is a class, even the main program. In Java, every program unit is a class, even the main program. In a Java program, one class must contain a method named main(). In a Java program, one class must contain a method named main().

18 Simple Greeting Source code Source code Source code Source code Note Note Import javax.swing.JOptionsPane;--for input panel Import javax.swing.JOptionsPane;--for input panel Class name SimpleGreeting—file must be saved as SimpleGreeting.java. Class name SimpleGreeting—file must be saved as SimpleGreeting.java. Method main()—starts the program Method main()—starts the program SimpleGreeting greet = new SimpleGreeting();-- instantiates an object SimpleGreeting greet = new SimpleGreeting();-- instantiates an object greet.greeting();--method call greet.greeting();--method call System.exit(0);--terminate program when window is closed System.exit(0);--terminate program when window is closed

19 Greeting with Current Time Source code Source code Source code Source code Note Note Import statements—uses three library classes Import statements—uses three library classes GregorianCalendar today;--declaration of object GregorianCalendar today;--declaration of object today = new GregorianCalendar();--instantiation (creation) of object. today = new GregorianCalendar();--instantiation (creation) of object.


Download ppt "Introduction to Object-oriented Programming Java API."

Similar presentations


Ads by Google