Click here to load reader
Upload
coral-farmer
View
241
Download
1
Embed Size (px)
Citation preview
Introduction toIntroduction toObject-oriented Object-oriented Programming Programming
Java API
OOP ConceptsOOP Concepts
Object-oriented vs Procedure-Object-oriented vs Procedure-oriented Programmingoriented Programming
Objects and ClassesObjects and Classes Encapsulation and Information HidingEncapsulation and Information Hiding Java APIJava API Java ProgramsJava Programs Example ProgramsExample Programs
Procedure-Oriented Procedure-Oriented ProgramingPrograming
In procedure-oriented programs In procedure-oriented programs (FORTRAN, COBOL, C, Pascal, BASIC, (FORTRAN, COBOL, C, Pascal, BASIC, etc.), emphasis is on etc.), emphasis is on sequence of sequence of commands (procedure) to the commands (procedure) to the computer.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.
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 Read, Sort, Print (procedural)(procedural)
Object-oriented Object-oriented ProgrammingProgramming
As programs become complex, As programs become complex, procedural programming becomes procedural programming becomes inadequate—too many data and inadequate—too many data and actions to keep track of.actions to keep track of.
Many program behaviors are not Many program behaviors are not simply simply sequentialsequential but are but are concurrentconcurrent..
In OOP, a program consists of In OOP, a program consists of a collection of objects that interact a collection of objects that interact with each otherwith each other..
Emphasis on ObjectsEmphasis on Objects
aFile.open() while( aFile.EOF() is not true) anItem = aFile.read() aList.insertNewItem(anItem) end while aFile.close() aList.sort() aList.print()
In OOP Program, note…In OOP Program, note… Objects—e.g., Objects—e.g., aListaList and and aFileaFile—are the focus —are the focus
of program, not procedures.of program, not procedures. Each object possesses operations peculiar to Each object possesses operations peculiar to
it—e.g., it—e.g., aFile.aFile.open(),open(), aFile. aFile.close(),close(), aList.aList.readread(), aList.i(), aList.insertNewItemnsertNewItem(), (), aList.aList.sortsort()()
Objects are called (asked) to perform its Objects are called (asked) to perform its operation to get a job done—e.g., operation to get a job done—e.g., aFile.openaFile.open..
In OOP, the inner structure of In OOP, the inner structure of aListaList is not is not revealed to the user of the object—user revealed to the user of the object—user need not know whether it is an array, linked need not know whether it is an array, linked list, or something else.list, or something else.
Objects and ClassesObjects and Classes
An OO program consists of a collection An OO program consists of a collection of objects interacting with each other.of objects interacting with each other.
ClassClass A template for creating objectsA template for creating objects It consists of It consists of attributesattributes (characteristics) (characteristics)
and and methodsmethods ( (operationsoperations)) ObjectObject
An instance of a classAn instance of a class Many object can be created from a classMany object can be created from a class
Objects and ClassesObjects and Classes
Suppose you are writing a game that Suppose you are writing a game that involves several dogs, cats, and cars. involves several dogs, cats, and cars. (Use your imagination.)(Use your imagination.)
You need to define a You need to define a DogDog class. class. Here are some dog objects: Here are some dog objects:
Fido—a big brown bulldogFido—a big brown bulldog Lassie—a tall collie dogLassie—a tall collie dog Taro—a small, white akita dogTaro—a small, white akita dog
ObjectsObjects
Attributes: name = Fido breed = bulldog color = brown weight = 30 kg height = 50 cmOperations: bark() display()
Attributes: name = Lassie breed = collie color = brow weight = 25 kg height = 70 cmOperations bark() display()
Attributes: name = Taro breed = akita color = white weight = 10kg height = 30 cmOperations bark() display()
Dog objectDog object
Dog object
ClassClass A class A class
Is an Is an abstractionabstraction of the objects of the same of the objects of the same type. Fido, Lassie, and Taro are all type. Fido, Lassie, and Taro are all instancesinstances of the of the DogDog class. class.
Is a template for creating (specifying) Is a template for creating (specifying) objects of a particular type.objects of a particular type.
Is like a blueprint for creating objectsIs like a blueprint for creating objects Specifies attributes and methods Specifies attributes and methods
(operations)(operations) Each objects has Each objects has
Different Different attributesattributes values values SameSame operations operations
Class (cont.)Class (cont.)
A class is composed ofA class is composed of Class NameClass Name AttributesAttributes OperationsOperations
Once a class is defined, objects can Once a class is defined, objects can be created (instantiated) from it.be created (instantiated) from it.
Class DiagramClass Diagram
Dog
breedcolorweightheight
Bark()display()
Class name
Attributes
Operations
Car
makeengineSizecolormaxSpeed
Start()accelerate()stop()display()
PracticePractice
Specify some relevant attributes and Specify some relevant attributes and operations for the following classes.operations for the following classes. Bank AccountBank Account BookBook Browser windowBrowser window Circle (geometric figure)Circle (geometric figure) Rectangle (geometric figure)Rectangle (geometric figure)
Java API Java API (Application Program (Application Program
Interfaces)Interfaces) API is a collection of predefined API is a collection of predefined
classes in the library.classes in the library. You can use them to write complex You can use them to write complex
programs.programs. You should bookmark it so that you You should bookmark it so that you
can refer to it at anytime. (can refer to it at anytime. (http://java.sun.com/j2se/1.4.2/docs/ahttp://java.sun.com/j2se/1.4.2/docs/api/ pi/ ))
PackagePackage
A package is a sructure to organize a A package is a sructure to organize a collection of related classes and collection of related classes and other resources.other resources.
java.iojava.io—contains classes related to —contains classes related to File I/OFile I/O
java.utiljava.util—contains classes like Date, —contains classes like Date, Currency, and GregorianCalendarCurrency, and GregorianCalendar
javax.swingjavax.swing—contains classes for —contains classes for creating graphical interfacecreating graphical interface
Java ProgramsJava Programs
Two types of Java Programs (both use the Two types of Java Programs (both use the same syntax).same syntax). Applications—general programs that executes Applications—general programs that executes
on any computer (with a Java Virtual Machine)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 In Java, every program unit is a classclass, even , even the main program.the main program.
In a Java program, one class must contain In a Java program, one class must contain a method named a method named main().main().
Simple GreetingSimple Greeting Source codeSource code NoteNote
Import javax.swing.JOptionsPane;--for input panelImport javax.swing.JOptionsPane;--for input panel Class name Class name SimpleGreeting—SimpleGreeting—file must be saved file must be saved
asas SimpleGreeting.java. SimpleGreeting.java. Method Method main()—main()—starts the programstarts the program SimpleGreeting greet = new SimpleGreeting();--SimpleGreeting greet = new SimpleGreeting();--
instantiates an objectinstantiates an object greet.greeting();--greet.greeting();--method callmethod call System.exit(0);--System.exit(0);--terminate program when window terminate program when window
is closedis closed
Greeting with Current Greeting with Current TimeTime
Source codeSource code NoteNote
Import statements—uses three library Import statements—uses three library classesclasses
GregorianCalendar todayGregorianCalendar today;--declaration of ;--declaration of objectobject
today = new GregorianCalendar();--today = new GregorianCalendar();--instantiation (creation) of object.instantiation (creation) of object.