thus contain the descriptions of all the behaviors of the objects that it information about the name of the interface and its methods somewhere else, one could then change the internal strategy
Previous PDF | Next PDF |
Classes and Interfaces
nt wrong? Internally, HashSet's addAll method is implemented on top of its add method, although
Solutions to Exercises
identifies some behavior that is common to all objects and cannot access a specific of reserved word implements followed by the interface's name, to a class header and by
Chapter 8: Objects and Classes
(and interface methods) are public ▫ Instance variables are All methods inside the class have access to them • Can change or The internal values can be changed through
18UIT204 - Programming in Java UNIT 1 1 Encapsulation
a and internal methods using access modifiers in a class C Using Interfaces D All of the above 6
Design, implementation, and evaluation of a Java - CORE
Cité 33 fois — Figure 21: Internal method of an external generic function and its translation these message sends do not all invoke the same method A reference type in Java is any class type, interface type, or array type
Everything you wanted to know about interfaces, but were
2010 · Cité 7 fois — showing all external interfaces of your SOI with all other systems for all lifecycles Then for SOI, this approach is repeated at the next level to address your system's internal interfaces
The Java Object Model
hts Reserved Java Object Model -3 the method is a class method For example, to return an array
Principles of Object-Oriented Programming
thus contain the descriptions of all the behaviors of the objects that it information about the name of the interface and its methods somewhere else, one could then change the internal strategy
[PDF] allemand langage familier
[PDF] aller + infinitif exercices
[PDF] aller retour paris ajaccio air france
[PDF] aller retour paris nice avion
[PDF] alliance gradebook pinnacle
[PDF] allocate memory for struct in c
[PDF] alloprof fonction polynomiale du second degré
[PDF] alloschool 2 bac
[PDF] alloschool 2 bac economie examen national
[PDF] allow siri server logging
[PDF] almacenes paris puerto montt chile
[PDF] almacenes paris puerto montt telefono
[PDF] alphabet du langage des signes francais
[PDF] alphabet in french for beginners pdf
Principles of Object-Oriented
Programming
© Stephen Wong , Dung Nguyen
2@AKOGJCAKDA;=FK= ]FL=JF9LAGF9D*A;=FK= -JA?AF9DKGMJ;="-,,$6]-,1 @LLH;FP GJ?;GFL=FLK:9<;>>=9<:>=
.JAF;AHD=KG> -:B=;L-JA=FL=< Contents
Chapter 1 Introduction
1.1 Abstraction
]FLJG2QH= :KLJ9;LAGF
+G<=DAF?9.=JKGF -:B=;LK9F<"D9KK=K -:B=;LK "D9KK=K ]EHD=E=FL9LAGFAF(9N9 #GOFDG9<;G<= -:B=;L0=D9LAGFK@AHK ]K9GJ]F@=JAL9F;= '9K9GJ"GEHGKALAGF GJ?;GFL=FLK:9<;>>=9<:>=
.JAF;AHD=KG> -:B=;L-JA=FL=<Contents
Chapter 1 Introduction
1.1 Abstraction
]FLJG3+*#A9?J9EK
"D9KK#A9?J9EKChapter 2 Polymorphism in Action
2.1 Union Design Pattern: Inheritance and Polymorphism
3FAGF#=KA?F.9LL=JF
:KLJ9;LAGFNK "GEEGF9DALQ ]F@=JAL9F;=9F<.GDQEGJH@AKE $PHDGJAF?.GDQEGJH@AKE $P=J;AK= $P=J;AK= $P=J;AK= $P=J;AK= $P=J;AK= $P=J;AK= $P=J;AK= $P=J;AK= $P=J;AK= $P=J;AK= $P=J;AK= !9DDOGJD49JA9FLNK
]FN9JA9FL!=@9NAGJK (9N91QFL9P]KKM=K .9;C9?=K1L9LA;Y=D "9DDAF?E=L@GL@=KMH=J;D9KK !9DDOGJD<;GEHGKALAGF:9K=< 2.3.1 The Problem with Inheritance
.ARR9K9F<1@9H=K %JGE]F@=JAL9F;=LG"GEHGKALAGF "GEHGKAF?!=@9NAGJK !9DDKL@9L;@9F?=L@=AJKLJ9L=?A=K $P=J;AK= !9DDKOAL@EMDLAHD=;GEHGKAL=:=@9NAGJK $P=J;AK= $P=J;AK= "GEHGKAL=.9LL=JFK 1GDMLAGFKLG$P=J;AK=KAF"@9HL=J
Chapter 3 Immutable List Structure
3.1 List Structure and the Composite Design Pattern
&GAF?1@GHHAF? 5@9LAK9DAKL
*AKL#=KA?F9F5@9L;9F9DAKL "G<=2=EHD9L= 0=;MJKAGF
0=;MJKAN=#9L91LJM;LMJ=K
0=;MJKAN= D?GJAL@EK
29AD0=;MJKAGF
4AKALGJ#=KA?F.9LL=JF
#=;GMHDAF? D?GJAL@EK>JGE#9L91LJM;LMJ=K 2G"GGCGJ,GL2G"GGC
2@=%GG<
2@=%GG<"GFKME=JK
2@="@=>
-J<=JAF?%GG<%JGE2@="@=> 2@=4AKALGJ.9LL=JF
*AKL4AKALGJ$P9EHD=K :KLJ9;L%9;LGJQ#=KA?F.9LL=JF ]F>GJE9LAGF'A3.6.4 Factory with Anonymous Inner Classes
"D9KK=K<=YF=2.3.1 The Problem with Inheritance
.ARR9K9F<1@9H=K %JGE]F@=JAL9F;=LG"GEHGKALAGF "GEHGKAF?!=@9NAGJK !9DDKL@9L;@9F?=L@=AJKLJ9L=?A=K $P=J;AK= !9DDKOAL@EMDLAHD=;GEHGKAL=:=@9NAGJK $P=J;AK= $P=J;AK= "GEHGKAL=.9LL=JFK1GDMLAGFKLG$P=J;AK=KAF"@9HL=J
Chapter 3 Immutable List Structure
3.1 List Structure and the Composite Design Pattern
&GAF?1@GHHAF?5@9LAK9DAKL
*AKL#=KA?F9F0=;MJKAGF
0=;MJKAN=#9L91LJM;LMJ=K
0=;MJKAN= D?GJAL@EK
29AD0=;MJKAGF
4AKALGJ#=KA?F.9LL=JF
#=;GMHDAF? D?GJAL@EK>JGE#9L91LJM;LMJ=K2G"GGCGJ,GL2G"GGC
2@=%GG<
2@=%GG<"GFKME=JK
2@="@=>
-J<=JAF?%GG<%JGE2@="@=>2@=4AKALGJ.9LL=JF
*AKL4AKALGJ$P9EHD=K :KLJ9;L%9;LGJQ#=KA?F.9LL=JF ]F>GJE9LAGF'A1;GH=1H=;AY=J
;;=KK1H=;AY=J $PL=FKA:ADALQ1H=;AY=J :KLJ9;L1H=;AY=J ]F@=JAL9F;=1H=;AY=J 3K9?= "DGKMJ= $P9EHD=KChapter 4 Mutable Data Structures
4.1 State Design Pattern
/M=KLAGF -FO9J< +ML9:D=*AF=9J0=;MJKAN=1LJM;LMJ=1L9L=.9LL=JF9F<#QF9EA;0=;D9KKAY;9LAGF
+ML9:D=*AF=9J0=;MJKAN=1LJM;LMJ=%J9E=OGJC $P9EHD= !AF9JQ2J==1LJM;LMJ= !AF9JQ2J==-:B=;L+G<=D ]EHD=E=FL9LAGF#=L9ADK JJ9QK9F< JJ9Q.JG;=KKAF? $P9EHD= JJ9QKAF(9N9 JJ9Q2QH=K JJ9Q49JA9:D=K JJ9Q"J=9LAGF JJ9Q ;;=KK=K JJ9Q.JG;=KKAF?3KAF?*GGHK5@AD=DGGHK
>GJ=9;@DGGHK JJ9QKNK *AKLKChapter 5 Restricted Access Containers
5.1 Restricted Access Containers
]FLJG09F B9N9 -J<=JAF?-:B=;L9F<.JAGJALQ/M=M= Chapter 6 GUI Programming
6.1 Graphical User Interfaces in Java
&J9H@A;9D3K=J]FL=J>9;=KAF(9N9 :KLJ9;L%J9E= %J9E= B9N9 $N=FL 49JA9FL!=@9NAGJK
"9N=9L $P=J;AK= 1AEHD=(%J9E=%J9E=G
B9N9 B9N9 1LJ9L=?Q.9LL=JF
+GJ=(9N9&3].JG?J9EEAF? (%J9E=OAL@(!MLLGFK9F<=N=FL@9FChapter 7 Labs 7.1 DrJava
$0MFFAF?
Chapter 6 GUI Programming
6.1 Graphical User Interfaces in Java
&J9H@A;9D3K=J]FL=J>9;=KAF(9N9 :KLJ9;L%J9E= %J9E= B9N9 $N=FL49JA9FL!=@9NAGJK
"9N=9L $P=J;AK=1AEHD=(%J9E=%J9E=G
B9N9 B9N91LJ9L=?Q.9LL=JF
+GJ=(9N9&3].JG?J9EEAF? (%J9E=OAL@(!MLLGFK9F<=N=FL@9F7.1 DrJava
$2=KLAF?
#=:M??AF?3FAL2=KLAF?OAL@(3FALAF#J(9N9
3KAF?(3FALAF#J(9N9
9KK=JL$IM9DK
9KK=JL2JM=
>9AD <8.1 Java Syntax Primer
"GFKLJM;LGJK $P9EHD= $P9EHD=Attributions
Principles of Object-Oriented Programming
Chapter 1 Introduction
1.1 Abstraction
1.1.1 Introduction
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF KGJ?DA;=FK=K:QK9
Abstraction is the process of hiding the details and exposing only the essential >=9LMJ=KG>9H9JLA;MD9J;GF;=HLGJG:B=;L "GEHML=JK;A=FLAKLKMK=9:KLJ9;LAGFLGAFKGE=H9JLA;MD9J;GEHML=JD9F?M9?=
5=ADDMKLJ9L=L@AKHJG;=KK:QO9QG>LJQAF?LG
Problem:Given a rectangle 4.5 ft wide and 7.2 ft high, compute its area.1G9DDO=@9N=LG KGDN=L@=9:GN=HJG:D=EAKLGEMDLAHDQ
:Q 9F=LL@=9FKO=J
2@=IM=KLAGFAK
;GEHML9LAGF 1.1.2 Data Abstraction
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
The product of 4.5 by 7.2 is expressed in Java as:4.5 * 7.2. In this expression, the 9F< 9J=;9DD= literals. Using DrJava, we can type in the expression4.5 * 7.2directly in the AFL=J9;LAGFKOAF 9F<@=A?@L
@9KL@=essenceof the original problem changed? After all, the formula for computing L@=9FKO=JAKKLADDL@=K9E=
DDO=@9N=LG1.1.2.1 Type Abstraction
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
The problem has not changed in that it still deals with the same geometric shape, a 5@9LN9JQ1
are simply the values of the width and the height. The formula to compute the area of width * height 5@9LAL;9J=K
GH=J9LAGFE9C=KK=FK=
'GOK=LG>J=9DFME:=JK 9K=L9F<;9DD= 2@=<=L9ADKG>L@AKAFL=JF9D
]F9<AFL=?=JK9FL@=E9KL@=AJF==<9JAK=KAF>MLMJ==P9EHD=K KLGGMJHJG:D=EO=GFDQF==J=KLJA;LGMJK=DN=KLGL@=LQH= double width; double height; 2@=9:GN=LOGKL9L=E=FLK9J=;9DD= K9A ;GEHML=J 2@AK9(9N9
KQFL9PJMD=
4AGD9LAF?9KQFL9PJMD=;GFKLALML=K9F=JJGJ
5@=FO=<=YF=9N9JA9:D=AF
L@=(9N9D9F?M9?=
Note:Use the interactions paneof DrJa
va to evaluate width and height andverify that their values are set to 0. Once we have deifined the width and height variables, we can solve our problem by OAGDDGOK
width * height @=A?@L ,GO operation. To assign the value 4.5 to the variable width and the value 7.2 to the width = 4.5; height = 7.2; =PHJ=KKAGF9F2@=K=E9FLA;A
E=9FAF?G>KM;@9F
GFL@=D=>L@9FL@==IM9DKA<=
]LAK9F=JJGJA>L@=LQH=G>L@==PHJ=KKAGFGF L@=JA?@L@9FO==N9DM9L=width * heightagain (using the Interactions Window of DrJava), we K@GMD=LL@=<=KAJ=<9FKO=J
*A>=AK?GG9JL@GM?@L@=J=AK9DALLD=:ALG> AF;GFN=FA=F;=@=J=O=@9N=LGLQH=L@==PHJ=KKAGFwidth * heighteach time we are 2@AK ]KL@=J=9O9QLG@9N=L@= AFHMLKLOGJ=9DFME:=JKJ=;9DDLQH=double) with a button. When we put in two 2@AK:D9;C
9J=;L9F?D=?AN=F9OA 2G:MAD ;9DD=<9;D9KKO@A;@DGGCKDAC=L@=>GDDGOAF? class AreaCalc { double rectangle(double width, double height) { return width * height; 5@9LL@AK(9N9;G<=E=9FKAKKGE=L@AF?DAC=AreaCalcis ablue printof a specialized
;GEHMLAF?E9;@AF=L@9LAK;9H9:D=G>9;;=HLAF?LOGAFHMLdoubles, one labeled width 2@AK ;GEHML9LAGFAK?AN=F9F9E=rectangle. In Java parlance, it is called amethodfor the ;D9KKAreaCalc. '=J=AK9F=P9EHD=G>@GOO=MK=AreaCalcto compute area of a rectangle of width 9F<@=A?@L
3 AreaCalc calc = new AreaCalc();
calc.rectangle(4.5, 7.2) The ifirst line of code deifinescalcas a variable of typeAreaCalcand assign to it an instanceof the classAreaCalc.newis a keyword in Java. It is an example of what is ;9DD=<9;D9KKGH=J9LGJ object) of the given class. The second line of code is a call to the objectcalcto H=J>GJEL@=rectangletask wherewidthis assigned the value 4.5 andheightis 9KKA?F= 2G?=LL@=9J=9G>9
:Q J=;L9F?D=O=KAEHDQMK=L@=
K9E=;9D;MD9LGJ;9D;9?9AF
calc.rectangle(5.6, 8.4); >LOA<=9F< >L@A?@ J=;L9F?D=
:9K=5=;9FFGLKAEHDQMK=L@AK;9D;MD9LGJ 5=F==<9FGL@=JKH=;A9DAR=<;9D;MD9LGJ
L@=CAF9;AJ;D=
X;J=9L=9F=O;D9KK;9DD=< J=9"9D;OAL@GF=E=L@G<;9DD=LQH=double. This corresponds to designing a diffferent 9J=9;9D;MD9LGJOAL@GF=:MLLGFD9:=D= X9<LQH= double. This corresponds to designing an area calculator with two buttons: one D9:=D=OAL@LOGAFHMLKDGLK 9J=9G>L@=?AN=F?=GE=LJA;K@9H=
1AF;=L@=LOG;GEHML9LAGFKJ=IMAJ==P9;LDQL@=
:=;9J=>MDFGL?=LEAP= 2;12number of shapes? How can we, as programmers, buid a calculator that can
@9F1.1.2.2 Modeling a Person
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
For the ifirst few years of his life, Peter did not have a clue what birthdays were, let 9DGF=@AKGOF:AJL@<9L=
:AJL@<9Q 9 AFL=DDA?=F;=9F<;9H9:ADALQ
,GO.=L=JAK9;GDD=?=KLM<=FL 2@=J=AK9HA=;=G>E=EGJQ
KE9JLH=JKGF
'GOX9:AJL@<9L=9F< EGFL@ :AJL@<9L=;GFKAKLKG>9EGFL@9<9Q9F<9Q=9J $9;@G>L@=K=<9L9;9F:= KAFL@= ;D9KK DMEH:GL@L@=<9L9A
AFLGGF=;D9KK
;9DD=<=F;9HKMD9LAGF 2@=DAF=
1public class Person {
2/** 3* All data fields are private in order to prevent code
outside of this 4* class to access them.
5*/ 6private int bDay; // birth day
7private int bMonth; // birth month; for example, 3 means
March.
8private int bYear; // birth year
9 10* Constructor: a special code used to initialize the fields
of the class. 11* The only way to instantiate a Person object is to call
new on the constructor.5 12* For example: new Person(28, 2, 1945) will create a Person
object with 13* birth date February 28, 1945.
14*/ 15public Person(int day, int month, int year) {
16_bDay = day;
17_bMonth = month;
18_bYear = year;
19} 20/** 21* Uses "modulo" arithmetic to compute the number of months
till the next 22* birth day given the current month.
23* @param currentMonth an int representing the current
month. 24*/
25public int nMonthTillBD(int currentMonth) {
26return (_bMonth - currentMonth + 12) % 12;
27}
28}
Download the above code:
ZPerson.java(http://www
We now explain what the above Java code means.
XDAF=<=YF=K9;D9KK;9DD=<.=JKGF
2@=GH=FAF?;MJDQ:J9;=9LL@==FL@=DAF=
2@=C=QOGJ KQKL=E;9FJ=>=J=F;=L@AK;D9KK
XDAF=K9J=;GEE=FLK
$N=JQL@AF?:=LO==F/*and*/are ingored by the ;GEHAD=J XDAF=K<=YF=L@J==AFL=?=JN9JA9:D=K
2@=K=N9JA9:D=K9J=;9DD= ;D9KK ;G<=GMLKA<=G>L@=;D9KK -FDQ;G<=AFKA<=G>L@=;D9KK;9F9;;=KKL@=E $9;@Y=D< AK>GDDGO=<:Q9;GEE=FL<=DAEAL=<:Q//and the end-of-line. So there two ways LG;GEE=FL;G<=AF(9N9KL9JLOAL@/*and end with*/or start with//and =FDAF= XDAF=K9J=;GEE=FLK
XDAF=K;GFKLALML=O@9LAK;9DD=<9constructor. It is used to initialize the ifields G>L@=;D9KKLGKGE=H9JLA;MD9JN9DM=K
2@=F9E=G>L@=;GFKLJM;LGJK@GMD =P9;LDQDAC=L@=;D9KKF9E= '=J=ALAKpublic, meaning it can be called by code GMLKA<=G>L@=;D9KK.=JKGFNA9L@=GH=J9LGJF=O
%GJ=P9EHD=F=OPerson(28, 2,6 1945)will create an instance of a Person with _bDay = 28, _bMonth = 2 and _bYear
XDAF=K9J=;GEE=FLK
2@AK >GJE9LAK;9DD=GJE9L 5=OADDD=9JFEGJ=9:GMLB9N9 EG :AJL@<9QMKAF?L@=J=E9AF<=JGH=J9LGJ PQ?AN=KL@=J=E9AF<=JG>L@=AFL=?=J
1.2 Objects and Classes
1.2.1 Objects
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
In the "real" world, objects are the entities of which the world is comprised. Everything OGJD< 2@=J=9D
5@AD==9;@G:B=;LE9Q
L@=F9LMJ9DOGJD<
KQKL=EK
]F9FG:B=;LGJA=FL=LO9J=KQKL=Eobjectsare entities used to represent or model 9H9JLA;MD9JHA=;=G>L@=KQKL=E
Objects are the primary units used to create abstract models. "There are a number of schools of object-oriented programming, which difffer slightly GF@GOL@=QNA=OG:B=;LK
'=J=O=OADDL9C=9:=@9NAGJAKLGMJL=JEKL9F;= An object is characterized solely by it behaviors. "Essentially this deifines an object by the way it interacts with its world. An object that ;;=KKLGAFL=JF9DDQ ]LAK <9L9AK:=AF?KLGJ=9FGL@=JG:B=;L "7 Note:A beautiful example of a model that exhibits a particular beha vior butwithout exactly replicating the mechanics we expect to produce that beha vioris the "Dragon" optical illusion. A printout to create this simple folded paper display can be found at theweb site of the Grand Illusions T oy Shop in England(http://www.grand-illusions.com/opticalillusions/dragon%20illusion/) This does not mean however, that an object may not contain data (information) in Y=DJ=KMDLG>ALK<9L9K@GMD The existence of data in an object is an implementation technique used to 1.2.2 Classes
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
Many objects difffer from each other only in thevalueof the data that they hold. For -MJG:B=;L L@=N9DM=G>ALK;GDGJ
equivalent, difffering only in the values of their attributes and perhaps, thus difffering AFL@=:=@9NAGJKL@9LJ=KMDL>JGEL@GK=N9DM=K
EGJ=KH=;A>Q<=L9ADK
%GJ ;@9J9;L=JAKLA;KG>9DD JL@JGHG2@=FGLAGFG>9F
9:KLJ9;LMFAY= -:B=;LGJA=FL=J=HJ=K=FLK methods. In addition, a class may, but not always, contain descriptions of the internal <9L9@=D<:QL@=G:B=;LK;9DD=E=L@G "8 Turning the description around, we can say that a class is a template or recipe for the ;J=9LAGFG>9H9JLA;MD9JLQH=G>G:B=;L 2@9LAKGF=;9FMK=9;D9KKLG;J=9L=
instantiate") objects of the type described by the class. Be careful not to make the ;D9KKAK9 ]FL=;@FA;9DL=JEK9;D9KK<=YF=K9F=Otypein the system. Types are identiifies usedquotesdbs_dbs19.pdfusesText_25
KGDN=L@=9:GN=HJG:D=EAKLGEMDLAHDQ
:Q9F=LL@=9FKO=J
2@=IM=KLAGFAK
;GEHML9LAGF1.1.2 Data Abstraction
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF KGJ?DA;=FK=K:QK9
The product of 4.5 by 7.2 is expressed in Java as:4.5 * 7.2. In this expression, the 9F<9J=;9DD= literals. Using DrJava, we can type in the expression4.5 * 7.2directly in the AFL=J9;LAGFKOAF 9F<@=A?@L
@9KL@=essenceof the original problem changed? After all, the formula for computing L@=9FKO=JAKKLADDL@=K9E=
DDO=@9N=LG1.1.2.1 Type Abstraction
AFL=J9;LAGFKOAF 9F<@=A?@L
@9KL@=essenceof the original problem changed? After all, the formula for computing L@=9FKO=JAKKLADDL@=K9E=
DDO=@9N=LG1.1.2.1 Type Abstraction
9F<@=A?@L
@9KL@=essenceof the original problem changed? After all, the formula for computingL@=9FKO=JAKKLADDL@=K9E=
DDO=@9N=LGAvailable under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF KGJ?DA;=FK=K:QK9
The problem has not changed in that it still deals with the same geometric shape, a5@9LN9JQ1
are simply the values of the width and the height. The formula to compute the area of width * height5@9LAL;9J=K
GH=J9LAGFE9C=KK=FK=
'GO9K=L9F<;9DD= 2@=<=L9ADKG>L@AKAFL=JF9D
]F9<AFL=?=JK9FL@=E9KL@=AJF==<9JAK=KAF>MLMJ==P9EHD=K KLGGMJHJG:D=EO=GFDQF==J=KLJA;LGMJK=DN=KLGL@=LQH= double width; double height; 2@=9:GN=LOGKL9L=E=FLK9J=;9DD= K9A ;GEHML=J 2@AK9(9N9
KQFL9PJMD=
4AGD9LAF?9KQFL9PJMD=;GFKLALML=K9F=JJGJ
5@=FO=<=YF=9N9JA9:D=AF
L@=(9N9D9F?M9?=
Note:Use the interactions paneof DrJa
va to evaluate width and height andverify that their values are set to 0. Once we have deifined the width and height variables, we can solve our problem by OAGDDGOK
width * height @=A?@L ,GO operation. To assign the value 4.5 to the variable width and the value 7.2 to the width = 4.5; height = 7.2; =PHJ=KKAGF9F2@=K=E9FLA;A
E=9FAF?G>KM;@9F
GFL@=D=>L@9FL@==IM9DKA<=
]LAK9F=JJGJA>L@=LQH=G>L@==PHJ=KKAGFGF L@=JA?@L@9FO==N9DM9L=width * heightagain (using the Interactions Window of DrJava), we K@GMD=LL@=<=KAJ=<9FKO=J
*A>=AK?GG9JL@GM?@L@=J=AK9DALLD=:ALG> AF;GFN=FA=F;=@=J=O=@9N=LGLQH=L@==PHJ=KKAGFwidth * heighteach time we are 2@AK ]KL@=J=9O9QLG@9N=L@= AFHMLKLOGJ=9DFME:=JKJ=;9DDLQH=double) with a button. When we put in two 2@AK:D9;C
9J=;L9F?D=?AN=F9OA 2G:MAD ;9DD=<9;D9KKO@A;@DGGCKDAC=L@=>GDDGOAF? class AreaCalc { double rectangle(double width, double height) { return width * height; 5@9LL@AK(9N9;G<=E=9FKAKKGE=L@AF?DAC=AreaCalcis ablue printof a specialized
;GEHMLAF?E9;@AF=L@9LAK;9H9:D=G>9;;=HLAF?LOGAFHMLdoubles, one labeled width 2@AK ;GEHML9LAGFAK?AN=F9F9E=rectangle. In Java parlance, it is called amethodfor the ;D9KKAreaCalc. '=J=AK9F=P9EHD=G>@GOO=MK=AreaCalcto compute area of a rectangle of width 9F<@=A?@L
3 AreaCalc calc = new AreaCalc();
calc.rectangle(4.5, 7.2) The ifirst line of code deifinescalcas a variable of typeAreaCalcand assign to it an instanceof the classAreaCalc.newis a keyword in Java. It is an example of what is ;9DD=<9;D9KKGH=J9LGJ object) of the given class. The second line of code is a call to the objectcalcto H=J>GJEL@=rectangletask wherewidthis assigned the value 4.5 andheightis 9KKA?F= 2G?=LL@=9J=9G>9
:Q J=;L9F?D=O=KAEHDQMK=L@=
K9E=;9D;MD9LGJ;9D;9?9AF
calc.rectangle(5.6, 8.4); >LOA<=9F< >L@A?@ J=;L9F?D=
:9K=5=;9FFGLKAEHDQMK=L@AK;9D;MD9LGJ 5=F==<9FGL@=JKH=;A9DAR=<;9D;MD9LGJ
L@=CAF9;AJ;D=
X;J=9L=9F=O;D9KK;9DD=< J=9"9D;OAL@GF=E=L@G<;9DD=LQH=double. This corresponds to designing a diffferent 9J=9;9D;MD9LGJOAL@GF=:MLLGFD9:=D= X9<LQH= double. This corresponds to designing an area calculator with two buttons: one D9:=D=OAL@LOGAFHMLKDGLK 9J=9G>L@=?AN=F?=GE=LJA;K@9H=
1AF;=L@=LOG;GEHML9LAGFKJ=IMAJ==P9;LDQL@=
:=;9J=>MDFGL?=LEAP= 2;12number of shapes? How can we, as programmers, buid a calculator that can
@9F1.1.2.2 Modeling a Person
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
For the ifirst few years of his life, Peter did not have a clue what birthdays were, let 9DGF=@AKGOF:AJL@<9L=
:AJL@<9Q 9 AFL=DDA?=F;=9F<;9H9:ADALQ
,GO.=L=JAK9;GDD=?=KLM<=FL 2@=J=AK9HA=;=G>E=EGJQ
KE9JLH=JKGF
'GOX9:AJL@<9L=9F< EGFL@ :AJL@<9L=;GFKAKLKG>9EGFL@9<9Q9F<9Q=9J $9;@G>L@=K=<9L9;9F:= KAFL@= ;D9KK DMEH:GL@L@=<9L9A
AFLGGF=;D9KK
;9DD=<=F;9HKMD9LAGF 2@=DAF=
1public class Person {
2/** 3* All data fields are private in order to prevent code
outside of this 4* class to access them.
5*/ 6private int bDay; // birth day
7private int bMonth; // birth month; for example, 3 means
March.
8private int bYear; // birth year
9 10* Constructor: a special code used to initialize the fields
of the class. 11* The only way to instantiate a Person object is to call
new on the constructor.5 12* For example: new Person(28, 2, 1945) will create a Person
object with 13* birth date February 28, 1945.
14*/ 15public Person(int day, int month, int year) {
16_bDay = day;
17_bMonth = month;
18_bYear = year;
19} 20/** 21* Uses "modulo" arithmetic to compute the number of months
till the next 22* birth day given the current month.
23* @param currentMonth an int representing the current
month. 24*/
25public int nMonthTillBD(int currentMonth) {
26return (_bMonth - currentMonth + 12) % 12;
27}
28}
Download the above code:
ZPerson.java(http://www
We now explain what the above Java code means.
XDAF=<=YF=K9;D9KK;9DD=<.=JKGF
2@=GH=FAF?;MJDQ:J9;=9LL@==FL@=DAF=
2@=C=QOGJ KQKL=E;9FJ=>=J=F;=L@AK;D9KK
XDAF=K9J=;GEE=FLK
$N=JQL@AF?:=LO==F/*and*/are ingored by the ;GEHAD=J XDAF=K<=YF=L@J==AFL=?=JN9JA9:D=K
2@=K=N9JA9:D=K9J=;9DD= ;D9KK ;G<=GMLKA<=G>L@=;D9KK -FDQ;G<=AFKA<=G>L@=;D9KK;9F9;;=KKL@=E $9;@Y=D< AK>GDDGO=<:Q9;GEE=FL<=DAEAL=<:Q//and the end-of-line. So there two ways LG;GEE=FL;G<=AF(9N9KL9JLOAL@/*and end with*/or start with//and =FDAF= XDAF=K9J=;GEE=FLK
XDAF=K;GFKLALML=O@9LAK;9DD=<9constructor. It is used to initialize the ifields G>L@=;D9KKLGKGE=H9JLA;MD9JN9DM=K
2@=F9E=G>L@=;GFKLJM;LGJK@GMD =P9;LDQDAC=L@=;D9KKF9E= '=J=ALAKpublic, meaning it can be called by code GMLKA<=G>L@=;D9KK.=JKGFNA9L@=GH=J9LGJF=O
%GJ=P9EHD=F=OPerson(28, 2,6 1945)will create an instance of a Person with _bDay = 28, _bMonth = 2 and _bYear
XDAF=K9J=;GEE=FLK
2@AK >GJE9LAK;9DD=GJE9L 5=OADDD=9JFEGJ=9:GMLB9N9 EG :AJL@<9QMKAF?L@=J=E9AF<=JGH=J9LGJ PQ?AN=KL@=J=E9AF<=JG>L@=AFL=?=J
1.2 Objects and Classes
1.2.1 Objects
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
In the "real" world, objects are the entities of which the world is comprised. Everything OGJD< 2@=J=9D
5@AD==9;@G:B=;LE9Q
L@=F9LMJ9DOGJD<
KQKL=EK
]F9FG:B=;LGJA=FL=LO9J=KQKL=Eobjectsare entities used to represent or model 9H9JLA;MD9JHA=;=G>L@=KQKL=E
Objects are the primary units used to create abstract models. "There are a number of schools of object-oriented programming, which difffer slightly GF@GOL@=QNA=OG:B=;LK
'=J=O=OADDL9C=9:=@9NAGJAKLGMJL=JEKL9F;= An object is characterized solely by it behaviors. "Essentially this deifines an object by the way it interacts with its world. An object that ;;=KKLGAFL=JF9DDQ ]LAK <9L9AK:=AF?KLGJ=9FGL@=JG:B=;L "7 Note:A beautiful example of a model that exhibits a particular beha vior butwithout exactly replicating the mechanics we expect to produce that beha vioris the "Dragon" optical illusion. A printout to create this simple folded paper display can be found at theweb site of the Grand Illusions T oy Shop in England(http://www.grand-illusions.com/opticalillusions/dragon%20illusion/) This does not mean however, that an object may not contain data (information) in Y=DJ=KMDLG>ALK<9L9K@GMD The existence of data in an object is an implementation technique used to 1.2.2 Classes
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
Many objects difffer from each other only in thevalueof the data that they hold. For -MJG:B=;L L@=N9DM=G>ALK;GDGJ
equivalent, difffering only in the values of their attributes and perhaps, thus difffering AFL@=:=@9NAGJKL@9LJ=KMDL>JGEL@GK=N9DM=K
EGJ=KH=;A>Q<=L9ADK
%GJ ;@9J9;L=JAKLA;KG>9DD JL@JGHG2@=FGLAGFG>9F
9:KLJ9;LMFAY= -:B=;LGJA=FL=J=HJ=K=FLK methods. In addition, a class may, but not always, contain descriptions of the internal <9L9@=D<:QL@=G:B=;LK;9DD=E=L@G "8 Turning the description around, we can say that a class is a template or recipe for the ;J=9LAGFG>9H9JLA;MD9JLQH=G>G:B=;L 2@9LAKGF=;9FMK=9;D9KKLG;J=9L=
instantiate") objects of the type described by the class. Be careful not to make the ;D9KKAK9 ]FL=;@FA;9DL=JEK9;D9KK<=YF=K9F=Otypein the system. Types are identiifies usedquotesdbs_dbs19.pdfusesText_25
2@=<=L9ADKG>L@AKAFL=JF9D
]F9<2@=9:GN=LOGKL9L=E=FLK9J=;9DD= K9A ;GEHML=J 2@AK9(9N9
KQFL9PJMD=
4AGD9LAF?9KQFL9PJMD=;GFKLALML=K9F=JJGJ
5@=FO=<=YF=9N9JA9:D=AF
L@=(9N9D9F?M9?=
Note:Use the interactions paneof DrJa
va to evaluate width and height andverify that their values are set to 0. Once we have deifined the width and height variables, we can solve our problem by OAGDDGOK
width * height @=A?@L ,GO operation. To assign the value 4.5 to the variable width and the value 7.2 to the width = 4.5; height = 7.2; =PHJ=KKAGF9F2@=K=E9FLA;A
K9A ;GEHML=J 2@AK9(9N9
KQFL9PJMD=
4AGD9LAF?9KQFL9PJMD=;GFKLALML=K9F=JJGJ
5@=FO=<=YF=9N9JA9:D=AF
L@=(9N9D9F?M9?=
Note:Use the interactions paneof DrJa
va to evaluate width and height andverify that their values are set to 0. Once we have deifined the width and height variables, we can solve our problem by OAGDDGOK
width * height @=A?@L ,GO operation. To assign the value 4.5 to the variable width and the value 7.2 to the width = 4.5; height = 7.2; =PHJ=KKAGF9F2@=K=E9FLA;A
2@AK9(9N9
KQFL9PJMD=
4AGD9LAF?9KQFL9PJMD=;GFKLALML=K9F=JJGJ
5@=FO=<=YF=9N9JA9:D=AF
L@=(9N9D9F?M9?=
Note:Use the interactions paneof DrJa
va to evaluate width and height andverify that their values are set to 0. Once we have deifined the width and height variables, we can solve our problem byOAGDDGOK
width * height @=A?@L ,GO operation. To assign the value 4.5 to the variable width and the value 7.2 to the width = 4.5; height = 7.2; =PHJ=KKAGF9FE=9FAF?G>KM;@9F
GFL@=D=>L@9FL@==IM9DKA<=
]LAK9F=JJGJA>L@=LQH=G>L@==PHJ=KKAGFGF L@=JA?@L@9FK@GMD=LL@=<=KAJ=<9FKO=J
*A>=AK?GG2@AK:D9;C
9J=;L9F?D=?AN=F9OA 2G:MAD ;9DD=<9;D9KKO@A;@DGGCKDAC=L@=>GDDGOAF? class AreaCalc { double rectangle(double width, double height) { return width * height; 5@9LL@AK(9N9;G<=E=9FKAKKGE=L@AF?DAC=AreaCalcis ablue printof a specialized
;GEHMLAF?E9;@AF=L@9LAK;9H9:D=G>9;;=HLAF?LOGAFHMLdoubles, one labeled width 2@AK ;GEHML9LAGFAK?AN=F9F9E=rectangle. In Java parlance, it is called amethodfor the ;D9KKAreaCalc. '=J=AK9F=P9EHD=G>@GOO=MK=AreaCalcto compute area of a rectangle of width 9F<@=A?@L
3 AreaCalc calc = new AreaCalc();
calc.rectangle(4.5, 7.2) The ifirst line of code deifinescalcas a variable of typeAreaCalcand assign to it an instanceof the classAreaCalc.newis a keyword in Java. It is an example of what is ;9DD=<9;D9KKGH=J9LGJ object) of the given class. The second line of code is a call to the objectcalcto H=J>GJEL@=rectangletask wherewidthis assigned the value 4.5 andheightis 9KKA?F= 2G?=LL@=9J=9G>9
:Q J=;L9F?D=O=KAEHDQMK=L@=
K9E=;9D;MD9LGJ;9D;9?9AF
calc.rectangle(5.6, 8.4); >LOA<=9F< >L@A?@ J=;L9F?D=
:9K=5=;9FFGLKAEHDQMK=L@AK;9D;MD9LGJ 5=F==<9FGL@=JKH=;A9DAR=<;9D;MD9LGJ
L@=CAF9;AJ;D=
X;J=9L=9F=O;D9KK;9DD=< J=9"9D;OAL@GF=E=L@G<;9DD=LQH=double. This corresponds to designing a diffferent 9J=9;9D;MD9LGJOAL@GF=:MLLGFD9:=D= X9<LQH= double. This corresponds to designing an area calculator with two buttons: one D9:=D=OAL@LOGAFHMLKDGLK 9J=9G>L@=?AN=F?=GE=LJA;K@9H=
1AF;=L@=LOG;GEHML9LAGFKJ=IMAJ==P9;LDQL@=
:=;9J=>MDFGL?=LEAP= 2;12number of shapes? How can we, as programmers, buid a calculator that can
@9F1.1.2.2 Modeling a Person
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
For the ifirst few years of his life, Peter did not have a clue what birthdays were, let 9DGF=@AKGOF:AJL@<9L=
:AJL@<9Q 9 AFL=DDA?=F;=9F<;9H9:ADALQ
,GO.=L=JAK9;GDD=?=KLM<=FL 2@=J=AK9HA=;=G>E=EGJQ
KE9JLH=JKGF
'GOX9:AJL@<9L=9F< EGFL@ :AJL@<9L=;GFKAKLKG>9EGFL@9<9Q9F<9Q=9J $9;@G>L@=K=<9L9;9F:= KAFL@= ;D9KK DMEH:GL@L@=<9L9A
AFLGGF=;D9KK
;9DD=<=F;9HKMD9LAGF 2@=DAF=
1public class Person {
2/** 3* All data fields are private in order to prevent code
outside of this 4* class to access them.
5*/ 6private int bDay; // birth day
7private int bMonth; // birth month; for example, 3 means
March.
8private int bYear; // birth year
9 10* Constructor: a special code used to initialize the fields
of the class. 11* The only way to instantiate a Person object is to call
new on the constructor.5 12* For example: new Person(28, 2, 1945) will create a Person
object with 13* birth date February 28, 1945.
14*/ 15public Person(int day, int month, int year) {
16_bDay = day;
17_bMonth = month;
18_bYear = year;
19} 20/** 21* Uses "modulo" arithmetic to compute the number of months
till the next 22* birth day given the current month.
23* @param currentMonth an int representing the current
month. 24*/
25public int nMonthTillBD(int currentMonth) {
26return (_bMonth - currentMonth + 12) % 12;
27}
28}
Download the above code:
ZPerson.java(http://www
We now explain what the above Java code means.
XDAF=<=YF=K9;D9KK;9DD=<.=JKGF
2@=GH=FAF?;MJDQ:J9;=9LL@==FL@=DAF=
2@=C=QOGJ KQKL=E;9FJ=>=J=F;=L@AK;D9KK
XDAF=K9J=;GEE=FLK
$N=JQL@AF?:=LO==F/*and*/are ingored by the ;GEHAD=J XDAF=K<=YF=L@J==AFL=?=JN9JA9:D=K
2@=K=N9JA9:D=K9J=;9DD= ;D9KK ;G<=GMLKA<=G>L@=;D9KK -FDQ;G<=AFKA<=G>L@=;D9KK;9F9;;=KKL@=E $9;@Y=D< AK>GDDGO=<:Q9;GEE=FL<=DAEAL=<:Q//and the end-of-line. So there two ways LG;GEE=FL;G<=AF(9N9KL9JLOAL@/*and end with*/or start with//and =FDAF= XDAF=K9J=;GEE=FLK
XDAF=K;GFKLALML=O@9LAK;9DD=<9constructor. It is used to initialize the ifields G>L@=;D9KKLGKGE=H9JLA;MD9JN9DM=K
2@=F9E=G>L@=;GFKLJM;LGJK@GMD =P9;LDQDAC=L@=;D9KKF9E= '=J=ALAKpublic, meaning it can be called by code GMLKA<=G>L@=;D9KK.=JKGFNA9L@=GH=J9LGJF=O
%GJ=P9EHD=F=OPerson(28, 2,6 1945)will create an instance of a Person with _bDay = 28, _bMonth = 2 and _bYear
XDAF=K9J=;GEE=FLK
2@AK >GJE9LAK;9DD=GJE9L 5=OADDD=9JFEGJ=9:GMLB9N9 EG :AJL@<9QMKAF?L@=J=E9AF<=JGH=J9LGJ PQ?AN=KL@=J=E9AF<=JG>L@=AFL=?=J
1.2 Objects and Classes
1.2.1 Objects
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
In the "real" world, objects are the entities of which the world is comprised. Everything OGJD< 2@=J=9D
5@AD==9;@G:B=;LE9Q
L@=F9LMJ9DOGJD<
KQKL=EK
]F9FG:B=;LGJA=FL=LO9J=KQKL=Eobjectsare entities used to represent or model 9H9JLA;MD9JHA=;=G>L@=KQKL=E
Objects are the primary units used to create abstract models. "There are a number of schools of object-oriented programming, which difffer slightly GF@GOL@=QNA=OG:B=;LK
'=J=O=OADDL9C=9:=@9NAGJAKLGMJL=JEKL9F;= An object is characterized solely by it behaviors. "Essentially this deifines an object by the way it interacts with its world. An object that ;;=KKLGAFL=JF9DDQ ]LAK <9L9AK:=AF?KLGJ=9FGL@=JG:B=;L "7 Note:A beautiful example of a model that exhibits a particular beha vior butwithout exactly replicating the mechanics we expect to produce that beha vioris the "Dragon" optical illusion. A printout to create this simple folded paper display can be found at theweb site of the Grand Illusions T oy Shop in England(http://www.grand-illusions.com/opticalillusions/dragon%20illusion/) This does not mean however, that an object may not contain data (information) in Y=DJ=KMDLG>ALK<9L9K@GMD The existence of data in an object is an implementation technique used to 1.2.2 Classes
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
Many objects difffer from each other only in thevalueof the data that they hold. For -MJG:B=;L L@=N9DM=G>ALK;GDGJ
equivalent, difffering only in the values of their attributes and perhaps, thus difffering AFL@=:=@9NAGJKL@9LJ=KMDL>JGEL@GK=N9DM=K
EGJ=KH=;A>Q<=L9ADK
%GJ ;@9J9;L=JAKLA;KG>9DD JL@JGHG2@=FGLAGFG>9F
9:KLJ9;LMFAY= -:B=;LGJA=FL=J=HJ=K=FLK methods. In addition, a class may, but not always, contain descriptions of the internal <9L9@=D<:QL@=G:B=;LK;9DD=E=L@G "8 Turning the description around, we can say that a class is a template or recipe for the ;J=9LAGFG>9H9JLA;MD9JLQH=G>G:B=;L 2@9LAKGF=;9FMK=9;D9KKLG;J=9L=
instantiate") objects of the type described by the class. Be careful not to make the ;D9KKAK9 ]FL=;@FA;9DL=JEK9;D9KK<=YF=K9F=Otypein the system. Types are identiifies usedquotesdbs_dbs19.pdfusesText_25
2G:MAD ;9DD=<9;D9KKO@A;@DGGCKDAC=L@=>GDDGOAF? class AreaCalc { double rectangle(double width, double height) { return width * height; 5@9LL@AK(9N9;G<=E=9FKAKKGE=L@AF?DAC=AreaCalcis ablue printof a specialized
;GEHMLAF?E9;@AF=L@9LAK;9H9:D=G>9;;=HLAF?LOGAFHMLdoubles, one labeled width 2@AK ;GEHML9LAGFAK?AN=F9F9E=rectangle. In Java parlance, it is called amethodfor the ;D9KKAreaCalc. '=J=AK9F=P9EHD=G>@GOO=MK=AreaCalcto compute area of a rectangle of width 9F<@=A?@L
3 AreaCalc calc = new AreaCalc();
calc.rectangle(4.5, 7.2) The ifirst line of code deifinescalcas a variable of typeAreaCalcand assign to it an instanceof the classAreaCalc.newis a keyword in Java. It is an example of what is ;9DD=<9;D9KKGH=J9LGJ object) of the given class. The second line of code is a call to the objectcalcto H=J>GJEL@=rectangletask wherewidthis assigned the value 4.5 andheightis 9KKA?F= 2G?=LL@=9J=9G>9
:Q J=;L9F?D=O=KAEHDQMK=L@=
K9E=;9D;MD9LGJ;9D;9?9AF
calc.rectangle(5.6, 8.4); >LOA<=9F< >L@A?@ J=;L9F?D=
:9K=5=;9FFGLKAEHDQMK=L@AK;9D;MD9LGJ 5=F==<9FGL@=JKH=;A9DAR=<;9D;MD9LGJ
L@=CAF9;AJ;D=
X;J=9L=9F=O;D9KK;9DD=< J=9"9D;OAL@GF=E=L@G<;9DD=LQH=double. This corresponds to designing a diffferent 9J=9;9D;MD9LGJOAL@GF=:MLLGFD9:=D= X9<LQH= double. This corresponds to designing an area calculator with two buttons: one D9:=D=OAL@LOGAFHMLKDGLK 9J=9G>L@=?AN=F?=GE=LJA;K@9H=
1AF;=L@=LOG;GEHML9LAGFKJ=IMAJ==P9;LDQL@=
:=;9J=>MDFGL?=LEAP= 2;12number of shapes? How can we, as programmers, buid a calculator that can
@9F1.1.2.2 Modeling a Person
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
For the ifirst few years of his life, Peter did not have a clue what birthdays were, let 9DGF=@AKGOF:AJL@<9L=
:AJL@<9Q 9 AFL=DDA?=F;=9F<;9H9:ADALQ
,GO.=L=JAK9;GDD=?=KLM<=FL 2@=J=AK9HA=;=G>E=EGJQ
KE9JLH=JKGF
'GOX9:AJL@<9L=9F< EGFL@ :AJL@<9L=;GFKAKLKG>9EGFL@9<9Q9F<9Q=9J $9;@G>L@=K=<9L9;9F:= KAFL@= ;D9KK DMEH:GL@L@=<9L9A
AFLGGF=;D9KK
;9DD=<=F;9HKMD9LAGF 2@=DAF=
1public class Person {
2/** 3* All data fields are private in order to prevent code
outside of this 4* class to access them.
5*/ 6private int bDay; // birth day
7private int bMonth; // birth month; for example, 3 means
March.
8private int bYear; // birth year
9 10* Constructor: a special code used to initialize the fields
of the class. 11* The only way to instantiate a Person object is to call
new on the constructor.5 12* For example: new Person(28, 2, 1945) will create a Person
object with 13* birth date February 28, 1945.
14*/ 15public Person(int day, int month, int year) {
16_bDay = day;
17_bMonth = month;
18_bYear = year;
19} 20/** 21* Uses "modulo" arithmetic to compute the number of months
till the next 22* birth day given the current month.
23* @param currentMonth an int representing the current
month. 24*/
25public int nMonthTillBD(int currentMonth) {
26return (_bMonth - currentMonth + 12) % 12;
27}
28}
Download the above code:
ZPerson.java(http://www
We now explain what the above Java code means.
XDAF=<=YF=K9;D9KK;9DD=<.=JKGF
2@=GH=FAF?;MJDQ:J9;=9LL@==FL@=DAF=
2@=C=QOGJ KQKL=E;9FJ=>=J=F;=L@AK;D9KK
XDAF=K9J=;GEE=FLK
$N=JQL@AF?:=LO==F/*and*/are ingored by the ;GEHAD=J XDAF=K<=YF=L@J==AFL=?=JN9JA9:D=K
2@=K=N9JA9:D=K9J=;9DD= ;D9KK ;G<=GMLKA<=G>L@=;D9KK -FDQ;G<=AFKA<=G>L@=;D9KK;9F9;;=KKL@=E $9;@Y=D< AK>GDDGO=<:Q9;GEE=FL<=DAEAL=<:Q//and the end-of-line. So there two ways LG;GEE=FL;G<=AF(9N9KL9JLOAL@/*and end with*/or start with//and =FDAF= XDAF=K9J=;GEE=FLK
XDAF=K;GFKLALML=O@9LAK;9DD=<9constructor. It is used to initialize the ifields G>L@=;D9KKLGKGE=H9JLA;MD9JN9DM=K
2@=F9E=G>L@=;GFKLJM;LGJK@GMD =P9;LDQDAC=L@=;D9KKF9E= '=J=ALAKpublic, meaning it can be called by code GMLKA<=G>L@=;D9KK.=JKGFNA9L@=GH=J9LGJF=O
%GJ=P9EHD=F=OPerson(28, 2,6 1945)will create an instance of a Person with _bDay = 28, _bMonth = 2 and _bYear
XDAF=K9J=;GEE=FLK
2@AK >GJE9LAK;9DD=GJE9L 5=OADDD=9JFEGJ=9:GMLB9N9 EG :AJL@<9QMKAF?L@=J=E9AF<=JGH=J9LGJ PQ?AN=KL@=J=E9AF<=JG>L@=AFL=?=J
1.2 Objects and Classes
1.2.1 Objects
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
In the "real" world, objects are the entities of which the world is comprised. Everything OGJD< 2@=J=9D
5@AD==9;@G:B=;LE9Q
L@=F9LMJ9DOGJD<
KQKL=EK
]F9FG:B=;LGJA=FL=LO9J=KQKL=Eobjectsare entities used to represent or model 9H9JLA;MD9JHA=;=G>L@=KQKL=E
Objects are the primary units used to create abstract models. "There are a number of schools of object-oriented programming, which difffer slightly GF@GOL@=QNA=OG:B=;LK
'=J=O=OADDL9C=9:=@9NAGJAKLGMJL=JEKL9F;= An object is characterized solely by it behaviors. "Essentially this deifines an object by the way it interacts with its world. An object that ;;=KKLGAFL=JF9DDQ ]LAK <9L9AK:=AF?KLGJ=9FGL@=JG:B=;L "7 Note:A beautiful example of a model that exhibits a particular beha vior butwithout exactly replicating the mechanics we expect to produce that beha vioris the "Dragon" optical illusion. A printout to create this simple folded paper display can be found at theweb site of the Grand Illusions T oy Shop in England(http://www.grand-illusions.com/opticalillusions/dragon%20illusion/) This does not mean however, that an object may not contain data (information) in Y=DJ=KMDLG>ALK<9L9K@GMD The existence of data in an object is an implementation technique used to 1.2.2 Classes
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
Many objects difffer from each other only in thevalueof the data that they hold. For -MJG:B=;L L@=N9DM=G>ALK;GDGJ
equivalent, difffering only in the values of their attributes and perhaps, thus difffering AFL@=:=@9NAGJKL@9LJ=KMDL>JGEL@GK=N9DM=K
EGJ=KH=;A>Q<=L9ADK
%GJ ;@9J9;L=JAKLA;KG>9DD JL@JGHG2@=FGLAGFG>9F
9:KLJ9;LMFAY= -:B=;LGJA=FL=J=HJ=K=FLK methods. In addition, a class may, but not always, contain descriptions of the internal <9L9@=D<:QL@=G:B=;LK;9DD=E=L@G "8 Turning the description around, we can say that a class is a template or recipe for the ;J=9LAGFG>9H9JLA;MD9JLQH=G>G:B=;L 2@9LAKGF=;9FMK=9;D9KKLG;J=9L=
instantiate") objects of the type described by the class. Be careful not to make the ;D9KKAK9 ]FL=;@FA;9DL=JEK9;D9KK<=YF=K9F=Otypein the system. Types are identiifies usedquotesdbs_dbs19.pdfusesText_25
5@9LL@AK(9N9;G<=E=9FKAKKGE=L@AF?DAC=AreaCalcis ablue printof a specialized
;GEHMLAF?E9;@AF=L@9LAK;9H9:D=G>9;;=HLAF?LOGAFHMLdoubles, one labeled width 2@AK ;GEHML9LAGFAK?AN=F9F9E=rectangle. In Java parlance, it is called amethodfor the ;D9KKAreaCalc. '=J=AK9F=P9EHD=G>@GOO=MK=AreaCalcto compute area of a rectangle of width9F<@=A?@L
3AreaCalc calc = new AreaCalc();
calc.rectangle(4.5, 7.2) The ifirst line of code deifinescalcas a variable of typeAreaCalcand assign to it an instanceof the classAreaCalc.newis a keyword in Java. It is an example of what is ;9DD=<9;D9KKGH=J9LGJ object) of the given class. The second line of code is a call to the objectcalcto H=J>GJEL@=rectangletask wherewidthis assigned the value 4.5 andheightis9KKA?F= 2G?=LL@=9J=9G>9
:Q J=;L9F?D=O=KAEHDQMK=L@=
K9E=;9D;MD9LGJ;9D;9?9AF
calc.rectangle(5.6, 8.4); >LOA<=9F< >L@A?@ J=;L9F?D=
:9K=5=;9FFGLKAEHDQMK=L@AK;9D;MD9LGJ 5=F==<9FGL@=JKH=;A9DAR=<;9D;MD9LGJ
L@=CAF9;AJ;D=
X;J=9L=9F=O;D9KK;9DD=< J=9"9D;OAL@GF=E=L@G<;9DD=LQH=double. This corresponds to designing a diffferent 9J=9;9D;MD9LGJOAL@GF=:MLLGFD9:=D= X9<LQH= double. This corresponds to designing an area calculator with two buttons: one D9:=D=OAL@LOGAFHMLKDGLK 9J=9G>L@=?AN=F?=GE=LJA;K@9H=
1AF;=L@=LOG;GEHML9LAGFKJ=IMAJ==P9;LDQL@=
:=;9J=>MDFGL?=LEAP= 2;12number of shapes? How can we, as programmers, buid a calculator that can
@9F1.1.2.2 Modeling a Person
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
For the ifirst few years of his life, Peter did not have a clue what birthdays were, let 9DGF=@AKGOF:AJL@<9L=
:AJL@<9Q 9 AFL=DDA?=F;=9F<;9H9:ADALQ
,GO.=L=JAK9;GDD=?=KLM<=FL 2@=J=AK9HA=;=G>E=EGJQ
KE9JLH=JKGF
'GOX9:AJL@<9L=9F< EGFL@ :AJL@<9L=;GFKAKLKG>9EGFL@9<9Q9F<9Q=9J $9;@G>L@=K=<9L9;9F:= KAFL@= ;D9KK DMEH:GL@L@=<9L9A
AFLGGF=;D9KK
;9DD=<=F;9HKMD9LAGF 2@=DAF=
1public class Person {
2/** 3* All data fields are private in order to prevent code
outside of this 4* class to access them.
5*/ 6private int bDay; // birth day
7private int bMonth; // birth month; for example, 3 means
March.
8private int bYear; // birth year
9 10* Constructor: a special code used to initialize the fields
of the class. 11* The only way to instantiate a Person object is to call
new on the constructor.5 12* For example: new Person(28, 2, 1945) will create a Person
object with 13* birth date February 28, 1945.
14*/ 15public Person(int day, int month, int year) {
16_bDay = day;
17_bMonth = month;
18_bYear = year;
19} 20/** 21* Uses "modulo" arithmetic to compute the number of months
till the next 22* birth day given the current month.
23* @param currentMonth an int representing the current
month. 24*/
25public int nMonthTillBD(int currentMonth) {
26return (_bMonth - currentMonth + 12) % 12;
27}
28}
Download the above code:
ZPerson.java(http://www
We now explain what the above Java code means.
XDAF=<=YF=K9;D9KK;9DD=<.=JKGF
2@=GH=FAF?;MJDQ:J9;=9LL@==FL@=DAF=
2@=C=QOGJ KQKL=E;9FJ=>=J=F;=L@AK;D9KK
XDAF=K9J=;GEE=FLK
$N=JQL@AF?:=LO==F/*and*/are ingored by the ;GEHAD=J XDAF=K<=YF=L@J==AFL=?=JN9JA9:D=K
2@=K=N9JA9:D=K9J=;9DD= ;D9KK ;G<=GMLKA<=G>L@=;D9KK -FDQ;G<=AFKA<=G>L@=;D9KK;9F9;;=KKL@=E $9;@Y=D< AK>GDDGO=<:Q9;GEE=FL<=DAEAL=<:Q//and the end-of-line. So there two ways LG;GEE=FL;G<=AF(9N9KL9JLOAL@/*and end with*/or start with//and =FDAF= XDAF=K9J=;GEE=FLK
XDAF=K;GFKLALML=O@9LAK;9DD=<9constructor. It is used to initialize the ifields G>L@=;D9KKLGKGE=H9JLA;MD9JN9DM=K
2@=F9E=G>L@=;GFKLJM;LGJK@GMD =P9;LDQDAC=L@=;D9KKF9E= '=J=ALAKpublic, meaning it can be called by code GMLKA<=G>L@=;D9KK.=JKGFNA9L@=GH=J9LGJF=O
%GJ=P9EHD=F=OPerson(28, 2,6 1945)will create an instance of a Person with _bDay = 28, _bMonth = 2 and _bYear
XDAF=K9J=;GEE=FLK
2@AK >GJE9LAK;9DD=GJE9L 5=OADDD=9JFEGJ=9:GMLB9N9 EG :AJL@<9QMKAF?L@=J=E9AF<=JGH=J9LGJ PQ?AN=KL@=J=E9AF<=JG>L@=AFL=?=J
1.2 Objects and Classes
1.2.1 Objects
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
In the "real" world, objects are the entities of which the world is comprised. Everything OGJD< 2@=J=9D
5@AD==9;@G:B=;LE9Q
L@=F9LMJ9DOGJD<
KQKL=EK
]F9FG:B=;LGJA=FL=LO9J=KQKL=Eobjectsare entities used to represent or model 9H9JLA;MD9JHA=;=G>L@=KQKL=E
Objects are the primary units used to create abstract models. "There are a number of schools of object-oriented programming, which difffer slightly GF@GOL@=QNA=OG:B=;LK
'=J=O=OADDL9C=9:=@9NAGJAKLGMJL=JEKL9F;= An object is characterized solely by it behaviors. "Essentially this deifines an object by the way it interacts with its world. An object that ;;=KKLGAFL=JF9DDQ ]LAK <9L9AK:=AF?KLGJ=9FGL@=JG:B=;L "7 Note:A beautiful example of a model that exhibits a particular beha vior butwithout exactly replicating the mechanics we expect to produce that beha vioris the "Dragon" optical illusion. A printout to create this simple folded paper display can be found at theweb site of the Grand Illusions T oy Shop in England(http://www.grand-illusions.com/opticalillusions/dragon%20illusion/) This does not mean however, that an object may not contain data (information) in Y=DJ=KMDLG>ALK<9L9K@GMD The existence of data in an object is an implementation technique used to 1.2.2 Classes
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
Many objects difffer from each other only in thevalueof the data that they hold. For -MJG:B=;L L@=N9DM=G>ALK;GDGJ
equivalent, difffering only in the values of their attributes and perhaps, thus difffering AFL@=:=@9NAGJKL@9LJ=KMDL>JGEL@GK=N9DM=K
EGJ=KH=;A>Q<=L9ADK
%GJ ;@9J9;L=JAKLA;KG>9DD JL@JGHG2@=FGLAGFG>9F
9:KLJ9;LMFAY= -:B=;LGJA=FL=J=HJ=K=FLK methods. In addition, a class may, but not always, contain descriptions of the internal <9L9@=D<:QL@=G:B=;LK;9DD=E=L@G "8 Turning the description around, we can say that a class is a template or recipe for the ;J=9LAGFG>9H9JLA;MD9JLQH=G>G:B=;L 2@9LAKGF=;9FMK=9;D9KKLG;J=9L=
instantiate") objects of the type described by the class. Be careful not to make the ;D9KKAK9 ]FL=;@FA;9DL=JEK9;D9KK<=YF=K9F=Otypein the system. Types are identiifies usedquotesdbs_dbs19.pdfusesText_25
2G?=LL@=9J=9G>9
:QJ=;L9F?D=O=KAEHDQMK=L@=
K9E=;9D;MD9LGJ;9D;9?9AF
calc.rectangle(5.6, 8.4); >LOA<=9F< >L@A?@J=;L9F?D=
:9K=5=;9FFGLKAEHDQMK=L@AK;9D;MD9LGJ5=F==<9FGL@=JKH=;A9DAR=<;9D;MD9LGJ
L@=CAF9;AJ;D=
X;J=9L=9F=O;D9KK;9DD=< J=9"9D;OAL@GF=E=L@G<;9DD=9J=9;9D;MD9LGJOAL@GF=:MLLGFD9:=D= X9<LQH= double. This corresponds to designing an area calculator with two buttons: one D9:=D=OAL@LOGAFHMLKDGLK 9J=9G>L@=?AN=F?=GE=LJA;K@9H=
1AF;=L@=LOG;GEHML9LAGFKJ=IMAJ==P9;LDQL@=
:=;9J=>MDFGL?=LEAP= 2;12number of shapes? How can we, as programmers, buid a calculator that can
@9F1.1.2.2 Modeling a Person
9J=9G>L@=?AN=F?=GE=LJA;K@9H=
1AF;=L@=LOG;GEHML9LAGFKJ=IMAJ==P9;LDQL@=
:=;9J=>MDFGL?=LEAP=2;12number of shapes? How can we, as programmers, buid a calculator that can
@9FAvailable under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF KGJ?DA;=FK=K:QK9
For the ifirst few years of his life, Peter did not have a clue what birthdays were, let9DGF=@AKGOF:AJL@<9L=
:AJL@<9Q9 AFL=DDA?=F;=9F<;9H9:ADALQ
,GO.=L=JAK9;GDD=?=KLM<=FL 2@=J=AK9HA=;=G>E=EGJQ
KE9JLH=JKGF
'GOX9:AJL@<9L=9F< EGFL@ :AJL@<9L=;GFKAKLKG>9EGFL@9<9Q9F<9Q=9J $9;@G>L@=K=<9L9;9F:= KAFL@= ;D9KK DMEH:GL@L@=<9L9A
AFLGGF=;D9KK
;9DD=<=F;9HKMD9LAGF 2@=DAF=
1public class Person {
2/** 3* All data fields are private in order to prevent code
outside of this 4* class to access them.
5*/ 6private int bDay; // birth day
7private int bMonth; // birth month; for example, 3 means
March.
8private int bYear; // birth year
9 10* Constructor: a special code used to initialize the fields
of the class. 11* The only way to instantiate a Person object is to call
new on the constructor.5 12* For example: new Person(28, 2, 1945) will create a Person
object with 13* birth date February 28, 1945.
14*/ 15public Person(int day, int month, int year) {
16_bDay = day;
17_bMonth = month;
18_bYear = year;
19} 20/** 21* Uses "modulo" arithmetic to compute the number of months
till the next 22* birth day given the current month.
23* @param currentMonth an int representing the current
month. 24*/
25public int nMonthTillBD(int currentMonth) {
26return (_bMonth - currentMonth + 12) % 12;
27}
28}
Download the above code:
ZPerson.java(http://www
We now explain what the above Java code means.
XDAF=<=YF=K9;D9KK;9DD=<.=JKGF
2@=GH=FAF?;MJDQ:J9;=9LL@==FL@=DAF=
2@=C=QOGJ KQKL=E;9FJ=>=J=F;=L@AK;D9KK
XDAF=K9J=;GEE=FLK
$N=JQL@AF?:=LO==F/*and*/are ingored by the ;GEHAD=J XDAF=K<=YF=L@J==AFL=?=JN9JA9:D=K
2@=K=N9JA9:D=K9J=;9DD= ;D9KK ;G<=GMLKA<=G>L@=;D9KK -FDQ;G<=AFKA<=G>L@=;D9KK;9F9;;=KKL@=E $9;@Y=D< AK>GDDGO=<:Q9;GEE=FL<=DAEAL=<:Q//and the end-of-line. So there two ways LG;GEE=FL;G<=AF(9N9KL9JLOAL@/*and end with*/or start with//and =FDAF= XDAF=K9J=;GEE=FLK
XDAF=K;GFKLALML=O@9LAK;9DD=<9constructor. It is used to initialize the ifields G>L@=;D9KKLGKGE=H9JLA;MD9JN9DM=K
2@=F9E=G>L@=;GFKLJM;LGJK@GMD =P9;LDQDAC=L@=;D9KKF9E= '=J=ALAKpublic, meaning it can be called by code GMLKA<=G>L@=;D9KK.=JKGFNA9L@=GH=J9LGJF=O
%GJ=P9EHD=F=OPerson(28, 2,6 1945)will create an instance of a Person with _bDay = 28, _bMonth = 2 and _bYear
XDAF=K9J=;GEE=FLK
2@AK >GJE9LAK;9DD=GJE9L 5=OADDD=9JFEGJ=9:GMLB9N9 EG :AJL@<9QMKAF?L@=J=E9AF<=JGH=J9LGJ PQ?AN=KL@=J=E9AF<=JG>L@=AFL=?=J
1.2 Objects and Classes
1.2.1 Objects
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
In the "real" world, objects are the entities of which the world is comprised. Everything OGJD< 2@=J=9D
5@AD==9;@G:B=;LE9Q
L@=F9LMJ9DOGJD<
KQKL=EK
]F9FG:B=;LGJA=FL=LO9J=KQKL=Eobjectsare entities used to represent or model 9H9JLA;MD9JHA=;=G>L@=KQKL=E
Objects are the primary units used to create abstract models. "There are a number of schools of object-oriented programming, which difffer slightly GF@GOL@=QNA=OG:B=;LK
'=J=O=OADDL9C=9:=@9NAGJAKLGMJL=JEKL9F;= An object is characterized solely by it behaviors. "Essentially this deifines an object by the way it interacts with its world. An object that ;;=KKLGAFL=JF9DDQ ]LAK <9L9AK:=AF?KLGJ=9FGL@=JG:B=;L "7 Note:A beautiful example of a model that exhibits a particular beha vior butwithout exactly replicating the mechanics we expect to produce that beha vioris the "Dragon" optical illusion. A printout to create this simple folded paper display can be found at theweb site of the Grand Illusions T oy Shop in England(http://www.grand-illusions.com/opticalillusions/dragon%20illusion/) This does not mean however, that an object may not contain data (information) in Y=DJ=KMDLG>ALK<9L9K@GMD The existence of data in an object is an implementation technique used to 1.2.2 Classes
Available under"J=9LAN="GEEGFK1@9J= DAC=
]FL=JF9LAGF9D*A;=FK=@LLH;J=9LAN=;GEEGF K GJ?DA;=FK=K:QK9
Many objects difffer from each other only in thevalueof the data that they hold. For -MJG:B=;L L@=N9DM=G>ALK;GDGJ
equivalent, difffering only in the values of their attributes and perhaps, thus difffering AFL@=:=@9NAGJKL@9LJ=KMDL>JGEL@GK=N9DM=K
EGJ=KH=;A>Q<=L9ADK
%GJ ;@9J9;L=JAKLA;KG>9DD JL@JGHG2@=FGLAGFG>9F
9:KLJ9;LMFAY= -:B=;LGJA=FL=J=HJ=K=FLK methods. In addition, a class may, but not always, contain descriptions of the internal <9L9@=D<:QL@=G:B=;LK;9DD=E=L@G "8 Turning the description around, we can say that a class is a template or recipe for the ;J=9LAGFG>9H9JLA;MD9JLQH=G>G:B=;L 2@9LAKGF=;9FMK=9;D9KKLG;J=9L=
instantiate") objects of the type described by the class. Be careful not to make the ;D9KKAK9 ]FL=;@FA;9DL=JEK9;D9KK<=YF=K9F=Otypein the system. Types are identiifies usedquotesdbs_dbs19.pdfusesText_25
AFL=DDA?=F;=9F<;9H9:ADALQ
,GO.=L=JAK9;GDD=?=KLM<=FL2@=J=AK9HA=;=G>E=EGJQ
KE9JLH=JKGF
'GODMEH:GL@L@=<9L9A
AFLGGF=;D9KK
;9DD=<=F;9HKMD9LAGF2@=DAF=
1public class Person {
2/**3* All data fields are private in order to prevent code
outside of this4* class to access them.
5*/6private int bDay; // birth day
7private int bMonth; // birth month; for example, 3 means
March.
8private int bYear; // birth year
910* Constructor: a special code used to initialize the fields
of the class.11* The only way to instantiate a Person object is to call
new on the constructor.512* For example: new Person(28, 2, 1945) will create a Person
object with13* birth date February 28, 1945.
14*/15public Person(int day, int month, int year) {
16_bDay = day;
17_bMonth = month;
18_bYear = year;
19} 20/**21* Uses "modulo" arithmetic to compute the number of months
till the next22* birth day given the current month.
23* @param currentMonth an int representing the current
month. 24*/25public int nMonthTillBD(int currentMonth) {
26return (_bMonth - currentMonth + 12) % 12;
27}28}