4/5/21, 10)50 PMIntermediate Programming
Page 1 of 185http://itec2150.gitlab.io/
Intermediate Programming
!"#$%&'()*+,-(./&*0(.1(2#,3*&#-(4#5(65&-(7"&835*(6/3&$/&-(9*&(:/&'(25&'-(;*<,$//(=>
4/5/21, 10)50 PMIntermediate Programming
Page 2 of 185http://itec2150.gitlab.io/
Table of Contents
1. Objects
1.1. Learning Outcomes
1.2. Resources
1.2.1. Text
1.2.2. Videos
1.3. Introduction
1.4. Creating Objects
1.5. Dierence between primitive type and reference type variables
1.6. Dierence between Stack and Heap Memory
1.7. Passing Objects to Methods and Returning Objects from Methods
1.8. Key Terms
1.9. Exercises
1.9.1. Exercise 1
1.9.2. Exercise 2
1.9.3. Exercise 3
1.10. Issue Tracker/Comments
2. Object-Oriented Programming Design, Class Relationship, and Inheritance
2.1. Learning Outcomes
2.2. Resources
2.2.1. Text
2.2.2. Videos
2.3. Introduction
2.4. Object Oriented Program Design
2.4.1. Class Abstraction and Encapsulation
2.5. Class Relationship
2.5.1. Association
2.5.2. Aggregation
2.5.3. Composition
2.6. Inheritance
2.6.1. Overloading vs. Overriding
2.6.2. Accessibility Issue
2.7. Key Terms
2.8. Exercises
2.8.1. Exercise 1
2.8.2. Exercise 2
2.9. References
2.10. Issue Tracker/Comments
3. Polymorphism / Abstract Classes / Interfaces
3.1. Learning Outcomes
4/5/21, 10)50 PMIntermediate Programming
Page 3 of 185http://itec2150.gitlab.io/
3.2. Resources
3.2.1. Text
3.2.2. Videos
3.3. Introduction
3.4. Polymorphism through Inheritance
3.4.1. Dynamic Binding and Polymorphism
3.4.2. Why Polymorphism is Useful?
3.5. Abstract Class
3.5.1. Polymorphism through Abstract Class
3.5.2. An Experiment: Removing the Abstract Methods from the Super Class
3.6. Interface
3.6.1. A Class Can Implement Multiple Interfaces
3.6.2. Polymorphism through Interface
3.6.3. Interface Inheritance
3.7. Summary
3.8. Key Terms
3.9. Exercises
3.9.1. Exercise 1
3.9.2. Exercise 2
3.9.3. Exercise 3
4. Exceptions
4.1. Learning Outcomes
4.2. Resources
4.2.1. Text and Tutorials
4.2.2. Videos
4.3. Overview
4.4. Exception Handling
4.5. Checked Exception vs UnChecked Exception
4.6. Try/Catch/Finally Blocks
4.6.1. Declaring an exception
4.6.2. Throwing an exception
4.6.3. Catching an exception
4.7. Handling Exception vs Throwing Exception
4.8. Custom Exceptions
4.9. An example of Exception Handling used to validate input from user
4.10. Summary
4.11. Key Terms
4.12. Exercises
4.12.1. Exercise 1
4.12.2. Exercise 2
4/5/21, 10)50 PMIntermediate Programming
Page 4 of 185http://itec2150.gitlab.io/
4.12.3. Exercise 3
4.13. Issue Tracker/Comments
5. File Input/Output
5.1. Learning Outcomes
5.2. Resources
5.2.1. Text and Tutorials
5.2.2. Videos
5.3. Overview
5.4. Streams and Files
5.5. The File class
5.6. BueredReader and BueredWriter
5.6.1. An Example using BueredReader and BueredWriter
5.7. PrintWriter
5.7.1. Example of writing data to a text le
5.8. Scanner
5.8.1. How does Scanner work?
5.8.2. File reading Examples
5.9. Regular Expressions and parsing a le
5.10. Case Study: A Client Database
5.11. Key Terms
5.12. Exercises
5.12.1. Exercise 1
5.12.2. Exercise 2
5.12.3. Exercise 3
5.13. Issue Tracker/Comments
6. Generics
6.1. Learning Outcomes
6.2. Resources
6.2.1. Text
6.2.2. Videos
6.3. Introduction
6.4. Motivations and Benets
6.5. Dening Generic Classes
6.5.1. Type Parameters/Type Variables
6.5.2. How to Use Generics?
6.5.3. Dening Generic Interfaces
6.6. Generic Methods
6.6.1. The Bounded Generic Type
6.7. Raw Types and Backward Compatibility
6.8. Wildcard Generic Types
4/5/21, 10)50 PMIntermediate Programming
Page 5 of 185http://itec2150.gitlab.io/
6.9. Erasure
6.9.1. Class Type Erasure
6.9.2. Erasure of Generic Methods
6.10. Key Terms
6.11. Exercises
6.11.1. Exercise 1 (Palindrome)
6.11.2. Exercise 2
6.11.3. Exercise 3
6.11.4. Exercise 4
6.11.5. Exercise 5
6.12. References
6.13. Issue Tracker/Comments
7. Recursion
7.1. Learning Outcomes
7.2. Resources
7.2.1. Text and Tutorials
7.2.2. Videos
7.3. Introduction
7.4. Permutations and Combinations
7.5. Using Recursion in Mazes
7.6. Summary
7.7. Key Terms
7.8. Exercises
7.9. Issue Tracker/Comments
8. Basic Data Structures and Sorting
8.1. Learning Objectives
8.2. Resources
8.2.1. Text
8.3. Introduction
8.4. Lists
8.4.1. The Java List Interface
8.4.2. The ArrayList and LinkedList Classes
8.4.3. The List Iterator
8.4.4. Putting Things Together By Examples
8.4.5. Notes on Eciency: Array List vs. Linked List
8.5. Stacks
8.5.1. The Stack Class
8.5.2. Applications of Stacks
8.5.3. A Classical Application: Checking for Balanced Parentheses
8.6. Queues
4/5/21, 10)50 PMIntermediate Programming
Page 6 of 185http://itec2150.gitlab.io/
8.6.1. The Queue Interface
8.6.2. Applications of Queues
8.7. Priority Queues
8.7.1. The PriorityQueue Class
8.7.2. The Comparator Interface
8.7.3. Applications of Priority Queues
8.8. Sorting
8.8.1. Naive Iterative Algorithms
8.8.2. Ecient Algorithms
8.9. Exercises
8.9.1. Exercise 1
8.9.2. Exercise 2
8.9.3. Exercise 3
8.9.4. Exercise 4
8.9.5. Exercise 5
8.9.6. Exercise 6
8.9.7. Exercise 7
8.9.8. Exercise 8
8.9.9. Exercise 9
8.9.10. Exercise 10
8.10. Issue Tracker/Comments
9. Glossary
4/5/21, 10)50 PMIntermediate Programming
Page 7 of 185http://itec2150.gitlab.io/
4/5/21, 10)50 PMIntermediate Programming
Page 8 of 185http://itec2150.gitlab.io/
1. Objects
1.1. Learning Outcomes
.8%?#&8$(@500(A#(*A0#(8/B
C1( 7+#*8#(*&?(%$#(/AD#<8$
E1( F<<#$$(/AD#<8$(%$5&'(+#G#+#&<#(H*+5*A0#$
I1( 7+#*8#(+#G#+#&<#(H*+5*A0#$(%$5&'(+#G#+#&<#(8"J#$
K1( 25GG#+#&85*8#(A#8@##&(+#G#+#&<#(H*+5*A0#$(*&?(J+5>585H#(H*+5*A0#$
L1( 25GG#+#&85*8#(A#8@##&($8*<,(*&?(3#*J(>#>/+"
M1( )*$$(/AD#<8$(*$(*+'%>#&8$(8/(>#83/?$(*&?(%$#(*$(+#8%+&(8"J#$(G+/>(>#83/?$
1.2. Resources
1.2.1. Text
;35&,(6*H*(B(!/@(8/(;35&,(N5,#(*(7/>J%8#+(.<5#&85$8 O388J$BPP'+##&8#*J+#$$1</>P@JP835&,QD*H*PR(A"(F00#&(2/@&#"(*&?
73+5$(S*"G5#0?
;35&,(6*H*(73*J8#+(CTB(UAD#<8$ O388JBPP'+##&8#*J+#$$1</>P835&,D*H*MP38>0P835&,D*H*MTCC138>0R
)+/'+*>>5&'(V%&?*>#&8*0$(73*J8#+(WB(UAD#<8QU+5#&8#?()+/'+*>>5&'
O388JBPP58#<ECKT1??&$1&#8PXY/AD#<8Y/+5#&8#?YJ+/'+*>>5&'R
=&8+/?%<85/&(8/()+/'+*>>5&'(Z$5&'(6*H*B(73*J8#+(L O388JBPP>*8313@$1#?%P#<,P<$CEKP?/@&0/*?$PD*H*&/8#$[Q05&,#?1J?GR
1.2.2. Videos
UAD#<8$(H$1()+5>585H#(8"J#(H*+5*A0#$ O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]N;&JW^_#[V=R
)*$$5&'(/AD#<8$(8/(>#83/?$ O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]`!8GAI0G<Q'R
S#>/+"(S*&*'#>#&8(5&(6*H* O388J$BPP@@@105&,#?5&1</>P0#*+&5&'PD*H*Q>#>/+"Q>*&*'#>#&8P5&8+/?%<85/&\%]WMCCMETER
1.3. Introduction
UAD#<8Q/+5#&8#?(J+/'+*>>5&'(0*&'%*'#$-($%<3(*$(6*H*(*+#(/+'*&5a#?(*+/%&?(/AD#<8$-(5&$8#*?(/G(*<85/&$1(F&(/AD#<8
+#J+#$#&8$(*&(#&858"(5&(83#(+#*0(@/+0?-($%<3(*$(*(<*+-(*(3/>#-(*(J#+$/&-(#8<1(b*<3(/AD#<8(3*$(state-(+#J+#$#&8#?(A"(58$
?*8*(G5#0?$(/+(*88+5A%8#$(*&?(behavior(+#J+#$#&8#?(A"(>#83/?$1(UAD#<8$(/G(83#($*>#(8"J#(*+#(?#G5&#?(%$5&'(*(<0*$$1(F
<0*$$(5$(</&$5?#+#?(8/(A#(*(A0%#J+5&8(G/+(83#(/AD#<8$(83*8(58(+#J+#$#&8$1(b*<3(<0*$$(<*&(3*H#(>*&"(/AD#<8$-(*0$/
+#G#++#?(8/(*$(5&$8*&<#$1
V/+(#c*>J0#-(</&$5?#+(*(Person(<0*$$1(b*<3(J#+$/&(<*&(3*H#(?*8*B(name-(age-(eyeColor1(b*<3(J#+$/&(<*&(*0$/
J#+G/+>(83#(G/00/@5&'(*<85/&$(/+(A#3*H5/+$B(walk-(talk1(70*$$(Person(?#G5&#$(83#($8*8#(*&?(A#3*H5/+(83*8(5$
</>>/&(8/(*00(/AD#<8$(83*8(A#0/&'(8/(581(bc*>J0#$(/G(/AD#<8$(/+(5&$8*&<#$(/G(<0*$$()#+$/&(5&<0%?#-($J#<5G5<(J#+$/&$-
4/5/21, 10)50 PMIntermediate Programming
Page 9 of 185http://itec2150.gitlab.io/
$%<3(*$(person1-(person2-(person3-(#8<1(b*<3(/G(83#$#(J#+$/&$(@500(3*H#(*(H*0%#(G/+(name-(height(*&?(eyeColor1
;3#(H*0%#$(<*&(A#(?5GG#+#&8(G/+(#*<3(/AD#<81(b*<3(/G(83#$#(J#+$/&$(*0$/(3*H#(83#(*A5058"(8/(J#+G/+>(*<85/&$(walk
*&?(talk1(.##(83#(G5'%+#1
S/+#(?#8*50$(*A/%8(<0*$$#$(*+#(*H*50*A0#(3#+#
O388J$BPP*0'1>*&5G/0?*JJ1/+'P+#*?PJ+/'+*>>5&'QG%&?*>#&8*0$P$#<85/&PCWELIWG<Q[A#GQKC^EQAL?TQ<#E^[<KMMMEIR1
`#0/@("/%(<*&($##($*>J0#(</?#(G/+(<+#*85&'(*(<0*$$(8/(>/?#0(*($d%*+#B
4/5/21, 10)50 PMIntermediate Programming
Page 10 of 185http://itec2150.gitlab.io/
;3#(*A/H#(</?#(<+#*8#$(*(A0%#J+5&8(G/+(*00(.d%*+#$1(F00(/AD#<8$(/G(<0*$$(Square(@500(3*H#(*88+5A%8#(length1(F&?(83#"
*00(3*H#(83#(*A5058"(8/(J#+G/+>(*<85/&$(?#G5&#?(A"(#*<3(/G(83#(>#83/?$1(`#0/@(5$(*&(#c*>J0#(/G(*(8#$8#+(<0*$$-(%$#?
8/(8#$8(83#(Square(<0*$$1(;3#(8#$8#+(<0*$$(@500(3*H#(*(main()(>#83/?(*&?(<*&(A#(%$#?(8/(<+#*8#(/AD#<8$(/G(<0*$$
Square1(;3#$#(/AD#<8$(<*&(83#&(J#+G/+>(*<85/&$(A"(5&H/,5&'(83#(>#83/?$(?#G5&#?(5&(83#(A%$5&#$$(<0*$$-(Square1
1.4. Creating Objects
=&(83#($8*8#>#&8B(.d%*+#($d%*+#C(](&#@(.d%*+#ORe(*&(/AD#<8(/G(<0*$$(Square(5$(A#5&'(<+#*8#?1(F(<0*$$(5$(A*$5<*00"(*
?*8*(8"J#(?#G5&#?(A"(*(J+/'+*>>#+1(=&$8#*?(/G(*(J+5>585H#(?*8*(8"J#(O#1'1(5&8-(?/%A0#-(A//0#*&-(*&?(<3*+R-(*(<0*$$(5$
*(reference type1(=8(5$('/5&'(8/(3/0?(*(+#G#+#&<#(8/(*&(/AD#<81(4#(<*&(835&,(/G(*(+#G#+#&<#(8/(*&(/AD#<8(*$(83#
publicpublic classclass Square{
privateprivate doubledouble length;
publicpublic Square(){
length = 0.0;
}
publicpublic Square(doubledouble inLength){
length = inLength;
}
publicpublic voidvoid setLength(doubledouble inLength){
length = inLength;
}
publicpublic doubledouble getLength(){
returnreturn length;
}
publicpublic doubledouble computeArea(){
returnreturn length*length;
}
publicpublic doubledouble computePerimeter(){
returnreturn 4*length;
}
publicpublic String toString(){
String result = "square length: " + length + "\nperimeter: " + computePerimeter() + "\narea: " +
computeArea();
returnreturn result;
}
}
JAVA
publicpublic classclass SquareTester{
publicpublic staticstatic voidvoid main(String[] args){
Square square1 = newnew Square();
square1.setLength(10.5);
System.out.println(square1);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 11 of 185http://itec2150.gitlab.io/
f*??+#$$g(/G(83#(/AD#<81(;3+/%'3(83#(+#G#+#&<#-(83#(6*H*(+%&85>#($"$8#>(Q(6*H*(h5+8%*0(S*<35&#(O6hSR(i(<*&(0/<*8#
@3#+#(83#(/AD#<8(5$(0/<*8#?(5&(>#>/+"1
=&(83#(#c*>J0#-(square1(5$(*(H*+5*A0#(/G(835$(+#G#+#&<#(8"J#(*&?(5$(+#G#++#?(8/(*$(*(reference variable1(;3#(new
/J#+*8/+(<+#*8#$(*(.d%*+#(/AD#<81(;3#(*??+#$$(/G(83#(/AD#<8(5$(*$$5'&#?(8/(83#(+#G#+#&<#(H*+5*A0#(square11
1.5. Dierence between primitive type and reference type variables
F(H*+5*A0#(+#J+#$#&8$(*(&*>#?(>#>/+"(0/<*85/&(83*8($8/+#$(*(H*0%#1(43#&(@#(?#<0*+#(*(H*+5*A0#-(@#($J#<5G"(@3*8
8"J#(/G(H*+5*A0#(58(5$1(43#83#+(*(H*+5*A0#(5$(J+5>585H#(/+(+#G#+#&<#(8"J#(?#<5?#$(3/@(83*8(H*+5*A0#(5$($8/+#?(5&(83#
>#>/+"1(V/+(#c*>J0#-(5G(@#(@+58#(83#(G/00/@5&'($8*8#>#&8B(5&8(*'#(](Le(83#&(K(A"8#$(/G(>#>/+"(5$(*00/<*8#?(G/+(age
*&?(83#(H*0%#(/G(L(5$($8/+#?(5&(581(;35$(>#>/+"(5$(+#G#++#?(8/(*$(age1(43#+#*$-(5G(@#(@+58#(83#(G/00/@5&'($8*8#>#&8B
.d%*+#($d%*+#C(](&#@(.d%*+#ORe
square1(?/#$(&/8(?5+#<80"($8/+#(83#(H*0%#(/G(*($d%*+#-(*$(age(?/#$1(=&$8#*?-(>#>/+"(5$(*00/<*8#?(G/+(/AD#<8(/G
+#G#+#&<#(8"J#(Square(*&?(square1(O+#G#+#&<#(H*+5*A0#R(83#&(+#G#+#&<#$(83*8(/AD#<81(=&$8#*?(/G(3/0?5&'(83#(/AD#<8
58$#0G-(square1(3/0?$(83#(5&G/+>*85/&(&#<#$$*+"(O+#G#+#&<#(/+(>#>/+"(0/<*85/&R(8/(G5&?(83#(/AD#<8(5&(>#>/+"1(.##
5>*'#(A#0/@1
;35$(?5GG#+#&<#(5&(3/@(>#>/+"(5$(*00/<*8#?(G/+(J+5>585H#(*&?(+#G#+#&<#(H*+5*A0#$(5>J*<8$(*$$5'&>#&8($8*8#>#&8$1
V/+(#c*>J0#-(5G(@#(</&$5?#+(83#(G/00/@5&'($8*8#>#&8$B
83#&(@#(,&/@(83*8(*G8#+(83/$#(83+##($8*8#>#&8$(#c#<%8#-(age1(0/$#$(83#(/+5'5&*0(H*0%#(/G(KT(*&?('#8$(83#(H*0%#(/G
C[1(;3#(>#>/+"(*00/<*85/&(A#G/+#(*&?(*G8#+(0//,$(*$($3/@&(A#0/@1(
intint age1 = 40;
intint age2 = 18;
age1 = age2;
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 12 of 185http://itec2150.gitlab.io/
=&(835$($<#&*+5/-(A/83(H*+5*A0#$(*+#(*8(83#($*>#(>#>/+"(0/<*85/&(*$(A#G/+#1(h*0%#(/G(age1(3*$(<3*&'#?1(j/@
</&$5?#+(*($5>50*+($<#&*+5/(@583(/AD#<8$1
!#+#-(square1(*&?(square2(*+#(+#G#+#&<#(H*+5*A0#$(83*8(3/0?(*(+#G#+#&<#(8/(83#(*<8%*0(.d%*+#(/AD#<8$1(./-(@3#&
@#(*$$5'&(/&#(+#G#+#&<#(H*+5*A0#(8/(*&/83#+-(83#(+#G#+#&<#$('#8(*$$5'&#?-(&/8(83#(H*0%#$1(.##(83#(5>*'#(A#0/@1
j/85<#(83*8(83#(0#&'83(H*0%#$(G/+(#*<3($d%*+#(/AD#<8(+#>*5&(*8(83#5+(/+5'5&*0(>#>/+"(0/<*85/&$1(;3#"(?/(&/8('#8
</J5#?(/H#+1(;3#(+#G#+#&<#(/G(square2(3/@#H#+-('#8$(*$$5'&#?(8/(+#G#+#&<#(H*+*5A0#(square11(;35$(>#*&$(square1
&/(0/&'#+(+#G#+#&<#$(83#(/AD#<8(@583(length(CT1L1(=8(&/@(+#G#+#&<#$(83#(/AD#<8(/G(length(L1E1
1.6. Dierence between Stack and Heap Memory
6*H*(h5+8%*0(S*<35&#(O6hSR(?5H5?#$(83#(>#>/+"(5&8/(8@/(8"J#$-(83#($8*<,(*&?(83#(3#*J1(;3#($8*<,(>#>/+"(5$(%$#?
8/($8/+#(0/<*0(H*+5*A0#$(/G(J+5>585H#(8"J#(*&?(+#G#+#&<#(H*+5*A0#$1(U&(83#(/83#+(3*&?-(3#*J($8/+#$(83#(*<8%*0(/AD#<8$1
.8*<,(>#>/+"(5$(%$#?(G/+($8*85<(>#>/+"(*00/<*85/&(*&?(5$(*<<#$$#?(5&(*(V5+$8(=&(N*$8(U%8(>*&&#+1(bH#+"(85>#(*
>#83/?(5$(<*00#?-($8*<,(>#>/+"(5$(*00/<*8#?(8/($8/+#(83#(J+5>585H#(*&?(+#G#+#&<#(H*+5*A0#$1(43#&(83#(>#83/?
G5&5$3#$(#c#<%85/&-(58$(>#>/+"(5$(+#0#*$#?-(</&8+/0('/#$(A*<,(8/(83#(<*005&'(>#83/?(*&?($8*<,(>#>/+"(5$(&/@
*H*50*A0#(G/+(/83#+(>#83/?$1(;35$(H5?#/
O388J$BPP@@@105&,#?5&1</>P0#*+&5&'PD*H*Q>#>/+"Q>*&*'#>#&8P83#Q+/0#Q/GQ83#Q$8*<,\%]WMCCMETER(?#>/&$8+*8#$(835$(5?#*1(!#*J
>#>/+"(/&(83#(/83#+(3*&?(5$(?"&*>5<*00"(*00/<*8#?(*8(+%&85>#1(=8($8/+#$(83#(/AD#<8$(83#>$#0H#$1(./-(5&(83#(*A/H#
#c*>J0#-(+#G#+#&<#(H*+5*A0#$($d%*+#C(*&?($d%*+#E(@/%0?(A#($8/+#?(5&(83#($8*<,-(@3#+#*$(83#(/AD#<8$(83*8(83#"
+#G#+(8/(@/%0?(A#($8/+#?(/&(83#(3#*J1(;35$(H5?#/
O388J$BPP@@@105&,#?5&1</>P0#*+&5&'PD*H*Q>#>/+"Q>*&*'#>#&8P83#Q+/0#Q/GQ83#Q3#*J\%]WMCCMETER(?#>/&$8+*8#$(3/@(3#*J
>#>/+"(@/+,$1
Square square1 = newnew Square(10.5);
Square square2 = newnew Square(5.2);
square1 = square2;
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 13 of 185http://itec2150.gitlab.io/
;3#(>#>/+"(500%$8+*85/&(83*8(5&<0%?#$(83#($8*<,(*&?(3#*J(G/+(83#(J+#H5/%$(#c*>J0#(*+#(*$(G/00/@$(A#G/+#(*&?(*G8#+
83#(*$$5'&>#&8($8*8#>#&8(5$(#c#<%8#?1
1.7. Passing Objects to Methods and Returning Objects from Methods
UAD#<8$(<*&(A#(J*$$#?(*$(*+'%>#&8$(8/(>#83/?$-($5>50*+(8/(J+5>585H#(8"J#(H*+5*A0#$1(43#&(/AD#<8$(*+#(J*$$#?(8/
>#83/?$-(83#(+#G#+#&<#$(/G(83#(/AD#<8$(*+#(J*$$#?(*$($3/@&(5&(835$(H5?#/ O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]`!8GAI0G<Q'R1
`#<*%$#(/G(83#(?5GG#+#&<#(A#8@##&(J+5>585H#(*&?(+#G#+#&<#(8"J#(H*+5*A0#$-(J*$$5&'(/AD#<8$PJ+5>585H#(8"J#$(8/
>#83/?$(+#$%08$(5&(?5GG#+#&8(A#3*H5/+1(7/>J*+#(83#(#c*>J0#(5&(83#(*A/H#(05&,#?(H5?#/(8/(83#(#c*>J0#($3/@&(5&
835$(H5?#/ O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]3^%2W5Jd%I@k8]LT$R1(j/85<#(3/@(@3#&(*(J+5>585H#(8"J#(H*+5*A0#(5$(J*$$#?
8/(*(>#83/?-(83#(H*0%#(/G(H*+5*A0#(x(5&(83#(main()(>#83/?(?/#$(&/8(<3*&'#1(43#+#*$-(@3#&(*&(/AD#<8(5$(J*$$#?(*$
*&(*+'%>#&8(8/(*(>#83/?-(83#(H*0%#(/G(A/83(*88+5A%8#$(/G(/AD#<8(person1(*+#(<3*&'#?1
l#8%+&5&'(/AD#<8$(G+/>(>#83/?$-(*0$/(5&H/0H#$(+#8%+&5&'(83#(+#G#+#&<#(/G(83#(/AD#<81(;/($##(*&(#c*>J0#(/G(835$-(0#8m$
*??(*(&#@(>#83/?(8/(/%+(Square(<0*$$(<+#*8#?(*A/H#1(;35$(>#83/?(0//,$(*$($3/@&(A#0/@B
4/5/21, 10)50 PMIntermediate Programming
Page 14 of 185http://itec2150.gitlab.io/
;35$(>#83/?(<+#*8#$(*(&#@(/AD#<8(/G(<0*$$(Square(@583(length(H*0%#(/G(CTT(*&?(+#8%+&$(835$(&#@0"(<+#*8#?(/AD#<81
!#+#-(58(?/#$(&/8(*<8%*00"(+#8%+&(*(</J"(/G(83#(&#@0"(<+#*8#?(/AD#<8-(+*83#+(58(+#8%+&$(83#(+#G#+#&<#(/G(83#(&#@0"
<+#*8#?(/AD#<81(;/(8#$8(835$(&#@(>#83/?-(5&(83#(8#$8#+(>#83/?-(@#(@500(5&H/,#(83#(getSquare()(>#83/?(*$($3/@&
A#0/@B
;3#($8*8#>#&8(.d%*+#($d%*+#E(]($d%*+#C1'#8.d%*+#ORe(5&H/,#$(>#83/?(getSquare()(/&(+#G#+#&<#(H*+5*A0#
square11(;35$(<+#*8#$(*(&#@(/AD#<8(@583(length(CTT-(*$(?#G5&#?(5&(83#(getSquare()(>#83/?(*&?(*(+#G#+#&<#(8/(83*8
&#@(/AD#<8(5$(+#8%+&#?1(;35$(5$(?#J5<8#?(5&(83#(5>*'#(A#0/@B(
publicpublic Square getSquare(){
Square newSquare = newnew Square(100);
returnreturn newSquare;
}
JAVA
publicpublic classclass SquareTester{
publicpublic staticstatic voidvoid main(String[] args){
Square square1 = newnew Square();
square1.setLength(10.5);
System.out.println(square1);
Square square2 = square1.getSquare();
System.out.println(square2);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 15 of 185http://itec2150.gitlab.io/
;35$(+#8%+&#?(/AD#<8-(/+(+*83#+(+#G#+#&<#(5$(83#&(*$$5'&#?(8/(square21(;3#(+#$%08(/G(83*8(5$($3/@&(A#0/@1(
./-(@3#&(square2(5$(J+5&8#?-(83#(+#$%08$($3/%0?(A#(<*0<%0*8#?(*<</+5?&'(8/(CTT(*$(83#(0#&'83(/G(83#($d%*+#1(;3#$#
+#$%08$(/G(A/83(square1(*&?(square2(*+#($3/@&(A#0/@1(
1.8. Key Terms
`#3*H5/+B(*<85/&$(/G(*&(/AD#<8e(+#J+#$#&8#?(A"(83#(>#83/?$(/G(*&(/AD#<81
70*$$B(*(A0%#J+5&8(83*8(?#G5&#$(*&(/AD#<81
!#*J(>#>/+"B(?"&*>5<*00"(*00/<*8#?(>#>/+"(%$#?(8/($8/+#(/AD#<8$1
UAD#<8P=&$8*&<#B(+#J+#$#&8$(*&(#&858"(5&(83#(+#*0(@/+0?(*&?(3*$($8*8#(*&?(A#3*H5/+1
UAD#<8Q/+5#&8#?(J+/'+*>>5&'B(*(@*"(/G(/+'*&5a5&'(</?#(*+/%&?(/AD#<8$-(5&$8#*?(/G(*<85/&$1
l#G#+#&<#B(>#>/+"(*??+#$$(/G(@3#+#(83#(/AD#<8(5$(0/<*8#?1
l#G#+#&<#(8"J#B(*(<0*$$e(H*+5*A0#(/G(835$(8"J#(<*&(+#G#+#&<#(*&(/AD#<8(/G(*(<0*$$1
l#G#+#&<#(H*+5*A0#B(H*+5*A0#(/G(*(<0*$$(8"J#-(@35<3(</&8*5&$(*(+#G#+#&<#(8/(83#(/AD#<8(/G(83*8(<0*$$1
.8*<,(>#>/+"B($8/+#$(0/<*0(H*+5*A0#$(/G(J+5>585H#(8"J#(*&?(+#G#+#&<#(H*+5*A0#$e(>#>/+"(5$(*<<#$$#?(5&(V5+$8(=&(N*$8
U%8(/+?#+1
4/5/21, 10)50 PMIntermediate Programming
Page 16 of 185http://itec2150.gitlab.io/
.8*8#B(+#J+#$#&8#?(A"(?*8*(G5#0?$(/+(*88+5A%8#$(/G(83#(/AD#<81
1.9. Exercises
1.9.1. Exercise 1
7+#*8#()#+$/&(*&?()#+$/&;#$8#+(<0*$$#$(8/(>/?#0(*(J#+$/&1(2#8*50$(*+#(*$(G/00/@$B
Tasks: Create business class Person
C1( )#+$/&($3/%0?(3*H#(83#(G/00/@5&'(J+/J#+85#$(O*88+5A%8#$-(5&$8*&<#(H*+5*A0#$-(>#>A#+(H*+5*A0#$R1()5<,
*JJ+/J+5*8#(?*8*(8"J#$B
*1( &*>#(i($8/+#$(83#(G%00(&*>#(/G(*(J#+$/&
A1( *'#(i($8/+#$(*'#(/G(*(J#+$/&(5&(@3/0#("#*+$
<1( #"#7/0/+(i($8/+#$(83#(</0/+(/G(*(J#+$/&m$(#"#$
E1( 7+#*8#(*(&/Q*+'%>#&8(</&$8+%<8/+(G/+(835$(<0*$$
*1( =&(83#(</&$8+%<8/+(G/+(83#()#+$/&(<0*$$-(%$#("/%+(5&G/+>*85/&(8/(5&585*05a#(83#(5&$8*&<#(H*+5*A0#$
I1( 7+#*8#(*(IQ*+'(</&$8+%<8/+(83*8(5&585*05a#$(83#(5&$8*&<#(H*+5*A0#$(8/(J*$$#?(J*+*>#8#+$
K1( 7+#*8#('#88#+$P>%8*8/+$(G/+(#*<3(5&$8*&<#(H*+5*A0#
L1( 7+#*8#($#88#+$P*<<#$$/+$(G/+(#*<3(5&$8*&<#(H*+5*A0#
M1( UH#++5?#(83#(8/.8+5&'OR(>#83/?(8/(+#8%+&(*(<0#*+(>#$$*'#(J+/H5?5&'(?#8*50$(/G(#*<3(J#+$/&1
W1( 7+#*8#(5&$8*&<#(>#83/?(8*0,OR(83*8(J+5&8$(*(>#$$*'#(f4#0</>#-(</?#+$n(=(*>(g-(%&?#+05&#($3/%0?(A#(+#J0*<#?(A"
83#(J#+$/&m$(&*>#1(=8(?/#$(&/8(&##?(*&"(J*+*>#8#+$(*&?(?/#$(&/8(+#8%+&(*&"(H*0%#$1
Tasks: Create tester class PersonTester
C1( =&(83#(>*5&OR(>#83/?-(<+#*8#(*&(/AD#<8(/G(<0*$$()#+$/&(<*00#?(J#+$/&C(%$5&'(83#(&/Q*+'%>#&8(</&$8+%<8/+1
E1( .U)(J#+$/&C(?#8*50$(%$5&'(8/.8+5&'OR1
I1( =&H/,#(83#(*JJ+/J+5*8#($#88#+(>#83/?(8/($#8P<3*&'#(83#(&*>#(/G(J#+$/&C(8/(f!/@*+?(l/*+,g1
K1( =&H/,#(83#(8*0,OR(>#83/?(/&(J#+$/&C1
L1( b&$%+#(83*8(*00(/G("/%+(</?#(5$(83/+/%'30"(</>>#&8#?1
1.9.2. Exercise 2
7+#*8#(*(A%$5&#$$(<0*$$(8/(>/?#0(*(A5,#1(;3#&(<+#*8#(*(8#$8#+(<0*$$(8/(8#$8(83#(A5,#(<0*$$(*&?(<+#*8#(05$8(/G(A5,#$1
2#8*50$(*+#(*$(G/00/@$B
Bike Class
C1( 7+#*8#(*(A%$5&#$$(<0*$$(<*00#?(`5,#
E1( `5,#$($3/%0?(*8(0#*$8(3*H#(&%>A#+(/G(@3##0$-(>*&%G*<8%+#+(*&?("#*+(?*8*1(F??585/&*0(?*8*(<*&(A#(<+#*8#?(*8
83#(J+/'+*>>#+m$(?5$<+#85/&1
4/5/21, 10)50 PMIntermediate Programming
Page 17 of 185http://itec2150.gitlab.io/
I1( F(&/Q*+'(</&$8+%<8/+(@583(H*0%#$(/G(E(G/+(&%>A#+(/G(@3##0$(8/(E-(f.<3@5&&g(G/+(>*&%G*<8%+#+-(*&?(ETCK(G/+
"#*+($3/%0?(#A(<+#*8#?1
K1( U83#+(</&$8+%<8/+$-('#88#+$-($#88#+$(*&?(8/.8+5&'OR(>#83/?($3/%0?(A#(<+#*8#?1
BikeTester Class
C1( Z$#(83#(`5,#;#$8#+(<0*$$(8/(<+#*8#(/AD#<8(A5,#C(%$5&'(&/Q*+'(</&$8+%<8/+1()+5&8(58$(?#8*50$(*G8#+(<+#*85/&1
E1( 7+#*8#(/AD#<8(A5,#E(5&(83#($*>#(>*&&#+-(*&?(%$#(83#($#88#+(>#83/?$(8/(<3*&'#(83#(5&$8*&<#(H*+5*A0#(H*0%#$(8/
83/$#(/G("/%+(<3/5<#1
I1( )+5&8(?#8*50$(/G(A5,#E1
K1( 7+#*8#(*&(*++*"05$8(/G(A5,#$(<*00#?(A5,#N5$81
L1( F??(A5,#C(*&?(A5,#E(8/(835$(05$8
M1( .U)(A5,#N5$8
W1( 7/%&8(83#(8/8*0(&%>A#+(/G(@3##0$(/&(*00(A5,#$(5&(83#(A5,#N5$8(*&?(J+5&8(83#>
[1( V5&?(83#("#*+(/G(83#(/0?#$8(A5,#(5&(83#(05$8(*&?(J+5&8(83*8("#*+1
^1( b&$%+#(83*8(*00(/G("/%+(</?#(5$(83/+/%'30"(</>>#&8#?1
1.9.3. Exercise 3
7+#*8#(<0*$$#$(8/(>/?#0(*&?(8#$8(8#c8A//,$1(V/00/@(83#(5&$8+%<85/&$($3/@&(A#0/@B
C1( 7+#*8#(*(A%$5&#$$(<0*$$(8/(>/?#0(*(8#c8A//,1(2#<5?#(@3*8(?*8*($3/%0?(A#($8/+#?(G/+(*(8#c8A//,(*&?(<+#*8#(*8
0#*$8(83+##(5&$8*&<#(H*+5*A0#$-(8@/(</&$8+%<8/+$-(*&?(*JJ+/J+5*8#('#88#+$-($#88#+$(*&?(8/.8+5&'OR(>#83/?1(7+#*8#
*8(0#*$8(/&#(5&$8*&<#(>#83/?(/G("/%+(<3/5<#1
E1( 7+#*8#(*(8#$8#+(<0*$$(@35<3(<+#*8#$(*8(0#*$8(8@/(/AD#<8$(/G(83#(A%$5&#$$(<0*$$1(=&H/,#(H*+5/%$('#88#+$-($#88#+$(*&?
5&$8*&<#(>#83/?O$R(8/(8#$8(83#(A%$5&#$$(<0*$$1
I1( 7/>>#&8("/%+(</?#(83/+/%'30"1
1.10. Issue Tracker/Comments
=$$%#(;+*<,#+ O388J$BPP'583%A1</>P3J*+,WP3#0JY?#$,P5$$%#$R
4/5/21, 10)50 PMIntermediate Programming
Page 18 of 185http://itec2150.gitlab.io/
2. Object-Oriented Programming Design, Class Relationship, and
Inheritance
2.1. Learning Outcomes
.8%?#&8$(@500(A#(*A0#(8/B
C1( 2#G5&#(83#(+#0*85/&$35J$(*>/&'(*$$/<5*85/&-(*''+#'*85/&-(</>J/$585/&-(*&?(5&3#+58*&<#1
E1( 2#G5&#(*($%A<0*$$(G+/>(*($%J#+<0*$$(83+/%'3(5&3#+58*&<#1
I1( =&H/,#(83#($%J#+<0*$$o(</&$8+%<8/+$(*&?(>#83/?$(%$5&'(83#($%J#+(,#"@/+?1
K1( UH#++5?#(5&$8*&<#(>#83/?$(5&(83#($%A<0*$$1
L1( 25$85&'%5$3(A#8@##&(/H#++5?5&'(*&?(/H#+0/*?5&'1
M1( bcJ0/+#(83#(?5GG#+#&<#$(A#8@##&(83#(J+/<#?%+*0(J*+*?5'>(*&?(/AD#<8Q/+5#&8#?(J*+*?5'>1
W1( 2#$5'&(J+/'+*>(%$5&'(83#(/AD#<8Q/+5#&8#?(J*+*?5'>1
2.2. Resources
2.2.1. Text
;35&,(6*H*(B(=&3#+58*&<# O388J$BPPA//,$18+5&,#815/P835&,D*H*EP<3*J8#+CK138>0X$#<CMKR(A"(F00#&(2/@&#"(*&?(73+5$(S*"G5#0?
;35&,(6*H*(B(70*$$(l#0*85/&$35J O388J$BPPA//,$18+5&,#815/P835&,D*H*EP<3*J8#+CK138>0X$#<CM[R(A"(F00#&(2/@&#"(*&?(73+5$
S*"G5#0?
;35&,(6*H*B(UAD#<8$(/G(UAD#<8$ O388J$BPPA//,$18+5&,#815/P835&,D*H*P<3*J8#+CK138>0R(A"(F00#&(2/@&#"(*&?(73+5$(S*"G5#0?
6*H*(UU) O388J$BPP@@@1@I$<3//0$1</>PD*H*PD*H*Y//J1*$JR
=&3#+58*&<# O388JBPP8%8/+5*0$1D#&,/H1</>PD*H*P5&3#+58*&<#138>0R
U+*<0#B(;3#(6*H*(;%8/+5*0 O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0PD*H*P=*&?=P$%A<0*$$#$138>0R
=&3#+58*&<# O388JBPP>*8313@$1#?%P#<,P<$CEKPD*H*&/8#$IP<LP$K138>0R
S#83/?(UH#+0/*?5&'(*&?(UH#++5?5&'(5&(6*H* O388J$BPP@@@1A*#0?%&'1</>PD*H*Q>#83/?Q/H#+0/*?Q/H#++5?#R
2.2.2. Videos
=&3#+58*&<# O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]N$?*a8JIY0@R
=&3#+58*&<#(5&(6*H* O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]aAhFZW0_ELpR
ZSN(70*$$(25*'+*>(*&?(70*$$(+#0*85/&$35J O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]Z=M0d!Uh!5<R
b&<*J$%0*85/&(5&(6*H* O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]<Z^K./LK<+[R
2.3. Introduction
4/5/21, 10)50 PMIntermediate Programming
Page 19 of 185http://itec2150.gitlab.io/
V+/>(J+#H5/%$(<3*J8#+$(*&?(/J#&($/%+<#(8#c8A//, O388JBPP58#<ECKT1??&$1&#8PR-("/%(0#*+&#?(3/@(8/(?#G5&#(<0*$$#$(G/+
/AD#<8$1(V5+$8-(83#(A/?"(/G(*(<0*$$(?#<0*+#$(>#>A#+$(OG5#0?$(*&?(>#83/?$R-(5&$8*&<#(*&?($8*85<(5&585*05a#+$-(*&?
</&$8+%<8/+$1(l#'*+?5&'(83#($</J#(/G(*(>#>A#+-(58(5$(83#(#&85+#(A/?"(/G(83#(?#<0*+*85/&(/G(83#(<0*$$(8/(@35<3(83#
>#>A#+(A#0/&'$1(V5#0?-(>#83/?-(>#>A#+(<0*$$-(>#>A#+(5&8#+G*<#(O"/%(@500(0#*+&($//&R(*&?(</&$8+%<8/+
?#<0*+*85/&$(>*"(5&<0%?#(83#(*<<#$$(>/?5G5#+$($%<3(*$(J%A05<-(J+/8#<8#?-(J+5H*8#(/+(?#G*%081
=&(835$(<3*J8#+-(@#(@500(#cJ0/+#(/AD#<8Q/+5#&8#?(J+/'+*>(?#$5'&-(<0*$$(+#0*85/&$35J(*&?(5&3#+58*&<#1(9/%(@500(0#*+&
3/@(>#>A#+(/G(*(<0*$$(5&<0%?#$(A/83(?#<0*+#?(*&?(5&3#+58#?(>#>A#+$(5&(8#+>$(/G(#&<*J$%0*85/&(*&?(5&3#+58*&<#1
=8(>#*&$(83*8("/%(@500(0#*+&(3/@(83#(&#@0"(?#<0*+#?(fields(<*&(35?#(G5#0?$(?#<0*+#?(5&(*($%J#+<0*$$-(&#@0"(?#<0*+#?
class(>#>A#+(<*&(35?#(<0*$$(/+(5&8#+G*<#(>#>A#+$(?#<0*+#?(5&(*($%J#+<0*$$-(*&?(&#@0"(?#<0*+#?(methods(<*&
35?#-(5>J0#>#&8-(/+(/H#++5?#(>#83/?$(?#<0*+#?(5&(*($%J#+<0*$$1(O$/%+<#B(;3#(6*H*(N*&'%*'#(.J#<5G5<*85/&(A"(6*>#$
q/$05&'-(`500(6/"-(q%"(.8##0#-(q50*?(`+*<3-(F0#c(`%<,0#0"R
C1( UAD#<8(U+5#&8#?()+/'+*>(2#$5'&
E1( 70*$$(l#0*85/&$35J
I1( =&3#+58*&<#
2.4. Object Oriented Program Design
UAD#<8(/+5#&8#?(J+/'+*>>5&'(5$(*$$/<5*8#?(@583(83#(</&<#J8$(/G(<0*$$-(/AD#<8-(5&3#+58*&<#-(#&<*J$%0*85/&-
*A$8+*<85/&-(J/0">/+J35$>1(=&(J+#H5/%$(<3*J8#+-("/%(0#*+&#?(*A/%8(<0*$$#$-(*&?(/AD#<8$1(;3#(G/<%$(/G(835$(<3*J8#+
5$(8/(#cJ0/+#(/AD#<8Q/+5#&8#?(J+/'+*>>5&'1
2.4.1. Class Abstraction and Encapsulation
9/%(@500(0#*+&(*A/%8(FA$8+*<85/&(5&(&#c8(<3*J8#+(A%8(G/00/@5&'(?#$<+5J85/&(#cJ0*5&$(*A/%8(FA$8+*<85/&(5&(A+5#G(8/
3#0J("/%(%&?#+$8*&?(83#(b&<*J$%0*85/&(5&(8#+>$(/G(?*8*(35?5&'(@35<3(5$(/&#(/G(J+5&<5J0#$(/G(/AD#<8Q/+5#&8#?
J+/'+*>>5&'1
FA$8+*<85/&(Q(FA$8+*<85/&(5$(*A/%8(83#(d%*058"(/G(?#*05&'(@583(5?#*$(+*83#+(83*&(#H#&8$1(=8(>#*&$(83*8(*A$8+*<85/&
O388J$BPP@@@1@I$<3//0$1</>PD*H*PD*H*Y*A$8+*<81*$JR(5$(*A/%8(35?5&'(83#(?#8*50$(*&?($3/@5&'(83#(#$$#&85*0(</>J/&#&8$
8/(83#(%$#+1(V/+(#c*>J0#-(@583/%8(,&/@5&'(*A/%8(*00(83#(>#<3*&5<$(*A/%8(83#(<*+-("/%(<*&(?+5H#(*(<*+1(UG
</%+$#-(5G("/%(*+#(*&(*%8/>/85H#(#&'5&##+-(83#&("/%+(D/A(5$(8/(<+#*8#(83#(>/$8(J/@#+G%0(*&?(35'3(d%*058"(H#35<0#
83*8(>*&"(J#/J0#(<*&(#&D/"(?+5H5&'1(9/%(>%$8(*0$/(,&/@(*00(83#(?#8*50$(8/(?#$5'&($/G8@*+#($"$8#>$-(*$$#>A0"-
8//05&'-(#H*0%*8#(J+/A0#>$-(#8<1(`%8(5G("/%(D%$8(?+5H#(*(<*+-("/%(?/(&/8(&##?(8/(,&/@(3/@(#*<3(</>J/&#&8(@/+,$
5&8#+*<85H#0"(5&$5?#(/G(*(<*+("/%(?+5H#(#H#+"?*"1(;35$(</&<#J8(5$(*A/%8(@35<3(?*8*(/+(5&G/+>*85/&($3/%0?(A#
H5$5A0#(*&?(@35<3(?*8*(/+(5&G/+>*85/&($3/%0?(A#(35??#&1
FA$8+*<85/&(<*&(A#(*<35#H#?(@583(#583#+(*A$8+*<8(<0*$$#$(/+(5&8#+G*<#$(*&?("/%(@500(0#*+&(>/+#(*A/%8(*A$8+*<85/&(5&
&#c8(<3*J8#+1
b&<*J$%0*85/&(Q(rClass encapsulation and abstraction are two sides of the same coinr(ON5*&'-(ETC[-
=&8+/?%<85/&(8/(6*H*B()+/'+*>>5&'(*&?(2*8*(.8+%<8%+#R(=8(5$(*(J+/<#$$(/G(@+*JJ5&'(</?#(*&?(?*8*(8/'#83#+(5&(*
$5&'0#(%&581(`"(%$5&'(83#(</&<#J8(/G(*<<#$$#+(*&?(>/?5G5#+O#1'('#88#+(*&?($#88#+(>#83/?$R-("/%(<*&(+#*?(?*8*(/+
4/5/21, 10)50 PMIntermediate Programming
Page 20 of 185http://itec2150.gitlab.io/
/&0"(*<<#$$(?*8*1(=G("/%(+#*?(83#(Person(<0*$$-(58(3*$(8@/(J+5H*8#(?*8*B(name(*&?(countryOfOrigin1(`"(%$5&'
83#(getName()(*&?(setName()(>#83/?$-(5&(83#(Main(8#$8#+(<0*$$-("/%(<*&(<3*&'#(83#(name(A"(<*005&'
setName(String name)(*&?(*<<#$$(83#(name(A"(<*005&'(getName()1(!/@#H#+-(83#+#(5$(&/($#88#+(8/(>/?5G"(83#
</%&8"(/G(/+5'5&1(U&0"(getCountryOfOrigin()(>#83/?(5$(J+/H5?#?1(;3#+#G/+#-("/%(<*&&/8(>/?5G"(/+($#8(83#
H*0%#(5&(83#(countryOfOrigin(?*8*(>#>A#+1(b&<*J$%0*85/&(5$(8/(35?#(83#(?*8*-(5>J0#>#&8*85/&-(83#(<0*$$
O*A$8+*<8(<0*$$(/+(5&8#+G*<#("/%(@500(0#*+&(*G8#+(835$(<3*J8#+R-(?#$5'&(*&?(5&$8*&85*85/&1
2.5. Class Relationship
;/(?#$5'&(<0*$$#$-(F$$/<5*85/&-(F''+#'*85/&-(7/>J/$585/&-(*&?(=&3#+58*&<#(*+#(G/%+(J/5&8$(@#(%$#(8/(?5$<%$$(83#
+#0*85/&$35J$(*>/&'(<0*$$#$1
2.5.1. Association
rF$$/<5*85/&(5$(*('#&#+*0(A5&*+"(+#0*85/&$35J(83*8(?#$<+5A#$(*&(*<85H58"(A#8@##&(8@/(<0*$$#$1(V/+(#c*>J0#-(*($8%?#&8
8*,5&'(*(</%+$#(5$(*&(*$$/<5*85/&(A#8@##&(83#(Student(<0*$$(*&?(83#(Course(<0*$$1(F&?(*(G*<%08"(>#>A#+(8#*<35&'
*(</%+$#(5$(*&(*$$/<5*85/&(A#8@##&(Faculty(<0*$$(*&?(83#(Course(<0*$$1r(ON5*&'1(ETC[(Q(=&8+/?%<85/&(8/(6*H*R
bc*>J0#B(s(7/%+$#-(;#*<3#+(*&?(.8%?#&8B(U&#(/+(S/+#($8%?#&8$(<*&(*$$/<5*8#(@583(*($5&'0#(8#*<3#+(Ul(U&#($5&'0#
$8%?#&8(<*&(*$$/<5*8#(@583(/&#(/+(>/+#(8#*<3#+$1(F&?(A/83(/G(83#>(<*&(A#(<+#*8#?(*&?(?#0#8#?(5&?#J#&?#&80"1(./
@3#&(*(8#*<3#+(0#*H#$(83#($<3//0-(83#&(8#*<3#+(@500(A#(+#>/H#?(G+/>(83#(05$8(*&?(@#(?/(&/8(&##?(8/(?#0#8#(*&"
publicpublic classclass Person
{
privateprivate String name;
privateprivate String countryOfOrigin = "Germany";
publicpublic String getName()
{
returnreturn name;
}
publicpublic voidvoid setName(String name)
{
thisthis.name = name;
}
publicpublic String getCountryOfOrigin()
{
returnreturn countryOfOrigin;
}
}
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
Person person = newnew Person();
person.setName("Ludwig van Beethoven");
System.out.println(person.getName() + " was a " + person.getCountryOfOrigin() + " composer." );
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 21 of 185http://itec2150.gitlab.io/
$8%?#&8$1(F&?(@3#&(*($8%?#&8(0#*H#$(83#($<3//0-(*&"(8#*<3#+$(*+#(&/8(0#*H5&'1(V/00/@5&'(#c*>J0#($/%+<#(</?#(5$
$3/@5&'(3/@(8/(5>J0#>#&8(*$$/<5*85/&(%$5&'(83#(<0*$$#$(/G(Student-(Course-(*&?(Teacher1(V/+(#c*>J0#-
addstudent()(>#83/?(5&(83#(Course(<0*$$(*&?(addCourse()(>#83/?(5&(83#(Student(<0*$$($3/@$(3/@(83#
*$$/<5*85/&(+#0*85/&$35J(5$(5>J0#>#&8#?1
./>#(/G(83#(*$$/<5*85/&(+#0*85/&$35J(<*&(A#(<*8#'/+5a#?(*$(#583#+(F''+#'*85/&(/+(7/>J/$585/&1(.##(;35$(#c*>J0#
O388J$BPP@@@1'##,$G/+'##,$1/+'P*$$/<5*85/&Q</>J/$585/&Q*''+#'*85/&QD*H*PR
2.5.2. Aggregation
classclass Course
{
privateprivate String courseName;
privateprivate ArrayList<Student> enrollmentList = newnew ArrayList<Student>();
privateprivate Teacher teacher;
publicpublic voidvoid addStudent(Student student)
{
enrollmentList.add(student);
}
}
classclass Student {
privateprivate ArrayList<Course> registeredCourse;
privateprivate String studentName;
publicpublic voidvoid addCourse(Course course)
{
//
}
}
classclass Teacher
{
privateprivate ArrayList<Course> teachingCourse;
publicpublic voidvoid addCourse(Course c)
{
//
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 22 of 185http://itec2150.gitlab.io/
;35$(+#0*85/&$35J(5$(r3*$Q*r(+#0*85/&$35J(A#8@##&(8@/(/AD#<8$1(=8(5$(>/+#($J#<5*05a#?(+#0*85/&$35J(/G(83#(*$$/<5*85/&
+#0*85/&$35J1(;3#(/@&#+(/AD#<8(5$(<*00#?(*&(*''+#'*85&'(/AD#<8(*&?(58$(<0*$$(5$(<*00#?(*''+#'*85&'(<0*$$1(7350?(/AD#<8
/+($%AD#<8(/AD#<8(5$(<*00#?(*''+#'*8#?(/AD#<8(*&?(58$(<0*$$(5$(<*00#?(*''+#'*8#?(<0*$$1
bc*>J0#$B(Student(<0*$$(*&?(Address(<0*$$1(.8%?#&8(<0*$$(<*&(3*H#(+#G#+#&<#(/G(F??+#$$(<0*$$(A%8(F??+#$$(<0*$$
?/#$(&/8(3*H#(83#(+#G#+#&<#(/G(.8%?#&8(<0*$$1(=8(?/#$(&/8(5&H/0H#(/@&5&'($/(*??+#$$(?/#$(&/8(&##?(8/(A#(85#?(8/(*
$8%?#&81(F&(address can exist by itself1
Employee(<0*$$(>*"(3*H#(G5#0?$(id, name, email, phone-(#8<1(F&?(*0$/(</&8*5&$(*&(/AD#<8(&*>#?(address1(N#8m$
$%JJ/$#(835$(*??+#$$(3*$(G5#0?$(city, state, zipcode-(#8<1(=&(835$(<*$#-(Employee(3*$(*&(address($/(83#(+#0*85/&$35J
5$(rb>J0/"##(Has-A(*??+#$$1r(O.##(A#0/@R
2.5.3. Composition
;35$(+#0*85/&$35J(5$(*0$/(3*$Q*(+#0*85/&$35J(@3#&(*(<0*$$(3*$(83#(/@&#+$35J(/G(*&/83#+($%AD#<8(<0*$$(A%8(83#($%AD#<8
depends on(83#(/@&#+(<0*$$1(=G(83#(#c5$8#&<#(/G(83#(*''+#'*8#?(/AD#<8(5$(?#J#&?#&8(/&(83#(*''+#'*85&'(/AD#<8-(83#&
83#(+#0*85/&$35J(5$(</>J/$585/&1(7/>J/$585/&(5$(83#($8+/&'#$8(G/+>(/G(*$$/<5*85/&(*&?(*''+#'*85/&(5$(@#*,#+
+#0*85/&$35J(@350#(F''+#'*85/&(*&?(7/>J/$585/&(*+#(H#+"($5>50*+1(V/+(#c*>J0#-(J0#*$#(8*,#(*(0//,(*8(83#(G/00/@5&'
J+/'+*>$-(Person(*&?(Job1(;3#(Person(3*$(*(D/A(5&$8*&<#(H*+5*A0#($/(@#(<*&(<+#*8#(*(Job(<0*$$1(=8(3*$(r*(person
3*$(*(jobr(+#0*85/&$35J1
classclass Employee
{
intint id;
String name;
Address address;
//Address is a class
}
JAVA
publicpublic classclass Person
{
//composition has-a relationship
privateprivate Job job;
publicpublic Person()
{
thisthis.job=newnew Job();
job.setSalary(1000L);
}
publicpublic longlong getSalary()
{
returnreturn job.getSalary();
}
}
publicpublic classclass Job
{
privateprivate String position;
privateprivate longlong salary;
privateprivate intint id;
publicpublic String getRole()
{
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 23 of 185http://itec2150.gitlab.io/
;3/+>A#&(6*&$$#&(#cJ0*5&$(*A/%8(83#(</>J/$585/&(</&&#<85&'(8/(83#(+#*0(@/+0?(5&<0%?5&'(A#&#G58$(@583(>/+#
#c*>J0#$ O388J$BPP$8*<,5G"1</>P//JQ</&<#J8$Q</>J/$585/&PR1
2.6. Inheritance
=&3#+58*&<#(5$(*(J/@#+G%0(>#<3*&5$>(83*8(*00/@$("/%(8/(?#G5&#(&#@(<0*$$#$(G+/>(*&(#c5$85&'(<0*$$1(;3#(#c5$85&'
<0*$$(5$(*(>/+#('#&#+*0(<0*$$-(<*00#?(*($%J#+<0*$$1(F($%J#+<0*$$(5$(*0$/(+#G#++#?(8/(*$(*(J*+#&8(<0*$$(/+(*(A*$#(<0*$$1
;3#(6*H*(5&3#+58*&<#(*00/@$(*(6*H*(<0*$$(8/(5&3#+58(G+/>(*($5&'0#($%J#+<0*$$1(U&0"($5&'%0*+(5&3#+58*&<#(5$(*00/@#?1
;3#(&#@(<0*$$#$(*+#($J#<5*05a#?(<*$#$(/G(83#($%J#+<0*$$1(;3#"(*+#(<*00#?($%A<0*$$#$(/G(83*8($%J#+(<0*$$1(F($%A<0*$$
*%8/>*85<*00"(5&3#+58$(*00(83#(5&$8*&<#(H*+5*A0#$(>#83/?$(G+/>(58$($%J#+<0*$$1(=8(>*"(?#G5&#(58$(/@&(&#@(H*+5*A0#$
*&?(>#83/?$1(=8(>*"(*0$/(/H#++5?#(*&"(5&3#+58#?(>#83/?$1(=8(>#*&$(83*8(83#(>#83/?(3*$(83#($*>#(>#83/?
$5'&*8%+#(5&(A/83(83#($%J#+<0*$$(*&?($%A<0*$$1(=8(>#*&$(8/(J+/H5?#(*(&#@(5>J0#>#&8*85/&(G/+(83*8(>#83/?(5&(83#
$%A<0*$$1
returnreturn position;
}
publicpublic voidvoid setRole(String position)
{
thisthis.position = position;
}
publicpublic longlong getSalary()
{
returnreturn salary;
}
publicpublic voidvoid setSalary(longlong salary)
{
thisthis.salary = salary;
}
publicpublic intint getId()
{
returnreturn id;
}
publicpublic voidvoid setId(intint id)
{
thisthis.id = id;
}
}
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
Person john = newnew Person();
longlong salary = john.getSalary();
System.out.println("John's Salary is " + salary);
}
}
4/5/21, 10)50 PMIntermediate Programming
Page 24 of 185http://itec2150.gitlab.io/
;3#($%A<0*$$(*&?(58$($%J#+<0*$$(G/+>(*(is-a(+#0*85/&$35J1(.##(83#(?5*'+*>(/G(*($%J#+<0*$$(<*00#?(Vehicle(*&?(8@/
$%A<0*$$#$(<*00#?(Car(*&?(Truck-(Car(5$(*(Vehicle(*&?(Truck(5$(*(Vehicle1(=&(/83#+(@/+?$-(Car(<0*$$(5&3#+58$(*00
*<<#$$5A0#(?*8*(G5#0?$(*&?(>#83/?$(G+/>(83#(Vehicle(<0*$$(*&?(83#(;+%<,(<0*$$(5&3#+58$(*00(*<<#$$5A0#(?*8*(G5#0?$(*&?
>#83/?$(G+/>(83#(Vehicle(<0*$$1(=&(ZSN-(*&(*++/@(J/5&8$(G+/>(*($%A<0*$$(8/(58$($%J#+<0*$$1
F<</+?5&'(8/(2*H5?(F1(;*"0/+(5&(35$(A//,(UAD#<8QU+5#&8#?(;#<3&/0/'"-(3#(>#&85/&#?(83*8(83#(/AD#<8Q/+5#&8#?
*JJ+/*<3(5$(>/+#(&*8%*01(V/+(#c*>J0#-r83#(A*$5<(A%50?5&'(A0/<,(/%8(/G(@35<3(*00(05H5&'(835&'$(*+#(</>J/$#?(5$(83#
<#001(7#00$(*+#(/+'*&5<(J*<,*'#(83*8-(05,#(/AD#<8$-(</>A5&#(+#0*8#?(5&G/+>*85/&(*&?(A#3*H5/+1(S/$8(/G(83#
5&G/+>*85/&(5$(</&8*5&#?(5&(J+/8#5&(>/0#<%0#$(@5835&(83#(&%<0#%$(/G(83#(<#001(;3#(A#3*H5/+-(@35<3(>*"(+*&'#(G+/>
#&#+'"(</&H#+$5/&(8/(>/H#>#&8-(5$(<*++5#?(/%8(A"($8+%<8%+#(/%8$5?#(83#(&%<0#%$1g(!#(*0$/(#cJ0*5&#?(*A/%8(83#
35#+*+<3"(/G(<#00(8"J#$(*$("/%($##(83#(5&3#+585&'(<#00(?5*'+*>1(r;3#(<#00(5$(8+%0"(%&5H#+$*0(A%50?5&'(A0/<,1(F00(<#00$
$3*+#(*(</>>/&($8+%<8%+#(*&?(/J#+*8#(*<</+?5&'(8/(83#($*>#(A*$5<(J+5&<5J0#$1(45835&(835$(A*$5<($8+%<8%+#-(J0*&8
<#00$(3*H#(*(3*+?(/%8#+(@*00(8/(>*,#(83#>(+5?'5?-(A0//?(<#00$(*+#(>/A50#(*&?($J#<5*05a#?(8/(8+*&$J/+8('*$#$-(>%$<0#
<#00$(*+#(*A0#(8/(?5$8/+8(83#5+($3*J#(8/(J#+G/+>(>#<3*&5<*0(@/+,1r
2.6.1. Overloading vs. Overriding
UH#+0/*?5&'(*(>#83/?(5$(8/(?#G5&#(>%085J0#(>#83/?$(@583(83#($*>#(&*>#(A%8(?5GG#+#&8($5'&*8%+#$1
4/5/21, 10)50 PMIntermediate Programming
Page 25 of 185http://itec2150.gitlab.io/
UH#++5?5&'(>#83/?$(5$(8/(J+/H5?#(*(&#@(5>J0#>#&8*85/&(G/+(*(>#83/?(5&(83#($%A<0*$$(O.##(#c*>J0#(>#83/?$
G+/>(ClassB(*&?(NamePointR1
=&(G/00/@5&'(?5*'+*>-(83#(ClassA(5$(83#($%J#+<0*$$(*&?(ClassB(5$(83#($%A<0*$$1
publicpublic classclass Addition
{
publicpublic intint add(intint a, intint b)
{
returnreturn a + b;
}
publicpublic intint add(intint a, intint b, intint c)
{
returnreturn a + b + c;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 26 of 185http://itec2150.gitlab.io/
=&(G/00/@5&'(?5*'+*>-(83#(ClassA(5$(83#($%J#+<0*$$(G/+(ClassC(*&?(ClassB(*&?(ClassC(5$(83#($%J#+<0*$$(G/+(ClassD1
/**
* Class: ClassA
*/
publicpublic classclass ClassA {
protectedprotected intint m;
publicpublic String toString()
{
returnreturn newnew String("(" + m + ")");
}
}
/**
* Class: ClassB
*/
publicpublic classclass ClassB extendsextends ClassA
{
privateprivate intint n;
publicpublic String toString()
{
returnreturn newnew String ("(" + m + "," + n + ")");
}
}
/**
* Class: Main
*/
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
ClassA a = newnew ClassA();
System.out.println("a = " + a);
ClassB b = newnew ClassB();
System.out.println("b = " + b);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 27 of 185http://itec2150.gitlab.io/
N#8m$(8*,#(*(0//,(*8(;3#(Point(<0*$$(*&?(NamedPoint(<0*$$1(;3#(Point(<0*$$(3*$(8@/(J+5H*8#(5&$8*&<#(H*+5*A0#$(c
*&?("(*&?(58(5$(83#($%J#+<0*$$(/G(*($%A<0*$$(NamedPoint(<0*$$1(F$(*(+#$%08(/G(5&3#+58*&<#-(83#(NamedPoint(<0*$$
5&$8*&<#P/AD#<8(@500(3*H#(83+##(G5#0?$B(x(*&?(y(5&3#+58#?(G+/>(83#($%J#+<0*$$(Point(*&?(83#(.8+5&'(5&$8*&<#(H*+5*A0#
name(@35<3(5$(?#G5&#?(5&(83#(NamedPoint(<0*$$1
;3#(NamedPoint(<0*$$(3*$(*(8/8*0(G5H#(+#'%0*+(>#83/?$1(=8(5&3#+58$(83#(equals(), getX(), and getY()(>#83/?$(G+/>
83#($%J#+<0*$$(Point1(=8(<+#*8#$(*(&#@(>#83/?(getName()(*&?(/H#++5?#$(*&(#c5$85&'(>#83/?(toString()1(`#$5?#$
83#(G5H#(+#'%0*+(>#83/?$-(83#(NamedPoint(<0*$$(*0$/(</&8*5&$(*(</&$8+%<8/+(@583(83+##(J*+*>#8#+$B(x, y-(*&?
name1(;3#(</&$8+%<8/+(/G(83#(NamedPoint(5&H/,#$(83#(</&$8+%<8/+(/G(83#(Point($%J#+<0*$$(%$5&'(83#(,#"@/+?
$%J#+(A"(J*$$5&'(x(*&?(y1(=&3#+58*&<#(*JJ05#$(8/(<0*$$(G5#0?(x(*&?(y(*&?(>#83/?$(getX(), getY(), equals()-(A%8(&/8
</&$8+%<8/+$1(=8(5$(A#<*%$#(83#(</&$8+%<8/+(>%$8(*0@*"$(3*H#(83#($*>#(&*>#(*$(58$(<0*$$1(;3%$-($%A<0*$$#$(%$%*00"
#cJ05<580"(?#G5&#(83#5+(/@&(</&$8+%<8/+$1
publicpublic classclass Point
{
//objects represent lattice points in the cartesian plane
//object are immutable
protectedprotected doubledouble x, y;
// the point's coordinates. In this program, we used protected.
publicpublic Point(doubledouble x, doubledouble y){
thisthis.x = x;
thisthis.y = y;
}
publicpublic booleanboolean equals(Point p)
{
returnreturn (x == p.x && y == p.y);
}
publicpublic doubledouble getX()
{
returnreturn x;
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 28 of 185http://itec2150.gitlab.io/
}
publicpublic doubledouble getY()
{
returnreturn y;
}
publicpublic String toString()
{
returnreturn newnew String("(" + getX() + ", " + getY() + ")");
//why getX() and getY()?
}
}
/**
* this class to hold named location's information for data point (coordinates of x and y).
*/
publicpublic classclass NamedPoint extendsextends Point
{
finalfinal privateprivate String name;
publicpublic NamedPoint(doubledouble x, doubledouble y, String name)
{
supersuper(x, y);
//super keyword is to invoke superclass' constructor. This statement super(x,y) invokes the Point class
constructor
//that matches the arguments and it must be the first statement of the subclass' constructor as you see
here.
//this is the way to explicitly invoke a superclass construtor.
thisthis.name = name;
}
publicpublic String getName()
{
returnreturn name;
}
@Override
publicpublic String toString()
{
returnreturn name + "(" + x + ", " + y + ")";
//why x and y?
}
}
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
NamedPoint p = newnew NamedPoint(0,0, "Origin (O)");
System.out.println("p: " + p);
System.out.println("P.getName(): " + p.getName());
System.out.println("p.getX(): " + p.getX());
//getX() method is inherited from the Point superclass.
NamedPoint q = newnew NamedPoint(1,10,"A");
System.out.println("q: " + q);
System.out.println("q.equals(p): " + q.equals(p));
//q is able to invoke the equals() method that it inherited from the superclass.
4/5/21, 10)50 PMIntermediate Programming
Page 29 of 185http://itec2150.gitlab.io/
2.6.2. Accessibility Issue
Table 1. Access Levels
Modifier Class Package Subclass World
J%A05< 9 9 9 9
J+/8#<8#? 9 9 9 j
&/(>/?5G5#+(/+
?#G*%08
9 9 j j
J+5H*8# 9 j j j
=G(83#(5&$8*&<#(H*+5*A0#$(5&(83#($%J#+(<0*$$(*+#(J+5H*8#-(83#($%A<0*$$(<*&&/8(?5+#<80"(*<<#$$(83#$#(H*+5*A0#$-(#H#&
83/%'3(83#"(*+#(5&3#+58#?1(;/(+#8+5#H#P%J?*8#(83#(H*0%#$(/G(83#$#(H*+5*A0#$-(83#('#88#+P$#88#+(>#83/?(/G(83#($%J#+
<0*$$(@500(&##?(8/(A#(%$#?1(;3#(G/00/@5&'(5$(*&(#c*>J0#1
}
}
// Testing if a subclass can access the private members of a superclass
classclass Vehicle
{
privateprivate String licensePlate;
publicpublic String getLicensePlate()
{
returnreturn licensePlate;
}
publicpublic voidvoid setLicensePlate(String licensePlate)
{
thisthis.licensePlate = licensePlate;
}
}
classclass Car extendsextends Vehicle
{
privateprivate intint numberOfSeat;
privateprivate intint manufacturedYear;
publicpublic intint getNumberOfSeat()
{
returnreturn numberOfSeat;
}
publicpublic voidvoid setNumberOfSeat(intint numberOfSeat)
{
thisthis.numberOfSeat = numberOfSeat;
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 30 of 185http://itec2150.gitlab.io/
2.7. Key Terms
F<<#$$($J#<5G5#+$B(J+5H*8#-(?#G*%08-(J+/8#<8#?-(*&?(J%A05<1
F$$/<5*85/&B(*('#&#+*0(A5&*+"(+#0*85/&(A#8@##&(8@/($#J*+*8#(<0*$$#$1(V/+(#c*>J0#-(*($8%?#&8(8*,5&'(*(</%+$#(5$(*&
*$$/<5*85/&(A#8@##&(83#(.8%?#&8(<0*$$(*&?(83#(7/%+$#(<0*$$1
F''+#'*85/&B(*&(*$$/<5*85/&(@3#&(/&#(/AD#<8(%$#$(*&/83#+(/AD#<81
7/>J/$585/&B(*&(*$$/<5*85/&(@3#&(/&#(/AD#<8(/@&$(/83#+(<0*$$(*&?(/83#+(<0*$$(<*&&/8(>#*&5&'G%00"(#c5$81
7/>J/$585/&(5$($8+/&'#+(83*&(*''+#'*85/&1
2#G*%08(*<<#$$($J#<5G5#+(O&/(>/?5G5#+RB(*<<#$$5A0#(@5835&(83#($*>#(J*<,*'#1
}
publicpublic intint getManufacturedYear()
{
returnreturn manufacturedYear;
}
publicpublic voidvoid setManufacturedYear(intint manufacturedYear)
{
thisthis.manufacturedYear = manufacturedYear;
}
@Override
publicpublic String toString()
{
returnreturn "Car\n" +
"Number of Seats: " + numberOfSeat + '\n' +
"Manufactured Year: " + manufacturedYear;
}
}
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
Vehicle vehicle = newnew Vehicle();
Car car = newnew Car();
//no error to set value because setLicensePlate() is public method of the Vehicle superclass
//but you cannot modify the value of licensePlate directly from the superclass - Vehicle
vehicle.setLicensePlate("UIO678");
car.setNumberOfSeat(7);
//no error to setNumberOfSeat because setNumberOfSeat() is a public method of
the Car class
car.setManufacturedYear(2020);
//no error to setManufacturedYear because setManufacturedYear() is a
public method of the Car class
System.out.println(car.toString());
}
}
4/5/21, 10)50 PMIntermediate Programming
Page 31 of 185http://itec2150.gitlab.io/
=&3#+58*&<#B(*(>#<3*&5$>(8/(?#G5&#(&#@(<0*$$#$(G+/>(#c5$85&'(<0*$$#$1(=&(D*H*-(<0*$$#$(<*&(5&3#+58(83#(J+/J#+85#$
*&?(>#83/?$(/G($%J#+<0*$$1
J+5H*8#(*<<#$$($J#<5G5#+B(*<<##$$5A0#(@5835&(83#(<0*$$(@3#+#(?#G5&#?1
J+/8#<8#?(*<<#$$($J#<5G5#+B(*<<#$$5A0#(8/(<0*$$
J%A05<(*<<#$$($J#<5G5#+B(*<<#$$5A0#(G+/>($%A<0*$$#$(*&?(>#>A#+$(/G(83#($*>#(J*<,*'#1
UH#++5?5&'(>#83/?B43#&(*(>#83/?(5&(*($%A<0*$$(3*$(83#($*>#(&*>#-($*>#(J*+*>#8#+$(/+($5'&*8%+#-(*&?($*>#
+#8%+&(8"J#$O/+($%AQ8"J#R(*$(*(>#83/?(5&(58$($%J#+<0*$$1
UH#+0/*?5&'(>#83/?B(*(<0*$$(5$(*00/@#?(8/(3*H#(>/+#(83*&(/&#(>#83/?(3*H5&'(83#($*>#(&*>#(*$(0/&'(*$(83#5+
J*+*>#8#+(05$8$(*+#(?5GG#+#&81
.%J#+<0*$$(O*(J*+#&8(<0*$$(/+(*(A*$#(<0*$$RB(*('#&#+*0(<0*$$(@35<3(*(>#83/?O$R(8/(*($%A<0*$$1(U+(83#(<0*$$(A#5&'
5&3#+58#?(G+/>1
.%A<0*$$(O<350?RB(;3#(?#+5H#?(<0*$$(83*8(5$(?#+5H#?(G+/>($%J#+<0*$$1
2.8. Exercises
2.8.1. Exercise 1
V50#(*00/<*85/&B(4+58#(*(J+/'+*>(&*>#?(.8/+*'#(*&?(*(;#$8#+()+/'+*>(8/(*00/<*8#(G50#$(A*$#?(/&(83#(G50#($5a#(*&?
>*c5>%>(<*J*<58"(/G(83#($8/+*'#1(F($8/+*'#(3*$(>%085J0#(A0/<,$(8/($8/+#(G50#$1(b*<3(A0/<,(3*$(>*c5>%>(<*J*<58"(8/
$8/+#(G50#$(*&?(>*c5>%>(<*J*<58"(@500(A#('5H#&(A"(83#(%$#+1()0#*$#(%$#(83#(G5+$8(G58(*0'/+583>(83*8(J0*<#$(#*<3(G50#
5&8/(83#(G5+$8(A0/<,(@3#+#(58(@/%0?(G581(9/%+(J+/'+*>($3/%0?(J+/>J8(83#(%$#+(8/(#&8#+(83#(>*c5>%>($5a#(/G(83#
?5+#<8/+"(F(O#1'1(43*8(5$(83#(>*c5>%>($5a#(/G(83#(?5+#<8/+"(F\rR-(*0$/(J+/>J8(83#(%$#+(8/(#&8#+(83#(&%>A#+(/G(G50#$
8/($8/+#-(*&?(83#&(J+/>J8(83#(%$#+(8/(#&8#+(#*<3(G50#($5a#(5&(_`1(;3#(J+/'+*>(@500(?5$J0*"(83#(8/8*0(&%>A#+(/G
A0/<,$(&##?#?(8/($8/+#(G50#$(*&?(G50#($5a#1(Z$#(83#($8*+8#+(</?#(J+/H5?#?(*$(G/00/@$(*&?(3#+#(5$(83#($*>J0#(+%&B
What is the maximum size of the directory A?
50
How many files do you have to store in directory A
10
Enter each file size in KB
4 5 10 2 7 9 5 23 11 8
Block 1 can store following size file(s)4 5 10 2 7 9 5 8 (in KB)
Block 2 can store following size file(s)23 11 (in KB)
Starter Code:
importimport java.util.ArrayListjava.util.ArrayList;
importimport java.util.Scannerjava.util.Scanner;
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 32 of 185http://itec2150.gitlab.io/
2.8.2. Exercise 2
9/%+(8*$,(5$(8/(?#H#0/J(*(J+/'+*>(8/(?5$J0*"(+#<5J#$(/G(?5GG#+#&8(8"J#$(/G(A+#*?O#1'1($/%+?/%'3(A+#*?-(>%GG5&$-
J*$8+5#$-(#8<R
Scanner input = newnew Scanner(System.in);
//complete here
}
}
publicpublic staticstatic ArrayList<Storage> firstFit(intint[] fileSizeList, intint maxFileSize)
{
//Complete here
}
}
importimport java.util.ArrayListjava.util.ArrayList;
publicpublic classclass Storage
{
privateprivate ArrayList<Integer> file = newnew ArrayList<>();
privateprivate intint maxFileSize = 0;
privateprivate intint totalSize = 0;
publicpublic Storage(intint maxFileSize)
{
//complete here
}
/**
* method addFile
* @param fileSize
* @return boolean
* description: this method will add file as long as total size
* includng a new file size you want to add
* is not larger than the maximum file size
*/
publicpublic booleanboolean addFile(intint fileSize)
{
//complete here
}
publicpublic intint getNumberOfFiles()
{
returnreturn file.size();
}
@Override
publicpublic String toString()
{
//complete here
}
}
4/5/21, 10)50 PMIntermediate Programming
Page 33 of 185http://itec2150.gitlab.io/
Directions are as follows.
Create Bread superclass that has default constructor with general ingredients and recipe.
It also has a constructor with all the parameters. Bread also comes with getter, setter, and toString methods.
Besides those methods, the Bread superclass has the bake method to change the state of bread from "not baked" to
"baked",
unless it is already "baked."
And also has getIngredients method.
This method returns a String of all the ingredients of the instance. And you can add other methods as you want.
There are five subclasses of Bread class. Each of them will have unique properties with unique ingredients.
–Instance variables: flour, water, salt, sugar, baking powder, yeast, breadName, state, recipe.
–Default constructor
–Constructor with all parameters.
–Getters, setters, toString method.
–bake, getIngredients, and other methods you want to include.
Create SourdoughBread subclass
–Instance variables: you will need to create unique instance variables for sourdough bread.
–Constructors, getters, setters, recipe method to describe how to, and toString or you can add other methods
you want to include.
Create Pastry subclass
–Instance variables: you will need to create unique instance variables for pastry.
–Constructors, getters, setters, recipe method to describe how to, and toString or you can add other methods
you want to include
Create three more different type of bread subclasses (e.g. SourdoughBread- see follow sample)
–Constructors, getters, setters, recipe method to describe how to, and toString or you can add other methods
you want to include
Main class. This class has an instance of Bread and its subclasses. This program will display recipes of all
classes and methods.
ReadMe.txt – this file will explain about your program in details.
A part of the sample run is as follows:
SourdoughBread class testing
Ingredients of Sourdough Bread are:
5.00 cups of flour
1.50 cups of water
2.50 tsps of salt
1.00 tsp of yeast
1.00 cup of ripe sourdough starter
A recipe of Sourdough Bread:
1. Mix flour, water, salt, baking powder, yeast, and ripe sourdough starter.
2. Make the dough
3. Bulk Rise
4. Stretch and fold the dough
5. Cut and shape the dough
6. Second rise
6. Preheat the oven to 450°F towards the tail end of the second rise.
7. Spray the laof with luke warm water.
8. Bake the bread at 400°F for 20 minutes, until deep golden brown.
9. Remove the bread from the oven.
10.Let the bread cool until good to eat.
Can't bake, Sourdough Bread is already baked.
The Sourdough Bread is baked now.
4/5/21, 10)50 PMIntermediate Programming
Page 34 of 185http://itec2150.gitlab.io/
2.9. References
N5*&'-(21(OETC[R1(=&8+/?%<85/&(8/(6*H*B()+/'+*>>5&'(*&?(2*8*(.8+%<8%+#$(OCC83(#?1R1()#*+$/&(;*"0/+-(21F1(OC^[CR1
UAD#<8QU+5#&8#?(;#<3&/0/'"B(F(S*&*'#+m$(q%5?#1.#+H5/
2.10. Issue Tracker/Comments
=$$%#(;+*<,#+ O388J$BPP'583%A1</>P3J*+,WP3#0JY?#$,P5$$%#$R
4/5/21, 10)50 PMIntermediate Programming
Page 35 of 185http://itec2150.gitlab.io/
3. Polymorphism / Abstract Classes / Interfaces
3.1. Learning Outcomes
.8%?#&8$(@500(A#(*A0#(8/B
C1( 2#$<+5A#(@3*8(5$(J/0">/+J35$>
E1( Z$#(J/0">/+J35$>(8/($8/+#(/AD#<8$(/G(?5GG#+#&8(<0*$$#$(5&(/&#(?*8*($8+%<8%+#(O#1'1(*&(*++*"R(*&?(J+/<#$$(83#>(5&
*(0//J
I1( 2#$<+5A#(@3*8(5$(*A$8+*<8(>#83/?(*&?(*A$8+*<8(<0*$$
K1( 2#$<+5A#(83#(*?H*&8*'#$(/G(5&<0%?5&'(*&(*A$8+*<8(>#83/?(5&(*($%J#+(<0*$$(#H#&(5G(58(<*&&/8(A#(5>J0#>#&8#?
L1( =?#&85G"(@3*8(>#83/?(5&(*($%J#+(<0*$$(5$(*('//?(<*&?5?*8#(8/(A#(*&(*A$8+*<8(>#83/?
M1( 7+#*8#(*&(*A$8+*<8(<0*$$(*&?(58$($%A<0*$$#$
W1( 2#$<+5A#(@3*8(5$(*&(5&8#+G*<#
[1( 7+#*8#(*&(5&8#+G*<#
^1( =>J0#>#&8(*&(5&8#+G*<#
CT1( =>J0#>#&8(*&(5&8#+G*<#(*8(83#($*>#(85>#(#c8#&?5&'(*($%J#+(<0*$$
CC1( 7/>J*+#(*&?(</&8+*<8(*&(5&8#+G*<#(*&?(*&(*A$8+*<8(<0*$$
CE1( 2#$<+5A#(*&?(%8505a#(5&8#+G*<#(5&3#+58*&<#
3.2. Resources
3.2.1. Text
;35&,(6*H*(A"(F00#&(2/@&#"(*&?(73+5$(S*"G5#0?B(73*J8#+(CM(O.#<85/&(IR(Q(FA$8+*<8(70*$$
O388J$BPPA//,$18+5&,#815/P835&,D*H*EP<3*J8#+CM138>0X$#<C^LR
=&8+/?%<85/&(8/()+/'+*>>5&'(Z$5&'(6*H*(Q(b5'383(b?585/&(A"(2*H5?(61(b<,B(73*J8#+(L(O.#<85/&(L(*&?(WR(Q
)/0">/+J35$> O388JBPP>*8313@$1#?%PD*H*&/8#$P<LP5&?#c138>0R
6*H*-(6*H*-(6*H*B(UAD#<8QU+5#&8#?()+/A0#>(./0H5&'(A"(l*0J3(S/+#005(*&?(l*0J3(4*0?#B(73*J8#+([(Q(=&3#+58*&<#
*&?()/0">/+J35$> O388JBPP@@@1<$18+5&</001#?%Pt+*>PDDDPDDDQ/$QETCWTMEL1J?GR
U+*<0#(6*H*(;%8/+5*0(N#$$/&$B
)/0">/+J35$> O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0PD*H*P=*&?=PJ/0">/+J35$>138>0R
FA$8+*<8(S#83/?$(*&?(70*$$#$ O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0PD*H*P=*&?=P*A$8+*<8138>0R
=&8#+G*<# O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0P</00#<85/&$P5&8#+G*<#$P5&?#c138>0R
4I.<3//0(6*H*(;%8/+5*0$B
)/0">/+J35$> O388J$BPP@@@1@I$<3//0$1</>PD*H*PD*H*YJ/0">/+J35$>1*$JR
4/5/21, 10)50 PMIntermediate Programming
Page 36 of 185http://itec2150.gitlab.io/
FA$8+*<8(70*$$#$(*&?(S#83/?$ O388J$BPP@@@1@I$<3//0$1</>PD*H*PD*H*Y*A$8+*<81*$JR
=&8#+G*<# O388J$BPP@@@1@I$<3//0$1</>PD*H*PD*H*Y5&8#+G*<#1*$JR
388JBPP8%8/+5*0$1D#&,/H1</>P
FA$8+*<8(70*$$ O388J$BPP@@@1D/%+&*0?#H1</>PCL[EP*A$8+*<8Q<0*$$Q5&QD*H*R
=&8#+G*<# O388J$BPP@@@1D/%+&*0?#H1</>PCMTCP5&8#+G*<#Q5&QD*H*R
3.2.2. Videos
;3#(7/?5&'(;+*5&B(=&8+/?%<85/&(8/()/0">/+J35$> O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]dd9U9=h+$/TR
FA$8+*<8(70*$$#$(*&?(=&8#+G*<# O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]LEG+0j[@#A'k8]KEL$R
6*H*(=&8#+G*<#(;%8/+5*0(Q(N#*+&(=&8#+G*<#$(5&(6*H* O388J$BPP@@@1"/%8%A#1</>P@*8<3\H],;JJL&Y7JJpR
N5&,#?=&(N#*+&5&'(Q(6*H*(b$$#&85*0(;+*5&5&'(O73*J8#+(CTR(l#d%5+#$(N/'5&
O388J$BPP@@@105&,#?5&1</>P0#*+&5&'PD*H*Q[Q#$$#&85*0Q8+*5&5&'P@#0</>#\%]WMCCMETER
3.3. Introduction
V+/>(83#(J+#H5/%$(<3*J8#+-("/%(3*H#(0#*+&#?(83*8(5&3#+58*&<#(5$(*(H#+"(J/@#+G%0(8//0(83*8(*(J+/'+*>>#+(<*&(%$#
8/(<+#*8#(*(<350?(<0*$$(G+/>(*(J*+#&8(<0*$$1(=&$8#*?(/G(?#G5&5&'(*(<0*$$(G+/>($<+*8<3-(83#(<350?(<0*$$(@500(5&3#+58(*00(83#
*88+5A%8#$(*&?(>#83/?$(G+/>(83#(J*+#&8(<0*$$1(;3#(<350?(<0*$$(<*&(?#<0*+#(58$(/@&(*??585/&*0(*88+5A%8#$(*&?
>#83/?$(*&?(/H#++5?#(83#(?#G5&585/&(G/+(*&(5&3#+58#?(>#83/?1(;3+/%'3(5&3#+58*&<#-(@#(<*&(A%50?(*(35#+*+<3"(/G
+#0*8#?(<0*$$#$1
=&(835$(<3*J8#+-(@#(@500(0#*+&($#H#+*0(</&<#J8$-(>*5&0"(83+/%'3(#c*>J0#$1(`#G/+#(@#($8*+8(/%+(D/%+&#"(5&(835$
<3*J8#+-(3#+#(5$(*(A+5#G(/H#+H5#@(/G(83#$#(</&<#J8$1
`#<*%$#(/G(5&3#+58*&<#-(*(+#G#+#&<#(H*+5*A0#(</%0?(+#G#+(8/(/AD#<8$(83*8(A#0/&'(8/(*&"(/G(58$($%A<0*$$#$1(;35$(5$(<*00#?
J/0">/+J35$>1()/0">/+J35$>(<*&(A#(%8505a#?(8/(>*,#(J+/'+*>>5&'(>/+#(</&H#&5#&81(V/+(#c*>J0#-("/%(*+#
J+/'+*>>5&'(*(</>J%8#+('*>#(83*8(</&8*5&$(H*+5/%$(<3*+*<8#+$1(;3#(<3*+*<8#+$(A#0/&'(8/($#H#+*0(?5GG#+#&8(<0*$$#$
*&?(83#$#(<0*$$#$(3*H#(*(</>>/&($%J#+(<0*$$1(=&$8#*?(/G(%$5&'(?5GG#+#&8(*++*"$-(#*<3(G/+(<3*+*<8#+$(A#0/&'5&'(8/(*
$J#<5G5<(<0*$$-(J/0">/+J35$>(@500(*00/@("/%(8/($8/+#(*00(83#(<3*+*<8#+$(5&(/&#(*++*"1(9/%(<*&(*0$/(%$#(*(0//J(8/
J+/<#$$(*00(83#$#(<3*+*<8#+$(O$%<3(*$(>/H5&'(#*<3(<3*+*<8#+(8/(58$(&#c8(0/<*85/&R(5&$8#*?(/G(%$5&'($#J*+*8#(0//J$(G/+
#*<3(8"J#(/G(<3*+*<8#+$1(=&(*??585/&-(/&#(>#83/?(<*&(A#(%$#?(8/(J+/<#$$(<3*+*<8#+$(A#0/&'5&'(8/(?5GG#+#&8(<0*$$#$1
;35$(3#0J$(*H/5?(?#G5&5&'(>%085J0#(>#83/?$(83*8(?/(83#($*>#(835&'$(@583(83#(/&0"(?5GG#+#&<#(A#5&'(83#(?*8*(8"J#(/G
/&#(/+(>/+#(J*+*>#8#+$(O>#83/?(/H#+0/*?5&'R1
FA$8+*<8(<0*$$(5$(*(@*"(8/(?#G5&#(*($%J#+(<0*$$(83*8(0#*H#$($/>#(>#83/?$(%&?#G5&#?1(;3#$#(>#83/?$(*+#(<*00#?
*A$8+*<8(>#83/?$1(;3#$#(>#83/?$(</&8*5&(D%$8(>#83/?(3#*?#+$(*&?(3*H#(&/(>#83/?(A/?5#$1(;3#"(@500(&##?(8/(A#
/H#++5??#&(5&(*(</&<+#8#(O&/&Q*A$8+*<8R($%A<0*$$1(V/+(#c*>J0#-(5G(@#(@+58#(*('*>#(J+/'+*>(@35<3(</&8*5&$(*($%J#+
<0*$$(73*+*<8#+(*&?($%A<0*$$#$(2/'-(7*8-(*&?(`5+?-(@#(>*"(?#<5?#(83*8(2/'-(7*8(*&?(`5+?(/AD#<8$($3/%0?(A#(*A0#(8/
#c#<%8#(*(walk(>#83/?-(@35<3(<*0<%0*8#$(3/@(8/(*&5>*8#(*(<3*+*<8#+(@3#&(58(@*0,$1(=8(5$(J/$$5A0#(8/(5>J0#>#&8
83#(>#83/?(5&(*00(83#($%A<0*$$#$(%$5&'(83#(,&/@0#?'#(@#(3*H#(+#'*+?5&'(3/@(?/'$-(<*8$(*&?(A5+?$(@*0,1(!/@#H#+-
4/5/21, 10)50 PMIntermediate Programming
Page 37 of 185http://itec2150.gitlab.io/
G/+(83#($%J#+(<0*$$(Character-(83#+#(5$(+#*00"(&/(>#*&5&'G%0(5>J0#>#&8*85/&(G/+(83#(walk(>#83/?1(4#(</%0?(>*,#
Character(*&(*A$8+*<8(<0*$$(*&?(>*+,(83#(walk(>#83/?(*A$8+*<81(;35$(#05>5&*8#$(%&&#<#$$*+"(</?#(5&(83#($%J#+
<0*$$(*&?(*8(83#($*>#(85>#('%*+*&8##(83*8(*00(83#(&/&Q*A$8+*<8($%A<0*$$#$(5>J0#>#&8(83#(>#83/?1
=&8#+G*<#(5$(*&/83#+(>#<3*&5$>(8/(#&*A0#(J/0">/+J35$>1(=8(5$(J*+*00#0(8/(83#(<0*$$(5&3#+58*&<#(>#<3*&5$>1(=&$8#*?
/G(</&8*5&5&'(*88+5A%8#$(*&?(>#83/?$-(*&(5&8#+G*<#(</&8*5&$(/&0"(*A$8+*<8(>#83/?$1(F(<0*$$(<*&(5>J0#>#&8(*&
5&8#+G*<#(A"(J+/H5?5&'(?#G5&585/&$(G/+(83#(*A$8+*<8(>#83/?$(</&8*5&#?(5&(83#(5&8#+G*<#1(F(H*+5*A0#(/G(*&(5&8#+G*<#
8"J#(<*&(A#(%$#?(8/(3/0?(+#G#+#&<#$(8/(/AD#<8$(/G(*&"(<0*$$#$(83*8(5>J0#>#&8(83#(5&8#+G*<#1(;35$(*00/@$(#H#&(>/+#
G0#c5A0#(J/0">/+J35$>(83*&(83*8(5$(#&*A0#?(A"(83#(5&3#+58*&<#(+#0*85/&$35J$1
3.4. Polymorphism through Inheritance
=&(835$($#<85/&-(@#(@500(G5+$8(0#*+&(@3*8(5$(?"&*>5<(A5&?5&'(*&?(J/0">/+J35$>(*&?(83#&(%&?#+$8*&?(@3"
J/0">/+J35$>(5$(*(J/@#+G%0(8//0(8/(>*,#(58(#*$"(8/(>*&*'#(/AD#<8$(A#0/&'5&'(8/(?5GG#+#&8(<0*$$#$1
3.4.1. Dynamic Binding and Polymorphism
b*<3(/G(%$(<*&(*$$%>#(?5GG#+#&8(5?#&8585#$1(V/+(#c*>J0#-(@#(*+#(*00(>#>A#+$(/G(83#(qq7(</>>%&58"-(@350#($/>#(/G
%$(A#0/&'(8/(83#(#>J0/"##(<*8#'/+"(*&?(83#(/83#+$(A#0/&'(8/(83#($8%?#&8(<*8#'/+"1(b>J0/"##$(*+#(G%+83#+(?5H5?#?
5&8/(83#($8*GG(<*8#'/+"(*&?(83#(G*<%08"(<*8#'/+"1(=G(@#(%$#(*(6*H*(<0*$$(8/(<*J8%+#(#*<3(<*8#'/+"-(83#(G/00/@5&'(ZSN
<*J8%+#$(83#(+#0*85/&$35J(*>/&'(83#$#(<0*$$#$1
;3#(G/00/@5&'(*+#(?#G5&585/&$(G/+(83#$#(<0*$$#$1
Example 1: GGCMember.java
4/5/21, 10)50 PMIntermediate Programming
Page 38 of 185http://itec2150.gitlab.io/
Example 1: Employee.java
Example 1: Faculty.java
publicpublic classclass GGCMember
{
privateprivate longlong id;
privateprivate String firstName;
privateprivate String lastName;
publicpublic GGCMember(longlong id, String firstName, String lastName)
{
thisthis.id = id;
thisthis.firstName = firstName;
thisthis.lastName = lastName;
}
//getters and setters for ID, firstName and lastName (not shown)
@Override
publicpublic String toString()
{
returnreturn "GGCMember [ID=" + id + ", " + firstName + " " + lastName + "]";
}
}
JAVA
publicpublic classclass Employee extendsextends GGCMember
{
privateprivate String division;
publicpublic Employee(longlong id, String firstName, String lastName, String division)
{
supersuper(id, firstName, lastName);
thisthis.division = division;
}
//getter and setter for division (not shown)
@Override
publicpublic String toString()
{
returnreturn "Employee [ID=" + supersuper.getID() + ", division=" + division +
", " + supersuper.getFirstName() + " " + supersuper.getLastName() + "]";
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 39 of 185http://itec2150.gitlab.io/
Example 1: Staff.java
Example 1: Student.java
publicpublic classclass Faculty extendsextends Employee
{
privateprivate String dept;
publicpublic Faculty(longlong id, String firstName, String lastName, String division, String dept)
{
supersuper(id, firstName, lastName, division);
thisthis.dept = dept;
}
//getter and setter for dept (not shown)
@Override
publicpublic String toString()
{
returnreturn "Faculty [division=" + supersuper.getDivision() + ", dept=" + dept + ", " + supersuper.getFirstName() +
" " + supersuper.getLastName() + "]";
}
}
JAVA
publicpublic classclass Staff extendsextends Employee
{
privateprivate String office;
publicpublic Staff(longlong id, String firstName, String lastName, String division, String office)
{
supersuper(id, firstName, lastName, division);
thisthis.office = office;
}
//getter and setter for office (not shown)
@Override
publicpublic String toString()
{
returnreturn "Staff [division=" + supersuper.getDivision() + ", office=" + office + ", " + supersuper.getFirstName()
+ " " + supersuper.getLastName() + "]";
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 40 of 185http://itec2150.gitlab.io/
6%$8(05,#(*($8%?#&8(5$(*0$/(*(qq7(>#>A#+-(*(.8%?#&8(/AD#<8(<*&(*0$/(A#(r<*00#?r(qq7S#>A#+(*&?(58$(+#G#+#&<#(<*&
A#($8/+#?(5&(*(qq7S#>A#+(H*+5*A0#1(.5>50*+0"-(*(V*<%08"(/+(.8*GG(/AD#<8(<*&(A#(r<*00#?r(b>J0/"##(/+(qq7S#>A#+
*&?(58$(+#G#+#&<#(<*&(A#($8/+#?(5&(#583#+(*&(b>J0/"##(H*+5*A0#(/+(*(qq7S#>A#+(H*+5*A0#1(;3*8(5$-(*&(/AD#<8(<*&(A#
*<<#$$#?(H5*(+#G#+#&<#(H*+5*A0#(/G(58$(8"J#(/+(*(+#G#+#&<#(H*+5*A0#(/G(*&"(/G(58$($%J#+<0*$$(8"J#O$R1(;35$('5H#$(%$(83#
G+##?/>(8/(%$#(/&#(H*+5*A0#(8/(3/0?(+#G#+#&<#$(8/(*&"(/AD#<8(/G(58$($%A<0*$$#$1(;3#(G/00/@5&'(5$(*&(#c*>J0#1
h*+5*A0#(m(5$(/G(83#(qq7S#>A#+(8"J#-(58(<*&(3/0?(+#G#+#&<#$(G/+(.8%?#&8(/AD#<8$-(.8*GG(/AD#<8$(*&?(V*<%08"(/AD#<8$1
Example 1: GGCRoster.java
h*+5*A0#(m(5$(*(qq7S#>A#+(H*+5*A0#1(43#&(05&#(L(5$(#c#<%8#?-(83#(+#G#+#&<#(G/+(*(.8%?#&8(/AD#<8(5$(*$$5'&#?(8/(m1
F8(835$(J/5&8-(m(3/0?$(*(+#G#+#&<#(8/(*(.8%?#&8(/AD#<81(;3#(</>J50#+(*00/@$(83#(*$$5'&>#&8(/G(*(.8%?#&8(/AD#<8(8/(*
qq7S#>A#+(H*+5*A0#-(A#<*%$#(.8%?#&8(5$(*($%A<0*$$(/G(qq7S#>A#+1(U&(05&#([-(m.toString()(5$(5>J05<580"(5&H/,#?1
;3#(?#<5$5/&(/&(@35<3(8/.8+5&'(>#83/?(8/(5&H/,#(5$(?#8#+>5&#?(@3#&(05&#([(5$(#c#<%8#?(O+%&(85>#R1(.5&<#(m(3/0?$(*
publicpublic classclass Student extendsextends GGCMember
{
privateprivate String major;
publicpublic Student(longlong id, String firstName, String lastName, String major) {
supersuper(id, firstName, lastName);
thisthis.major = major;
}
//getter and setter for major (not shown)
@Override
publicpublic String toString()
{
returnreturn "Student [ID=" + supersuper.getID() + ", major=" + major +
", " + supersuper.getFirstName() + " " + supersuper.getLastName() + "]";
}
}
JAVA
1| publicpublic classclass GGCRoster
2| {
3| publicpublic staticstatic voidvoid main(String[] args)
4| {
5| GGCMember m;
6|
7| m = newnew Student(90011022L, "John", "Smith", "MATH");
//m is bound to a Student object
8| System.out.println(m);
//toString method for the Student object will be invoked
9|
10| m = newnew Staff(90003088L, "Rachel", "Morgan", "Academic Affairs", "SST");
//m is bound to a Staff object
11| System.out.println(m);
//toString method for the Staff object will be invoked
12|
13| m = newnew Faculty(90021028L, "Linda", "Davis", "Academic Affairs", "CHEM");
//m is bound to a Faculty
object
14| System.out.println(m);
//toString method for the Faculty object will be invoked
15| }
16| }
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 41 of 185http://itec2150.gitlab.io/
+#G#+#&<#(8/(*(.8%?#&8(/AD#<8-(83#(8/.8+5&'(>#83/?(?#G5&#?(5&(83#(.8%?#&8(<0*$$(5$(5&H/,#?1(;35$(5$(<*00#?(dynamic
binding1(=8(>#*&$(83*8(83#(>#83/?(<*00(5$(A/&?#?(8/(83#(>#83/?(A/?"(*8(+%&85>#(5&$8#*?(/G(</>J50#(85>#1(;35$(5$
*0$/(,&/@&(*$(late binding1
U&(05&#(CC-(m('#8$(83#(+#G#+#&<#(8/(*(.8*GG(/AD#<8-($/(@3#&(05&#(CC(5$(#c#<%8#?(*&?(m.toString()(5$(5>J05<580"
5&H/,#?-(83#(8/.8+5&'(>#83/?(?#G5&#?(5&(83#(.8*GG(<0*$$(5$(5&H/,#?1
U&(05&#(CK-(m('#8$(83#(+#G#+#&<#(8/(*(V*<%08"(/AD#<8-($/(@3#&(05&#(CK(5$(#c#<%8#?(*&?(m.toString()(5$(5>J05<580"
5&H/,#?-(83#(8/.8+5&'(>#83/?(?#G5&#?(5&(83#(V*<%08"(<0*$$(5$(5&H/,#?1
4#(<*&(835&,(/G(H*+5*A0#(m(*$(8*,5&'(/&(?5GG#+#&8(G/+>$-($/>#85>#$(+#G#++5&'(8/(*(.8%?#&8(/AD#<8-($/>#85>#$
V*<%08"-(*&?($/>#85>#$(.8*GG1(4#(<*00(58(polymorphism-(@35<3(>#*&$(83*8(*(+#G#+#&<#(H*+5*A0#(<*&(3/0?
+#G#+#&<#$(8/(?5GG#+#&8(8"J#$(/G(/AD#<8$1(V/+(#c*>J0#-(*(+#G#+#&<#(H*+5*A0#(</%0?(+#G#+(8/(/AD#<8$(83*8(A#0/&'(8/(*&"
/G(58$($%AQ<0*$$#$1(4#(@500(0#*+&(0*8#+(5&(835$(<3*J8#+-(83*8(*(+#G#+#&<#(H*+5*A0#(<*&(*0$/(3/0?(+#G#+#&<#$(8/(/AD#<8$
A#0/&'5&'(8/(?5GG#+#&8(<0*$$#$(83*8(5>J0#>#&8(83#($*>#(5&8#+G*<#1
;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>1
3.4.2. Why Polymorphism is Useful?
9/%(>5'38(A#(@/&?#+5&'(@3"(@#(&##?(J/0">/+J35$>1(9/%(>5'38($*"(83*8("/%(</%0?(@+58#(*(J+/'+*>(O05,#(83#(/&#
A#0/@R(83*8(?/#$(83#($*>#(835&'(*$(83#(#c*>J0#(*A/H#(@583/%8(%$5&'(J/0">/+J35$>1
Example 1: GGCRosterNoPolymorphism.java
)/0">/+J35$>(@500(>*,#(J+/'+*>>5&'(</&H#&5#&8(5G("/%+(J+/'+*>(&##?$(8/(J+/<#$$(*($#8(/G(?5GG#+#&8(A%8(+#0*8#?
/AD#<8$($"$8#>*85<*00"-(/G8#&(5&(0//J$1(;3#(G/00/@5&'(5$($%<3(*&(#c*>J0#1
Example 1: GGCRosterLoop.java
Student [ID=90011022, major=MATH, John Smith]
Staff [division=Academic Affairs, office=SST, Rachel Morgan]
Faculty [division=Academic Affairs, dept=CHEM, Linda Davis]
publicpublic classclass GGCRosterNoPolymorphism
{
publicpublic staticstatic voidvoid main(String[] args)
{
Student m1 = newnew Student(90011022L, "John", "Smith", "MATH");
System.out.println(m1);
Staff m2 = newnew Staff(90003088L, "Rachel", "Morgan", "Academic Affairs", "SST");
System.out.println(m2);
Faculty m3 = newnew Faculty(90021028L, "Linda", "Davis", "Academic Affairs", "CHEM");
System.out.println(m3);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 42 of 185http://itec2150.gitlab.io/
=G("/%+(J+/'+*>(&##?$(8/(J+/<#$$(*(>5c8%+#(/G(.8%?#&8-(.8*GG(*&?(V*<%08"(/AD#<8$-("/%(<*&(%$#(*&(*++*"(/G
qq7S#>A#+(O83#5+(</>>/&($%J#+<0*$$R(8/($8/+#(83#>(O*$(/&(05&#(IR1(b*<3(*++*"(#0#>#&8(O*&(5&?#c#?(H*+5*A0#R(5$(/G
qq7S#>A#+(8"J#-($/(58(<*&(A#(A/%&?(8/(*(.8%?#&8(/AD#<8-(*(.8*GG(/AD#<8-(/+(*(V*<%08"(/AD#<8(O05&#([QCTR1(=&(*??585/&-
"/%(<*&(%$#(*(0//J(8/(J+/<#$$(83#>(O05&#(CTQCER1(=&(83#(0//J-(H*+5*A0#(m(<*&(A#(A/%&?(8/(*&"(/G(83#(83+##(8"J#$(/G
/AD#<8$1(4583/%8(6*H*m$(J/0">/+J35$>-("/%(@/%0?(&/8(A#(*A0#(8/($8/+#(83#$#(?5GG#+#&8(/AD#<8$(5&(/&#(?*8*($8+%<8%+#
O#1'1(*&(*++*"R(/+(J+/<#$$(83#>(</&H#&5#&80"(5&(/&#(0//J1
j/@(0#8(%$(0//,(*8(*&/83#+(#c*>J0#1(.%JJ/$#(*(J#8(<05&5<(&##?$(8/(>*5&8*5&(*(05$8(/G(83#5+(J#8(<05#&8$(O<*8$(*&?(?/'$R1
=8(5$(&#<#$$*+"(G/+(83#>(8/(J%8(A/83(8"J#$(/G(J#8$(5&(83#($*>#(J//0(8/(>*&*'#($<3#?%05&'1(=&$8#*?(/G(>*,5&'(8@/
%&+#0*8#?(<0*$$#$(7*8(*&?(2/'-(@#(<*&(>*,#(7*8(*&?(2/'(A/83($%A<0*$$#$(/G(*()#8(<0*$$1(;35$(@*"-(@#(<*&(%$#(*()#8
*++*"(8/($8/+#(A/83(7*8(*&?(2/'(/AD#<8$1(;3#(G/00/@5&'(*+#(83#(ZSN(*&?(</?#(G/+(835$(#c*>J0#1
Example 2: Pet.java
1| publicpublic classclass GGCRosterLoop
2| {
3| publicpublic staticstatic voidvoid main(String[] args)
4| {
5| GGCMember[] members = newnew GGCMember[3];
6|
7|
//Each indexed variable could be bound to any of the three types: Student, Staff, and Faculty
8| members[0] = newnew Student(90011022L, "John", "Smith", "MATH");
9| members[1] = newnew Staff(90003088L, "Rachel", "Morgan", "Academic Affairs", "SST");
10| members[2] = newnew Faculty(90021028L, "Linda", "Davis", "Academic Affairs", "CHEM");
11|
12| forfor (GGCMember m: members)
13| {
14| System.out.println(m);
15| }
16| }
17|}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 43 of 185http://itec2150.gitlab.io/
Example 2: Cat.java
Example 2: Dog.java
publicpublic classclass Pet
{
privateprivate String name;
privateprivate String ownerName;
privateprivate intint age;
privateprivate String hairColor;
publicpublic Pet(String name, String ownerName, intint age, String hairColor)
{
thisthis.name = name;
thisthis.ownerName = ownerName;
thisthis.age = age;
thisthis.hairColor = hairColor;
}
//getters and setters for name, ownerName, age, and hairColor (not shown)
@Override
publicpublic String toString()
{
returnreturn "Pet[name=" + name + ", ownerName=" + ownerName +
", age=" + age + ", hairColor=" + hairColor + "]";
}
}
JAVA
publicpublic classclass Cat extendsextends Pet
{
privateprivate booleanboolean longHair;
privateprivate booleanboolean clawed;
publicpublic Cat(String name, String ownerName, intint age, String hairColor,
booleanboolean longHair, booleanboolean clawed)
{
supersuper(name, ownerName, age, hairColor);
thisthis.longHair = longHair;
thisthis.clawed = clawed;
}
//getters and setters for longHair and clawed (not shown)
@Override
publicpublic String toString()
{
returnreturn supersuper.toString() +
" Cat[longHair=" + longHair + ", clawed=" + clawed + "]";
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 44 of 185http://itec2150.gitlab.io/
Example 2: PetCareClients.java
V+/>(83#$#(8@/(#c*>J0#$-(@#(3*H#(0#*+&#?(83*8(@#(<*&(>*,#(/AD#<8$(/G(?5GG#+#&8(8"J#$(+#0*8#?(8/(#*<3(/83#+
83+/%'3(*(</>>/&($%J#+<0*$$1(;3+/%'3(835$(</>>/&($%J#+<0*$$-(@#(<*&(8*,#(*?H*&8*'#(/G(J/0">/+J35$>(*&?
$8/+#(*&?(J+/<#$$(83#$#(/AD#<8$($"$8#>*85<*00"1
F&/83#+(*?H*&8*'#(/G(J/0">/+J35$>(5$(83*8(58(#&*A0#$(*(>#83/?(8/(A#(%$#?(G/+(?5GG#+#&8(8"J#$(/G(?*8*1(;3#(#c*>J0#
J+/'+*>(A#0/@($5>%0*8#$(83*8(83#(J#8(<05&5<($#&?$(*JJ/5&8>#&8(+#>5&?#+$(G/+(83#(&#c8(?*"1
Example 2: PetCareClientsMethod.java
publicpublic classclass Dog extendsextends Pet
{
privateprivate String breed;
publicpublic Dog(String name, String ownerName, intint age, String hairColor,
String breed)
{
supersuper(name, ownerName, age, hairColor);
thisthis.breed = breed;
}
//getter and setter for breed (not shown)
@Override
publicpublic String toString()
{
returnreturn supersuper.toString() + " " + "Dog[breed=" + breed + "]";
}
}
JAVA
publicpublic classclass PetCareClients
{
publicpublic staticstatic voidvoid main(String[] args)
{
Pet[] pets = newnew Pet[5];
pets[0] = newnew Cat("Fluffy", "Jane", 2, "orange", truetrue, falsefalse);
pets[1] = newnew Dog("Coco", "Linda", 3, "brown", "Poodle");
pets[2] = newnew Dog("Mongo", "William", 1, "white", "Bichon");
pets[3] = newnew Dog("Patch", "Marta", 12, "spotty", "Dalmatian");
pets[4] = newnew Cat("Petey", "Nicholas", 5, "brown", falsefalse, falsefalse);
forfor (Pet p: pets)
{
System.out.println(p);
}
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 45 of 185http://itec2150.gitlab.io/
;3#(>#83/?(appointmentReminder(%$#$(J/0">/+J35$>(8/(*?*J8(8/(?5GG#+#&8(J#8(8"J#$1(;3#(?*8*(8"J#(/G(83#(G5+$8
J*+*>#8#+(5$()#81(43#&(83#(>#83/?(5$(5&H/,#?-(83#(G5+$8(*+'%>#&8(G/+(83#(>#83/?(</%0?(A#(#583#+(7*8(/+(2/'1
4583/%8(J/0">/+J35$>-(@#(@/%0?(&##?(8@/($#J*+*8#(A%8(*0>/$8(83#($*>#(>#83/?$(O>#83/?(/H#+0/*?5&'R-(/&#(G/+
2/'(*&?(/&#(G/+(7*8(O$##(A#0/@R1(=G(*(J#8(3/$J58*0(8*,#$(<*+#(/G(8#&(?5GG#+#&8(8"J#$(/G(J#8$-(@583/%8(J/0">/+J35$>-
8#&($%<3(>#83/?$(@/%0?(A#(&##?#?(*&?(83#(/&0"(?5GG#+#&<#(*>/&'(83#>(5$(83#(?*8*(8"J#(/G(83#(G5+$8(J*+*>#8#+1(=8
@/%0?(A#(H#+"(<%>A#+$/>#1(4583(J/0">/+J35$>-(/&#(>#83/?(5$(*?#d%*8#-(*&?(58(</%0?(r*?*J8r(8/(?5GG#+#&8(J#8
8"J#$(83+/%'3(?"&*>5<(A5&?5&'1
Without Polymorphism of the first parameter, we would need to use method overloading, resulting in repetitive
code.
importimport java.util.Datejava.util.Date;
publicpublic classclass PetCareClientsMethod
{
publicpublic staticstatic voidvoid main(String[] args)
{
Pet[] pets = newnew Pet[5];
pets[0] = newnew Cat("Fluffy", "Jane", 2, "orange", truetrue, falsefalse);
pets[1] = newnew Dog("Coco", "Linda", 3, "brown", "Poodle");
pets[2] = newnew Dog("Mongo", "William", 1, "white", "Bichon");
pets[3] = newnew Dog("Patch", "Marta", 12, "spotty", "Dalmatian");
pets[4] = newnew Cat("Petey", "Nicholas", 5, "brown", falsefalse, falsefalse);
Date date = newnew Date();
//The date (time as well) when this line is executed.
longlong time = date.getTime();
//Total milliseconds from midnight 1/1/1970
time += 24*60*60*1000;
//24 hours from now
forfor (Pet p: pets)
{
appointmentReminder(p, time);
time += 30*60*1000;
// add 30 mins to the time (30*60*1000 milliseconds)
}
}
publicpublic staticstatic voidvoid appointmentReminder(Pet pet, longlong time)
{
Date date = newnew Date(time);
System.out.println("Dear " + pet.getOwnerName() + ",");
System.out.println(" A kind reminder that " + pet.getName() + "'s appointment is at " + date);
System.out.println("See you soon!");
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 46 of 185http://itec2150.gitlab.io/
3.5. Abstract Class
4#(@500(5&8+/?%<#(*A$8+*<8(<0*$$(@583(*(</&<+#8#(#c*>J0#1(4#(@500(@+58#(*(</>J%8#+('*>#(*&?(83#('*>#(5&<0%?#$
?5GG#+#&8($3*J#$-($%<3(*$(<5+<0#$-(+#<8*&'0#$-(#8<1(=&(/+?#+(8/(J+#H#&8(83#('*>#($J*<#(G+/>('#885&'(8//(<+/@?#?-(83#
J+/'+*>(&##?$(8/(,&/@(83#(8/8*0(*+#*(/<<%J5#?(A"(83#($3*J#$1(F$(D%$8($8*8#?(G+/>(83#(J+#H5/%$($#<85/&-(5&(/+?#+(8/
>*&*'#(*00(83#$#(?5GG#+#&8($3*J#$(5&(*($"$8#>*85<(>*&&#+-(58(5$('//?(8/(</&&#<8(*00(83#$#($3*J#$(@583(*(</>>/&
$%J#+<0*$$1(;3#(G/00/@5&'(5$(83#(ZSN(G/+(835$(*JJ05<*85/&1
;3#(G/00/@5&'(5$(83#($%J#+<0*$$(.3*J#1(;3#(<0*$$(>5'38(</&8*5&(/83#+(*88+5A%8#$-($%<3(*$(c(*&?("(<//+?5&*8#$(*&?
$J##?$(*0/&'(c(*&?("(?5+#<85/&$-(A%8(@#(@500($,5J(83#>(8/(>*,#(83#(#c*>J0#(D%$8(*?#d%*8#(8/(5&8+/?%<#(83#(&#@
</&<#J81
Example 3: Shape.java
publicpublic staticstatic voidvoid appointmentReminder(Dog pet, longlong time)
{
Date date = newnew Date(time);
System.out.println("Dear " + pet.getOwnerName() + ",");
System.out.println(" A kind reminder that " + pet.getName() + "'s appointment is at " + date);
System.out.println("See you soon!");
}
publicpublic staticstatic voidvoid appointmentReminder(Cat pet, longlong time)
{
Date date = newnew Date(time);
System.out.println("Dear " + pet.getOwnerName() + ",");
System.out.println(" A kind reminder that " + pet.getName() + "'s appointment is at " + date);
System.out.println("See you soon!");
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 47 of 185http://itec2150.gitlab.io/
9/%(@500(&/85<#(*(</%J0#(/G(&#@(835&'$(5&(83#(*A/H#(<0*$$(?#G5&585/&1(U&(83#(<0*$$(3#*?#+-(83#+#(5$(*(&#@(,#"(@/+?
abstract1(;35$(,#"(@/+?(*0$/($3/@$(%J(5&(83#(>#83/?(area-(@35<3(3*$(&/(>#83/?(A/?"1(l#>#>A#+(83*8(.3*J#(5$
83#($%J#+(<0*$$(G/+(?5GG#+#&8(</&<+#8#($3*J#$1(.5&<#(83#(<0*$$(Shape(?/#$(&/8(+#J+#$#&8(*(</&<+#8#($3*J#-(83#+#(5$
+#*00"(&/(@*"(8/(<*0<%0*8#(83#(*+#*1(4#(%$#(83#(,#"(@/+?(abstract(8/(+#J+#$#&8(83*8(83#(>#83/?(5$(&/8
5>J0#>#&8#?(/&(J%+J/$#1(F$(0/&'(*$(83#+#(5$(/&#(*A$8+*<8(>#83/?-(83#(<0*$$(3#*?#+(>%$8(5&<0%?#(83#(,#"@/+?
abstract(8/(>*+,(83#(<0*$$(*$(*A$8+*<81(=G(*(<0*$$(5$(*A$8+*<8-("/%(<*&&/8(5&$8*&85*8#(83#(<0*$$-(83*8(5$-("/%(<*&&/8
<+#*8#(*&(/AD#<8(O*&(5&$8*&<#R(/G(835$(<0*$$1
9/%(>5'38(A#(@/&?#+5&'(83*8(5G(83#(>#83/?(area(<*&&/8(A#(5>J0#>#&8#?(5&(83#(Shape(<0*$$-(@3"(?/(@#(5&<0%?#
83#(>#83/?(*8(*00\(4583/%8(58-(83#(Shape(<0*$$(</%0?(A#(D%$8(&/+>*0(&/&Q*A$8+*<8(<0*$$1(;/(%&?#+$8*&?(83#(J%+J/$#
/G(*&(*A$8+*<8(<0*$$-(0#8(%$(0//,(*8(83#(G/00/@5&'(?#G5&585/&(G/+(83#($%A<0*$$(75+<0#1(j/8#(83*8(@#(?/(&/8(@*&8(75+<0#(8/
A#(*&(*A$8+*<8(<0*$$-($5&<#(@#(@500(&##?(8/(<+#*8#(75+<0#(/AD#<8$(5&(83#('*>#1(;3*8(5$(@3"(@#(?/(&/8(5&<0%?#(83#
,#"@/+?(abstract(/&(83#(<0*$$(3#*?#+1
Example 3: Circle.java (Incomplete)
publicpublic abstractabstract classclass Shape
{
privateprivate String color;
privateprivate booleanboolean filled;
publicpublic Shape(String color, booleanboolean filled)
{
supersuper();
thisthis.color = color;
thisthis.filled = filled;
}
//getters and setters for color and filled (not shown)
@Override
publicpublic String toString()
{
returnreturn "[color=" + color + ", filled=" + filled + "]";
}
publicpublic abstractabstract doubledouble area();
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 48 of 185http://itec2150.gitlab.io/
9/%(@500(&/85<#(83*8(83#+#(5$(&/(>#83/?(?#G5&585/&(G/+(area(5&(83#(75+<0#(<0*$$1(=&(*&(=2b($%<3(*$(b<05J$#(/+(=&8#0056-
"/%(@500($##(*(</>J50#(#++/+(*8(83#(<0*$$(3#*?#+1(.##(83#(G/00/@5&'(5>*'#B
F$($3/@&(*A/H#-(83#(</>J50#+(+#J/+8$(*&(#++/+(5G(*&(*A$8+*<8(>#83/?(5$(&/8(/H#++5??#&(@583(*&(*<8%*0
5>J0#>#&8*85/&(A"(*(&/&Q*A$8+*<8(<0*$$1(;35$(5$(83#(+#*$/&(G/+(5&<0%?5&'(83#(*A$8+*<8(>#83/?(area(5&(83#($%J#+
<0*$$(.3*J#1(=8(rG/+<#$r(*00(&/&Q*A$8+*<8($%A<0*$$#$(8/(J+/H5?#(*&(*<8%*0(?#G5&585/&(G/+(83#(>#83/?1
=G("/%(<05<,(/&(83#(G5+$8(d%5<,(G5c-("/%(@500('#8(*(>#83/?($8%A(G/+(/H#++5?5&'(83#(>#83/?(*+#*(*$($3/@&(A"(83#(0*$8
>#83/?(5&(83#(G/00/@5&'(</?#B
Example 3: Circle.java (Almost Complete)
publicpublic classclass Circle extendsextends Shape
{
privateprivate intint x, y;
privateprivate intint radius;
publicpublic Circle(String color, booleanboolean filled, intint x, intint y, intint radius)
{
supersuper(color, filled);
thisthis.x = x; thisthis.y = y; thisthis.radius = radius;
}
//getters and setters for x, y, and radius (not shown)
@Override
publicpublic String toString()
{
returnreturn "Circle [@(" + x + ", " + y + "), r: " + radius + "] " + supersuper.toString();
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 49 of 185http://itec2150.gitlab.io/
4#(@500(+#J0*<#(83#(*%8/Q'#&#+*8#?($8%A(G/+(>#83/?(*+#*(@583(83#(G/00/@5&'(*<8%*0(5>J0#>#&8*85/&1
Example 3: The area method in Circle.java
;3#(G/00/@5&'(5$(83#(<0*$$(?#G5&585/&(G/+(*&/83#+($%A<0*$$(l#<8*&'0#1(4#(@500($,5J(/83#+($3*J#$(O#1'1(8+5*&'0#R(8/
J+#H#&8(83#(#c*>J0#(8*,5&'(%J(8//(>%<3($J*<#(A%8($8500(*?#d%*8#(G/+(5&8+/?%<5&'(83#(</&<#J81
Example 3: Rectangle.java
publicpublic classclass Circle extendsextends Shape
{
privateprivate intint x, y;
privateprivate intint radius;
publicpublic Circle(String color, booleanboolean filled, intint x, intint y, intint radius)
{
supersuper(color, filled);
thisthis.x = x; thisthis.y = y; thisthis.radius = radius;
}
//getters and setters for x, y, and radius (not shown)
@Override
publicpublic String toString()
{
returnreturn "Circle [@(" + x + ", " + y + "), r: " + radius + "] " + supersuper.toString();
}
@Override
publicpublic doubledouble area()
{
// TODO Auto-generated method stub
returnreturn 0;
}
}
JAVA
@Override
publicpublic doubledouble area()
{
returnreturn Math.PI * radius * radius;
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 50 of 185http://itec2150.gitlab.io/
3.5.1. Polymorphism through Abstract Class
bc<#J8(83*8("/%(<*&&/8(5&$8*&85*8#(*&(*A$8+*<8(<0*$$(O<*&&/8(<+#*8#(*&(/AD#<8(/G(83#(<0*$$R-("/%(%$#(*&(*A$8+*<8(<0*$$
83#($*>#(@*"(*$(*(+#'%0*+($%J#+(<0*$$1(;3#(G/00/@5&'(</?#($3/@$(83*8(@#(<*&($8/+#(/AD#<8$(/G(?5GG#+#&8(8"J#$(O75+<0#
*&?(l#<8*&'0#-(A/83(.3*J#m$($%A<0*$$#$R(5&(*(Shape(*++*"(*&?(J+/<#$$(83#>(5&(*($"$8#>*85<(>*&&#+(A#<*%$#(/G
J/0">/+J35$>1
Example 3: AreasOfAllShapes.java
publicpublic classclass Rectangle extendsextends Shape
{
privateprivate intint x, y;
privateprivate intint width, height;
publicpublic Rectangle(String color, booleanboolean filled, intint x, intint y, intint width, intint height)
{
supersuper(color, filled);
thisthis.x = x;
thisthis.y = y;
thisthis.width = width;
thisthis.height = height;
}
//getters and setters for x, y, width, and height (not shown)
@Override
publicpublic doubledouble area()
{
returnreturn width*height;
}
@Override
publicpublic String toString()
{
returnreturn "Rectangle [@ (" + x + ", " + y + "), w: " + width + ", h: " + height + "] " +
supersuper.toString();
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 51 of 185http://itec2150.gitlab.io/
U%8J%8(/G(83#(J+/'+*>B
3.5.2. An Experiment: Removing the Abstract Methods from the Super Class
;3#(J+#H5/%$(#c*>J0#($3/@$(83*8(*A$8+*<8(>#83/?$-(#H#&(83/%'3(&/8(5>J0#>#&8#?(5&(83#($%J#+(<0*$$-(@500(G/+<#(*00
83#(&/&Q*A$8+*<8($%A<0*$$#$(8/(5>J0#>#&8(83#>1(!#+#(@#(@500(?/(*&(#cJ#+5>#&8(8/(3#0J("/%(%&?#+$8*&?(83#
*A$8+*<8(>#83/?(G+/>(*&/83#+(*&'0#1
9/%(>5'38(A#(@/&?#+5&'(@3*8(</%0?(3*JJ#&(5G(@#(D%$8(>*,#(83#($%J#+(<0*$$(Shape(&/&Q*A$8+*<8(A"(+#>/H5&'(83#
*A$8+*<8(>#83/?(area(*08/'#83#+1(4#(<*&($5>J0"(5>J0#>#&8(83#(*+#*(>#83/?(5&(83#($%A<0*$$#$(75+<0#(*&?
l#<8*&'0#1(=G(@#(+#>/H#(83#(*A$8+*<8(>#83/?(area(G+/>(Shape(*&?(>*,#(58(*(+#'%0*+(<0*$$(@350#(,##J5&'(83#
>#83/?(area(5&(75+<0#(*&?(83*8(5&(l#<8*&'0#(5&8*<8-(@#(@500('#8(83#(G/00/@5&'(</>J50#(#++/+1
1| publicpublic classclass AreasOfAllShapes
2| {
3| publicpublic staticstatic voidvoid main(String[] args)
4| {
5| Shape[] shapes = newnew Shape[3];
6|
7| shapes[0] = newnew Circle("blue", truetrue, 30, 30, 5);
// center at (30, 30), radius 5
8| shapes[1] = newnew Rectangle("orange", falsefalse, 50, 100, 30, 15);
// upper-left corner (50, 100), width
30, height 15
9| shapes[2] = newnew Circle("red", falsefalse, 250, 250, 20);
// center at (250, 250), radius 20
10|
11| intint area = 0;
12| forfor (Shape s: shapes)
13| {
14| area += s.area();
15| }
16| System.out.println("Total area of the shapes: " + area);
17| }
18| }
JAVA
Total area of the shapes: 1784
4/5/21, 10)50 PMIntermediate Programming
Page 52 of 185http://itec2150.gitlab.io/
4#(@500('#8(*(</>J50#(#++/+(/&(05&#(CK(@583(83#(>#83/?(5&H/<*85/&(s.area()1(;3#+#(>%$8(A#(*(area(>#83/?(5&(83#
Shape(<0*$$(5&(/+?#+(8/('#8(83#(</?#(</>J50#?1(V+/>(835$(#cJ#+5>#&8-(@#(<*&($##(83*8(83#(*A$8+*<8(>#83/?(3*$(58$
$"&8*<85<(J%+J/$#(*&?(>%$8(A#(5&<0%?#?(5&(83#($%J#+(<0*$$(5&(/+?#+(G/+(J/0">/+J35$>(8/(@/+,1
4#(<*&(*0$/(</&G5+>(83*8(H*+5*A0#(m(5$(A/%&?(8/(?5GG#+#&8(/AD#<8$(?%+5&'(+%&(85>#(O83%$(<*00#?(?"&*>5<(A5&?5&'R
A%8(&/8(*8(</>J50#(85>#1(=&(G*<8-(G/+(>/$8(<*$#$-(58(5$(5>J/$$5A0#(8/(,&/@(@3*8(*(H*+5*A0#(5$(A/%&?(8/(*8(</>J50#
85>#1
3.6. Interface
;3#($"&8*c(G/+(?#G5&5&'(*&(5&8#+G*<#(5$(H#+"($5>50*+(8/(83*8(G/+(?#G5&5&'(*(<0*$$1(=&8#+G*<#(</&8*5&$(/&0"(J%A05<
*A$8+*<8(>#83/?O$R1(=8(?/#$(&/8(</&8*5&(*&"(*88+5A%8#$(*&?(83#+#G/+#(?/#$(&/8(</&8*5&(*&"(</&$8+%<8/+$(O&/835&'(8/
5&585*05a#R1(;3#(G/00/@5&'(5$(83#(5&8#+G*<#(?#G5&585/&(G/+(7/>J*+*A0#(O5&<0%?#?(5&(83#($8*&?*+?(6*H*(05A+*+"
388J$BPP?/<$1/+*<0#1</>P#&PD*H*PD*H*$#PCKP?/<$P*J5PD*H*1A*$#PD*H*P0*&'P7/>J*+*A0#138>0R1(4#(<*&($##(83*8(5&$8#*?(/G
class-(83#(,#"@/+?(/&(83#(3#*?#+(5$(interface1(V/+(>#83/?O$R-(83#(,#"@/+?$(public(*&?(abstract(*+#($,5JJ#?-
$5&<#(*00(>#83/?$(*+#(J%A05<(*&?(*A$8+*<81
Interface Comparable (Contained in the Java Library)
Comparing Abstract Classes and InterfacesB(9/%(>*"($*"(83*8(=&8#+G*<#(5$(+#*00"(*($J#<5*0(*A$8+*<8(<0*$$(83*8(/&0"
</&8*5&$(*A$8+*<8(>#83/?$1(=&(G*<8-(835$(5$(&/8(</++#<81(;3#(5&8#+G*<#(>#<3*&5$>(5$(+#*00"(r5&(J*+*00#0r(@583(83#(<0*$$
5&3#+58*&<#(>#<3*&5$>1(=8(5$(>#*&8(G/+(*(<0*$$(8/(r5>J0#>#&8r(83#(>#83/?$1(F(<0*$$(<*&(#c8#&?(*($%J#+(<0*$$(/+
5>J0#>#&8(*&(5&8#+G*<#1(=8(<*&(*0$/(#c8#&?(*($%J#+(<0*$$(*&?(5>J0#>#&8$(*&(5&8#+G*<#(*8(83#($*>#(85>#1(F&(*A$8+*<8
<0*$$(5$($8500(*(<0*$$1(;3*8(5$-(58(5$(@583(83#(<0*$$(5&3#+58*&<#(>#<3*&5$>(83*8(5$(r5&(J*+*00#0r(@583(83#(5&8#+G*<#
5>J0#>#&8*85/&(>#<3*&5$>1
V/+(#c*>J0#-(83#(G/00/@5&'(<0*$$(5>J0#>#&8$(7/>J*+*A0#1(;/(5>J0#>#&8(*&(5&8#+G*<#-(OCR(83#(<0*$$($3/%0?(A#
>*+,#?(/&(83#(3#*?#+(*$(r5>J0#>#&8$(83#(5&8#+G*<#r(*&?(OER(83#(*A$8+*<8(>#83/?$(5&(83#(5&8#+G*<#(@500(&##?(8/(A#
/H#++5??#&-(/83#+@5$#(83#"(*+#(r5&3#+58#?r(*$(5$(*$(*A$8+*<8(>#83/?$1
Example 4: Circle.java
publicpublic interfaceinterface Comparable<T>
{
intint compareTo(T o);
/*Compares this object with the specified object for order. Returns a negative integer, zero, or a positive
integer as this object is less than, equal to, or greater than the specified object.*/
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 53 of 185http://itec2150.gitlab.io/
9/%(<*&($##(83*8(835$(5$(83#($*>#(75+<0#(<0*$$(*$(83#(0*$8($#<85/&-(A%8(@583(8@/(835&'$(*??#?B
F8(83#(#&?(/G(83#(<0*$$(3#*?#+-(implements Comparable<Shape>(5$(*??#?1
F(compareTo(>#83/?(5$(*??#?(8/(83#(<0*$$(?#G5&585/&1(;35$(5$($5>50*+(8/(/H#++5?#(*(>#83/?(5&(*($%J#+(<0*$$1(;3#
</>J*+#;/(>#83/?(+#8%+&$(*(J/$585H#(5&8#'#+(C(5G(83#(75+<0#(/AD#<8m$(*+#*(5$('+#*8(83*&(83#(J*+*>#8#+(oo$(*+#*-
+#8%+&$(*(&#'*85H#(5&8#'#+(QC(5G(0#$$(83*&-(*&?(+#8%+&$(T(5G(#d%*01
4#(</%0?(?/(83#($*>#(G/+(83#(l#<8*&'0#(<0*$$(A"(>*,5&'(58(5>J0#>#&8(83#(7/>J*+*A0#(5&8#+G*<#B
Example 4: Rectangle.java
publicpublic classclass Circle extendsextends Shape implementsimplements Comparable<Shape>
{
privateprivate intint x, y;
privateprivate intint radius;
publicpublic Circle(String color, booleanboolean filled, intint x, intint y, intint radius)
{
supersuper(color, filled);
thisthis.x = x; thisthis.y = y; thisthis.radius = radius;
}
//getters and setters for x, y and radius (not shown)
@Override
publicpublic String toString()
{
returnreturn "Circle [@(" + x + ", " + y + "), r: " + radius + "] " + supersuper.toString();
}
@Override
publicpublic doubledouble area()
{
returnreturn Math.PI * radius * radius;
}
@Override
publicpublic intint compareTo(Shape o)
{
ifif (thisthis.area() > o.area())
returnreturn 1;
elseelse ifif (thisthis.area() < o.area())
returnreturn -1;
elseelse
returnreturn 0;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 54 of 185http://itec2150.gitlab.io/
Example 4: Shape.java
`/83(83#(75+<0#(*&?(l#<8*&'0#(<0*$$#$(3*H#(&/(</>J50*85/&(#++/+$1(9/%(<*&($##(83*8(*(<0*$$(<*&(#c8#&?(*($%J#+(<0*$$
*&?(5>J0#>#&8(*&(5&8#+G*<#(*8(83#($*>#(85>#1(F(<0*$$(<*&(/&0"(#c8#&?(/&#($%J#+Q<0*$$-(3/@#H#+-(*(<0*$$(<*&
5>J0#>#&8(>/+#(83*&(/&#(5&8#+G*<#-(@35<3(@#(@500(8*0,(*A/%8(0*8#+1
j/@(0#8m$(0//,(*8(83#(G/00/@5&'(J+/'+*>-(@35<3(5&8#&?$(8/(%$#(83#(compareTo(>#83/?$(5&(75+<0#(*&?(l#<8*&'0#(8/
?#8#+>5&#(83#($3*J#(@583(83#(>*c5>%>(*+#*1(Z&G/+8%&*8#0"-(58(</&8*5&$(*(</>J50#(#++/+1
publicpublic classclass Rectangle extendsextends Shape implementsimplements Comparable<Shape>
{
privateprivate intint x, y;
privateprivate intint width, height;
publicpublic Rectangle(String color, booleanboolean filled, intint x, intint y, intint width, intint height)
{
supersuper(color, filled);
thisthis.x = x;
thisthis.y = y;
thisthis.width = width;
thisthis.height = height;
}
//getters and setters for x, y, width and height (not shown)
@Override
publicpublic String toString()
{
returnreturn "Rectangle [@ (" + x + ", " + y + "), w: " + width + ", h: " + height + "] " +
supersuper.toString();
}
@Override
publicpublic doubledouble area()
{
returnreturn width*height;
}
@Override
publicpublic intint compareTo(Shape o)
{
ifif (thisthis.area() > o.area())
returnreturn 1;
elseelse ifif (thisthis.area() < o.area())
returnreturn -1;
elseelse
returnreturn 0;
}
}
JAVA
The same as Shape.java in Example 3
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 55 of 185http://itec2150.gitlab.io/
;3#+#(5$(*&(#++/+(G/+(p.compareTo(maxShape)-($5&<#(83#(H*+5*A0#(po$(8"J#(5$(.3*J#(*&?(83#+#(5$(&/(compareTo
>#83/?(?#G5&#?(5&(83#(.3*J#(<0*$$1(;35$(5$(*($5>50*+(#++/+(*$(83#(/&#(@#($##(5&(83#(#cJ#+5>#&8(G/+(*A$8+*<8(<0*$$1
;3#($%J#+(<0*$$(Shape(&##?$(8/(</&8*5&(83#(>#83/?(5&(/+?#+(G/+(83#(*A/H#(</?#(8/(A#(</>J50#?1
;/(G5c(835$(J+/A0#>-(@#(@500(5>J0#>#&8(83#(compareTo(>#83/?(5&(83#(.3*J#(<0*$$-(5&$8#*?(/G(5&(83#(75+<0#(<0*$$
*&?(83#(l#<8*&'0#(<0*$$1(;35$(@*"-(@#(/&0"(&##?(8/(5>J0#>#&8(83#(>#83/?(/&<#(*&?(58(@500(A#(5&3#+58#?(A"(*00(83#
$%A<0*$$#$(O75+<0#(*&?(l#<8*&'0#R1(4#(@500(,##J(83#(75+<0#(*&?(l#<8*&'0#(<0*$$#$(83#($*>#(*$(83#(J+#H5/%$($#<85/&
O@583/%8(83#(compareTo(>#83/?R1(.##(A#0/@(G/+(83#(&#@(.3*J#(<0*$$1
;3#(G/00/@5&'(5$(83#(ZSN(83*8(<*J8%+#$(83#(+#0*85/&$35J(*>/&'(83#(<0*$$#$(*&?(83#(5&8#+G*<#(5&(bc*>J0#(L1(;3#
?/88#?(05&#(*++/@(+#J+#$#&8$(83#(5>J0#>#&8*85/&(+#0*85/&$35J1
4/5/21, 10)50 PMIntermediate Programming
Page 56 of 185http://itec2150.gitlab.io/
Example 5: Shape.java
4/5/21, 10)50 PMIntermediate Programming
Page 57 of 185http://itec2150.gitlab.io/
Example 5: Circle.java
Example 5: Rectangle.java
Example 5: MaxShape.java
43#&("/%(+%&(S*c.3*J#-(83#(J+/'+*>(@500(?5$J0*"(83#(G/00/@5&'B
publicpublic abstractabstract classclass Shape implementsimplements Comparable<Shape>
{
privateprivate String color;
privateprivate booleanboolean filled;
publicpublic Shape(String color, booleanboolean filled)
{
supersuper();
thisthis.color = color;
thisthis.filled = filled;
}
//getters and setters for color and filled (not shown)
publicpublic abstractabstract doubledouble area();
@Override
publicpublic String toString()
{
returnreturn "[color=" + color + ", filled=" + filled + "]";
}
@Override
publicpublic intint compareTo(Shape o)
{
ifif (thisthis.area() > o.area())
returnreturn 1;
elseelse ifif (thisthis.area() < o.area())
returnreturn -1;
elseelse
returnreturn 0;
}
}
JAVA
The same as Circle.java in Example 3
JAVA
The same as Circle.java in Example 3
JAVA
The same as MaxShape.java in Example 4
JAVA
The shape with maximum area is: Circle [@(250, 250), r: 20.0] [color=red, filled=false]
4/5/21, 10)50 PMIntermediate Programming
Page 58 of 185http://itec2150.gitlab.io/
N#8(%$(0//,(*8(83#(Shape(<0*$$(>/+#(<*+#G%00"1(9/%(@500($##(83*8(83#(>#83/?(area(5$(*A$8+*<81(9/%(>5'38(@/&?#+
@3#83#+(58(5$(U_(G/+(83#(compareTo(>#83/?(8/(5&H/,#(83#(area(>#83/?1(=8(5$(U_(A#<*%$#(*&(*A$8+*<8(<0*$$(<*&&/8
A#(5&$8*&85*8#?1(;3#(*A$8+*<8(>#83/?(area(@500(A#(/H#++5??#&(A"(83#($%A<0*$$#$(75+<0#(*&?(l#<8*&'0#(*&?(83#
compareTo(>#83/?(@500(A#(5&3#+58#?(A"(83#($%A<0*$$#$(75+<0#(*&?(l#<8*&'0#1(=&(83#(0//J(O05&#(CCQCMR(5&(83#(>*5&
>#83/?-(H*+5*A0#$(p(*&?(maxShape(*+#(A/%&?(8/(#583#+(*(75+<0#(/+(l#<8*&'0#(/AD#<8(*8(*&"(J/5&8(?%+5&'(#c#<%85/&1
;3#(5&3#+58#?(>#83/?(compareTo(@500(5&H/,#(83#(J+/J#+(area(>#83/?(?#G5&#?(5&(#583#+(75+<0#(/+(l#<8*&'0#1
;35$(+#*00"($3/@$(*&/83#+(*?H*&8*'#(/G(%$5&'(*A$8+*<8(<0*$$#$1(=8(3/0?$(</&<+#8#(>#83/?$(83*8(<*&(A#(5>J0#>#&8#?
$/(83*8(83#($%AQ<0*$$#$(?/(&/8(3*H#(8/(5>J0#>#&8(83#>(*&?(<*&(5&3#+58(83#>(*$(83#"(*+#1(=8(<*&(*0$/(3/0?(*A$8+*<8
>#83/?$(83*8(58(<*&&/8(5>J0#>#&8(A%8(@500(rG/+<#r(*00(83#(&/&Q*A$8+*<8($%A<0*$$#$(8/(5>J0#>#&8(83#5+(/@&(H#+$5/&1
3.6.1. A Class Can Implement Multiple Interfaces
bH#&(83/%'3(*(<0*$$(<*&(/&0"(#c8#&?(/&#(*&?(/&0"(/&#($%J#+(<0*$$-(*(<0*$$(<*&(5>J0#>#&8(>%085J0#(5&8#+G*<#$1(4#
@500(>*,#(83#(.3*J#(<0*$$(5>J0#>#&8(8@/(5&8#+G*<#$(Comparable(*&?(Animation1
;3#(G/00/@5&'(5$(83#(Animation(5&8#+G*<#(83*8(</&8*5&$(8@/(J%A05<(*A$8+*<8(>#83/?$(talk(*&?(flipRight1
Example 6: Animation.java
43#&(*(<0*$$(5>J0#>#&8$(>/+#(83*&(/&#(5&8#+G*<#-(83#(5&8#+G*<#(&*>#$(*+#($#J*+*8#?(@583(</>>*O$R(/&(83#(<0*$$
3#*?#+1
Example 6: Shape.java
publicpublic interfaceinterface Animation
{
voidvoid talk();
//Simulate talking by displaying (printing) a message
voidvoid flipRight();
//flip to the right (mirror)
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 59 of 185http://itec2150.gitlab.io/
;3#(.3*J#(<0*$$(/&0"(5>J0#>#&8$(83#(talk(>#83/?(/G(83#(8@/(>#83/?$(5&(83#(Animation(5&8#+G*<#1(;3#(flipRight
>#83/?(5$(&/8(5>J0#>#&8#?(5&(.3*J#-($/(58(5$(r5&3#+58#?r(G+/>(83#(5&8#+G*<#(*$(5$-(83*8(5$-(58(5$($8500(*&(*A$8+*<8
>#83/?(5&(83#(.3*J#(<0*$$1(=8(5$(+#*00"(&/8(J/$$5A0#(8/(5>J0#>#&8(83#(flipRight(>#83/?(3#+#-($5&<#(3/@(*($3*J#(G05J$
?#J#&?$(/&(@3*8(83#(*<8%*0($3*J#(58(5$1(;35$(>#83/?(@500(&##?(8/(A#(5>J0#>#&8#?(5&(83#(</&<+#8#($3*J#(<0*$$#$-
75+<0#(*&?(l#<8*&'0#(O$##(A#0/@R1
;3#(G/00/@5&'(5$(83#(ZSN(83*8(<*J8%+#$(83#(+#0*85/&$35J(*>/&'(83#(<0*$$#$(*&?(83#(5&8#+G*<#(5&(bc*>J0#(M1
publicpublic abstractabstract classclass Shape implementsimplements Comparable<Shape>, Animation
{
privateprivate String color;
privateprivate booleanboolean filled;
publicpublic Shape(String color, booleanboolean filled)
{
supersuper();
thisthis.color = color;
thisthis.filled = filled;
}
//getters and setters for color and filled (not shown)
publicpublic abstractabstract doubledouble area();
@Override
publicpublic String toString()
{
returnreturn "[color=" + color + ", filled=" + filled + "]";
}
@Override
publicpublic intint compareTo(Shape o)
{
ifif (thisthis.area() > o.area())
returnreturn 1;
elseelse ifif (thisthis.area() < o.area())
returnreturn -1;
elseelse
returnreturn 0;
}
@Override
publicpublic voidvoid talk()
{
System.out.println("I am " + thisthis.toString());
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 60 of 185http://itec2150.gitlab.io/
Example 6: Circle.java
43#&(*(<5+<0#(G05J$(8/(83#(+5'38-(58(G05J$(/&(83#(H#+85<*0(8*&'#&8(/&(83#(+5'38-($/(58$(&#@(c(<//+?5&*8#(5$(83#(<%++#&8(c
<//+?5&*8#(J0%$(E(s(+*?5%$1
Example 6: Rectangle.java
publicpublic classclass Circle extendsextends Shape
{
privateprivate intint x, y;
privateprivate intint radius;
publicpublic Circle(String color, booleanboolean filled, intint x, intint y, intint radius)
{
supersuper(color, filled);
thisthis.x = x; thisthis.y = y; thisthis.radius = radius;
}
//getters and setters for x, y, and radius (not shown)
@Override
publicpublic String toString()
{
returnreturn "Circle [@(" + x + ", " + y + "), r: " + radius + "] " + supersuper.toString();
}
@Override
publicpublic doubledouble area()
{
returnreturn Math.PI * radius * radius;
}
@Override
publicpublic voidvoid flipRight()
{
x = x + 2 * radius;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 61 of 185http://itec2150.gitlab.io/
43#&(*(+#<8*&'0#(G05J$(8/(83#(+5'38-(58(G05J$(/&(83#(+5'38(H#+85<*0(#?'#-(83#(/+5'5&*0(%JJ#+(+5'38(</+&#+(A#</>#$(83#
%JJ#+(0#G8(</+&#+(G/+(83#(+#<8*&'0#(*8(83#(&#@(0/<*85/&1(;3#+#G/+#-(83#(&#@(c(<//+?5&*8#(/G(83#(+#<8*&'0#(5$(83#
/+5'5&*0(H*0%#(J0%$(83#(@5?831
;3#(</?#(A#0/@(@500(%8505a#(J/0">/+J35$>(8/(0#8(#*<3($3*J#(/AD#<8(O@35<3(</%0?(A#(75+<0#(/+(l#<8*&'0#R(8*0,-(G05J
+5'38-(*&?(83#&(8*0,(*'*5&1
Example 6: AnimateShapes.java
publicpublic classclass Rectangle extendsextends Shape
{
privateprivate intint x, y;
privateprivate intint width, height;
publicpublic Rectangle(String color, booleanboolean filled, intint x, intint y, intint width, intint height)
{
supersuper(color, filled);
thisthis.x = x;
thisthis.y = y;
thisthis.width = width;
thisthis.height = height;
}
//getters and setters for x, y, width, height
@Override
publicpublic String toString()
{
returnreturn "Rectangle [@ (" + x + ", " + y + "), w: " + width + ", h: " + height + "] " +
supersuper.toString();
}
@Override
publicpublic doubledouble area()
{
returnreturn width*height;
}
@Override
publicpublic voidvoid flipRight()
{
x = x + width;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 62 of 185http://itec2150.gitlab.io/
;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>1(FG8#+(G05JJ5&'(+5'38-(*(<5+<0#(@583(<#&8#+(*8(OIT-(ITR(*&?(+*?5%$(L
A#</>#$(*(<5+<0#(*8(OKT-(ITR(*&?(+*?5%$(&/8(<3*&'#?e(*(+#<8*&'0#(@583(83#(%JJ#+Q0#G8(</+&#+(*8(OLT-(CTTR(@583(@5?83
IT(A#</>#$(*(+#<8*&'0#(*8(O[T-(CTTR(*&?(?5>#&$5/&(&/8(<3*&'#?e(*&?($/(/&1
3.6.2. Polymorphism through Interface
;3#(J/0">/+J35$>(#c*>J0#$($/(G*+(*+#(*00(83+/%'3($%J#+(<0*$$#$1(=&(835$($#<85/&-(@#(@500($##(83#(*>*a5&'(J/@#+(/G
J/0">/+J35$>(83+/%'3(5&8#+G*<#-(@35<3(#&*A0#$($"$8#>*85<*00"($8/+5&'(*&?(J+/<#$$5&'($##>5&'0"(%&+#0*8#?
/AD#<8$1
V5+$8-(0#8(%$(0//,(*8(*(&#@(<0*$$-(Text-(83*8(5$(H#+"(?5GG#+#&8(G+/>(Shape1(=8$(/&0"(+#0*85/&$35J(8/(Shape(5$(83*8(58(*0$/
5>J0#>#&8$(83#(Animation(5&8#+G*<#1
Example 6: Text.java
publicpublic classclass AnimateShapes
{
publicpublic staticstatic voidvoid main(String[] args)
{
Shape[] shapes = newnew Shape[3];
shapes[0] = newnew Circle("blue", truetrue, 30, 30, 5);
// center at (30, 30), radius 5
shapes[1] = newnew Rectangle("orange", falsefalse, 50, 100, 30, 15);
// upper-left corner (50, 100), width 30,
height 15
shapes[2] = newnew Circle("red", falsefalse, 250, 250, 20);
// center at (250, 250), radius 20
forfor (Shape p: shapes)
{
p.talk();
p.flipRight();
p.talk();
System.out.println();
}
}
}
JAVA
I am Circle [@(30, 30), r: 5] [color=blue, filled=true]
I am Circle [@(40, 30), r: 5] [color=blue, filled=true]
I am Rectangle [@ (50, 100), w: 30, h: 15] [color=orange, filled=false]
I am Rectangle [@ (80, 100), w: 30, h: 15] [color=orange, filled=false]
I am Circle [@(250, 250), r: 20] [color=red, filled=false]
I am Circle [@(290, 250), r: 20] [color=red, filled=false]
4/5/21, 10)50 PMIntermediate Programming
Page 63 of 185http://itec2150.gitlab.io/
`/83(>#83/?$(5&(83#(Animation(5&8#+G*<#(*+#(5>J0#>#&8#?1(V/+(83#(;#c8(<0*$$-(G05JJ5&'(8/(83#(+5'38(>#*&$(+#H#+$*0
/G(83#($8+5&'(8#c81
Example 6: AnimateShapesTexts.java
publicpublic classclass Text implementsimplements Animation
{
privateprivate String text;
publicpublic Text(String text)
{
thisthis.text = text;
}
//getter and setter for text (not shown)
@Override
publicpublic String toString()
{
returnreturn "Text [text=" + text + "]";
}
@Override
publicpublic voidvoid talk()
{
System.out.println("I am " + thisthis.toString());
}
@Override
publicpublic voidvoid flipRight()
{
//reverse the text
String newText = "";
forfor (intint i = text.length()-1; i >= 0; i--)
{
newText = newText + text.charAt(i);
}
text = newText;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 64 of 185http://itec2150.gitlab.io/
;3#(</?#(*A/H#(?#>/&$8+*8#$(83*8(*&(5&8#+G*<#(<*&(A#(%$#?(*$(*(?*8*(8"J#1(=&(*($#&$#-(*(<0*$$(5>J0#>#&85&'(*&
5&8#+G*<#(5$($5>50*+(8/(*($%A<0*$$(#c8#&?5&'(*($%J#+(<0*$$1(F&(5&8#+G*<#(H*+5*A0#(<*&(A#(%$#?(8/(3/0?(+#G#+#&<#$(8/
/AD#<8$(/G(*&"(<0*$$(83*8(5>J0#>#&8$(83#(5&8#+G*<#1(`/83(83#(75+<0#(*&?(l#<8*&'0#(<0*$$#$(5>J0#>#&8(83#(F&5>*85/&
5&8#+G*<#(83+/%'3(83#5+($%J#+(<0*$$(.3*J#-($/(83#(+#G#+#&<#(8/(*(75+<0#(/+(l#<8*&'0#(/AD#<8(<*&(A#($8/+#?(5&(*&
Animation(H*+5*A0#1(;3#(;#c8(<0*$$(*0$/(5>J0#>#&8$(83#(F&5>*85/&(5&8#+G*<#-($/(83#(+#G#+#&<#$(8/(*(;#c8(/AD#<8(<*&
*0$/(A#($8/+#?(5&(*&(Animation(H*+5*A0#1
=&(83#(#c*>J0#(*A/H#-(H*+5*A0#(a(5$(/G(8"J#(Animation(O*&(5&8#+G*<#R1(=8(5$(G5+$8(*$$5'&#?(8/(83#(+#G#+#&<#(8/(*(75+<0#
/AD#<8-(83#&(*(l#<8*&'0#(/AD#<8-(*&?(0*$8(*(;#c8(/AD#<81(;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>1
.5&<#(75+<0#-(l#<8*&'0#-(*&?(;#c8(*00(5>J0#>#&8(F&5>*85/&-(@#(<*&($8/+#(83#(+#G#+#&<#$(8/(83#5+(/AD#<8$(5&(*&
F&5>*85/&(*++*"(*&?(J+/<#$$(83#>($"$8#>*85<*00"(5&(*(0//J(O$##(83#(</?#(A#0/@R1(V/+(*(J+/'+*>(83*8(>*&*'#$(*
0*+'#(&%>A#+(/G(/AD#<8$-(*A5058"(8/($8/+#(83#>(5&(*&(*++*"(*&?(J+/<#$$(83#>(@583(*(0//J(5&(*($"$8#>*85<(>*&&#+
@500(>*,#(J+/'+*>>5&'(>*&*'#*A0#1
Example 6: AnimateShapesTexts.java
publicpublic classclass AnimateShapesTexts
{
publicpublic staticstatic voidvoid main(String[] args)
{
Animation a;
a = newnew Circle("blue", truetrue, 30, 30, 5);
// center at (30, 30), radius 5
a.talk();
a.flipRight();
a.talk();
a = newnew Rectangle("orange", falsefalse, 50, 100, 30, 15);
// upper-left corner (50, 100), width 30, height
15
a.talk();
a.flipRight();
a.talk();
a = newnew Text("hello");
a.talk();
a.flipRight();
a.talk();
}
}
JAVA
I am Circle [@(30, 30), r: 5] [color=blue, filled=true]
I am Circle [@(40, 30), r: 5] [color=blue, filled=true]
I am Rectangle [@ (50, 100), w: 30, h: 15] [color=orange, filled=false]
I am Rectangle [@ (80, 100), w: 30, h: 15] [color=orange, filled=false]
I am Text [text=hello]
I am Text [text=olleh]
4/5/21, 10)50 PMIntermediate Programming
Page 65 of 185http://itec2150.gitlab.io/
;35$(J+/'+*>(3*$(#c*<80"(83#($*>#(/%8J%8(*$(83#(J+#H5/%$(J+/'+*>(83*8(?/#$(&/8(%$#(*(0//J1(;3#(J+/'+*>(<+#*8#$
*&(F&5>*85/&(*++*"(shapesAndTexts(@583($5a#(I1(b*<3(#0#>#&8(/G(83#(*++*"(<*&($8/+#(83#(+#G#+#&<#(8/(*&(/AD#<8(/G
*&"(<0*$$(83*8(5>J0#>#&8$(83#(F&5>*85/&(5&8#+G*<#1(;3#(+#G#+#&<#$(8/(*(75+<0#(/AD#<8-(*(l#<8*&'0#(/AD#<8-(*&?(*(;#c8
/AD#<8(*+#(*$$5'&#?(8/(#*<3(#0#>#&8(/G(83#(*++*"1(;3#(J+/'+*>(83#&(%$#$(*(G/+Q#*<3(0//J(8/(J+/<#$$(83#(*++*"1
70*$$#$(83*8(*+#(H#+"(?5GG#+#&8(G+/>(#*<3(/83#+(<*&(+#0*8#(8/(#*<3(/83#+(83+/%'3(83#(</>>/&(5&8#+G*<#(83#"
5>J0#>#&81(=8(5$(*$(5G(83*8(83#"(3*H#(*(</>>/&($%J#+(<0*$$1(=&8#+G*<#(>*,#$(58(H#+"(</&H#&5#&8(G/+(%$(8/(8*,#
*?H*&8*'#(/G(J/0">/+J35$>(8/($8/+#(H#+"(?5GG#+#&8(/AD#<8$(5&(*(?*8*($8+%<8%+#(O#1'1(*&(*++*"R(*&?(J+/<#$$(83#>(5&(*
$"$8#>*85<(>*&&#+1
3.6.3. Interface Inheritance
F&(5&8#+G*<#-(D%$8(05,#(*(<0*$$-(<*&(3*H#(*(J*+#&8(5&8#+G*<#1(;3*8(5$-(*&(5&8#+G*<#(<*&(#c8#&?(*(J*+#&8(5&8#+G*<#1(V/+
#c*>J0#-(83#(FullAnimation(5&8#+G*<#(A#0/@(5$(*(<350?(O$%AR(5&8#+G*<#(/G(83#(J*+#&8(O$%J#+R(5&8#+G*<#(Animation1
;3#(V%00F&5>*85/&(5&8#+G*<#(5&3#+58$(83#(8@/(*A$8+*<8(>#83/?$(O8*0,(*&?(G05Jl5'38R(G+/>(F&5>*85/&(*&?(*??$(83+##
&#@(*A$8+*<8(>#83/?$(G05JN#G8-(G05JZJ(*&?(G05J2/@&1
Example 7: FullAnimation.java
Example 7: Animation.java
publicpublic classclass AnimateShapesTextsLoop
{
publicpublic staticstatic voidvoid main(String[] args)
{
Animation[] shapesAndTexts = newnew Animation[3];
shapesAndTexts[0] = newnew Circle("blue", truetrue, 30, 30, 5);
// center at (30, 30), radius 5
shapesAndTexts[1] = newnew Rectangle("orange", falsefalse, 50, 100, 30, 15);
// upper-left corner (50, 100),
width 30, height 15
shapesAndTexts[2] = newnew Text("hello");
forfor (Animation a: shapesAndTexts)
{
a.talk();
a.flipRight();
a.talk();
System.out.println();
}
}
}
JAVA
publicpublic interfaceinterface FullAnimation extendsextends Animation
{
voidvoid flipLeft();
//flip to the left (mirror)
voidvoid flipUp();
//flip to the left (mirror)
voidvoid flipDown();
//flip to the left (mirror)
}
JAVA
The same as Animation.java in Example 6
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 66 of 185http://itec2150.gitlab.io/
4#(>/?5G5#?(83#(*A$8+*<8(<0*$$(.3*J#($/(83*8(58(5>J0#>#&8$(83#(V%00F&5>*85/&(5&$8#*?(/G(F&5>*85/&1(V+/>(83#(</?#
A#0/@-(@#(<*&($##(83*8(/&0"(83#(8*0,(>#83/?(5$(5>J0#>#&8#?-(83#(/83#+(G/%+(>#83/?$-(G05Jl5'38-(G05JN#G8-(G05JZJ-(*&?
G05J2/@&-(@500(A#(5>J0#>#&8#?(5&(83#(</&<+#8#($%A<0*$$#$(/G(.3*J#-(75+<0#(*&?(l#<8*&'0#1
Example 7: Shape.java
;3#(G/00/@5&'(5$(83#(ZSN(83*8(<*J8%+#$(83#(+#0*85/&$35J(*>/&'(83#(<0*$$#$(*&?(83#(5&8#+G*<#(5&(bc*>J0#(W1
publicpublic abstractabstract classclass Shape implementsimplements Comparable<Shape>, FullAnimation
{
privateprivate String color;
privateprivate booleanboolean filled;
publicpublic Shape(String color, booleanboolean filled)
{
supersuper();
thisthis.color = color;
thisthis.filled = filled;
}
//getters and setters for color and filled (not shown)
publicpublic abstractabstract doubledouble area();
@Override
publicpublic String toString()
{
returnreturn "[color=" + color + ", filled=" + filled + "]";
}
@Override
publicpublic intint compareTo(Shape o)
{
ifif (thisthis.area() > o.area())
returnreturn 1;
elseelse ifif (thisthis.area() < o.area())
returnreturn -1;
elseelse
returnreturn 0;
}
@Override
publicpublic voidvoid talk()
{
System.out.println("I am " + thisthis.toString());
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 67 of 185http://itec2150.gitlab.io/
.5&<#(83#(75+<0#(<0*$$(5$(*(&/&Q*A$8+*<8(<0*$$-(58(&##?$(8/(5>J0#>#&8(*00(83#(*A$8+*<8(>#83/?$(5&(V%00F&5>*85/&(A%8
&/8(5>J0#>#&8#?("#8(5&(.3*J#1
Example 7: Circle.java
4/5/21, 10)50 PMIntermediate Programming
Page 68 of 185http://itec2150.gitlab.io/
9/%(<*&(?/(*&(#cJ#+5>#&81(=G("/%(+#>/H#(83#(G05Jl5'38(>#83/?-(83#+#(@500(A#(*(</>J50#(#++/+1(;35$(D%$8(</&G5+>$
83*8(G05Jl5'38(5$(5&3#+58#?(A"(V%00F&5>*85/&(G+/>(F&5>*85/&(*&?(5$(J*+8(/G(83#(V%00F&5>*8/&(5&8#+G*<#1
;3#(G/00/@5&'(5$(83#(%J?*8#?(l#<8*&'0#(<0*$$B
Example 7: Rectangle.java
publicpublic classclass Circle extendsextends Shape
{
privateprivate intint x, y;
privateprivate intint radius;
publicpublic Circle(String color, booleanboolean filled, intint x, intint y, intint radius)
{
supersuper(color, filled);
thisthis.x = x; thisthis.y = y; thisthis.radius = radius;
}
//getters and setters for x, y, and radius
@Override
publicpublic String toString()
{
returnreturn "Circle [@(" + x + ", " + y + "), r: " + radius + "] " + supersuper.toString();
}
@Override
publicpublic doubledouble area()
{
returnreturn Math.PI * radius * radius;
}
@Override
publicpublic voidvoid flipRight()
{
x = x + 2 * radius;
}
@Override
publicpublic voidvoid flipLeft()
{
x = x - 2 * radius;
}
@Override
publicpublic voidvoid flipUp()
{
y = y - 2 * radius;
}
@Override
publicpublic voidvoid flipDown()
{
y = y + 2 * radius;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 69 of 185http://itec2150.gitlab.io/
;3#(G/00/@5&'(</?#(5$(H#+"($5>50*+(8/(*&(#c*>J0#(?#$<+5A#?(A#G/+#-(A%8(5&(*??585/&(8/(8*0,(*&?(G05J(+5'38-(#*<3($3*J#
<*&(*0$/(G05J(0#G8-(%J-(*&?(?/@&1
Example 7: AnimateShapes.java
publicpublic classclass Rectangle extendsextends Shape
{
privateprivate intint x, y;
privateprivate intint width, height;
publicpublic Rectangle(String color, booleanboolean filled, intint x, intint y, intint width, intint height)
{
supersuper(color, filled);
thisthis.x = x;
thisthis.y = y;
thisthis.width = width;
thisthis.height = height;
}
//getters and setters for x, y, width, and height (not shown)
@Override
publicpublic String toString()
{
returnreturn "Rectangle [@ (" + x + ", " + y + "), w: " + width + ", h: " + height + "] " +
supersuper.toString();
}
@Override
publicpublic doubledouble area()
{
returnreturn width*height;
}
@Override
publicpublic voidvoid flipRight()
{
x = x + width;
}
@Override
publicpublic voidvoid flipLeft()
{
x = x - width;
}
@Override
publicpublic voidvoid flipUp()
{
y = y - height;
}
@Override
publicpublic voidvoid flipDown()
{
y = y + height;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 70 of 185http://itec2150.gitlab.io/
;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>(*A/H#1(4#(<*&($##(83*8(5G(*($3*J#(G05J(+5'38(*&?(83#&(0#G8-(58(+#8%+&$(8/
83#(/+5'5&*0(0/<*85/&1(;3#($*>#(G/+(G05JJ5&'(%J(*&?(83#&(?/@&1
3.7. Summary
)/0">/+J35$>(<*&(A#(%$#?(8/(</&&#<8(?5GG#+#&8(<0*$$#$(83+/%'3(*(</>>/&($%J#+(<0*$$(/+(*(</>>/&(5&8#+G*<#(83#"
5>J0#>#&8-(*00/@5&'(/AD#<8$(A#0/&'5&'(8/(?5GG#+#&8(<0*$$#$(8/(A#($8/+#?(*&?(J+/<#$$#?($"$8#>*85<*00"1
3.8. Key Terms
publicpublic classclass AnimateShapes
{
publicpublic staticstatic voidvoid main(String[] args)
{
Shape[] shapes = newnew Shape[3];
shapes[0] = newnew Circle("blue", truetrue, 30, 30, 5);
// center at (30, 30), radius 5
shapes[1] = newnew Rectangle("orange", falsefalse, 50, 100, 30, 15);
// upper-left corner (50, 100), width 30,
height 15
shapes[2] = newnew Circle("red", falsefalse, 250, 250, 20);
// center at (250, 250), radius 20
forfor (Shape p: shapes)
{
p.talk();
p.flipRight();
p.talk();
p.flipLeft();
p.talk();
p.flipUp();
p.talk();
p.flipDown();
p.talk();
System.out.println();
}
}
}
JAVA
I am Circle [@(30, 30), r: 5] [color=blue, filled=true]
I am Circle [@(40, 30), r: 5] [color=blue, filled=true]
I am Circle [@(30, 30), r: 5] [color=blue, filled=true]
I am Circle [@(30, 20), r: 5] [color=blue, filled=true]
I am Circle [@(30, 30), r: 5] [color=blue, filled=true]
I am Rectangle [@ (50, 100), w: 30, h: 15] [color=orange, filled=false]
I am Rectangle [@ (80, 100), w: 30, h: 15] [color=orange, filled=false]
I am Rectangle [@ (50, 100), w: 30, h: 15] [color=orange, filled=false]
I am Rectangle [@ (50, 85), w: 30, h: 15] [color=orange, filled=false]
I am Rectangle [@ (50, 100), w: 30, h: 15] [color=orange, filled=false]
I am Circle [@(250, 250), r: 20] [color=red, filled=false]
I am Circle [@(290, 250), r: 20] [color=red, filled=false]
I am Circle [@(250, 250), r: 20] [color=red, filled=false]
I am Circle [@(250, 210), r: 20] [color=red, filled=false]
I am Circle [@(250, 250), r: 20] [color=red, filled=false]
4/5/21, 10)50 PMIntermediate Programming
Page 71 of 185http://itec2150.gitlab.io/
Dynamic BindingB(=&(?"&*>5<(A5&?5&'-(83#(>#83/?(<*00(5$(A/&?#?(8/(83#(>#83/?(A/?"(*8(+%&85>#1(;35$(5$(*0$/
,&/@&(*$(0*8#(A5&?5&'1
PolymorphismB()/0">/+J35$>(>#*&$(83*8(*(+#G#+#&<#(H*+5*A0#(<*&(3/0?(+#G#+#&<#$(8/(?5GG#+#&8(8"J#$(/G(/AD#<8$1
V/+(#c*>J0#-(*(+#G#+#&<#(H*+5*A0#(</%0?(+#G#+(8/(/AD#<8$(83*8(A#0/&'(8/(*&"(/G(58$($%A<0*$$#$1(F(+#G#+#&<#(H*+5*A0#
<*&(*0$/(3/0?(+#G#+#&<#$(8/(/AD#<8$(A#0/&'5&'(8/(?5GG#+#&8(<0*$$#$(83*8(5>J0#>#&8(83#($*>#(5&8#+G*<#1
Abstract MethodB(F&(*A$8+*<8(>#83/?(5$(*(>#83/?(83*8(5$(?#<0*+#?(A%8(</&8*5&$(&/(5>J0#>#&8*85/&1
Non-instantiableB(F(<0*$$(5$(&/&Q5&$8*&85*A0#(5G(/AD#<8$(/G(83*8(<0*$$(<*&(A#(<+#*8#?1
Abstract ClassB(F(<0*$$(83*8(</&8*5&$(*A$8+*<8(>#83/?$(5$(*&(*A$8+*<8(<0*$$1(;3#(<0*$$(3#*?#+(/G(*&(*A$8+*<8(<0*$$
&##?$(8/(</&8*5&(83#(,#"@/+?(*A$8+*<81(FA$8+*<8(<0*$$#$(>*"(&/8(A#(5&$8*&85*8#?(*&?(+#d%5+#($%A<0*$$#$(8/(J+/H5?#
5>J0#>#&8*85/&$(G/+(83#(*A$8+*<8(>#83/?$1
InterfaceB(F&(5&8#+G*<#(</&8*5&$(&/(*88+5A%8#$(A%8(/&0"(J%A05<(*A$8+*<8(>#83/?$1(F&(5&8#+G*<#(<*&&/8(A#
5&$8*&85*8#?1
Interface ImplementationB(F(<0*$$(5>J0#>#&8$(*&(5&8#+G*<#(A"(J+/H5?5&'(5>J0#>#&8*85/&$(/G(*8(0#*$8(/&#(/G(83#
*A$8+*<8(>#83/?$(</&8*5&#?(5&(83#(5&8#+G*<#1
Interface InheritanceB(;3#+#(5$(*0$/(5&3#+58*&<#(+#0*85/&$35J(*>/&'(5&8#+G*<#$-(D%$8(05,#(*>/&'(<0*$$#$1
Dynamic BindingB(=&(?"&*>5<(A5&?5&'-(83#(>#83/?(<*00(5$(A/&?#?(8/(83#(>#83/?(A/?"(*8(+%&85>#1(;35$(5$(*0$/
,&/@&(*$(0*8#(A5&?5&'1
PolymorphismB()/0">/+J35$>(>#*&$(83*8(*(+#G#+#&<#(H*+5*A0#(<*&(3/0?(+#G#+#&<#$(8/(?5GG#+#&8(8"J#$(/G(/AD#<8$1
V/+(#c*>J0#-(*(+#G#+#&<#(H*+5*A0#(</%0?(+#G#+(8/(/AD#<8$(83*8(A#0/&'(8/(*&"(/G(58$($%A<0*$$#$1(F(+#G#+#&<#(H*+5*A0#
<*&(*0$/(3/0?(+#G#+#&<#$(8/(/AD#<8$(A#0/&'5&'(8/(?5GG#+#&8(<0*$$#$(83*8(5>J0#>#&8(83#($*>#(5&8#+G*<#1
Abstract MethodB(F&(*A$8+*<8(>#83/?(5$(*(>#83/?(83*8(5$(?#<0*+#?-(A%8(</&8*5&$(&/(5>J0#>#&8*85/&1
Non-instantiableB(F(<0*$$(5$(&/&Q5&$8*&85*A0#(5G(/AD#<8$(/G(83*8(<0*$$(<*&(A#(<+#*8#?1
Abstract ClassB(F(<0*$$(83*8(</&8*5&$(*A$8+*<8(>#83/?$(5$(*&(*A$8+*<8(<0*$$1(;3#(<0*$$(3#*?#+(/G(*&(*A$8+*<8(<0*$$
&##?$(8/(</&8*5&(83#(,#"@/+?(*A$8+*$81(FA$8+*<8(<0*$$#$(>*"(&/8(A#(5&$8*&85*8#?-(*&?(+#d%5+#($%A<0*$$#$(8/(J+/H5?#
5>J0#>#&8*85/&$(G/+(83#(*A$8+*<8(>#83/?$1
InterfaceB(F&(5&8#+G*<#(</&8*5&$(&/(*88+5A%8#$(A%8(/&0"(J%A05<(*A$8+*<8(>#83/?$1(F&(5&8#+G*<#(<*&&/8(A#
5&$8*&85*8#?1
Interface ImplementationB(F(<0*$$(5>J0>#&8$(*&(5&8#+G*<#(A"(J+/H5?5&'(5>J0#>#&8*85/&$(/G(*8(0#*$8(/&#(/G(83#
*A$8+*<8(>#83/?$(</&8*5&#?(5&(83#(5&8#+G*<#1
Interface InheritanceB(;3#+#(5$(*0$/(5&3#+58*&<#(+#0*85/&$35J(*>/&'(5&8#+G*<#$-(D%$8(05,#(*>/&'(<0*$$#$1
4/5/21, 10)50 PMIntermediate Programming
Page 72 of 185http://itec2150.gitlab.io/
3.9. Exercises
3.9.1. Exercise 1
4+58#(*(J+/'+*>(8/($8/+#(83#(5&G/+>*85/&(G/+(*(&%>A#+(/G(?5GG#+#&8(G*+>(*&5>*0$(5&(*&(*++*"(/+(*++*"(05$8(*&?(83#&
?5$J0*"(83#($/%&?(83#(*&5>*0$(>*,#1
.%J#+70*$$(V*+>F&5>*0
F88+5A%8#$B(name-(gender-(weight-(*&?(age1
S#83/?$B
7/&$8+%<8/+
'#88#+$(*&?($#88#+$
sY8/.8+5&'(>#83/?(83*8(+#8%+&$(*($8+5&'(5&<0%?5&'(*00(*88+5A%8#$1
F&(*A$8+*<8(>#83/?(feedLoadingSchedule(83*8(?/#$(&/8(3*H#(*&"(J*+*>#8#+$(*&?(+#8%+&$(*($8+5&'1(=8
+#8%+&$(83#(85>#O$R(8/(0/*?(G##?$(#*<3(?*"1(=8(5$(A#88#+(G/+(835$(>#83/?(8/(A#(*A$8+*<8-($5&<#(/&0"(*(</&<+#8#
*&5>*0(8"J#(<*&(*<8%*00"(,&/@(83#(85>#O$R1
.%A<0*$$#$(735<,#&-(7/@-(2%<,1
F88+5A%8#$B(;3#"($3/%0?(#*<3(</&8*5&(*&(*88+5A%8#(sound1
S#83/?$B
b*<3(</&8*5&(*(</&$8+%<8/+(*&?('#88#+P$#88#+(>#83/?$1
UH#++5?#(83#(toString(>#83/?1
UH#++5?#(83#(feedLoadingSchedule(>#83/?1
!5&8B(V/+(A/83(83#(toString(*&?(feedLoadingSchedule(>#83/?$-(%$#(83#(/%8J%8(/G(83#(J+/'+*>(O$##
A#0/@R(8/(?#8#+>5&#(83#(G/+>*8(/G(83#($8+5&'(8/(+#8%+&1
FJJ05<*85/&B(4+58#(*(J+/'+*>(<*00#?(MyFarm1
7+#*8#(83#(G/00/@5&'($5c(/AD#<8$-($8/+#(83#>(5&(*(FarmAnimal(*++*"(/+(*++*"(05$81
)+5&8(/%8(83#5+(5&G/+>*85/&-(5&<0%?5&'(83#5+($/%&?$1
)+5&8(/%8(83#(G##?5&'($<3#?%0#(G/+(#*<3(*&5>*01
;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>1
4/5/21, 10)50 PMIntermediate Programming
Page 73 of 185http://itec2150.gitlab.io/
3.9.2. Exercise 2
4+58#(*(J+/'+*>(8/(J+/<#$$(?5GG#+#&8(/AD#<8$(83*8(<*&(G0"(5&(*($"$8#>*85<(>*&88#+(83+/%'3(83#($*>#(5&8#+G*<#(83#"
5>J0#>#&81
=&8#+G*<#(FlightB(7/&8*5&(/&0"(/&#(H/5?(>#83/?(G0"OR(O&/(J*+*>#8#+$R1
;@/(&/&Q*A$8+*<8(<0*$$#$(Airplane(*&?(Bird(83*8(5>J0#>#&8(83#(Flight(5&8#+G*<#1
Airplane(<0*$$B
;3+##(*88+5A%8#$B(>/?#0(*&?("#*+(A%508
Bird(<0*$$B
U&#(*88+5A%8#B(8"J#
`#$5?#$(83#(</&8+%<8/+$(*&?('#88#+$P$#88#+$-(A/83(83#(Airplane(*&?(Bird(<0*$$#$($3/%0?(5>J0>#&8(83#(fly(>#83/?1
;3#(F5+J0*&#m$(G0"(>#83/?($3/%0?(J+5&8(r=m>(*&(*5+J0*&#(83*8(+#05#$(/&(*&(#&'5&#(8/(G0"1r(;3#(`5+?m$(G0"(>#83/?
$3/%?(J+5&8(r=m>(*(A5+?(@3/(G0*J$(@5&'$(8/(G0"1r(b*<3($3/%0?(*0$/(5&<0%?#(*(toString()(>#83/?1
!5&8B(V/+(A/83(83#(toString(*&?(feedLoadingSchedule(>#83/?$-(%$#(83#(/%8J%8(/G(83#(J+/'+*>(O$##(A#0/@R(8/
?#8#+>5&#(83#(G/+>*8(/G(83#($8+5&'(8/(+#8%+&1
FJJ5<*85/&(J+/'+*>(ThingsThatFly
7+#*8#(/&#(Airplane(/AD#<8(*&?(8@/(Bird(/AD#<8$1(;3#(J+/'+*>(>%$8($8/+#(83#$#(/AD#<8$(5&(/&#(*++*"(/+(*++*"
05$81
;3#(J+/'+*>(>%$8(%$#(*(0//J(8/(J+5&8(83#(/AD#<8$(*&?(3/@(83#"(G0"1(9/%+(J+/'+*>($3/%0?(3*H#(83#(G/00/@5&'
/%8J%8B
3.9.3. Exercise 3
Duck Quack Quack [name=Donald, gender=male, weight=3.2, age=5]
Duck Quack Quack [name=Cheese, gender=female, weight=3.6, age=5]
Cow Moo Moo [name=Molly, gender=female, weight=1600.0, age=3]
Chicken Cock-a-Doodle-doo [name=Albert, gender=male, weight=1.6, age=2]
Chicken Cluck Cluck [name=Amelia, gender=female, weight=1.8, age=4]
Chicken Cluck Cluck [name=Dixie, gender=female, weight=1.7, age=4]
Donald: 8am-12pm-6pm
Cheese: 8am-12pm-6pm
Molly: 6am-4pm
Albert: 8am-4pm
Amelia: 8am-4pm
Dixie: 8am-4pm
Airplane [model=Boeing 747, year=2016]: I'm an airplane that relies on an engine to fly.
Bird [type=Eagle]: I'm a bird who flaps my wings to fly.
Bird [type=Hummingbird]: I'm a bird who flaps my wings to fly.
4/5/21, 10)50 PMIntermediate Programming
Page 74 of 185http://itec2150.gitlab.io/
;35$(#c#+<5$#(#c8#&?$(bc#+<5$#(E1(2#G5&#(*&/83#+(5&8#+G*<#(Movement(83*8(#c8#&?$(83#(5&8#+G*<#(Flight(G+/>
bc#+<5$#(E1(=8(</&8*5&$(8@/(&#@(*A$8+*<8(>#83/?$(walk()(*&?(jump()1(`/83(3*H#(&/(J*+*>#8#+$(*&?(&/(+#8%+&
H*0%#$1(j/@(>*,#(83#(Airplane(*&?(Bird(<0*$$#$(5>J0#>#&8(83#(Movement(5&8#+G*<#1(;3#(J+/'+*>(83*8(</&8*5&$
83#(>*5&(>#83/?($3/%0?(A#(<*00#?(*ThingsThatMoves1 ( =8 ( $3 / %0 ? (< + #* 8 #( 8 3# ( $* > #( / AD # <8 $ ( *$ ( 5& ( bc # +< 5 $# ( E( * &? ( $8 / +#
83#>(5&(*&(*++*"1(9/%+(J+/'+*>($3/%0?(3*H#(83#(G/00/@5&'(/%8J%8B
Airplane [model=Boeing 747, year=2016]:
I rely on my engine to fly.
I tax on my wheels.
I cannot jump.
Bird [type=Eagle]:
I flap my wings to fly.
I walk on my feet.
I jump by leaping from my feet.
Bird [type=Hummingbird]:
I flap my wings to fly.
I walk on my feet.
I jump by leaping from my feet.
4/5/21, 10)50 PMIntermediate Programming
Page 75 of 185http://itec2150.gitlab.io/
4. Exceptions
4.1. Learning Outcomes
2#$<+5A#(@3*8(*(6*H*(#c<#J85/&(5$(*&?(3/@(58(5$(%$#?(5&(/AD#<8Q/+5#&8#?(J+/'+*>>5&'
!*&?0#(6*H*(#c<#J85/&$(%$5&'(8+"P<*8<3($8*8#>#&8$
25GG#+#&85*8#(A#8@##&(3*&?05&'(*&(#c<#J85/&(*&?($5>J0"(r83+/@5&'r(/+(0#885&'(5$(3*JJ#&
2#$5'&(#GG#<85H#(#c<#J85/&(3*&?05&'
Z$#(*H*50*A0#(6*H*(bc<#J85/&(<0*$$#$(*H*50*A0#(5&(83#(6*H*(F)=
7+#*8#(*&?(%$#(*(<%$8/>(#c<#J85/&
4.2. Resources
4.2.1. Text and Tutorials
=&8+/?%<85/&(8/()+/'+*>>5&'(Z$5&'(6*H*Q(b5'383(b?585/&(A"(2*H5?(61(b<,B(73*J8#+([
O388JBPP>*8313@$1#?%PD*H*&/8#$P<[P5&?#c138>0R(73*J8#+([(Q(7/++#<8&#$$-(l/A%$8&#$$-(bGG5<5#&<"
6*H*-(6*H*-(6*H*B(UAD#<8QU+5#&8#?()+/A0#>(./0H5&'(AA"(l*0J3(S/+#005(*&?(l*0J3(4*0?#B
388JBPP@@@1<$18+5&</001#?%Pt+*>PDDDPDDDQ/$QETCWTMEL1J?G(73*J8#+(CT(bc<#J85/&$B(43#&(;35&'$(q/(4+/&'
U+*<0#(6*H*(;%8/+5*0(N#$$/&(/&(bc<#J85/&$B
388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0P#$$#&85*0P#c<#J85/&$P5&?#c138>0
4.2.2. Videos
;3#(;3#/+"(/G(bc<#J85/&$(h5?#/(B(388J$BPP@@@1"/%8%A#1</>P@*8<3\H][4;hN*Cu8$,
)+*<85<*0(Z$#(/G(bc<#J85/&$(h5?#/B(388J$BPP@@@1"/%8%A#1</>P@*8<3\H]l+_>@N`bHQZk8]ET$
F&/83#+(bc<#J85/&$(h5?#/B(388J$BPP@@@1"/%8%A#1</>P@*8<3\H]_YQIUN,u,a9
4.3. Overview
43#&(+%&&5&'("/%+(J+/'+*>-(58(5$(&/8(%&</>>/&(8/(+%&(5&8/(J+/A0#>$(@35<3(<*%$#(83#(J+/'+*>(8/(8#+>5&*8#(/+
$8/J(+%&&5&'1(43#&(83#$#(8"J#$(/G(#++/+$(*+#(?#8#<8#?(5&(6*H*(J+/'+*>$-(83#(+%&Q85>#(#&H5+/&>#&8(<+#*8#$(*&
/AD#<8(,&/@&(*$(*&(exception1(F&/83#+(?#G5&585/&(G/+(*&(#c<#J85/&(5$(*(+%&Q85>#(#++/+(83*8(<*%$#$(*(J+/'+*>(8/
<+*$31(;3#(&*>#(*0$/(5>J05#$(83*8(58(5$(*($58%*85/&(83*8(5$(&/8(&/+>*0(#c#<%85/&1(;3#(6*H*(F)=(</&8*5&$(*($#8(/G
<0*$$#$(%$#?(*$(8#>J0*8#$(G/+(83#(/AD#<8$(<+#*8#?(@3#&(83#(#++/+$(*+#(?#8#<8#?1(=&(83#(J*<,*'#(D*H*10*&'-(83#$#
<0*$$#$(*+#($/>#85>#$(<*00#?(83#(#c<#J85/&(<0*$$#$1(=&(*&(5?#*0(@/+0?-(&/(J+/'+*>(@/%0?(#&</%&8#+(#c<#J85/&$1
!/@#H#+-(@#(?/&m8(05H#(5&(*&(5?#*0(@/+0?($/(83#(6*H*(0*&'%*'#(</&8*5&$($8*8#>#&8$(*&?(</&$8+%<8$(83*8(*00/@(%$(8/
r3*&?0#r(#c<#J85/&$(@3#&(83#"(/<<%+1(!*&?05&'(*&(#c<#J85/&(>#*&$(83*8(83#(J+/'+*>(</&8*5&$(</?#(8/(#583#+
</++#<8(*&(#c<#J85/&(/+(J+/H5?#(*&(*08#+&*85H#(8/(*00/@5&'(83#(J+/'+*>(8/(<+*$31(bc<#J85/&$(<*&(A#(<*%$#?(A"
#++/+$('#&#+*8#?(A"(#c8#+&*0(5&J%8$(/+($5>J0"(*(A*?(J+/'+*>(?#$5'&1(6*H*(%$#$(83#(8#+>(r83+/@r(8/(?#$<+5A#(@3*8
3*JJ#&$(@3#&(*&(#c<#J85/&(/<<%+$1(;3#(+%&Q85>#(#&H5+/&>#&8(5$($*5?(8/(83+/@(*&(#c<#J85/&(@3#&(58(/<<%+$1(43*8
5$(*<8%*00"(3*JJ#&5&'(5$(83*8(83#(+%&Q85>#(#&H5+/&>#&8(5$('*83#+5&'(5&G/+>*85/&(*A/%8(83#(8"J#(/G(#++/+(83*8
4/5/21, 10)50 PMIntermediate Programming
Page 76 of 185http://itec2150.gitlab.io/
/<<%++#?(*&?(83#(05&#(*8(@35<3(58(/<<%++#?(*&?($8/+5&'(83*8(5&G/+>*85/&(5&8/(*&(/AD#<8(83*8(5$(?#+5H#?(G+/>(83#(6*H*
#c<#J85/&(<0*$$#$1(;3#(J+/'+*>>#+(<*&(*0$/(5&<0%?#(</?#(@35<3(*00/@$(G/+(*&(#c<#J85/&(8/(A#(83+/@&(A"(83#
J+/'+*>(58$#0G(%&?#+(<#+8*5&(<5+<%>$8*&<#$1(F&(%&3*&?0#?(#c<#J85/&(@500(<*%$#(83#(+%&&5&'(J+/'+*>>5&'(8/
8#+>5&*8#(*&?(J+5&8(*($8*<,(8+*<#1(;/(3#0J(J+#H#&8(835$-(83#(J+/'+*>>#+(<*&(5&<0%?#(8+"P<*8<3(A0/<,$(@35<3(*00/@
83#(J+/'+*>>#+(8/(J+/H5?#(*&(*08#+&*8#(<3/5<#(8/(#&?5&'(83#(J+/'+*>1(;35$(5$(,&/@&(*$(r3*&?05&'r(83#(#c<#J85/&1
=&(*??585/&-(58(5$(J/$$5A0#(8/(@+58#(</?#(@35<3(<+#*8#$-(83+/@$(*&?(3*&?0#$(<%$8/>(#c<#J85/&$(@35<3(5&3#+58(G+/>
83#(F)=(J+/H5?#?(#c<#J85/&(<0*$$#$1(;35$(<3*J8#+(?#$<+5A#$(</>>/&(6*H*(F)=(#c<#J85/&$-(3/@(8/(J+/'+*>>*85<*00"
83+/@(#c<#J85/&$-(3/@(8/(3*&?0#(#c<#J85/&$(*&?(3/@(8/(<+#*8#("/%+(/@&(<%$8/>5a#?(#c<#J85/&$1
4.4. Exception Handling
;3#(5?#*(/G(3*&?05&'(J+/A0#>$(?%+5&'(#c#<%85/&(3*$(A##&(*(J*+8(/G(J+/'+*>>5&'($5&<#(83#+#(3*$(A##&
J+/'+*>>5&'1(!/@#H#+-(6*H*(@*$(/&#(/G(83#(G5+$8(0*&'%*'#$(8/(J+/H5?#($J#<5*0($8*8#>#&8$(G/+(83#(3*&?05&'(/G(83#$#
#c<#J85/&*0(J+/A0#>$1(./>#(</>>/&(#++/+$(83*8('#&#+*8#(#c<#J85/&$(*+#(?5H5?#(A"(a#+/(#++/+$(*&?(*++*"(5&?#c
/%8(/G(A/%&?$1(F(J$#%?/</?#($/0%85/&(8/(J+#H#&8(835$(#++/+(@583/%8($J#<5*0($8*8#>#&8$(5$($3/@&(A#0/@1
*Divide by Zero Exception Handling Pseudo Code*
if (denominator not equal to 0)
answer = numerator/denominator;
else
inform user that division is not possible due to divide by zero
;35$(8+*?585/&*0(*JJ+/*<3(5&</+J/+*8#$(83#(3*&?05&'(/G(83#(#++/+(5&8/(83#(&/+>*0(#c#<%85/&(/G(83#(</?#1(6*H*(3*$
5&</+J/+*8#?(*&(#c<#J85/&Q3*&?05&'(>/?#0(5&8/(83#(</?#(58$#0G1(=&(83#(<*$#(/G(*(?5H5?#QA"Qa#+/(#++/+(/<<%++5&'
@583/%8(#c<#J85/&(3*&?05&'(</?#-(83#(+%&Q85>#(#&H5+/&>#&8(?#8#<8$(83#(#++/+(*&?(*A/+8$(83#(J+/'+*>1(;3#&(58
J+5&8$(*($8*<,(8+*<#1(F&(#c*>J0#($8*<,(8+*<#(G/+(*(?5H5?#QA"Qa#+/(#++/+(5$($3/@&(A#0/@1
java.lang.ArithmeticException: / by zero
at DivideException.main(DivideException.java:10)
F$("/%(<*&($##-(83#($8*<,(8+*<#(G5+$8(?#$<+5A#$(83#(#c<#J85/&(83*8(/<<%+$(*&?(83#&(05$8$(83#(<*00($8*<,(#&?5&'(@583(83#
05&#(&%>A#+(*&?(>#83/?(&*>#(@3#+#(83#(#c<#J85/&(/<<%+$1(=&(835$(<*$#-(83#(#++/+(/<<%++#?(5&(83#(>*5&(>#83/?(/G
83#(D*H*(G50#(25H5?#bc<#J85/&1D*H*(/&(05&#(CT1(;3#($8*<,(8+*<#(5$(5&8#&?#?(8/(3#0J(83#(J+/'+*>>#+(?#A%'(83#
J+/A0#>(*&?(5$(&/8(</&$5?#+#?(*(H#+"('//?(#++/+(>#$$*'#(G/+(*(<*$%*0(%$#+1
;3#(*<8(/G(?#8#<85&'(83#(#++/+(*&?(<*%$5&'(83#(J+/'+*>(8/(*A/+8(A"(?#G*%08(5$(,&/@&(*$(r83+/@5&'r(83#(#c<#J85/&1
;3#(#c<#J85/&($3/@&(Q(D*H*10*&'1F+583>#85<bc<#J85/&(Q(5$(/&#(/G(>*&"(J+/H5?#?(5&(83#(6*H*(F)=1(b*<3(#c<#J85/&(5$(*
<0*$$(5&(83#(6*H*(05A+*+"1(;3#(?5*'+*>(A#0/@($3/@$(*(ZSN(?5*'+*>(/G(83#(#c<#J85/&(<0*$$#$(5&(83#(6*H*(05A+*+"1(F$
"/%(<*&($##(5&(83#(?5*'+*>-(*00(83#(bc<#J85/&$(5&3#+58(G+/>(83#(<0*$$(;3+/@*A0#1
Exception Hierarchy
4/5/21, 10)50 PMIntermediate Programming
Page 77 of 185http://itec2150.gitlab.io/
4.5. Checked Exception vs UnChecked Exception
;3#(#c<#J85/&(<0*$$#$(*+#(?5H5?#?(5&8/(<3#<,#?(*&?(%&<3#<,#?(#c<#J85/&$1(;3#(%&<3#<,#?(#c<#J85/&$(5&3#+58(G+/>
83#(<0*$$(l%&;5>#bc<#J85/&1(Z&<3#<,#?(>#*&$(83*8(83#(</>J50#+(@500(&/8(+#d%5+#(#c<#J85/&(3*&?05&'(</?#(*+/%&?
*($8*8#>#&8(83*8(3*$(83#(J/8#&85*0(8/(83+/@&(*&(#c<#J85/&1(25H5?#(A"(a#+/(5$(*&(#c*>J0#(/G(*&(%&<3#<,#?
#c<#J85/&1(=G(58(@#+#(<3#<,#?-(#H#+"(?5H5$5/&($8*8#>#&8(@/%0?(&##?(8/(3*H#(#c<#J85/&(3*&?05&'(</?#1(Z&<3#<,#?
#c<#J85/&$(*+#(8"J5<*00"(</&?585/&$(83*8(83#(J+/'+*>(<*&(&/8(*&85<5J*8#(/+(+#</H#+(G+/>1(S*&"(/G(83#$#(*+#
5&?5<*85/&$(/G(*(0/'5<(#++/+(5&(83#(</?#(83*8($3/%0?(A#(</++#<8#?(A"(<3*&'5&'(83#(</?#1(V/+(#c*>J0#-(J*$$5&'(*&
/AD#<8(+#G#+#&<#(H*+5*A0#(8/(*(>#83/?(A#G/+#(58(5$(5&585*05a#?(@500(+#$%08(5&(*(j%00)/5&8#+bc<#J85/&(A#5&'('#&#+*8#?1
;3#(J+/'+*>>#+($3/%0?(>*,#($%+#(83*8(83#(+#G#+#&<#(H*+5*A0#(5$(5&85*05a#?(A#G/+#(%$5&'(581(73#<,#?(#c<#J85/&$(*+#
</&?585/&$(83*8(*(@#00Q@+588#&(J+/'+*>($3/%0?(*&85<5J*8#(*&?(J0*&(G/+1(S*&"(/G(83#(<3#<,#?(#c<#J85/&$(5&H/0H#(G50#
J+/<#$$5&'(*&?(*+#(%$#?(J+/>5&*&80"(5&(83#(&#c8(<3*J8#+(/&(G50#(J+/<#$$5&'1(F(<*00(8/(*(>#83/?(83*8(5$(<*J*A0#(/G
83+/@5&'(*&(#c<#J85/&$(@500(+#<#5H#(</>J50#+(#++/+$(5G(58(?/#$(&/8(%$#(#c<#J85/&(3*&?05&'(</?#1(7%$8/>(?#$5'&#?
#c<#J85/&(<*&(A#(#583#+(<3#<,#?(/+(%&<3#<,#?(?#J#&?5&'(%J/&(@35<3(J*+#&8(#c<#J85/&(<0*$$(83#"(5&3#+58(G+/>1
4.6. Try/Catch/Finally Blocks
bc<#J85/&(3*&?05&'(</?#(<*&(A#(0//,#?(*8(*$(3*H5&'(83+##(8"J#$(/G(/J#+*85/&$1(s2#<0*+5&'(*&(#c<#J85/&(s;3+/@5&'
*&(#c<#J85/&(s7*8<35&'(*&(#c<#J85/&
4.6.1. Declaring an exception
F00(#c#<%8*A0#(</?#(5&(6*H*(A#0/&'$(8/(*(>#83/?1(=G(*(>#83/?(5$(<*J*A0#(/G(83+/@5&'(*(<3#<,#?(#c<#J85/&-(58
</&8*5&$(*(throws($8*8#>#&8(5&(58$(3#*?#+1(V/+(#c*>J0#-(*(>#83/?(@35<3(83+/@$(*&(/AD#<8(/G(8"J#(=Ubc<#J85/&
@/%0?(0//,(05,#(835$B(public void aMethod(int a) throws IOException
;35$(?/#$(&/8(>#*&(83#(*S#83/?(@500(*0@*"$(83+/@(*&(IOException-(58(>#*&$(83*8(83#(>#83/?(5$(<*J*A0#(/G(?/5&'
$/1
4.6.2. Throwing an exception
4/5/21, 10)50 PMIntermediate Programming
Page 78 of 185http://itec2150.gitlab.io/
=&(/+?#+(G/+(83*8(#++/+(8/(A#(83+/@&-(83#+#(>%$8(A#(</?#(#583#+(5&(*S#83/?(/+(/&#(/G(83#(>#83/?$(58(<*00$(83*8
?#8#<8$(*&(#++/+-(<+#*8#$(83#(#c<#J85/&(/AD#<8(*&?(83#&(83+/@$(83#(#c<#J85/&1(.%JJ/$#(83*8(83#(J+/'+*>(?#8#<8$
83*8(*&(*+'%>#&8(J*$$#?(8/(83#(>#83/?(5$(/G(5&</++#<8(8"J#(/+(H5/0*8#$(83#(>#83/?(</&8+*<8(O*(&#'*85H#(H*0%#
+#<#5H#?(@3#&(*(J/$585H#(/&#(5$(#cJ#<8#?R1(=8(</%0?(83#&(83+/@(*&(=00#'*0F+'%>#&8bc<#J85/&(/AD#<81(;3#(</?#
A#0/@$($3/@(3/@(83#(#c<#J85/&(5$(<+#*8#?(*&?(83+/@&1(;3#(</?#($3/@&(*$$%>#$(83*8(83#(<0*$$
java.lang.IllegaalArgumentException(3*$(A##&(J+/J#+0"(5>J/+8#?(5&8/(83#(6*H*(G50#(</&8*5&5&'(83#(</?#1
;5JB(;3#(,#"@/+?(8/(?#<0*+#(*&(#c<#J85/&(5$(throws(*&?(83#(,#"@/+?(8/(83+/@(*&(#c<#J85/&(5$(throw1(;3#$#(*+#
$5>50*+-(A%8(?/(?5GG#+#&8(835&'$1
4.6.3. Catching an exception
=G(*(J+/'+*>>#+(?/#$(&/8(@*&8(83#(#c<#J85/&(8/(8#+>5&*8#(83#(J+/'+*>-(*8($/>#(J/5&8(83#(#c<#J85/&(>%$8(A#
<*%'38(5&(*(try-catch(A0/<,1(;3#(try-catch(A0/<,(*00/@$(83#(J+/'+*>>#+(8/($#J*+*8#(83#(</?#(83*8(#c#<%8#$(@3#&
&/(#c<#J85/&(/<<%+$(G+/>(83#(#++/+(3*&?05&'(</?#1(;3#(try(A0/<,(</&8*5&$(83#(?#$5+#?(#c#<%85/&(/G(83#(</?#1(;3#
catch(A0/<,(</&8*5&$(83#(</?#(8/(3*&?0#(*&(#++/+(5G(*&?(@3#&(58(/<<%+$1(=G(&/(#c<#J85/&$(/<<%+(?%+5&'(83#(#c#<%85/&
/G(83#(try(A0/<,-(83#(catch(A0/<,(</?#(5$(5'&/+#?1
;3#(</?#(@5835&(83#(<*8<3(A0/<,(/+(A0/<,$(5$(83#(#c<#J85/&(3*&?05&'(</?#1(=G(&/(3*&?0#+(#c5$8$(G/+(83#(#c<#J85/&(83*8
/<<%+$-(83#(#c<#J85/&(@500(+#$%08(5&(83#(J+/'+*>(8#+>5&*85&'1(=&(83#(#c*>J0#(*A/H#-(83#+#(*+#(8@/(<*8<3(A0/<,$(G/+
83#($5&'0#(8+"(A0/<,1(."&8*c(/&0"(+#d%5+#$(/&#(<*8<3(A0/<,-(A%8(58(5$(/G8#&(%$#G%0(8/(A#(*A0#(8/(?/(?5GG#+#&8(835&'$(G/+
?5GG#+#&8(8"J#$(/G(#c<#J85/&$1(=8(5$(*0$/(J/$$5A0#(8/(%$#(5&3#+58*&<#(8/(<*8<3(>%085J0#(#c<#J85/&$(5&(*($5&'0#(<*8<3
A0/<,1
IllegalArgumentException ex = new IllegalArgumentException("Negative argument received");
throw ex;
trytry{
// code that could potentially thrown an exception
}
catchcatch (NullPointerException e)
{
// code that is called if a NullPointerException occurs in the code encased in the try block
}
catchcatch (ArithmeticException e2)
{
// code that is called if an ArithmeticException occurs in the code encased in the try block
}
JAVA
trytry
{
// code that could potentially throw an exception
}
catchcatch (Exception ex)
{
// this will catch any exception thrown by the program since all Exception types inherit from the parent
class named Exception.
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 79 of 185http://itec2150.gitlab.io/
=G(%$5&'(>%085J0#(<*8<3(A0/<,$Q(A#($%+#(83#(>/+#('#&#+*0(bc<#J85/&(<0*$$(5$(0*$81
F&(#c<#J85/&(/AD#<8(</&8*5&$(H*0%*A0#(5&G/+>*85/&(*A/%8(@3*8(3*JJ#&#?(8/(<*%$#(581(F00(#c<#J85/&$(5&3#+58(83#
G/00/@5&'(>#83/?$(G+/>(83#(J*+#&8(<0*$$(;3+/@*A0#1
F($8*<,(8+*<#(5$($/>#85>#$(<*00#?(*($8*<,(A*<,8+*<#(/+(#H#&(D%$8(*(A*<,8+*<#1(;3#($8*<,(8+*<#5$(*(05$8(/G($8*<,(G+*>#$1
F($8*<,(G+*>#(5&?5<*8#$(*(>/>#&8(?%+5&'(*&(*JJ05<*85/&m$(#c#<%85/&(@3#&(*(>#83/?(5$(<*00#?1(F($8*<,(G+*>#
</&8*5&$(5&G/+>*85/&(*A/%8(@3#+#(83#(>#83/?(@*$(<*00#?(G+/>(5&(83#(6*H*($/%+<#(</?#1(./(83#(6*H*($8*<,(8+*<#
'#&#+*8#?(@3#&(*&(#c<#J85/&(5$(<*00#?(5$(*(05$8(/G(G+*>#$(83*8($8*+8$(*8(05&#(5&(83#(>#83/?(83#(#c<#J85/&(/<<%++#?
*&?(#c8#&?$(A*<,(8/(@3#&(83#(J+/'+*>($8*+8#?1(4350#(&/8(J*+85<%0*+0"(%$#G%0(8/(J+/'+*>(%$#+-(58(J+/H5?#$(H*0%*A0#
5&G/+>*85/&(8/(83#(J+/'+*>>#+(*A/%8(@3#+#(83#(#c<#J85/&(/<<%++#?1
4.7. Handling Exception vs Throwing Exception
43#&(*(>#83/?(83+/@$(*(<3#<,#?(#c<#J85/&(/+(<*00$(*&/83#+(>#83/?(83*8(?/#$-(83#(</>J50#+(@500(&/8(</>J50#(83#
</?#(%&0#$$(83#(#c<#J85/&(5$(#583#+(83+/@&(/+(3*&?0#?1(;/(3*&?0#(83#(#c<#J85/&-(83#(>#83/?(>%$8(</&8*5&(*
8+"P<*8<3(A0/<,(*$(?#$<+5A#?(5&(83#(J+#H5/%$($#<85/&1(;3+/@5&'(*&(#c<#J85/&(J*$$#$(83#(A%+?#&(/G(3*&?05&'(58(8/(83#
// wrong way to do it
trytry
{
// code
}
catchcatch (Exception ex)
{
// this block will catch all exceptions since all handlable exception inherit from Exception
}
catchcatch (NullPointerException e)
{
// a waste of code- this will never be called because the NullPointerException inherits from Exception
}
JAVA
// right way
trytry
{
// code
}
catchcatch (NullPointerException e)
{
// This will handle any NullPointerExceptions
}
catchcatch (Exception ex)
{
// this block will catch any other exceptions that occur - order matters
}
JAVA
String getMessage() - this method returns a string identifying the type of exception that occurred.
void printStackTrace() - this method prints current stack trace to the console and the type of exception thrown.
4/5/21, 10)50 PMIntermediate Programming
Page 80 of 185http://itec2150.gitlab.io/
<*005&'(>#83/?1(=G(835$(5$(83#(>*5&OR(>#83/?-(83#+#(5$(&/(<*005&'(>#83/?(*&?(83#(#c<#J85/&(<*%$#$(J+/'+*>
8#+>5&*85/&1(;/(83+/@(*&(#c<#J85/&-(83#(>#83/?(>%$8(%$#(83#(,#"(@/+?(throws(5&(83#(>#83/?(3#*?#+1
4.8. Custom Exceptions
=8(5$(J/$$5A0#(8/(<+#*8#(*&?(83+/@(<%$8/>5a#?(#c<#J85/&$(G/+(*(J+/'+*>1(;/(?/(835$-(83#(G5+$8($8#J(5$(8/(<+#*8#(83#
<%$8/>(#c<#J85/&1(F&(#c<#J85/&(5$(*(6*H*(<0*$$(*&?(58(>%$8(5&3#+58(G+/>(/&#(/G(83#(#c5$85&'(6*H*(bc<#J85/&(<0*$$#$
G+/>(83#(6*H*(F)=1
U&<#(83#(<%$8/>(#c<#J85/&(<0*$$(5$(<+#*8#?(*&?(</>J50#?-("/%+(J+/'+*>(<*&(83#&(#cJ05<580"(<*%$#(83*8(#c<#J85/&(8/
3*JJ#&1
4.9. An example of Exception Handling used to validate input from user
F(</>>/&(%$#(/G(#c<#J85/&(3*&?05&'(5$(8/(>*,#($%+#(83*8(83#(%$#+(#&8#+$(83#(8"J#(/G(?*8*(?#$5+#?(@3#&(+#*?5&'
G+/>(83#(</&$/0#1(43#&(%$5&'(.<*&&#+(/AD#<8$(8/(+#*?(%$#+(5&J%8-(*(.8+5&'(5&J%8(@3#&(+#d%#$85&'(*(&%>#+5<(5&J%8
<*&('#&#+*8#(*&(=&J%8S5$>*8<3bc<#J85/&(<*%$5&'(83#(J+/'+*>(8/(*A+%J80"(8#+>5&*8#1(bc<#J85/&(3*&?05&'(<*&
J+#H#&8(83#(8#+>5&*85/&(*&?(*00/@(83#(%$#+(8/(8+"(*'*5&1(;3#(G/00/@5&'(#c*>J0#(*$,$(83#(%$#+(8/(#&8#+(K(&%>A#+$
*&?(83#&(</>J%8#$(83#(*H#+*'#1(;3#(G5+$8(H#+$5/&(3*$(&/(#c<#J85/&(3*&?05&'(*&?(5G(*(0#88#+(5$(#&8#+#?(5&$8#*?(/G(*
&%>A#+Q(83#(J+/'+*>(@500(83+/@(*&(#c<#J85/&(*&?(8#+>5&*8#Q(?/@&0/*?(58(*&?(8+"n
publicpublic intint someMethod() throwsthrows IOException
{
// code that can cause an IOException to occur
}
JAVA
// A custom Exception class
// CustomException.java
public class CustomException extends Exception
{
// custom exceptions must include a constructor that has a single parameter of type String.
public CustomException(String s)
{
// this constructor must call the parent class constructor
super(s)
}
}
publicpublic voidvoid someMethod() throwsthrows CustomException
{
// some code
ifif (bad thing happens)
{
// note that you throw an object of CustomException - not the class so an object must be created.
throwthrow newnew CustomException("A bad thing happened");
}
// some more code that only executes if the bad thing didnt happen
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 81 of 185http://itec2150.gitlab.io/
j/@(0#8(%$(0//,(*8(*&(#c*>J0#(@583(#c<#J85/&(3*&?05&'(83*8($5>J0"(5'&/+#$(83#(#c<#J85/&1
/**
* This class prompts the user for four numbers and then
* computes the average and prints it.
*
*/
importimport java.util.Scannerjava.util.Scanner;
publicpublic classclass AverageCalculator
{
publicpublic staticstatic voidvoid main(String[] args)
{
// declare variables
Scanner consoleInput = newnew Scanner(System.in);
doubledouble[] x = newnew doubledouble[4];
doubledouble average = 0;
doubledouble sum = 0;
// input numbers
forfor (intint i = 0; i < 4; i++)
{
System.out.println("Enter number " + (i + 1) + ":");
x[i] = consoleInput.nextDouble();
sum = sum + x[i];
}
// compute average
average = sum / 4.0;
// output average to console
System.out.println("The average is " + average);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 82 of 185http://itec2150.gitlab.io/
;3#(J+/A0#>(@583(5'&/+5&'(83#(A*?(5&J%8(5$(83*8(83#(%$#+(?/#$(&/8('#8(*(<3*&<#(8/(+##&8#+(83#(</++#<8(H*0%#1(;3*8
H*0%#(5$(J+#$#8(8/(T(*&?($,#@$(83#(*H#+*'#1(N#8m$(0//,(*8(*(835+?(H#+$5/&(83*8(J+/>J8$(83#(%$#+(8/(+##&8#+(83#
>5$8"J#?(H*0%#1
/**
* This class prompts the user for four numbers and then
* computes the average and prints it.
*
*/
importimport java.util.InputMismatchExceptionjava.util.InputMismatchException;
importimport java.util.Scannerjava.util.Scanner;
publicpublic classclass AverageCalculatorIgnore
{
publicpublic staticstatic voidvoid main(String[] args)
{
// declare variables
Scanner consoleInput = newnew Scanner(System.in);
doubledouble[] x = newnew doubledouble[4];
doubledouble average = 0;
doubledouble sum = 0;
// input numbers
forfor (intint i = 0; i < 4; i++)
{
trytry
{
System.out.println("Enter number " + (i + 1) + ":");
x[i] = consoleInput.nextDouble();
sum = sum + x[i];
} catchcatch (InputMismatchException ex)
{
// ignore the error by reading the lingering delimiter
consoleInput.nextLine();
}
}
// compute average
average = sum / 4.0;
// output average to console
System.out.println("The average is " + average);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 83 of 185http://itec2150.gitlab.io/
j/8#(83*8(*00(/G(83#(H*+5*A0#$(%$#?(*+#(?#<0*+#?(A#G/+#(83#(8+"P<*8<3(A0/<,1(;35$(5$(A#<*%$#(*&"(H*+5*A0#(?#<0*+#?(5&
83#(8+"(A0/<,(5$(/&0"(%$*A0#(5&(83#(<%+0"(A+*<#$($%++/%&?5&'(83#(8+"(</?#(?%#(8/(H*+5*A0#($</J#1(43#&(%$5&'
#c<#J85/&(3*&?05&'-(>*,#($%+#(*00(H*+5*A0#$(*+#(?#<0*+#?(A#G/+#(83#(8+"P<*8<3(A0/<,1
4.10. Summary
/**
* This class prompts the user for four numbers and then
* computes the average and prints it.
*
*/
importimport java.util.InputMismatchExceptionjava.util.InputMismatchException;
importimport java.util.Scannerjava.util.Scanner;
publicpublic classclass AverageCalculatorHandle
{
publicpublic staticstatic voidvoid main(String[] args)
{
// declare variables
Scanner consoleInput = newnew Scanner(System.in);
doubledouble[] x = newnew doubledouble[4];
doubledouble average = 0;
doubledouble sum = 0;
// input numbers
forfor (intint i = 0; i < 4; i++)
{
booleanboolean goodValueReceived = falsefalse;
whilewhile (!goodValueReceived)
{
trytry
{
System.out.println("Enter number " + (i + 1) + ":");
x[i] = consoleInput.nextDouble();
sum = sum + x[i];
goodValueReceived = truetrue;
} catchcatch (InputMismatchException ex)
{
// read in the bad value
consoleInput.nextLine();
System.out.println("Sorry- that wasn't a number -try again");
}
}
}
// compute average
average = sum / 4.0;
// output average to console
System.out.println("The average is " + average);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 84 of 185http://itec2150.gitlab.io/
bc<#J85/&$(5&(6*H*(J+/'+*>$(/<<%+(@3#&(*(+%&Q85>#(#++/+(/<<%+$(83*8(83#(6lb(<*&(&/8(+#</H#+(G+/>1
F(6*H*(bc<#J85/&(5$(*&(5&$8*&<#(/G(<0*$$(?#+5H#?(G+/>(D*H*10*&'1;3+/@*A0#
43#&(*(+%&Q85>#(#++/+(/<<%+$(83*8(<*%$#$(*&(bc<#J85/&(<0*$$(8/(A#(5&$8*&85*8#?(*&?(5&8#++%J8(83#(J+/'+*>
#c#<%85/&-(@#($*"(83*8(83#(#c<#J85/&(@*$(thrown
bc<#J85/&$(<*&(A#(#583#+(<3#<,#?(/+(%&<3#<,#?
73#<,#?(#c<#J85/&$(>#*&(83#(</>J50#+(+#d%5+#$(83#(J+/'+*>>#+(8/(*<,&/@0#?'#(83#(J/$$5A5058"(/G(83#
#c<#J85/&(83+/%'3(83#(%$#(/G(*(8+"P<*8<3(A0/<,(8/(3*&?0#(58(/+(*(83+/@$(<0*%$#(/&(83#(>#83/?(5&?5<*85&'(83*8
3*&?05&'(83#(#c<#J85/&(5$(A#5&'(J*$$#?(8/(83#(<*005&'(>#83/?
;3#(</>J50#+(?/#$(&/8(+#d%5+#(83*8(J+/'+*>>#+$(*<,&/@0#?'#(%&<3#<,#?(#c<#J85/&(J/$$5A5058"1
;+"P<*8<3(A0/<,$(*00/@(83#(%$#+(8/(3*&?0#(*&(#c<#J85/&(+*83#+(83*&($5>J0"(0#885&'(58(#&?(83#(J+/'+*>
7%$8/>(#c<#J85/&$(<*&(A#(<+#*8#?(83*8(5&3#+58(G+/>(#c5$85&'(05A+*+"(bc<#J85/&(<0*$$#$1
4.11. Key Terms
bc<#J85/&(Q(F&(#++/&#/%$(/+(*&/>*0/%$(</&?585/&(83*8(</>#$(%J(@3#&(*(J+/'+*>(5$(+%&&5&'1
bc<#J85/&(!*&?05&'(Q(F&(*JJ+/*<3(83*8($#J*+*8#$(*(J+/'+*>m$(&/+>*0(</?#(G+/>(58m$(#++/+Q3*&?05&'(</?#1
;3+/@(Q(;/(83+/@(*&(#c<#J85/&(5$(8/(<+#*8#(*&(#c<#J85/&(/AD#<8(*&?(J*$$(58(/GG(8/(83#(+%&Q85>#(#&H5+/&>#&81(;35$(5$
?/&#(#cJ05<58"(5&(</?#(%$5&'(83#(throw(,#"@/+?1
.8*<,(;+*<#QF($8*<,(8+*<#(5$($/>#85>#$(<*00#?(*($8*<,(A*<,8+*<#(/+(#H#&(D%$8(*(A*<,8+*<#1(;3#($8*<,(8+*<#5$(*(05$8(/G
$8*<,(G+*>#$1(F($8*<,(G+*>#(5&?5<*8#$(*(>/>#&8(?%+5&'(*&(*JJ05<*85/&m$(#c#<%85/&(@3#&(*(>#83/?(5$(<*00#?1(F($8*<,
G+*>#(</&8*5&$(5&G/+>*85/&(*A/%8(@3#+#(83#(>#83/?(@*$(<*00#?(G+/>(5&(83#(6*H*($/%+<#(</?#1(./(83#(6*H*($8*<,
8+*<#('#&#+*8#?(@3#&(*&(#c<#J85/&(5$(<*00#?(5$(*(05$8(/G(G+*>#$(83*8($8*+8$(*8(05&#(5&(83#(>#83/?(83#(#c<#J85/&
/<<%++#?(*&?(#c8#&?$(A*<,(8/(@3#&(83#(J+/'+*>($8*+8#?1
4.12. Exercises
4.12.1. Exercise 1
7+#*8#(*($5>J0#(*??585/&(<*0<%0*8/+(5&(6*H*1(;3#(J+/'+*>($3/%0?(J+/>J8(83#(%$#+(8/(#&8#+(E(5&8#'#+$-(83#&(*??$(83#
&%>A#+$(*&?(J+5&8$(83#(+#$%081(S*,#($%+#(83#(J+/'+*>(5&<0%?#$(*JJ+/J+5*8#(#c<#J85/&(3*&?05&'(5&(<*$#(83#(%$#+
?/#$(&/8(#&8#+(*JJ+/J+5*8#(5&8#'#+(H*0%#$1
4.12.2. Exercise 2
4+58#(*(6*H*(J+/'+*>(8/(+*&?/>0"(<+#*8#(*&(*++*"(/G(LT(?/%A0#(H*0%#$1()+/>J8(83#(%$#+(8/(#&8#+(*&(5&?#c(*&?
J+5&8$(83#(</++#$J/&?5&'(*++*"(H*0%#1(=&<0%?#(#c<#J85/&(3*&?05&'(83*8(J+#H#&8$(83#(J+/'+*>(G+/>(8#+>5&*85&'(5G
*&(/%8(/G(+*&'#(5&?#c(5$(#&8#+#?(A"(83#(%$#+1(O!=j;B(;3#(#c<#J85/&(83+/@&(@500(A#(F++*"=&?#cU%8UG`/%&?$R
4.12.3. Exercise 3
4/5/21, 10)50 PMIntermediate Programming
Page 85 of 185http://itec2150.gitlab.io/
7+#*8#(*(<%$8/>(bc<#J85/&(&*>#?(IllegalTriangleSideException1(7+#*8#(*(<0*$$(&*>#?(;+5*&'0#1(;3#(;+5*&'0#
<0*$$($3/%0?(</&8*5&(I(?/%A0#(H*+5*A0#$(</&8*5&5&'(83#(0#&'83(/G(#*<3(/G(83#(8+5*&'0#$(83+##($5?#$1(7+#*8#(*
</&$8+%<8/+(@583(83+##(J*+*>#8#+$(8/(5&585*05a#(83#(83+##($5?#$(/G(83#(8+5*&'0#1(F??(*&(*??585/&*0(>#83/?(&*>#?
<3#<,.5?#$(@583(>#83/?(3#*?#+(Q(sA//0#*&(<3#<,.5?#$OR(83+/@$(=00#'*0;+5*&'0#.5?#bc<#J85/&(s1(4+58#(</?#($/(83*8
<3#<,.5?#$(>*,#$($%+#(83*8(83#(83+##($5?#$(/G(83#(8+5*&'0#(>##8(83#(J+/J#+(<+58#+5*(G/+(*(8+5*&'0#1(=8(@500(+#8%+&(8+%#
5G(*&?(/&0"(5G(83#($%>(/G($5?#Cv($5?#E(5$('+#*8#+(83*&($5?#I(Fj2(83#($%>($5?#Ev$5?#I(5$('+#*8#+(83*&($5?#C(Fj2(83#
$%>(/G($5?#Cv($5?#I(5$('+#*8#+(83*&($5?#E1(=G(*&"(/G(83/$#(83+##(</&?585/&$(5$(&/8(>#8-(83#(>#83/?(@500(<+#*8#(*&?
83+/@(*&(=00#'*0;+5*&'0#.5?#bc<#J85/&1(F??(*(>*5&(>#83/?(8/(<+#*8#(*&?(<3#<,(8@/(8/(83+##(?5GG#+#&8(8+5*&'0#$1
4.13. Issue Tracker/Comments
=$$%#(;+*<,#+ O388J$BPP'583%A1</>P3J*+,WP3#0JY?#$,P5$$%#$R
4/5/21, 10)50 PMIntermediate Programming
Page 86 of 185http://itec2150.gitlab.io/
5. File Input/Output
5.1. Learning Outcomes
2#$<+5A#(83#(?5GG#+#&<#(A#8@##&(8#c8(*&?(A5&*+"(G50#$
F<<#$$(O/J#&R(G50#$(G/+(+#*?5&'(*&?(@+585&'
N#*+&(83#(</>>/&(G50#(3*&?05&'(#c<#J85/&$(*&?(3/@(8/(3*&?0#(83#>
Z$#(83#(F)=(<0*$$#$(`%GG#+#?V50#l#*?#+(*&?(`%GG#+#?V50#4+58#+(G/+(8#c8(G50#(*<<#$$
Z$#(.<*&&#+(8/(+#*?(*&?(J*+$#(8#c8(G50#$1
Z&?#+$8*&?(83#(J%+J/$#(/G(*&?(%$#(+#'%0*+(#cJ+#$$5/&$
Z$#()+5&84+58#+(8/(/%8J%8(8/(*&?(<+#*8#(8#c8(G50#$1
5.2. Resources
5.2.1. Text and Tutorials
=&8+/?%<85/&(8/()+/'+*>>5&'(Z$5&'(6*H*Q(b5'383(b?585/&(A"(2*H5?(61(b<,B
388JBPP>*8313@$1#?%PD*H*&/8#$P<CCP5&?#c138>0(73*J8#+(CC(Q(=&J%8PU%8J%8(.8+#*>$-(V50#$(*&?(j#8@/+,5&'
6*H*-(6*H*-(6*H*B(UAD#<8QU+5#&8#?()+/A0#>(./0H5&'(AA"(l*0J3(S/+#005(*&?(l*0J3(4*0?#B
388JBPP@@@1<$18+5&</001#?%Pt+*>PDDDPDDDQ/$QETCWTMEL1J?G(73*J8#+(K(*&?(73*J8#+(CC(V50#$(*&?(.8+#*>$
6*H*(F)=(D*H*15/(J*<,*'#(+#G#+#&<#(B
388J$BPP?/<$1/+*<0#1</>P#&PD*H*PD*H*$#PCCP?/<$P*J5PD*H*1A*$#PD*H*P5/PJ*<,*'#Q$%>>*+"138>0
U+*<0#(6*H*(;%8/+5*0(/&(l#'%0*+(bcJ+#$$5/&$B(388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0P#$$#&85*0P+#'#cP5&?#c138>0
5.2.2. Videos
h5?#/(/&(3/@(8/(+#*?(G50#$(%$5&'(.<*&&#+(B(388J$BPP@@@1"/%8%A#1</>P@*8<3\H]Ilj9Z_cF'>@
h5?#/(/&(3/@(8/(@+58#(G50#$(@583()+5&84+58#+(B(388J$BPP@@@1"/%8%A#1</>P@*8<3\
H]`@$^*p%F<?'k05$8])NVbE7bT^2[IbbIbE[k5&?#c][Ck8]T
h5?#/(/&(+#*?5&'(*&?(@+585&'(G50#Q(/&0"(G5+$8(8#&(>5&%8#$(5$(/&(8#c8(G50#$(B(388J$BPP@@@1"/%8%A#1</>P@*8<3\
H]YD37H"[Y0qb
5.3. Overview
7/>J%8#+(G50#$(*+#(%$#?(8/($8/+#(</00#<85/&$(/G(?*8*(/&(*(</>J%8#+($8/+*'#(?#H5<#($%<3(*$(*(3*+?(?+5H#1(6*H*
</&8*5&$(*($#+5#$(/G(F)=(<0*$$#$(8/(*5?(5&(83#(+#*?5&'(*&?(@+585&'(/G(?*8*(8/(*&?(G+/>(83#$#(G50#$1(=&(835$(<3*J8#+-(@#
?5$<%$$(8#c8(G50#(5&J%8(*&?(/%8J%81(;#c8(G50#$(<*&(A#(H5#@#?(*$(</>J%8#+(G50#$(</&8*5&5&'(*(</00#<85/&(/G(Z&5</?#(O/+
*($%A$#8(/G(Z&5</?#($%<3(*$(F.7==R(<3*+*<8#+$1(;"J5<*00"-(83#(?*8*(5$(/+'*&5a#?(5&8/(*($#+5#$(/G($8+5&'$(@35<3(<*&(A#
+#*?(5&8/(*(</>J%8#+(J+/'+*>1(`5&*+"(G50#$(*+#(*(</00#<85/&(/G(A5&*+"(</?#?(?*8*(5&(83#(&*85H#(G/+>*8(/G(83#
</>J%8#+(*&?(+#d%5+#$(*(?5GG#+#&8($#8(/G(F)=(<0*$$#$(8/(J+/<#$$(83#(?*8*1(;#c8(G50#$(*+#(%$#G%0(8/(J+/'+*>>#+$(*$
83#"(<*&(A#(%$#?(8/($8/+#(3%>*&(+#*?*A0#(?*8*(83*8(<*&(A#(#?58#?(#583#+(@5835&(*(6*H*(J+/'+*>(/+(#c8#+&*00"(%$5&'
4/5/21, 10)50 PMIntermediate Programming
Page 87 of 185http://itec2150.gitlab.io/
8#c8(#?58/+$1(b*<3(G50#(3*$(*($3/+8(&*>#(@583(*(G50#(#c8#&$5/&($%<3(*$(r!4C1D*H*r(*&?(*(J*83(&*>#1(;3#(J*83(&*>#
?#$<+5A#$(@3#+#(83#(G50#(5$($8/+#?(/&(83#(</>J%8#+($8/+*'#(?#H5<#1(=&($/>#(</>J%8#+$(835$(5$(,&/@&(*$(83#
?5+#<8/+"(/+(G/0?#+(0/<*85/&1(=&(*(45&?/@$(3*+?(?+5H#-(*&(#c*>J0#(/G(*(J*83(&*>#(>5'38(A#B
r7BwwZ$#+$ww<D/3&$ELww4/+,$J*<#ww!/>#@/+,Cww$+<r(1(j/8#(83*8(@3#&(%$5&'(83#(w(<3*+*<8#+(5&$5?#(*(6*H*(.8+5&'(8"J#-
@#(+#J#*8(58(8@5<#(A#<*%$#(83*8(<3*+*<8#+(5$(%$#?(8/(5&?5<*8#(*($J#<5*0(&/&J+5&85&'(<3*+*<8#+(5&$5?#(83#(.8+5&'1(;35$
<3*J8#+(#cJ0/+#$(3/@(8/(+#*?(*&?(%$#(?*8*(G+/>(*(8#c8(G50#(5&(*(6*H*(J+/'+*>(5&(*(</%J0#(/G(?5GG#+#&8(@*"$1(=8(*0$/
#cJ0/+#$(3/@(8/(<+#*8#(*&?(@+58#(8/(8#c8(G50#$(@5835&(*(6*H*(J+/'+*>1(;35$(5$(J+5>*+50"(?/&#(%$5&'(*($#8(/G(6*H*(F)=
<0*$$#$(5&(83#(D*H*15/(J*<,*'#1
5.4. Streams and Files
F00(?*8*(G+/>(#c8#+&*0($/%+<#$(</>5&'(5&8/(/+(/%8(/G(*(6*H*(J+/'+*>(%$#$($8+#*>$1(.8+#*>$(*+#(/&#(@*"
</&&#<85/&$(A#8@##&(*&(#c8#+&*0(?*8*($/%+<#(*&?(83#(6*H*(J+/'+*>1(=8(5$(%$#G%0(8/(835&,(/G(83#>(*$(*(J5J#(@3#+#
/&0"(/&#(J5#<#(/G(?*8*(>*"(G0/@(*8(*(85>#1(2*8*(<*&(*0$/(/&0"(G0/@(/&#(@*"1(./(@#(%$#(?5GG#+#&8($8+#*>$(G/+(+#*?5&'
*(G50#(*&?(@+585&'(*(G50#1(9/%(3*H#(A##&(%$5&'(83#(System.in($8+#*>(G/+(+#*?5&'(?*8*(G+/>(83#(</>J%8#+(,#"A/*+?1
9/%(3*H#(*0$/(A##&(@+585&'(?*8*(%$5&'(83#(System.out($8+#*>(8/(@+58#(8/(83#(</&$/0#(G/+(83#(%$#+(8/($##1(;3#
$8+#*>$(%$#?(8/(+#*?(*&?(@+58#(G50#$(/J#+*8#($5>50*+0"(*&?("/%(@500(+#</'&5a#($/>#(/G(83#(>#83/?$(@#(@500(%$#(8/
+#*?(*&?(@+58#(8/(G50#$1
5.5. The File class
`#G/+#(@#(<*&(+#*?(/+(@+58#(*(G50#-(@#(>%$8(r/J#&r(83#(G50#(G/+(+#*?5&'(*&?(@+585&'1(;3#(+%&Q85>#($"$8#>(3*$(A##&
8*,5&'(<*+#(/G(/J#&5&'(83#($8+#*>$(G/+(%$#+(5&J%8(*&?(/%8J%8-(A%8(83#(J+/'+*>>#+(5$(+#$J/&$5A0#(G/+(/J#&5&'(83#
$8+#*>$(G/+(G50#(3*&?05&'1(;3#(G5+$8($8#J(5$(8/(<+#*8#(*&(5&$8*&<#(/G(83#(V50#(<0*$$(@35<3(</&8*5&$(*00(83#(5&G/+>*85/&
83#($"$8#>(&##?$(8/(,&/@(*A/%8(83#(0/<*85/&(/G(83#(G50#1(=8(J+/H5?#$(*(J0*8G/+>(5&?#J#&?#&8(+#J+#$#&8*85/&(/G(83#
G50#$(&*>#(*&?(?5+#<8/+"(5&G/+>*85/&1(9/%(<*&(<+#*8#(*(V50#(5&$8*&<#(%$5&'(/&0"(83#(&*>#(/G(83#(G50#(*&?(*00/@(83#
=2b(8/(0//,(G/+(58(5&(83#(?#G*%08(?5+#<8/+"(G/+(G50#(5&J%81(!/@#H#+-(835$(?#G*%08(?5+#<8/+"(5$(?5GG#+#&8(G/+(?5GG#+#&8
=2bm$1(7/&$%08("/%+(=2b(?/<%>#&8*85/&(G/+(83#(?#G*%08(G50#(?5+#<8/+"(5G("/%(?#$5+#(8/(%$#(/&0"(83#(G50#(&*>#1
!/@#H#+-(58(5$(A#$8(8/(%$#(*(G%00"(+#*05a#?(J*83(&*>#(@3#&(<+#*85&'(83#(G50#1(;35$(5$('/5&'(8/(A#(?5GG#+#&8(?#J#&?5&'
%J/&(83#(/J#+*85&'($"$8#>(/G(83#(</>J%8#+("/%(*+#(%$5&'1(l#G#+(8/("/%+(/J#+*85&'($"$8#>(+#G#+#&<#(8/(G5'%+#(/%8
G%00"(+#*05a#?(J*83(&*>#$1
// creating a File instance using the default directory
importimport java.io.Filejava.io.File;
File myFile = newnew File("Hw1.txt");
JAVA
// creating a file using fully realized path name in Windows
import java.io.File;
// remember to get a single \ in a Java string we have to put \\
File myFile = new File("C:\\Users\\cjohns25\\Desktop\\HW1.txt");
// a full realized path name on an Apple MacBook
File myMacFile = new File("/users/cjohns25/Documents/2150/HW1.txt");
4/5/21, 10)50 PMIntermediate Programming
Page 88 of 185http://itec2150.gitlab.io/
U&<#(@#(3*H#(<+#*8#?(*(V50#(/AD#<8(G/+(83#(G50#(@#(@5$3(8/(5&8#+*<8(@583-(@#(*+#(+#*?"(8/(/J#&(/%+(?*8*($8+#*>(8/
83#(G50#1
5.6. BueredReader and BueredWriter
;3#+#(*+#(*<8%*00"($#H#+*0(@*"$(8/(+#*?(*&?(@+58#(G+/>(8#c8(G50#$(%$5&'(83#(6*H*(F)=(<0*$$#$1(4#(*+#('/5&'(8/(0//,(*8
$/>#(</>>/&(@*"$(8/(?/($/1(;#c8(G50#$(8"J5<*00"(</&$5$8(/G(3%>*&(+#*?*A0#(<3*+*<8#+$1(43#&(?#*05&'(@583(05&#$(/G
8#c8-(83#(#*$5#$8(<0*$$(8/(%$#(G/+(+#*?5&'(83/$#(05&#$(/G(8#c8(5&(5$(83#(BufferedReader(<0*$$1(;3#(*$$/<5*8#?(/%8J%8
<0*$$(G/+(@+585&'(8/(G50#$(5$(,&/@&(*$(BufferedWriter1(;3#+#(*+#(05>58*85/&$(@583(83#$#(<0*$$#$($5&<#(83#"(<*&(/&0"
+#*?(/+(@+58#(05&#$(/G(8#c81(BufferedReader(3*$(*(>#83/?(&*>#?(+#*?N5&#OR(G/+(+#*?5&'(*(05&#(/G(8#c81(;3#
#c*>J0#(A#0/@($3/@$(*&(#c*>J0#(/G(+#*?5&'(05&#$(/G(8#c8(G+/>(*(G50#(&*>#?(r8#c818c8r(*&?(83#&(@+585&'(8/(83#
</&$/0#(%$5&'(System.out1
;3#(BufferedReader(*&?(BufferedWriter(<0*$$(*+#(%$#G%0(@3#&(?#*05&'(@583(0*+'#(<3%&,$(/G(?*8*1(;35$(5$(%$#G%0
5&(<#+8*5&($58%*85/&$(@3#&(?#*05&'(@583(#c8#+&*0(?#H5<#$(83*8(?#*0(5&(0*+'#(<3%&,$(/G(?*8*-(A%8(&/8($/(>%<3(@3#&
8+"5&'(8/(8*,#(?5$<+#8#(J5#<#$(/G(?*8*(G+/>(*(G50#(*&?(J0*<#(83#>(5&8/(H*+5/%$(H*+5*A0#$1(N#8m$(0//,(*8(*&(#c*>J0#
@3#+#(835$(>5'38(A#(%$#G%01
5.6.1. An Example using BueredReader and BueredWriter
;3#(G/00/@5&'(#c*>J0#($3/@$(3/@(8/(%$#(`%GG#+#?l#*?#+(*&?(`%GG#+#?4+58#+(8/(</J"(G+/>(/&#(G50#(8/(*&/83#+1(=&
835$(</?#-(*(G50#(&*>#?(r#c*>J0#18c8r(5$(</J5#?(<3*+*<8#+(A"(<3*+*<8#+(5&8/(*(&#@(G50#(8580#?(r#c*>J0#7/J"18c8r1(j/8#
83*8(5G(83#(8#c8(G50#(r#c*>J0#7/J"18c8r(*0+#*?"(#c5$8$-(835$(J+/'+*>(@500(#+*$#(*&"(</&8#&8(<%++#&80"(5&(83#(G50#1
importimport java.io.Filejava.io.File;
importimport java.io.BufferedReaderjava.io.BufferedReader;
...
File f = newnew File("text.txt");
BufferedReader buffer = nullnull;
trytry
{
buffer= newnew BufferedReader(newnew FileReader(f));
String aLine = buffer.readLine();
System.out.println(aLine);
}
catchcatch (IOException ex)
{
// code to handle either an empty file or nonexistent file
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 89 of 185http://itec2150.gitlab.io/
5.7. PrintWriter
;3#(>/$8(</>>/&(@*"(8/(@+58#(8/(*(8#c8(G50#(5$(83+/%'3(83#(%$#(/G(83#(java.io.PrintWriter(<0*$$1(=8(5$(<*J*A0#(/G
@+585&'(*(H*+5#8"(/G(?*8*(8"J#$(*&?(5$(d%58#(H#+$*850#1(9/%(3*H#(*0$/(A##&(%$5&'(83#()+5&84+58#+(>#83/?$(#H#+"85>#
"/%(@+58#(8/(83#(</&$/0#1(System.out(5$(*&(/AD#<8(/G(8"J#()+5&84+58#+1(Z&05,#(."$8#>1/%8-(58(5$(&#<#$$*+"(8/(/J#&(*
/**
* This example shows how to copy one text file to another using BufferedReader
* BufferedWriter classes. Requires that the file "example.txt" be in the
* default file directory for your IDE.
*
*/
importimport java.io.Filejava.io.File;
importimport java.io.FileReaderjava.io.FileReader;
importimport java.io.FileWriterjava.io.FileWriter;
importimport java.io.IOExceptionjava.io.IOException;
importimport java.io.BufferedReaderjava.io.BufferedReader;
importimport java.io.BufferedWriterjava.io.BufferedWriter;
publicpublic classclass BufferedWriterExample
{
publicpublic staticstatic voidvoid main(String[] args)
{
File readFile = newnew File("example.txt");
File writeFile = newnew File("exampleCopy.txt");
BufferedReader readBuffer = nullnull;
BufferedWriter writeBuffer = nullnull;
// open files for reading and writing
intint readResult = 0;
trytry
{
readBuffer = newnew BufferedReader(newnew FileReader(readFile));
writeBuffer = newnew BufferedWriter(newnew FileWriter(writeFile));
dodo
{
readResult = readBuffer.read();
// -1 = nothing to read
ifif (readResult != -1)
{
writeBuffer.write((charchar) readResult);
}
} whilewhile (readResult != -1);
writeBuffer.close();
readBuffer.close();
} catchcatch (IOException ex)
{
ex.printStackTrace();
System.out.println("Problem with files");
}
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 90 of 185http://itec2150.gitlab.io/
G50#(*&?(<+#*8#(*&(/AD#<8(/G(8"J#()+5&8@+58#+(*$$/<5*8#?(@583(#*<3(G50#("/%(*+#(@+585&'1(;3#(</&$8+%<8/+$(G/+
)+5&84+58#+(*00/@("/%(8/(<+#*8#(@583(#583#+(*(V50#(/AD#<8(/+(D%$8(*(.8+5&'(</&8*5&5&'(83#(G50#(&*>#1(bc*>J0#$(A#0/@
$3/@(A/83(@*"$(/G(/J#&5&'(*(8#c8(G50#(G/+(@+585&'1
`/83(@*"$(/G(/J#&5&'(83#(G50#(*+#(#d%*00"(#GG#<85H#(*&?(#GG5<5#&81(S/$8(J+/'+*>>#+$(<3//$#(83#($#</&?(@*"
A#<*%$#(58(+#d%5+#$(0#$$(J+/'+*>>5&'1(=&(83#(#c*>J0#($3/@&-(*($5>J0#(.8+5&'(058#+*0(@*$(@+588#&(8/(83#(G50#(%$5&'
83#(println()(>#83/?1(9/%(3*H#(A##&(%$5&'(println(*&?(print(>#83/?$(8/(@+58#(8/(83#(</&$/0#1(4+585&'(8/(*(G50#
%$5&'(83#>(5&(*&(5?#&85<*0(G*$35/&1
j/8#B(UJ#&5&'(*&?(@+585&'(8/(*(G50#(3*$(J/8#&85*0(8/('#&#+*8#(/&#(/G($#H#+*0(<3#<,#?(#c<#J85/&(83*8(5&3#+58(G+/>
IOException1(;3#+#G/+#-(58(5$(5>J/+8*&8(8/(/J#&(*&?(J+/<#$$(G50#$(%$5&'(J+/J#+(#c<#J85/&(3*&?05&'1
5.7.1. Example of writing data to a text le
=&(835$(#c*>J0#-(83#(%$#+(5$(J+/>J8#?(G/+(*(G50#(&*>#1(F(8#c8(G50#(/G(83*8(&*>#(5$(<+#*8#?(*&?(@+588#&(8/1(=8(5$
5>J/+8*&8(8/(&/8#(83*8(5G(83#(&*>#?(G50#(*0+#*?"(#c5$8$(5&(83#(G50#(J*83(%$#?-(58(@500(#+*$#(83#(<%++#&8(</&8#&8$(/G(83*8
G50#(*&?(+#J0*<#(83#>(@583(83#(8#c8(5&(83#(#c*>J0#1(F0$/-(835$(5$(83#(G5+$8(85>#("/%(3*H#($##&(83#(%$#(/G(83#(finally
A0/<,(5&(83#(8+"P<*8<3(A0/<,$(%$#?(G/+(#c<#J85/&(3*&?05&'1(;3#(</?#(5&(83#(<%+0"(A+*<#$(/G(83#(finally(A0/<,(5$
#c#<%8#?(+#'*+?0#$$(/G(@3#83#+(83#(</?#(5&(83#(8+"(A0/<,(#c#<%8#$(@583/%8(J+/A0#>(/+(*&(#c<#J85/&(5$
File f = newnew File("outText.txt");
PrintWriter myWriter = nullnull;
trytry
{
// open the file using a File object
myWriter = newnew PrintWriter(f);
myWriter.println("I am writing this to the file");
myWriter.close();
}
catchcatch (IOException ex)
{
// handle exception generated if system can not open or write to file
}
JAVA
//same example- opening file using only file name
PrintWriter myWriter = nullnull;
trytry
{
// open the file using a File object
myWriter = newnew PrintWriter("outText.txt");
myWriter.println("I am writing this to the file");
myWriter.close();
}
catchcatch (IOException ex)
{
// handle exception generated if system can not open or write to file
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 91 of 185http://itec2150.gitlab.io/
#&</%&8#+#?1(;3#(%$#(5&(835$(#c*>J0#(5$(</>>/&(*$(@#(*+#(<0/$5&'(83#(G50#(5&(83#(G5&*00"(A0/<,1(F(G50#(A#5&'(@+588#&
8/(@35<3(5$(&/8(<0/$#?(J+/J#+0"(*8(83#(#&?(/G(83#(J+/'+*>(5$(&/8('%*+*&8##?(8/(</&8*5&(83#(8#c8(@+588#&(A"(83#
J+/'+*>1(70/$5&'(/G(83#(G50#$(5&(*(finally(A0/<,(5$(*(</>>/&(%$#(/G(83#(A0/<,(*&?(5$(</&$5?#+#?(*(A#$8(J+*<85<#(G/+
J+/'+*>>#+$1(;3#(</>J50#+(@500(@*+&("/%(5G(83#(G50#(5$(&/8(<0/$#?-(A%8(@500(*00/@("/%(8/(+%&("/%+(J+/'+*>(@583/%8
J+/J#+0"(<0/$5&'(83#(G50#1
importimport java.io.Filejava.io.File;
importimport java.io.IOExceptionjava.io.IOException;
importimport java.io.PrintWriterjava.io.PrintWriter;
importimport java.util.Scannerjava.util.Scanner;
/**
*
* Example of opening a text file for writing and writing various information to
* it.
*
*/
publicpublic classclass WriteTextFile
{
publicpublic staticstatic voidvoid main(String[] args)
{
// declare variables before try/catch block to avoid scope issues
// scanner to allow user input values
Scanner consoleInput = newnew Scanner(System.in);
// variable for file name
String fileName;
// variable to hold info about the File
File theFile;
// PrintWriter object to open for writing to file
PrintWriter outputFile = nullnull;
// open file with proper exception handling
booleanboolean fileOpen = falsefalse;
whilewhile (!fileOpen)
{
trytry
{
// ask user for a file name
System.out.println("Please enter name of file to be created and written to:");
fileName = consoleInput.nextLine();
theFile = newnew File(fileName);
outputFile = newnew PrintWriter(theFile);
fileOpen = truetrue;
} catchcatch (IOException ex)
{
System.out.println("Unable to open that file- check directory information and file name");
}
}
// using a second try/catch block to separate writing errors from
// file opening errors
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 92 of 185http://itec2150.gitlab.io/
5.8. Scanner
;3#(<0*$$(@#(%$#(8/(+#*?(5&?5H5?%*0(A58$(/G(?*8*(G+/>(*(8#c8(G50#(5$(Scanner1(9#$(835$(5$(83#($*>#(.<*&&#+(<0*$$(@#
%$#(G/+(+#*?5&'(5&J%8(G+/>(83#(,#"A/*+?1(./("/%(*+#(*0+#*?"(G*>505*+(@583(83#(</>>/&(>#83/?$(G/+(+#*?5&'
5&G/+>*85/&1(s&#c8OR(Q(G/+(+#*?5&'(*(.8+5&'(s&#c8N5&#OR(Q(G/+(+#*?5&'(*(05&#(/G(8#c8(8#+>5&*8#?(A"(83#(#&?(/G(05&#
<3*+*<8#+(s&#c8=&8OR(Q(G/+(+#*?5&'(*&(5&8#'#+(s&#c82/%A0#OR(Q(G/+(+#*?5&'(*(?/%A0#
43#&(+#*?5&'(G+/>(83#(,#"A/*+?-(@#(<+#*8#(*(.<*&&#+(/AD#<8(*&?(J*$$(83#(System.in(/AD#<8(*$(*(J*+*>#8#+(*8
</&$8+%<85/&1(;/(%$#(.<*&&#+(8/(+#*?(G+/>(*(V50#-(@#(<+#*8#(83#(.<*&&#+(/AD#<8(A"(J*$$5&'(#583#+(*(V50#(/AD#<8(/+
.8+5&'(</&8*5&5&'(83#(G50#(&*>#($5>50*+(8/()+5&84+58#+1(F0$/-(%&05,#(+#*?5&'(G+/>(83#(,#"A/*+?-(<+#*85&'(*(.<*&&#+
/AD#<8(05&,#?(8/(*(G50#(<*&(J/8#&85*00"('#&#+*8#(*(<3#<,#?(#c<#J85/&($/(58(5$(5>J/+8*&8(8/(%$#(J+/J#+(#c<#J85/&
3*&?05&'1(.##(83#(8@/(#c*>J0#$(G/+(/J#&5&'(*(.<*&&#+(8/(+#*?(*(G50#($3/@&(A#0/@1
trytry
{
// file opened for writing - let's put something in there
outputFile.println("This is how to write a string literal to a file");
doubledouble x = 55.6;
outputFile.println("This is how to print a double value: " + x);
intint y = 10;
String s = "This is a string variable";
outputFile.println(s + " this is an integer " + y);
} catchcatch (Exception ex)
{
// print the stack trace so programmer can see what line the exception
// occurred on
ex.printStackTrace();
// tell the user something
System.out.println("Unable to write to the file- program is terminating");
} finallyfinally
{
// regardless of whether exception occurred- close the file opened for writing
outputFile.close();
}
}
}
4/5/21, 10)50 PMIntermediate Programming
Page 93 of 185http://itec2150.gitlab.io/
j/8#(*A/H#(83*8(@#(%$#?(*(&#@(>#83/?(/&(.<*&&#+(83*8(@#(?5?&m8(%$#(@583(83#(,#"A/*+?1(;35$(>#83/?(5$(3*$j#c8OR1
;35$(>#83/?(5&?5<*8#$(@3#83#+(/+(&/8(83#+#(5$(*&"(+#>*5&5&'(?*8*(5&(83#(G50#(83*8(3*$(&/8(*0+#*?"(A##&(+#*?1(=8(?/#$
&/8(+#*?(83#(?*8*Q(58($5>J0"(5&?5<*8#$(83*8(83#+#(5$($8500(?*8*("#8(8/(A#(+#*?1(l#>#>A#+-(@#(*+#(+#*?5&'(G+/>(*
$8+#*>(/G(?*8*(*&?(<*&(/&0"(+#*?(#*<3(J5#<#(/G(?*8*(/&<#1(=8(5$(*0$/(&/8(J/$$5A0#(8/(+#+#*?(?*8*($/(A#(H#+"(<*+#G%0
&/8(8/(83+/@(?*8*(*@*"1
5.8.1. How does Scanner work?
;3#(>#83/?$(5&(.<*&&#+(%$#?(8/(+#*?(?*8*-(+#*?(5&J%8($#J*+*8#?(A"(?#05>58#+$1(2#05>58#+$(*+#(83#(&/&J+5&85&'
<3*+*<8#+$(%$#?(8/(G/+>*8(83#(8#c8($%<3(*$(A0*&,($J*<#$-(8*A$(*&?(#&?(/G(05&#(<3*+*<8#+$1(;3#(>#83/?$(*A/H#(G5+$8
$,5J(*(?#05>58#+(*&?(83#&(+#*?(<3*+*<8#+$(%&850(*&/83#+(?#05>58#+(5$(#&</%&8#+#?1(;3#"(?/(&/8(+#*?(83#($#</&?
?#05>58#+1(;3#(<3*+*<8#+$(+#*?(*+#(83#&(</&H#+8#?(8/(83#(?#$5+#?(8"J#(5&?5<*8#?(A"(@35<3(>#83/?(@*$(<*00#?1(=G(83#
<3*+*<8#+$(?/(&/8(>*8<3(@3*8(@*$(*$,#?(G/+-(*&(=&J%8S5$>*8<3bc<#J85/&(5$('#&#+*8#?1(V/+(#c*>J0#-(5G(83#(>#83/?
nextInt()(5$(<*00#?(*&?(83#(<3*+*<8#+$(+#*?(</&8*5&(*(&/&Q&%>#+5<(<3*+*<8#+-(*&(#c<#J85/&(@500(A#('#&#+*8#?1(;3#
>#83/?$(next()(*&?(nextLine()(+#*?(*&?(</&H#+8(<3*+*<8#+$(8/(.8+5&'$1(;3#(nextLine()(>#83/?(/&0"(%$#$(#&?(/G
05&#(<3*+*<8#+$(*$(?#05>58#+$1(;3#(#&?(/G(05&#(<3*+*<8#+(5$(<*00#?(*(05&#($#J*+*8/+(5&(6*H*(*&?(5$(?#G5&#?(5&(*(.8+5&'
*$(w+(/+(w&(5&(45&?/@$(*&?(w&(/&(Z&5c1(=8(5$(J0*8G/+>(?#J#&?#&8Q(*$(*(+#$%08("/%(>*"(/<<*$5/&*00"($##(5$$%#$(%$5&'
nextLine()(/&(<+/$$(J0*8G/+>(G50#$1
V50#$(<*&(*0$/(A#(+#*?(*<+/$$(83#(5&8#+&#8(*$$%>5&'(83#(G50#(?#$5+#?(8/(A#(+#*?(3*$(*(ZlN1(=&$8#*?(/G(<+#*85&'(*(V50#
/AD#<8(8/(%$#(5&(</&$8+%<85&'(.<*&&#+-(*(D*H*1&#81ZlN(/AD#<8(5$(<+#*8#?1
5.8.2. File reading Examples
File f = newnew File("dataFile.txt);
Scanner fileScanner = null;
try
{
fileScanner = new Scanner(f);
/*
alternately could have written
fileScanner = new Scanner("dataFile.txt");
*/
whilewhile (!fileScanner.hasNext())
{
// read the next piece of data in the file- this example shows all String data
String data = fileScanner.next();
}
fileScanner.close();
}
catchcatch (IOException ex)
{
// code to handle a problem opening or reading from the file.
}
JAVA
// opening a file using a URL
String URLString = "www.somedomain.com/someFile.txt";
java.net.URL theURL = newnew java.net.URL(URLString);
Scanner urlReader = newnew Scanner(theURL);
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 94 of 185http://itec2150.gitlab.io/
;3#+#(*+#(0/8$(/G(@*"$(8/(+#*?(?*8*(G+/>(*(8#c8(G50#1(F&?(58(>/$80"(?#J#&?$(/&(83#(G/+>*8(/G(83#(8#c8(G50#1(./(A#G/+#
@+585&'(83#(J+/'+*>(8/(+#*?(*(G50#-(83#(J+/'+*>>#+($3/%0?(A#(*@*+#(/G(83#(G/+>*8(/G(83#(?*8*1(=&(835$(G5+$8
#c*>J0#-(83#(G50#(&*>#(5$(?#8#+>5&#?(A"(J+/>J85&'(83#(%$#+(8/(#&8#+(581(;3#&(#*<3(05&#(/G(83#(8#c8(G50#(5$(+#*?(*$(*
.8+5&'1(`"(+#*?5&'(83#(?*8*(*$(*(.8+5&'-(83#(J+/'+*>>#+(?/#$(&/8(3*H#(8/(,&/@(5G(83#(<3*+*<8#+$(+#*?(*+#(8#c8-
&%>#+5<(/+($">A/05<1(!/@#H#+-(/&<#(+#*?(83*8(@*"-(58(>%$8(A#(J*+$#?(8/(%$#(*$(*&"835&'(/83#+(83*&(*(.8+5&'1
importimport java.io.Filejava.io.File;
importimport java.io.FileNotFoundExceptionjava.io.FileNotFoundException;
importimport java.io.IOExceptionjava.io.IOException;
importimport java.io.PrintWriterjava.io.PrintWriter;
importimport java.util.Scannerjava.util.Scanner;
/**
*
* This example shows how to open a text file for reading and read each line as
* a String and print to console
*
*/
publicpublic classclass ReadStringsFromFile
{
publicpublic staticstatic voidvoid main(String[] args)
{
// declare variables before using in try/catch loop to avoid scope issues
// open a Scanner for user input
Scanner consoleInput = newnew Scanner(System.in);
String fileName = nullnull;
File theFile = nullnull;
Scanner inputFile = nullnull;
// open file with proper exception handling
booleanboolean fileOpen = falsefalse;
whilewhile (!fileOpen)
{
trytry
{
// ask user for a file name
System.out.println("Please enter name of file to be created and written to:");
fileName = consoleInput.nextLine();
theFile = newnew File(fileName);
inputFile = newnew Scanner(theFile);
fileOpen = truetrue;
} catchcatch (FileNotFoundException ex)
{
System.out.println("Unable to open that file- check to make sure directory is corerct and file
exists");
}
}
// now read from the file - note- program will loop until valid file name
// entered
trytry
{
// continue to read while file contains info
whilewhile (inputFile.hasNextLine())
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 95 of 185http://itec2150.gitlab.io/
.%JJ/$#(@#(@*&8(8/(+#*?(*(A%&<3(/G(&%>A#+$(G+/>(G50#1(V/+(#c*>J0#-(@#(>5'38(3*H#(*(G50#(&*>#?(&%>A#+$18c8
83*8(</&8*5&$(*(A%&<3(/G(8#$8('+*?#$(83*8(@#(@*&8(8/(*H#+*'#1(;3#(8#c8(G50#(>5'38(0//,(05,#(835$B
=G(@#(%$#(83#(G5+$8(#c*>J0#-(83#$#(&%>A#+$(@500(A#</>#(5&?5H5?%*0(.8+5&'(?*8*(58#>$(5&(83#(J+/'+*>1(=&(/+?#+(8/
*H#+*'#(83#>-(@#(@/%0?(3*H#(8/(</&H#+8(83#>(8/(?/%A0#(H*0%#$1(U+(@#(</%0?(D%$8(+#*?(83#>(*$(?/%A0#(H*0%#$(G+/>
83#(G50#1(;35$(5$($3/@&(5&(83#(#c*>J0#(A#0/@(@35<3(+#*?$(5&(83#(&%>A#+$(*A/H#(*&?(*H#+*'#$(83#>(*00(8/'#83#+1
{
// read the line
String line = inputFile.nextLine();
// output it to console
System.out.println(line);
}
} catchcatch (Exception ex)
{
// stack trace for programmer
ex.printStackTrace();
// message for user
System.out.println("Unable to continue reading from file");
} finallyfinally
{
inputFile.close();
}
}
}
44.5
33.2
-100
66.7
55
2345
-234256
12343256
22.7
10
0
4/5/21, 10)50 PMIntermediate Programming
Page 96 of 185http://itec2150.gitlab.io/
5.9. Regular Expressions and parsing a le
l#'%0*+(#cJ+#$$5/&$(*00/@(%$(8/(#cJ*&?(83#(?#G5&585/&(/G(@3*8(83#(.<*&&#+($##$(*$(?#05>58#+$1(2#05>58#+$(*+#
<3*+*<8#+$(%$#?(A"(.<*&&#+(8/(?#8#+>5&#(@3#+#(/&#(J5#<#(/G(?*8*($8*+8$(*&?(*&/83#+(#&?$1(;3#(8"J#$(/G(?#05>58#+
*+#(/G8#&(?5<8*8#?(A"(83#(>#83/?(/G(.<*&&#+(%$#?1(V/+(#c*>J0#-(83#(>#83/?$(%$#?(8/(+#*?(*($J#<5G5<(8"J#(/G(?*8*Q
next()-(nextInt()-(nextDouble()-(#8<(%$#(@358#$J*<#(<3*+*<8#+$(A"(?#G*%081(4358#$J*<#(<3*+*<8#+$(*+#(&/&Q
J+5&85&'(<3*+*<8#+$(%$#?(8/(J+/H5?#(&#'*85H#($J*<#(/&(*(J+5&8#?(J*'#1(;3#$#(J+/H5?#(&#'*85H#(/+(r@358#r($J*<#(/&(*
importimport java.io.Filejava.io.File;
importimport java.io.IOExceptionjava.io.IOException;
importimport java.util.Scannerjava.util.Scanner;
/**
* Use with numbers.txt in default file directory Program reads the numbers.txt
* file and averages all the numbers
*
*/
publicpublic classclass ReadNumbersFromFile
{
publicpublic staticstatic voidvoid main(String[] args)
{
// declare variables outside try/catch block to avoid scope issues
Scanner inputFile = nullnull;
// keep a running total of numbers read
doubledouble sum = 0;
// keep count of numbers read
intint numberCount = 0;
trytry
{
inputFile = newnew Scanner(newnew File("numbers.txt"));
whilewhile (inputFile.hasNextDouble())
{
doubledouble num = inputFile.nextDouble();
numberCount++;
sum = sum + num;
}
} catchcatch (IOException ex)
{
System.out.println("Problem reading file encountered");
ex.printStackTrace();
} finallyfinally
{
// ALWAYS close file when done
inputFile.close();
}
// computer and print average of the numbers
doubledouble average = sum / numberCount;
System.out.println("The average of the numbers read is: " + average);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 97 of 185http://itec2150.gitlab.io/
J+5&8#?(J*'#1(bc*>J0#$(*+#(83#(8*A(<3*+*<8#+-(83#(A0*&,($J*<#(<3*+*<8#+(*&?(83#(&#@(05&#(<3*+*<8#+1(;3#(>#83/?
nextLine()(/&0"(%$#$(83#(&#@(05&#(<3*+*<8#+(*$(*(?#05>58#+1(;3#(5&J%8(>#83/?$(@/+,(A"($,5JJ5&'(*&"(?#05>58#+$-
+#*?5&'(<3*+*<8#+$(%&850(83#(&#c8(?#05>58#+(5$(+#*<3#?1(;3#(<3*+*<8#+$(+#*?(*+#(</&H#+8#?(5&8/(83#(?#$5+#?(8"J#(*&?
+#8%+&#?1(=G(83#(<3*+*<8#+$(+#*?(?/(&/8(>*8<3(83#(?#$5+#?(8"J#(O51#1(r*A<r(5$(+#*?(@3#&(<*005&'(nextInt()R-(*&
=&J%8S5$>*8<3bc<#J85/&(5$(83+/@&1
4#(<*&(>/?5G"(83#(<3*+*<8#+$(83#(.<*&&#+(/AD#<8(%$#$(*$(?#05>58#+(A"(J*$$5&'(5&(*(.8+5&'(,&/@&(*$(*(+#'%0*+
#cJ+#$$5/&(/+(+#'#c(%$5&'(83#(.<*&&#+(>#83/?(useDelimiter(String regex)1
./>#(</>>/&0"(%$#?(+#'#c(.8+5&'$(*+#(?#G5&#?(*8B(388J$BPP@@@1D+#A#01</>PA0/'PD*H*Q+#'%0*+Q#cJ+#$$5/&$Q<3#*8Q
$3##8(1(F?H*&<#?(%$#+$(/G(+#'%0*+(#cJ+#$$5/&(@500(%$#(83#(6*H*(N5A+*+"(<0*$$(Pattern(8/(?#G5&#(</>J0#c(+#'%0*+
#cJ+#$$5/&$1(F(</>>/&($5>J0#(%$#(5$(8/(+#*?(</>>*(?#05>58#?(G50#$(05,#(83/$#($/>#85>#$('#&#+*8#?(A"
$J+#*?$3##8(J+/'+*>$1(F(</>>*(?#05>58#?(G50#(</&8*5&$(?*8*(5&(*(8#c8(G50#($#J*+*8#?(A"(*(</>>*(<3*+*<8#+1
7/>>*(<3*+*<8#+$(*+#(&/8(?#G*%08(?#05>58#+$(%$#?(A"(.<*&&#+1(4#(<*&(*??(83#(</>>*(*$(*(?#05>58#+(A"(<*005&'(83#
useDelimiter()(>#83/?(/&(.<*&&#+(*&?(J*$$5&'(5&(*(</>>*(<3*+*<8#+1(.##(83#(#c*>J0#(A#0/@(@35<3(+#*?$(5&(*
</>>*(?#05>58#?(G50#(&*>#?(`//,C1<$H1(!#+#(5$(@3*8(83#(G50#(0//,$(05,#1
=G(@#(*88#>J8(8/(+#*?(83#(CT(&%>A#+$(%$5&'(nextInt()-(@#(@500('#8(=&J%8S5$>*8<3bc<#J85/&(#*<3(85>#(*(</>>*(5$
?#8#<8#?1(`"(*??5&'(83#(</>>*(*$(*(?#05>58#+-(@#(G5c(83*8(J+/A0#>1(.##(83#(G50#(A#0/@(83*8(*??$(83#(</>>*(*$(*
?#05>58#+(*&?($%<<#$$G%00"(+#*?$(83#(CT(&%>A#+$1
1,2,3,4,5,6,7,8,9,10
4/5/21, 10)50 PMIntermediate Programming
Page 98 of 185http://itec2150.gitlab.io/
5.10. Case Study: A Client Database
N#8m$(0//,(*8(*&(#c*>J0#(/G(%$5&'(8#c8(G50#$(8/(</&8*5&(5&G/+>*85/&(*A/%8(*(A%$5&#$$m$(<05#&8(%$5&'(8#c8(G50#$(8/($8/+#
83#(5&G/+>*85/&(*&?(/AD#<8Q/+5#&8#?(J+5&<5J0#$(8/(?#$5'&(83#(*JJ05<*85/&1(j/8#(83*8(83#(<05#&8(5&G/+>*85/&(5$
>5&5>*0(G/+(J%+J/$#$(/G(A+#H58"1(4#($8*+8(A"(<+#*85&'(*(<0*$$(8/(3/0?(5&G/+>*85/&(*A/%8(*($5&'0#(<05#&81(.##(83#(<0*$$
A#0/@1(j/8#(83*8(83#(<0*$$(</&8*5&$(83+##(*88+5A%8#$(*A/%8(*(<05#&8(*&?('#88#+(>#83/?$(G/+(83#(83+##(*88+5A%8#$1(;3#
<0*$$(*0$/(5>J0#>#&8$(83#(7/>J*+*A0#(5&8#+G*<#(5&(<*$#(@#(@*&8(8/($/+8(/%+(705#&8(05$8(A"(&*>#1(F&?(83#(<0*$$
/H#++5?#$(83#(?#G*%08(8/.8+5&'OR(>#83/?(8/(J+5&8(<05#&8(5&G/+>*85/&(5&(*(%$#+QG+5#&?0"(G/+>*81
importimport java.io.Filejava.io.File;
importimport java.io.IOExceptionjava.io.IOException;
importimport java.util.Scannerjava.util.Scanner;
/**
* This example reads a set of numbers stored in a comma delimited file (.csv)
* output from Excel using regular expressions- it sums the numbers and prints
* the sum. To run properly need example file Book1.csv in default file location
*
*/
publicpublic classclass ReadCSVFile
{
publicpublic staticstatic voidvoid main(String[] args)
{
Scanner inputFile = nullnull;
intint sum = 0;
// define the regular exppression
String regex = ",";
trytry
{
inputFile = newnew Scanner(newnew File("Book1.csv"));
inputFile.useDelimiter(regex);
// scanner will use the String regex to determine where one number starts
// and another ends
whilewhile (inputFile.hasNext())
{
String numString = inputFile.next();
intint num = Integer.parseInt(numString);
sum = sum + num;
System.out.println(numString);
}
} catchcatch (IOException ex)
{
ex.printStackTrace();
System.out.println("There was a problem opening or reading file");
} finallyfinally
{
inputFile.close();
}
System.out.println("The sum is : " + sum);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 99 of 185http://itec2150.gitlab.io/
/**
* This class contains information about a client/customer that is maintained in
* a business's data store. The class follows good object oriented principles
* for data encapsulation and hiding.
*
*/
publicpublic classclass Client implementsimplements Comparable<Client>
{
// attributes of the client
privateprivate intint clientNumber;
privateprivate String name;
privateprivate String address;
// constructor- no default constructor provided so number, name and address info
// must be provided
// at construction time. No set methods are provided to enforce this.
publicpublic Client(intint clientNumber, String name, String address)
{
thisthis.clientNumber = clientNumber;
thisthis.name = name;
thisthis.address = address;
}
// getter methods to allow information to be viewed outside the class
/**
* @return the clientNumber
*/
publicpublic intint getClientNumber()
{
returnreturn clientNumber;
}
/**
* @return the name
*/
publicpublic String getName()
{
returnreturn name;
}
/**
* @return the address
*/
publicpublic String getAddress()
{
returnreturn address;
}
// compare to method to allow list of clients to be sorted if desired
@Override
publicpublic intint compareTo(Client other)
{
// compare clients based on name and if same name- use clientNumber
ifif (thisthis.name.equalsIgnoreCase(other.name))
{
// name is the same - return based on clientNumber
ifif (thisthis.clientNumber < other.clientNumber)
{
returnreturn -1;
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 100 of 185http://itec2150.gitlab.io/
j/@(@#(3*H#(8/(@+58#(83#(*JJ05<*85/&(8/(,##J(*(05$8(/G(/%+(A%$5&#$$m$(<05#&8$1(;3#(705#&82*8*A*$#(<0*$$(</&8*5&$
>#83/?$(8/(+#*?(G+/>(*(8#c8(G50#(&*>#?(r<05#&818c8r1(;3#(>#83/?(<+#*8#$(/AD#<8$(/G(8"J#(705#&8(A*$#?(/&(83#(8#c8
+#*?(5&(*&?(83#&($8/+#$(83#>(5&(*&(F++*"N5$81(=&(*JJ05<*85/&$(05,#(835$-(58(5$(</>>/&(8/(3*+?(</?#(83#(8#c8(G50#(&*>#
$5&<#(83#(A%$5&#$$(J+/A*A0"(5$(&/8('/5&'(8/(<3*&'#1(4#(*0$/(3*H#(*(>#83/?(8/(@+58#(*&(F++*"N5$8(/G(705#&8(/AD#<8$(8/
83#($*>#(8#c8(G50#1(`"(%$5&'(83#($*>#(8#c8(G50#(&*>#-(@#(@500(?#0#8#(83#(/0?(?*8*(*&?(@+58#(83#(&#@1(;3#+#(*+#(*0$/
>#83/?$(8/(J+5&8(*(%$#+(>#&%(*&?(*&/83#+(8/(J+/>J8(G/+(*&?(+#*?(5&(83#(5&G/+>*85/&(8/(*??(*(&#@(705#&8(/AD#<8(8/
83#(?*8*A*$#1(`"(<+#*85&'($#J*+*8#(>#83/?$(8/(?/(83#$#(8*$,$-(@#(A+#*,(83#(J+/A0#>(%J(5&8/($>*00#+(#*$5#+(8/($/0H#
J+/A0#>$1(=8(*0$/(>*,#$(83#(</?#(>/+#(>/?%0*+-(+#*?*A0#(*&?(+#%$*A0#1(.##(83#(</?#(A#0/@1
} elseelse ifif (thisthis.clientNumber > other.clientNumber)
{
returnreturn 1;
} elseelse
// they must be equal if none of above is true
{
returnreturn 0;
}
} elseelse
// names are not equal- use String compareTo to determine greater than, less
// than or equal
{
returnreturn thisthis.name.compareTo(other.name);
}
}
// provide a toString method to print client information in easy to read fashion
@Override
publicpublic String toString()
{
returnreturn "clientNumber=" + clientNumber + "\nname=" + name + "\naddress=" + address + "\n";
}
}
importimport java.io.Filejava.io.File;
importimport java.io.FileNotFoundExceptionjava.io.FileNotFoundException;
importimport java.io.PrintWriterjava.io.PrintWriter;
importimport java.util.ArrayListjava.util.ArrayList;
importimport java.util.Scannerjava.util.Scanner;
/**
* This class reads data from textfile named client.txt and uses it to populate
* an ArrayList of Client objects. The user is then able to change the ArrayList
* by deleting or adding an object. Upon exit, the client.txt file is
* overwritten with the modified ArrayList
*
*/
publicpublic classclass ClientDatabase
{
/*
* This method reads in client.txt, creates Client objects based on the data,
* and populates theList with the new Client objects
*
* @param ArrayList<Client>
*
* @return ArrayList<Client>
*/
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 101 of 185http://itec2150.gitlab.io/
publicpublic ArrayList<Client> readFile(ArrayList<Client> theList)
{
String fileName = "client.txt";
// hard coding file name
File theFile = nullnull;
Scanner inputFile = nullnull;
trytry
{
theFile = newnew File(fileName);
inputFile = newnew Scanner(theFile);
} catchcatch (FileNotFoundException ex)
{
System.out.println("Unable to open client file- continuing with empty list");
returnreturn theList;
}
// file opened- now let's read from it
trytry
{
whilewhile (inputFile.hasNextLine())
// loop until we get to end of the file
{
// first read client Number as String- mixing reading Strings and ints
// causes issues- so read as String and convert
String number = inputFile.nextLine();
intint clientNumber = Integer.parseInt(number);
// now name
String name = inputFile.nextLine();
// now address
String address = inputFile.nextLine();
// now create Client object and add to ArrayList
Client c = newnew Client(clientNumber, name, address);
theList.add(c);
}
} catchcatch (Exception ex)
{
ex.printStackTrace();
System.out.println("Problem reading the client file- continuing with what was read");
} finallyfinally
{
inputFile.close();
}
returnreturn theList;
}
/*
* This method takes the Client objects in theList and converts them to the
* proper format for writing in to the client.txt file. Note existing contents
* of client.txt are overwritten.
*
* @param theList
*/
publicpublic voidvoid writeFile(ArrayList<Client> theList)
{
// variable for file name
String fileName = "client.txt";
// variable to hold info about the File
4/5/21, 10)50 PMIntermediate Programming
Page 102 of 185http://itec2150.gitlab.io/
File theFile;
// PrintWriter object to open for writing to file
PrintWriter outputFile = nullnull;
trytry
{
theFile = newnew File(fileName);
outputFile = newnew PrintWriter(theFile);
} catchcatch (FileNotFoundException ex)
{
System.out.println("Unable to write client.txt in default location- check file permissions and try
again");
// print stack trace for debugging - remove when program is working properly
ex.printStackTrace();
}
// now write to and close the file
trytry
{
// loop through the entries in ArrayList
forfor (intint i = 0; i < theList.size(); i++)
{
// get a local variable to shorten typing
Client c = theList.get(i);
// format of file = clientNumber, name, address - each on own line
outputFile.println(c.getClientNumber());
outputFile.println(c.getName());
outputFile.println(c.getAddress());
}
} catchcatch (Exception ex)
// using exception instead of specific exception because many possible wrong
// things could happen
{
ex.printStackTrace();
// print the stack trace for debug purposes.
} finallyfinally
{
outputFile.close();
}
}
/**
* Method to print the user menu to console
*/
publicpublic voidvoid printMenu()
{
System.out.println("a. View Client List");
System.out.println("b. Add a Client");
System.out.println("c. Delete a Client");
System.out.println("d. Quit");
}
/**
* Method to prompt user to enter info for new Client
*
* @param Scanner
* @return Client
*/
publicpublic Client addClient(Scanner s)
{
System.out.println("Please enter client number");
4/5/21, 10)50 PMIntermediate Programming
Page 103 of 185http://itec2150.gitlab.io/
intint number = s.nextInt();
// read extra delimiter left from reading in a number
s.nextLine();
System.out.println("Enter client name");
String name = s.nextLine();
System.out.println("Enter client address");
String address=s.nextLine();
Client c = newnew Client(number, name, address);
returnreturn c;
}
/**
* The main method
*
* @param args
*/
publicpublic staticstatic voidvoid main(String[] args)
{
// create scanner to read from console
Scanner keyboard = newnew Scanner(System.in);
// create an instance of this class to use non-static methods
ClientDatabase theClass = newnew ClientDatabase();
// create array list for client list and then read file
ArrayList<Client> theClientList = newnew ArrayList<Client>();
// call method to read file
theClientList = theClass.readFile(theClientList);
booleanboolean keepLooping = truetrue;
// loop until user wants to quite
whilewhile (keepLooping)
{
// print the menu
theClass.printMenu();
String choice = keyboard.nextLine();
ifif (choice.equalsIgnoreCase("a"))
{
// print the database
forfor (Client c: theClientList)
{
System.out.println(c);
}
}
elseelse ifif (choice.equalsIgnoreCase("b"))
{
// add a client
theClientList.add(theClass.addClient(keyboard));
}
elseelse ifif (choice.equalsIgnoreCase("c"))
{
// delete a client
System.out.println("Enter client number to delete");
// read as String to avoid delimiter issues
String num = keyboard.nextLine();
intint clientNumber = Integer.parseInt(num);
// find record in arrayList wiht matching client number and delete it
forfor (intint i = 0; i< theClientList.size(); i++)
4/5/21, 10)50 PMIntermediate Programming
Page 104 of 185http://itec2150.gitlab.io/
5.11. Key Terms
V50#(Q(F(+#$/%+<#(%$#?(8/($8/+#(*(</00#<85/&(/G(?*8*(/&(*(</>J%8#+($8/+*'#(?#H5<#1
;#c8(V50#(Q(F(</>J%8#+(G50#(</&$5$85&'(/G(3%>*&(+#*?*A0#(Z&5</?#(<3*+*<8#+$1(;"J5<*00"(+#*?(%$5&'(*(8#c8(#?58/+(05,#
83#(/&#(5&(>/$8(=2bm$1(7/&$5?#+#?(3%>*&(+#*?*A0#1
`5&*+"(V50#(Q(F(</>J%8#+(G50#($8/+#?(5&(83#(&*85H#(A5&*+"(</?#(/G(83#(</>J%8#+1(j/8(</&$5?#+#?(3%>*&(+#*?*A0#1
=&J%8(Q(=&G/+>*85/&(/+(?*8*(G+/>(*&(#c8#+&*0($/%+<#(+#*?(5&8/(*(6*H*(J+/'+*>1
U%8J%8(Q(=&G/+>*85/&(/+(?*8*(G+/>(*(6*H*(J+/'+*>(@+588#&(8/(*&(#c8#+&*0($/%+<#1
UJ#&(*(V50#Q(7+#*8#(*($8+#*>(/G(?*8*(8/(/+(G+/>(*(</>J%8#+(G50#1
V50#(.8+#*>(Q(F(/&#(@*"(d%#%#(/G(?*8*(#583#+(8/(/+(G+/>(*(G50#1(;3#(/+?#+(/G(?*8*(5&(83#(d%#%#(+#J+#$#&8$(83#(/+?#+
/G(83#(?*8*(5&(83#(G50#1
70/$#(*(V50#Q(V0%$3(*&?(<0/$#(*($8+#*>(/G(?*8*(8/(/+(G+/>(*(G50#1(43#&(@+585&'(*(G50#-(G/+<#$(83#(J+/'+*>(8/(@*58(%&850
*00(?*8*(5&(83#($8+#*>(3*$(A##&(@+588#&(8/(83#(G50#1(43#&(+#*?5&'(*(G50#-(8#+>5&*8#$(*&"(G%+83#+(?*8*(</>5&'(G+/>
83#(G50#1
{
Client c = theClientList.get(i);
ifif (c.getClientNumber() == clientNumber)
{
theClientList.remove(i);
System.out.println("Client removed");
breakbreak;
// stop searching once found
}
}
}
elseelse
//if a, b or c not chosen- end loop
{
keepLooping = falsefalse;
}
}
// write back to file
System.out.println("Writing new client list to file");
theClass.writeFile(theClientList);
}
}
4/5/21, 10)50 PMIntermediate Programming
Page 105 of 185http://itec2150.gitlab.io/
2#05>58#+(Q(2#05>58#+$(*+#(@358#$J*<#(<3*+*<8#+$(%$#?(8/($#J*+*8#(H*+5/%$(J5#<#$(/G(?*8*(5&(*(8#c8(G50#1(bc*>J0#$(*+#
*(A0*&,($J*<#-(8*A-(/+(#&?(/G(05&#(<3*+*<8#+$(@35<3(?/(&/8($3/@(%J(*$(J+5&8(5&(*(8#c8(G50#1
5.12. Exercises
5.12.1. Exercise 1
4+58#(*(6*H*(J+/'+*>(8/(+#*?(5&(83#(CT(&%>A#+$(5&(83#(#c*>J0#(G50#(`//,C1<$H(J+/H5?#?(*A/H#1(;3#(J+/'+*>
$3/%0?($%>(*00(83#(&%>A#+$-(G5&?(83#(0/@#$8(&%>A#+-(G5&?(83#(35'3#$8(&%>A#+-(*&?(</>J%8#+(83#(*H#+*'#1(ZJ/&
</>J0#85/&(/G(83#(J+/<#$$5&'-(83#(J+/'+*>($3/%0?(@+58#(*(&#@(8#c8(G50#(&*>#?($8*8$18c8(@583(83#(5&G/+>*85/&(G/%&?
5&(83#(G/00/@5&'(G/+>*8(@3#+#(ccc(+#J+#$#&8$(*(&%>A#+(<*0<%0*8#?(*A/H#1
5.12.2. Exercise 2
Z$5&'(83#(<0*$$()/#>(A#0/@1(4+58#(*(</>J0#8#(6*H*(J+/'+*>(83*8(<+#*8#$(83+##(?5GG#+#&8(/AD#<8$(/G(8"J#()/#>1(;3#
J+/'+*>($3*00(83#&(/J#&(*(8#c8(G50#(&*>#?(J/#>$18c8(G/+(@+585&'(*&?(@+58#(83#(5&G/+>*85/&(*A/%8(#*<3(J/#>(8/(83#
8#c8(G50#1(;3#(J+/'+*>($3*00(jU;(@+58#(83#(8/.8+5&'OR(H#+$5/&(/G(83#(/AD#<8(8/(83#(G50#-(A%8(@+58#(G5+$8(83#(J/#>(&*>#
/&(*(05&#(*&?(83#&(83#(J/#8(&*>#(/&(*($#</&?(05&#(G/+(#*<3(J/#>1
The sum of the numbers is: xxx
The lowest number is: xxx
The highest number is : xxx
The average of the numbers is : xxx
/**
* Poem.java
*
* A class representing information about a poem for use in Chapter 5 Exercise 2
*
*/
publicpublic classclass Poem
{
privateprivate String name;
privateprivate String poet;
/**
* no-arg constructor
*/
publicpublic Poem()
{
// initialize attributes
name = "unknown";
poet = "unknown";
}
/**
* @return the name
*/
publicpublic String getName()
{
returnreturn name;
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 106 of 185http://itec2150.gitlab.io/
5.12.3. Exercise 3
Z$5&'(83#()/#>(<0*$$('5H#&(5&(#c#+<5$#(E-(@+58#(*(6*H*(J+/'+*>(8/(+#*?(G+/>(*(8#c8(G50#(&*>#?(J/#>E18c8(J+/H5?#?
A#0/@1(;3#(J+/'+*>($3*00(+#*?(83#(&*>#(*&?(J/#8(/G(#*<3(J/#>-(<+#*8#(*&(/AD#<8(/G(8"J#()/#>(G/+(#*<3(&*>#PJ/#8
J*5+(*&?(J+5&8(*00(83#(+#*?(J/#>(5&G/+(8/(83#(</&$/0#1
5.13. Issue Tracker/Comments
=$$%#(;+*<,#+ O388J$BPP'583%A1</>P3J*+,WP3#0JY?#$,P5$$%#$R
/**
* @param name the name to set
*/
publicpublic voidvoid setName(String name)
{
thisthis.name = name;
}
/**
* @return the poet
*/
publicpublic String getPoet()
{
returnreturn poet;
}
/**
* @param poet the poet to set
*/
publicpublic voidvoid setPoet(String poet)
{
thisthis.poet = poet;
}
@Override
publicpublic String toString()
{
returnreturn "Poem [name=" + name + ", poet=" + poet + "]";
}
}
We Real Cool
Gwendolyn Brooks
I Know Why the Caged Bird Sings
Maya Angelou
Hope is the Thing with Feathers
Emily Dickinson
The Road Not Taken
Robert Frost
4/5/21, 10)50 PMIntermediate Programming
Page 107 of 185http://itec2150.gitlab.io/
6. Generics
6.1. Learning Outcomes
.8%?#&8$(@500(A#(*A0#(8/
C1( 2#$<+5A#(83#(A#&#G58$(/G('#&#+5<$
E1( 7+#*8#('#&#+5<(>#83/?$-('#&#+5<(<0*$$#$(*&?(5&8#+G*<#$
6.2. Resources
6.2.1. Text
;3#(`*$85<$(/G(6*H*(q#&#+5<$ O388J$BPP@@@1A*#0?%&'1</>PD*H*Q'#&#+5<$R(A"(A*#0?%&'
q#&#+5<$ O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0PD*H*P'#&#+5<$P5&?#c138>0R(A"(U+*<0#
q#&#+5<(S#83/?$ O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0P#c8+*P'#&#+5<$P>#83/?$138>0R(A"(U+*<0#
q#&#+5<$(5&(6*H* O388J$BPP@@@1D/%+&*0?#H1</>PCMMIPD*H*Q'#&#+5<$Q#c*>J0#Q>#83/?Q<0*$$Q5&8#+G*<#R
q#&#+5<()+/'+*>>5&' O388JBPP>*8313@$1#?%PD*H*&/8#$P<CTP$C138>0R
l*@(;"J#$ O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0PD*H*P'#&#+5<$P+*@;"J#$138>0R
q#&#+5<$ O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0P#c8+*P'#&#+5<$P5&?#c138>0R(A"(q50*?(`+*<3*
q#&#+5<$B(!/@(;3#"(4/+,(*&?(43"(;3#"(*+#(=>J/+8*&8
O388J$BPP@@@1/+*<0#1</>P8#<3&5<*0Q+#$/%+<#$P*+85<0#$PD*H*PD%&#*%Q'#&#+5<$138>0R(A"(6/$3(6%&#*%
6.2.2. Videos
6*H*(q#&#+5<$ O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]C8_>ap3['LbR(A"(2##'#(Z
6*H*(q#&#+5<$ O388J$BPP@@@1"/%8%A#1</>P@*8<3\H]K:UW%h/&Q,=R(A"(=>85*a(F3>*?
6.3. Introduction
q#&#+5<$(5$(r8/(?#8#<8(#++/+$(*8(</>J50#(85>#(+*83#+(83*&(*8(+%&85>#1r(ON5*&'-(ETC[R
q#&#+5<$(5$(r8/(?#8#<8(#++/+$(*8(</>J50#(85>#(+*83#+(83*&(*8(+%&85>#1r(ON5*&'-(ETC[R(=&(835$(<3*J8#+-("/%(@500(0#*+&
*A/%8(q#&#+5<$1(=&(83#(J+#H5/%$(<3*J8#+$(*&?(/J#&(8#c8A//, O388JBPP58#<ECKT1??&$1&#8PR-("/%(0#*+&#?(*A/%8(83#(F++*"N5$8
<0*$$(*&?(7/>J*+*A0#(5&8#+G*<#1(;3#"(*+#(#c*>J0#$(/G(*('#&#+5<(<0*$$(*&?(*('#&#+5<(5&8#+G*<#1(q#&#+5<$(#&*A0#
8"J#$(8/(A#(J*+*>#8#+$(@3#&(?#G5&5&'(<0*$$#$-(5&8#+G*<#$(*&?(>#83/?$1(4583(835$(<*J*A5058"-("/%(<*&(?#G5&#(*(<0*$$-
*&(5&8#+G*<#(/+(*(>#83/?(@583(*('#&#+5<(8"J#(83*8(83#(</>J50#+(<*&(+#J0*<#(@583(</&<+#8#(8"J#$1(;35$(<3*J8#+
</H#+$(83#(G/00/@5&'(8/J5<$(G/+('#&#+5<$B
S/85H*85/&$(*&?(`#&#G58$
2#G5&5&'(q#&#+5<(70*$$#$(*&?(=&8#+G*<#$
q#&#+5<(S#83/?$
4/5/21, 10)50 PMIntermediate Programming
Page 108 of 185http://itec2150.gitlab.io/
l*@(;"J#$(*&?(`*<,@*+?(7/>J*85A5058"
450?<*+?(q#&#+5<(;"J#$
b+*$%+#
6.4. Motivations and Benets
r;3#(>/85H*85/&(G/+(%$5&'(6*H*('#&#+5<$(5$(8/(?#8#<8(#++/+$(*8(</>J50#(85>#1r(ON5*&'-(ETC[R
=&(<3*J8#+(M(G+/>()+/'+*>>5&'(V%&?*>#&8*0$ O388JBPP58#<ECKT1??&$1&#8PX*++*"05$8$R(-("/%(0#*+&#?(*A/%8(F++*"N5$8$1(;3#
F++*"N5$8(<0*$$(OD*H*1%8501F++*"N5$8R(5$('#&#+5<(*&?(3*$(A##&($5&<#(62_(C1L1(q#&#+5<(>#*&$(83*8(83#(<0*$$(<*&(A#
+#%$#?(@583(*&"(+#G#+#&<#(?*8*(8"J#1(!/@#H#+-(A#G/+#(62_(C1L-(F++*"N5$8(%$#?(UAD#<8(*$(83#(?*8*(8"J#($/(/&#(<*&
*??(5&$8*&<#$(/G(*+A58+*+"(<0*$$#$(8/(83#(05$81(.%<3(*(J+#Q'#&#+5<(*++*"(05$8(3*$(8@/(?5$*?H*&8*'#$1(V5+$8-(A#<*%$#(58$
#0#>#&8$(*+#(5&8#+J+#8#?(*$(5&$8*&<#$(/G(83#(8/J>/$8(UAD#<8(<0*$$-(<*$85&'(5$(+#d%5+#?(#H#+"(85>#("/%(%$#(*&
#0#>#&8(*$(*&(/AD#<8(/G(58$(*<8%*0-(5&8#&?#?(8"J#1(.#</&?-(*00/@5&'(3#8#+/'#&#/%$(/AD#<8$(5&(83#(05$8(<*&(<*%$#(8"J#
#++/+$(83*8(*+#(&/8(?#8#<8#?(*8(</>J50#(85>#(A%8(<*%$#(#c<#J85/&$(*8(+%&85>#1(.5&<#(62_(C1L-(83#(F++*"N5$8(<0*$$(3*$
A##&('#&#+5<1(q#&#+5<$(/AH5*8#(83#(&##?(/G(<*$85&'(*&?(*00/@(G/+(8"J#(<3#<,5&'(*8(</>J50#(85>#(+*83#+(83*&
+%&85>#(@35<3(#05>5&*8#$(J/8#&85*0(+%&(85>#(#c<#J85/&$1(;3#(<0*$$(S*5&(A#0/@(?#>/&$8+*8#$(*&(#c<#J85/&(<*%$#?
@3#&(%$5&'(83#(J+#Q'#&#+5<(F++*"N5$8(@35<3(5$(&/8(8"J#Q$*G#1(=&(835$(#c*>J0#-(05$8(5$(>#*&8(*$(*&(*++*"(05$8(/G
5&8#'#+$1(!/@#H#+-(<+#*8#?(*$(*(+*@(8"J#-(58(*00/@$(*+A58+*+"(/AD#<8$(8/(A#(*??#?1(=G(/&#(>5$8*,#&0"(*??$(83#($8+5&'
r.8+*@A#++"r(8/(83#(05$8(A"(<*005&'(05$81*??Or.8+*@#A#++"rR-(*(70*$$7*$8bc<#J85/&(@500(/<<%+(*8(+%&85>#(@3#&(83#
>#83/?(J+5&8.%>(*88#>J8$(8/(<*$8(83#($8+5&'(8/(*&(5&8#'#+(5&(83#($8*8#>#&8($%>(v](O=&8#'#+R(5e(5&(/+?#+(8/(</>J%8#
83#($%>(/G(*00(#0#>#&8$(@35<3(*+#(#cJ#<8#?(8/(A#(5&8#'#+$1
//before JDK 1.5
importimport java.util.ArrayListjava.util.ArrayList;
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
ArrayList list = newnew ArrayList();
//<-- ArrayList without generic called raw type
list.add(67);
list.add(56);
list.add(4);
list.add("Straweberry");
//<-- it will throw the ClassCastException
list.add(52);
printSum(list);
}
privateprivate staticstatic voidvoid printSum(ArrayList n)
{
Integer sum = 0;
forfor(Object i: n)
{
//ArrayList (java.util.ArrayList) has the Object as a type
sum += (Integer)i;
//You need to type cast these to each particular data type every time
}
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 109 of 185http://itec2150.gitlab.io/
;3#(G/00/@5&'(<0*$$(?#>/&$8+*8#$(83#(%$#(/G(*(J*+*>#8+5a#?(8"J#(F++*"N5$8x=&8#'#+y(/G(83#('#&#+5<(<0*$$
F++*"N5$8x;y1(!#+#(83#(</&<+#8#(*+'%>#&8(8"J#(=&8#'#+(8*,#$(83#(J0*<#(/G(83#(G/+>*0(8"J#(J*+*>#8#+(;(5&(83#
<+#*85/&(/G(83#(*++*"(05$8-(@3#+#(83#(A+*<,#8(&/8*85/&(x=&8#'#+y($J#<5G5#$(83*8(#H#+"(#0#>#&8(/G(83#(*++*"(05$8(5$(*&
5&$8*&<#(/G(83#(=&8#'#+(<0*$$1(;3#+#(5$(&/(&##?(8/(<*$8(@3#&(%$5&'(83#(#0#>#&8$(*$(5&8#'#+$1(43#&(/&#(*88#>J8$(8/
*??(*(&/&Q=&8#'#+(/AD#<8(O#1'1(*($8+5&'R(8/(83#(05$8-(83#(</>J50#+(@500('#&#+*8#(*&(#++/+1
N#8m$(0//,(*8(*&/83#+($<#&*+5/1(43*8(5G("/%(@*&8(8/(?#H#0/J(*(</&8*5&#+
O388J$BPP@@@1/+*<0#1</>P8#<3&5<*0Q+#$/%+<#$P*+85<0#$PD*H*PD%&#*%Q'#&#+5<$138>0R(83*8(3*$(83#(*A5058"(8/(3/0?(/AD#<8$(/G(H*+5/%$
8"J#$\(V5+$8-("/%(<+#*8#(*(7/&8*5&#+(<0*$$(*$(A#0/@1
//Since JDK 1.5 - this example shows how the generic types can be used.
importimport java.util.ArrayListjava.util.ArrayList;
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
ArrayList<Integer> list = newnew ArrayList<>();
// <-- ArrayList with generic
list.add(67);
list.add(56);
printSum(list);
}
privateprivate staticstatic voidvoid printSum(ArrayList<Integer> n)
{
intint sum = 0;
forfor(intint i: n){
sum += i;
}
}
}
JAVA
Basic form to create an instance of generic classclass
BaseType<Type> object = newnew BaseType<Type>();
For the type argument, we cannot use a primitive type (e.g. intint, charchar, doubledouble, booleanboolean).
JAVA
publicpublic classclass Container
{
privateprivate Object obj;
publicpublic Object getObj()
{
returnreturn obj;
}
publicpublic voidvoid setObj(Object obj)
{
thisthis.obj = obj;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 110 of 185http://itec2150.gitlab.io/
;3#(G/00/@5&'(ContainerWithoutGenerics(<0*$$(%$#$(*(</&8*5&#+(8/($8/+#(*&?(+#8+5#H#(H*0%#$1(;3#(>*5&(>#83/?
<+#*8#$(*(</&8*5&#+(*&?($8/+#$(EI-(r6*H*r(*&?(KL1^[(5&(835$(/+?#+1(./(&/@(KL1^[(5$(83#($8/+#?(H*0%#1(;3#&("/%(&##?(8/
%$#(*&(#cJ05<58(<*$8(8/(+#8+5#H#(83#(H*0%#1
=&(</&8+*$8-(83#(G/00/@5&'(Container(<0*$$(%$#$(*('#&#+5<(8"J#(;1(=&(83#(8"J#(J*+*>#8#+($#<85/&(x;y-(83#(*&'0#
A+*<,#8$(#&<0/$#(83#(G/+>*0(8"J#(J*+*>#8#+(;1
;3#(>*5&(>#83/?(/G(83#(ContainerTester(<0*$$(<+#*8#$(*(</&8*5&#+(/AD#<8(/G(83#(J*+*>#8+5a#?(<0*$$
Container<String>-(*&?($8/+#$(?5GG#+#&8(8"J#$(/G(H*0%#$(8/(83#(</&8*5&#+1(;3#($8*8#>#&8(string.setObj(10.56)(@500
<*%$#(*(</>J50*85/&(#++/+(*$(*(+#$%08(/G(8"J#Q<3#<,5&'1
importimport java.util.ArrayListjava.util.ArrayList;
importimport java.util.Listjava.util.List;
publicpublic classclass ContainerWithoutGenerics
{
publicpublic staticstatic voidvoid main(String[] args)
{
Container obj = newnew Container();
obj.setObj(23);
//store an int which is autoboxed to an Integer object
obj.setObj("Java");
//then store a string
obj.setObj(45.98);
//then store an double which is autoboxed to a Double object - current value
List list = newnew ArrayList();
list.add(obj);
Double doubleValue = (Double)((Container)list.get(0)).getObj();
//current value is a double value
System.out.println("doubleValue: " + doubleValue);
}
}
JAVA
publicpublic classclass Container<T>
{
privateprivate T obj;
publicpublic T getObj()
{
returnreturn obj;
}
publicpublic voidvoid setObj(T obj)
{
thisthis.obj = obj;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 111 of 185http://itec2150.gitlab.io/
V/00/@5&'(<0*$$(O*R(5$(8/(5?#&85G"(5G(*(J#+$/&O#1'1(6/#-(6#$$#R(5$(*&(#>J0/"##(/G(<0*$$#$B(Microsoft, Apple, Google, or
Adobe1(;3#(J%+J/$#(/G(83#(J+/'+*>(5$(8/(>*,#($%+#(/&0"(</++#<8(Company(8"J#(@3#+#(83#(#>J0/"##(5$(@/+,5&'1
!/@#H#+-(#H#&(83/%'3(Google(/AD#<8(&*>#?(joe(5$(<+#*8#?(A%8("/%($8500(<*&(*??(joe(8/(83#(</>J*&"(Apple(*&?(OAR
5$(8/($3/@(3/@('#&#+5<(<0*$$((Company<T>)(5$(%$#G%0(8/(G5c(83#(A%'(G+/>(83#(</?#(O*R1=G("/%($##(Main(<0*$$OAR-("/%
/&0"(<*&(*??(#>J0/"##$(G+/>(</++#<8(</>J*&"1(=G(_ Jesse_(5$(*&(#>J0/"##(/G(Microsoft, addEmployee()(<*&(/&0"
8*,#(Jesse1
publicpublic classclass ContainterTester
{
publicpublic staticstatic voidvoid main(String[] args)
{
Container<String> string = newnew Container<String>();
string.setObj("Cupertino");
//Correct type value - String
string.setObj(10.56);
//won't compile because 10.56 is not a string value but a double value.
}
}
JAVA
a.
importimport java.util.ArrayListjava.util.ArrayList;
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
{
Google joe = newnew Google("Joe");
// Joe is an employee of Google
Microsoft jesse = newnew Microsoft("Jesse");
Adobe bill = newnew Adobe("Bill");
Company google = newnew Company("Google");
google.addEmployee(joe);
google.addEmployee(jesse);
google.addEmployee(jesse);
Company apple = newnew Company("Apple");
apple.addEmployee(joe);
//joe is an object of Google not Apple but still you can pass joe in
addEmployee().
apple.addEmployee(bill);
}
}
classclass Employee
{
privateprivate String name;
publicpublic Employee(String name)
{
thisthis.name = name;
}
publicpublic String getName()
{
returnreturn name;
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 112 of 185http://itec2150.gitlab.io/
}
classclass Google extendsextends Employee
{
publicpublic Google(String name) {
//name is an employee name
supersuper(name);
}
}
classclass Adobe extendsextends Employee
{
publicpublic Adobe(String name)
{
supersuper(name);
}
}
classclass Company
{
privateprivate String name;
privateprivate ArrayList<Employee> eList = newnew ArrayList<>();
publicpublic Company(String name)
{
thisthis.name = name;
}
publicpublic String getName()
{
returnreturn name;
}
publicpublic booleanboolean addEmployee(Employee employee)
{
ifif(eList.contains(employee))
{
System.out.println(employee.getName() + " is already confirmed as an employee of " +
thisthis.name);
returnreturn falsefalse;
}
elseelse {
eList.add(employee);
System.out.println(employee.getName() + " is an employee of " + thisthis.name);
returnreturn truetrue;
}
}
}
classclass Microsoft extendsextends Employee
{
publicpublic Microsoft(String name){
//name is an employee name
supersuper(name);
}
}
b.
importimport java.util.ArrayListjava.util.ArrayList;
publicpublic classclass Main
{
publicpublic staticstatic voidvoid main(String[] args)
4/5/21, 10)50 PMIntermediate Programming
Page 113 of 185http://itec2150.gitlab.io/
{
Google joe = newnew Google("Joe");
Microsoft jesse = newnew Microsoft("Jesse");
Adobe bill = newnew Adobe("Bill");
Company<Google> google = newnew Company("Google");
google.addEmployee(joe);
//google.addEmployee(jesse);
//jesse is an object of Microsoft so you cannot pass jesse in addEmployee() when it is called by google.
(1)
//google.addEmployee(bill); //same reason. (2)
Company<Microsoft> microsoft = newnew Company("Microsoft");
microsoft.addEmployee(jesse);
// microsoft.addEmployee(bill); //same reason (3)
//microsoft.addEmployee("Bill"); //(4) you also cannot pass a string to addPlayer() method.
}
}
classclass Employee
{
privateprivate String name;
publicpublic Employee(String name)
{
thisthis.name = name;
}
publicpublic String getName()
{
returnreturn name;
}
}
classclass Google extendsextends Employee
{
publicpublic Google(String name)
{
//name is an employee name
supersuper(name);
}
}
classclass Adobe extendsextends Employee
{
publicpublic Adobe(String name)
{
supersuper(name);
}
}
classclass Company<T>
{
privateprivate String name;
privateprivate ArrayList<T> eList = newnew ArrayList<>();
publicpublic Company(String name)
{
thisthis.name = name;
}
publicpublic String getName()
4/5/21, 10)50 PMIntermediate Programming
Page 114 of 185http://itec2150.gitlab.io/
=&(835$(J+/'+*>(A-(@3#&("/%(?#<0*+#(google.addEmployee(jesse) or google.addEmployee(bill) or
microsoft.addEmployee(bill)(A"(8+"5&'(8/(J*$$(5&</++#<8(8"J#(/AD#<8(/+(microsoft.addEmployee("Bill")(A"
8+"5&'(8/(J*$$(*($8+5&'(r`500r-(D*H*(@500($3/@(#++/+$(O$##(OCR-OER-OIR-OKRR1(;35$(5$(@3*8(@#(3*H#(?#G5&#?(83#
addEmployee()(>#83/?(5&(Company<T>(O$##(<1(A#0/@R1(./(83#(Company<T>(<0*$$(+#d%5+#(%$(8/(%$#(b>J0/"##
<0*$$(/+(*($%A<0*$$(/G(Employee1(F&?(*0$/(.8+5&'(8"J#(J*+*>#8#+(5$(&/8(8*,#&(G+/>(83#(addEmployee()(>#83/?1
=&$8#*?-(58(5$(8*,5&'('#&#+5<(8"J#(/AD#<8O;(#>J0/"##R1
{
returnreturn name;
}
publicpublic booleanboolean addEmployee(T employee)
{
ifif(eList.contains(employee))
{
System.out.println((((Employee)employee).getName() + " is already confirmed as an employee of " +
thisthis.name));
returnreturn falsefalse;
}
elseelse
{
eList.add(employee);
System.out.println((((Employee)employee).getName() + " is an employee of " + thisthis.name));
returnreturn truetrue;
}
}
}
classclass Microsoft extendsextends Employee
{
publicpublic Microsoft(String name)
{
supersuper(name);
}
}
c.
publicpublic booleanboolean addEmployee(T employee)
{
ifif(eList.contains(employee))
{
System.out.println((((Employee)employee).getName() + " is already confirmed as an employee of " +
thisthis.name));
returnreturn falsefalse;
}
elseelse
{
eList.add(employee);
System.out.println((((Employee)employee).getName() + " is an employee of " + thisthis.name));
returnreturn truetrue;
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 115 of 185http://itec2150.gitlab.io/
=&(*(&%8$3#00-(83#(,#"(A#&#G58(/G('#&#+5<$(5$(8/(#&*A0#(#++/+$(8/(A#(?#8#<8#?(*8(</>J50#(85>#(+*83#+(83*&(*8(+%&85>#
O#&$%+5&'(</>J50#Q85>#($*G#8"R1(F('#&#+5<(<0*$$(/+(>#83/?(J#+>58$("/%(8/($J#<5G"(*00/@*A0#(8"J#$(/G(/AD#<8$(83*8(83#
<0*$$(/+(>#83/?(<*&(@/+,(@5831(=G("/%(*88#>J8(8/(%$#(*&(5&</>J*85A0#(/AD#<8-(83#(</>J50#+(@500(?#8#<8(83*8(#++/+1
q#&#+5<$(*0$/(3#0J$(8/(+#%$#(83#(</?#(G/+(*&"(8"J#(@#(@*&8(8/(%$#1
6.5. Dening Generic Classes
F$("/%(3*H#($##&(G+/>(>%085J0#(#c*>J0#$(*A/H#-(3#+#(5$(*($5>J0#(#c*>J0#(/G(?#G5&5&'(q#&#+5<(<0*$$B
GenericClass<T>(*&?(*(8#$8#+(J+/'+*>1
6.5.1. Type Parameters/Type Variables
r;"J#(J*+*>#8#+$(O*1,1*1(8"J#(H*+5*A0#$R(*+#(%$#?(*$(J0*<#3/0?#+$(8/(5&?5<*8#(83*8(*(8"J#(@500(A#(*$$5'&#?(8/(83#
<0*$$(*8(+%&85>#1rO6%&#*%-(ETCKR
`"(</&H#&85/&-(8"J#(J*+*>#8#+$(*+#(*($5&'0#(%JJ#+<*$#1(V/00/@5&'(05$8(5$(83#($8*&?*+?(8"J#(J*+*>#8#+$B
/**
* Class:GenericClass
* @param <T>
*/
//Use <> to specify parameter type and define generic class
publicpublic classclass GenericClass<T>
{
//Type T reference variable named object declaration.
T object;
//constructor
GenericClass(T object)
{
thisthis.object = object;
}
publicpublic T getObject()
{
returnreturn thisthis.object;
}
}
publicpublic classclass Main {
publicpublic staticstatic voidvoid main(String[] args)
{
GenericClass<String> stringObject = newnew GenericClass<String>("\"String Value and check constructor
param\"");
System.out.println(stringObject.getObject());
GenericClass<Integer> integerObject = newnew GenericClass<Integer>(23);
// unboxing of new Integer(23);
System.out.println(integerObject.getObject());
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 116 of 185http://itec2150.gitlab.io/
bB(b0#>#&8
_B(_#"
jB(j%>A#+
;B(;"J#
hB(h*0%#
.-Z-h(5&(*(>%085J*+*>#8#+($58%*85/&1
6.5.2. How to Use Generics?
C1( 7+#*8#(*('#&#+5<(<0*$$B(J%A05<(<0*$$(q#&#+5<70*$$x;y(z({|(}
E1( =&$8*&85*8#(83#(/AD#<81(b*<3(/G(;"J#(J*+*>#8#+(O5&(835$(<*$#-(58(5$(;R(5$(+#J0*<#?(@583(83#(=&8#'#+(8"J#1
q#&#+5<70*$$x=&8#'#+y(5&8#'#+UAD#<8(](&#@(q#&#+5<70*$$x=&8#'#+yORe
I1( q#&#+5<$(<*&(*0$/(A#(%$#?(@5835&(</&$8+%<8/+$(8/(J*$$(8"J#(J*+*>#8#+$(G/+(<0*$$(G5#0?(5&585*05a*85/&1
q#&#+5<70*$$(/AC(](&#@(q#&#+5<70*$$OIRe(q#&#+5<70*$$(/AE(](&#@(q#&#+5<70*$$Orq#/+'5*rRe
K1( q#&#+5<$(3*$(+*@(8"J#(G/+(A*<,@*+?(</>J*85A5058"1(=8(@500(#05>5&*8#(8"J#Q<3#<,5&'(*8(</>J50#(85>#1
q#&#+5<70*$$(+*@l#G(](&#@(q#&#+5<70*$$ORe
L1( q#&#+5<$(?/#$(&/8($%JJ/+8($%AQ8"J5&'(N5$8xj%>A#+y(&%>A#+$(](&#@(F++*"N5$8x=&8#'#+yORe(PP@/&m8(</>J50#1
=&8#'#+(5$*($%A8"J#(/G(j%>A#+1(N5$8x=&8#'#+y(&%>A#+$(](&#@(F++*"N5$8x=&8#'#+yORe
6.5.3. Dening Generic Interfaces
!#+#(*+#(8@/(#c*>J0#$(/G(%$5&'('#&#+5<$(@583(5&8#+G*<#$(N5$8(*&?(=8#+*8/+
O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0P#c8+*P'#&#+5<$P$5>J0#138>0R(5&(83#(J*<,*'#(D*H*1%850B
6.6. Generic Methods
F$("/%(?#G5&#('#&#+5<(<0*$$#$-(*&?(5&8#+G*<#$-("/%(<*&(*0$/(%$#('#&#+5<(8"J#$(8/(?#G5&#('#&#+5<(>#83/?$1(;/(?#<0*+#
*('#&#+5<(>#83/?(&*>#?(print()-("/%(J0*<#(83#('#&#+5<(8"J#(xby(*G8#+(83#(,#"@/+?($8*85<(5&(83#(>#83/?(3#*?#+
*&?($8*85<(O<0*$$R(*&?(&/&Q$8*85<(O5&$8*&<#R(>#83/?$(*+#(*00/@#?1
publicpublic interfaceinterface List<E>
{
voidvoid add(E x);
Iterator<E> iterator();
}
publicpublic interfaceinterface Iterator<E>
{
E next();
booleanboolean hasNext();
}
JAVA
publicpublic staticstatic <E> voidvoid print(E[] list) {
//... }
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 117 of 185http://itec2150.gitlab.io/
V/00/@5&'(j/&q#&#+5<S#83/?2#>/(J+/'+*>(3*$(/H#+0/*?#?(J+5&8OR(>#83/?$(8/(J+5&8(?5GG#+#&8(8"J#$(/G(*++*"$
O=&8#'#+-(2/%A0#-(73*+*<8#+-(*&?(.8+5&'R1
publicpublic classclass NonGenericMethodDemo
{
publicpublic staticstatic voidvoid main(String[] args)
{
// create arrays of Integer, Double, Character, and String
Integer[] integerArray = {1, 2, 3, 4};
Double[] doubleArray = {2.1, 22.2, 31.65, 10.5};
Character[] characterArray = {'G', 'E', 'N', 'E', 'R','I','C','S'};
String[] stringArray = {"Lawrenceville", "Duluth", "Chicago", "New York", "Atlanta"} ;
System.out.println("Array integerArray contains:");
print(integerArray);
// pass an Integer array
System.out.println("Array doubleArray contains:");
print(doubleArray);
// pass a Double array
System.out.println("Array characterrArray contains:");
print(characterArray);
// pass a Character array
System.out.println("Array stringArray contains:");
print(stringArray);
// pass a Character array
}
/**
* method print to print Integer array
* @param arr
*/
publicpublic staticstatic voidvoid print(Integer[] arr)
{
// display array elements
forfor (Integer element : arr)
System.out.print(element + " ");
System.out.println();
}
/**
* method print to print Double array
* @param arr
*/
publicpublic staticstatic voidvoid print(Double[] arr)
{
// display array elements
forfor (Double element : arr)
System.out.print(element + " ");
System.out.println();
}
/**
* method print to print Character array
* @param arr
*/
publicpublic staticstatic voidvoid print(Character[] arr)
{
// display array elements
forfor (Character element : arr)
System.out.print(element + " ");
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 118 of 185http://itec2150.gitlab.io/
4350#(#*<3(print()(>#83/?(*A/H#(5$(@+588#&(G/+(*(?5GG#+#&8(?*8*(8"J#-(83#5+(</?#(5$(/83#+@5$#(5?#&85<*0(*&?(%$#$(*
G/+Q#*<3(0//J(8/(?5$J0*"(*++*"(#0#>#&8$1(`"(%$5&'('#&#+5<$-(@#(<*&(@+58#(*($5&'0#('#&#+5<(print()(>#83/?(G/+(*00
?*8*(8"J#$(@583(*('#&#+5<(8"J#(b1(;3#(3#*?#+(/G(83#('#&#+5<(>#83/?(5$(J%A05<($8*85<(xby(H/5?(print(E[] arr)1(F
</>J0#8#(GenericMethodDemo(J+/'+*>(5$(J+/H5?#?(*$(G/00/@$1(`*$#?(/&(83#(8"J#$(/G(83#(*+'%>#&8$(J*$$#?(8/
83#('#&#+5<(>#83/?-(83#(</>J50#+(3*&?0#$(#*<3(>#83/?(<*00(*JJ+/J+5*8#0"1(;3#(/%8J%8$(/G(GenericMethodDemo
*&?(MethodWithoutGenerics(*+#(5?#&85<*01
System.out.println();
}
/**
* method print to print String array
* @param arr
*/
publicpublic staticstatic voidvoid print(String[] arr)
{
forfor(String element: arr){
System.out.print(element + " ");
}
}
}
publicpublic classclass GenericMethodDemo
{
publicpublic staticstatic voidvoid main(String[] args)
{
// create arrays of Integer, Double, Character, and String
Integer[] integerArray = {1, 2, 3, 4};
Double[] doubleArray = {2.1, 22.2, 31.65, 10.5};
Character[] characterArray = {'G', 'E', 'N', 'E', 'R','I','C','S'};
String[] stringArray = {"Lawrenceville", "Duluth", "Chicago", "New York", "Atlanta"} ;
System.out.println("Array integerArray contains:");
print(integerArray);
// pass an Integer array
System.out.println("Array doubleArray contains:");
print(doubleArray);
// pass a Double array
System.out.println("Array characterrArray contains:");
print(characterArray);
// pass a Character array
System.out.println("Array stringArray contains:");
print(stringArray);
// pass a Character array
}
publicpublic staticstatic <E> voidvoid print(E[] arr)
{
// System.out.println(Arrays.toString(list));
forfor(E element: arr)
{
System.out.print(element + " ");
}
System.out.println();
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 119 of 185http://itec2150.gitlab.io/
6.6.1. The Bounded Generic Type
F('#&#+5<(8"J#(<*&(A#($J#<5G5#?(*$(*($%A8"J#(/G(*&/83#+($%J#+<0*$$(8"J#1(;35$($%A8"J#(5$(<*00#?(A/%&?#?1(N#8m$($*"(5G
"/%(@*&8(8/(+#$8+5<8(83#(8"J#$(83*8(<*&(A#(%$#?(*$(8"J#(*+'%>#&8$(5&(*(J*+*>#8+#+5a#?(8"J#1(=8(>*"(A#(>/+#(<0#*+(8/
,&/@(3/@(8/(?#<0*+#(*(A/%&?#?(8"J#(J*+*>#8#+1(V5+$8-(05$8(83#(8"J#(J*+*>#8#+m$(&*>#(G/00/@#?(A"(83#(#c8#&?$
,#"@/+?(*&?(A"(58$(%JJ#+(A/%&?(/+($%J#+<0*$$1(V/+(#c*>J0#-(5G(*(>#83/?(/J#+*8#$(/&(&%>A#+$-("/%(>*"(@*&8(8/
>*,#(58(/&0"(*<<#J8(5&$8*&<#$(/G(j%>A#+(/+(58$($%A<0*$$#$(05,#(x;(#c8#&?$(j%>A#+y
=G("/%($##(OCR(G+/>(A(O$##(A#0/@R-(Company(<0*$$(8*,#$(*('#&#+5<(8"J#(;(J*+*>#8#+(*&?(835$(#c*>J0#($3/@$(83*8(@#
<*&(%$#(*($5&'0#(A/%&?(@3#&(@#($J#<5G"(83#(Company(8"J#(;(J*+*>#8#+1(./(58(5$(+#$8+5<85&'(7/>J*&"(8/(A#5&'
<+#*8#?(G/+(/AD#<8$(83*8(8"J#(;(5$(5&3#+58#?(G+/>(83#(b>J0/"##(<0*$$(/+($/>#(<0*$$(/G(#>J0/"##(/&0"1(6*H*(*00/@$
>%085J0#(A/%&?1(F$(*($%A<0*$$(5$(5&3#+58#?(G+/>(/&#($%J#+<0*$$(*&?(5>J0#>#&8$(G+/>(>%085J0#(5&8#+G*<#$-($*>#
+%0#(*JJ05#$1
* <T extendsextends superclass>
//this class only accepts type parameters as any class which extends superclass or superclass itself. It means
that if any other type is passing
//it will cause compile time error.
JAVA
a.
classclass Company<T>
{
privateprivate String name;
privateprivate ArrayList<T> eList = newnew ArrayList<>();
publicpublic Company(String name)
{
thisthis.name = name;
}
publicpublic String getName()
{
returnreturn name;
}
publicpublic booleanboolean addEmployee(T employee)
{
ifif(eList.contains(employee))
{
System.out.println((((Employee)employee).getName() + " is already confirmed as an employee of " +
thisthis.name));
returnreturn falsefalse;
}
elseelse {
eList.add(employee);
System.out.println((((Employee)employee).getName() + " is an employee of " + thisthis.name));
returnreturn truetrue;
}
}
}
b.
classclass Company<T extendsextends Employee>
{
//(1)
privateprivate String name;
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 120 of 185http://itec2150.gitlab.io/
6.7. Raw Types and Backward Compatibility
rF('#&#+5<(<0*$$(/+(5&8#+G*<#(%$#?(@583/%8($J#<5G"5&'(*(</&<+#8#(8"J#-(<*00#?(*(+*@(8"J#1(;35$(+*@(8"J#(#&*A0#$
A*<,@*+?(</>J*85A5058"(@583(#*+05#+(H#+$5/&(/G(D*H*(OA#G/+#(62_(C1LRr(ON5*&'-(ETC[R(The raw types should be used
for backward compatibility(*&?(#05>5&*8#(*00(83#(A#&#G58$(/G(%$5&'(*('#&#+5<(<0*$$1(`*$5<*00"(+*@(8"J#$(A#3*H#$
#c*<80"(05,#(83#"(@#+#(A#G/+#('#&#+5<$1(./(@3#&(@#(%$(F++*"N5$8(@583/%8('#&#+5<(8"J#(*&?(@583(+*@(8"J#(O$##
#c*>J0#(CR-(</?#(+%&$(D%$8(G5&#(*&?(58(5$(0#'*0(*8(</>J50#Q85>#1(;35$(5$(83#(?+*@A*<,(?5$<%$$#?(5&(83#(>/85H*85/&$
*&?(A#&#G58$(5&(M1K(5&(835$(<3*J8#+1
#c*>J0#(C1
#c*>J0#(E1
privateprivate ArrayList<T> eList = newnew ArrayList<>();
publicpublic Company(String name)
{
thisthis.name = name;
}
publicpublic String getName()
{
returnreturn name;
}
publicpublic booleanboolean addEmployee(T employee)
{
ifif(eList.contains(employee))
{
System.out.println(employee.getName() + " is already confirmed as an employee of " + thisthis.name);
//
returnreturn falsefalse;
}
elseelse
{
eList.add(employee);
System.out.println(employee.getName() + " is an employee of " + thisthis.name);
returnreturn truetrue;
}
}
}
ArrayList list = newnew ArrayList();
//raw type -- equivalent to -- ArrayList<Object> list = new ArrayList<Object>
();
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 121 of 185http://itec2150.gitlab.io/
6.8. Wildcard Generic Types
r9/%(<*&(%$#(%&A/%&?#?(@50?<*+?$-(A/%&?#?(@50?<*+?$-(/+(0/@#+(A/%&?(@50?<*+?$(8/($J#<5G"(*(+*&'#(G/+(*('#&#+5<
8"J#1rON5*&'-(ETC[R(;3#$#(*+#(%$#G%0(5&(<#+8*5&(<*$#$(A%8(&/8(%$#?(*$(/G8#&(*$(A/%&?#?('#&#+5<(8"J#$1
;3#(G5+$8(8"J#(/G(@50?<*+?(5$(<*00#?(83#(unbounded wildcard(8"J#1(=8(5$($J#<5G5#?(%$5&'(83#(@50?<*+?(<3*+*<8#+(G/+>
?(*&?(*'*5&(58(5$(<*00#?(83#(unbounded wildcard
O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0PD*H*P'#&#+5<$P%&A/%&?#?450?<*+?$138>0R1(?(5$(83#($*>#(*$(? extends Object(O#1'1(N5$8x\
yR1(Z&A/%&?#?(8"J#(5$(%$#G%0(@3#&(*(>#83/?(?/#$(&/8($J#<5G"(@583(83#(*<8%*0(8"J#(/G(83#(J*+*>#8#+(8*,5&'1(V/+
#c*>J0#-(@3#&("/%(5&H/,#(*(>#83/?(@583(%&A/%&?#?(8"J#-(58(>#*&$(83*8(83#(>#83/?(@500(8*,#($/>#($J#<5G5<(8"J#
A%8(58(?/#$(&/8(,&/@(8500("/%(*??($J#<5G5<(8"J#1(V+/>(83#(G/00/@5&'(#c*>J0#-(J+5&8N5$8OR(>#83/?(8*,#$(05$8(@583
%&$J#<5G5#?(8"J#(*&?(58(5$(*(05$8(/G(%&A/%&?#?P&/&Q$J#<5G5<(8"J#(J*$$#?(*$(*(J*+*>#8#+(8/(*(>#83/?(83*8(8*,#$(*(05$8
/G(%&A/%&?#?(8"J#1
Container<T> is a generic Container classclass
public classclass Container<T>
{
publicpublic voidvoid set(T t){
// }
}
To create a parameterized type of *_Container<T>_*, you must provide a concrete type argument forfor the generic
type parameter T as below.
Container<Double> doubleContainer = newnew Container<>();
If the concrete type is omitted, you will create a raw type of Container<T> and it is
Container rawContainer = newnew Container();
*Using raw types are not safe, you should avoid using raw types.*
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 122 of 185http://itec2150.gitlab.io/
;3#($#</&?(8"J#(/G(@50?<*+?(5$(<*00#?((upper)bounded wildcard
O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0PD*H*P'#&#+5<$P%JJ#+`/%&?#?138>0R(*&?(58(5$($J#<5G5#?(@+585&'(r\(#c8#&?$(;r(O#1'1(x\
#c8#&?$(V//y(R1(43#&("/%(%$#(%JJ#+QA/%&?-(*+'%>#&8(<*&(A#(*&"(8"J#(/+($%A<0*$$(/G(8"J#1
;3#(835+?(8"J#(/G(@50?<*+?(5$(lower bounded wildcard
O388J$BPP?/<$1/+*<0#1</>PD*H*$#P8%8/+5*0PD*H*P'#&#+5<$P0/@#+`/%&?#?138>0R(*&?(58(5$($J#<5G5#?(@+585&'(r\($%J#+(;r(O#1'1(x\($%J#+
=&8#'#+yR
publicpublic classclass UnboundedDemo
{
publicpublic voidvoid printList(List<?> list)
{
forfor(Object e: list)
{
System.out.println(e);
}
}
publicpublic staticstatic voidvoid main(String[] ags)
{
ArrayList<Integer> list1 = newnew ArrayList<>();
list1.add(45);
list1.add(5);
list1.add(105);
printList(list1);
ArrayList<String> list2 = newnew ArrayList<>();
list2.add("Banana");
list2.add("Orange");
list2.add("Apple");
printList(list2);
}
}
JAVA
Source: https:
//tudip.com/blog-post/java-generics-lower-upper-bound/
publicpublic staticstatic voidvoid validateStringTypes(Collection<? extendsextends String> collection){
//Wild card with Upper bound
// Accept collection of objects of type string or SUB-CLASS of String
}
JAVA
Source: https:
//tudip.com/blog-post/java-generics-lower-upper-bound/
publicpublic staticstatic voidvoid validateTillStringType(Collection<? supersuper String> collection){
//Wild card with Lower bound
// Accept collection of objects of type string or SUPER-CLASS of String
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 123 of 185http://itec2150.gitlab.io/
6.9. Erasure (https://docs.oracle.com/javase/tutorial/java/generics/erasure.html)
r;3#(5&G/+>*85/&(/&('#&#+5<$(5$(%$#?(A"(83#(</>J50#+(A%8(5$(&/8(*H*50*A0#(*8(+%&85>#1(;35$(5$(<*00#?(8"J#(#+*$%+#1r
A"(2*&5#0(N5*&'1
q#&#+5<$(@#+#(5&8+/?%<#?(8/(83#(6*H*(0*&'%*'#(8/(J+/H5?#(85'38#+(8"J#(<3#<,$(*8(</>J50#(85>#(*&?(8/($%JJ/+8
'#&#+5<(J+/'+*>>5&'1(;/(5>J0#>#&8('#&#+5<$-(83#(6*H*(</>J50#+(*JJ05#$(8"J#(#+*$%+#(8/B
l#J0*<#(*00(8"J#(J*+*>#8#+$(5&('#&#+5<(8"J#$(@583(83#5+(A/%&?$(/+(UAD#<8(5G(83#(8"J#(J*+*>#8#+$(*+#(%&A/%&?#?1
;3#(J+/?%<#?(A"8#</?#-(83#+#G/+#-(</&8*5&$(/&0"(/+?5&*+"(<0*$$#$-(5&8#+G*<#$-(*&?(>#83/?$1
=&$#+8(8"J#(<*$8$(5G(&#<#$$*+"(8/(J+#$#+H#(8"J#($*G#8"1
q#&#+*8#(A+5?'#(>#83/?$(8/(J+#$#+H#(J/0">/+J35$>(5&(#c8#&?#?('#&#+5<(8"J#$1
;"J#(#+*$%+#(#&$%+#$(83*8(&/(&#@(<0*$$#$(*+#(<+#*8#?(G/+(J*+*>#8#+5a#?(8"J#$e(</&$#d%#&80"-('#&#+5<$(5&<%+(&/
+%&85>#(/H#+3#*?1
6.9.1. Class Type Erasure
2%+5&'(83#(8"J#(#+*$%+#(J+/<#$$-(83#(6*H*(</>J50#+(#+*$#$(*00(8"J#(J*+*>#8#+$(*&?(+#J0*<#(*00(8"J#(J*+*>#8#+$(5&
'#&#+5<(8"J#$(@583(83#5+(A/%&?$(/+(UAD#<8(5G(83#(8"J#(J*+*>#8#+$(*+#(%&A/%&?#?1(;3#(J+/?%<#?(A"8#</?#-
83#+#G/+#-(</&8*5&$(/&0"(/+?5&*+"(<0*$$#$-(5&8#+G*<#$-(*&?(>#83/?$1=&$#+8(8"J#(<*$8$(5G(&#<#$$*+"(8/(J+#$#+H#(8"J#
* complete example.
importimport java.util.ArrayListjava.util.ArrayList;
importimport java.util.Collectionjava.util.Collection;
publicpublic classclass GenericsDemo {
publicpublic staticstatic voidvoid validateTillStringType(Collection<? supersuper String> collection){
//Wild card with Lower bound
// Accept collection of objects of type string or SUPER-CLASS of String
}
publicpublic staticstatic voidvoid validateStringTypes(Collection<? extendsextends String> collection){
//Wild card with Upper bound
// Accept collection of objects of type string or SUB-CLASS of String
}
publicpublic staticstatic voidvoid main(String [] args){
GenericsDemo.validateTillStringType(newnew ArrayList<Object>());
//OK
// GenericsDemo.validateTillStringType(new ArrayList<Integer>());//Error
// GenericsDemo.validateStringTypes(new ArrayList<Object>());//Error
GenericsDemo.validateStringTypes(newnew ArrayList<String>());
//OK
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 124 of 185http://itec2150.gitlab.io/
$*G#8"1(q#&#+*8#(A+5?'#(>#83/?$(8/(J+#$#+H#(J/0">/+J35$>(5&(#c8#&?#?('#&#+5<(8"J#$1;"J#(#+*$%+#(#&$%+#$(83*8
&/(&#@(<0*$$#$(*+#(<+#*8#?(G/+(J*+*>#8#+5a#?(8"J#$e(</&$#d%#&80"-('#&#+5<$(5&<%+(&/(+%&85>#(/H#+3#*?1r
6.9.2. Erasure of Generic Methods
43#&(83#(</>J50#+(8+*&$0*8#$('#&#+5<(>#83/?(J+5&8(5&8/(A"8#</?#$-(58(+#>/H#$(83#(8"J#QJ*+*>#8#+(*&?(+#J0*<#(83#
8"J#(J*+*>#8#+(@583(*<8%*0(8"J#1(;35$(J+/<#$$(5$(,&/@&(*$(#+*$%+#1
Source: https:
//www.baeldung.com/java-type-erasure[Baeldung]
publicpublic staticstatic <E> booleanboolean containsElement(E [] elements, E element)
{
forfor (E e : elements)
{
ifif(e.equals(element))
{
returnreturn truetrue;
}
}
returnreturn falsefalse;
}
After the compilation, the the compiler replaces the unbound type parameter E with Object.
publicpublic classclass Stack
{
privateprivate Object[] stackContent;
publicpublic Stack(intint capacity)
{
thisthis.stackContent = (Object[]) newnew Object[capacity];
}
publicpublic voidvoid push(Object data)
{
// ..
}
publicpublic Object pop()
{
// ..
}
}
JAVA
print method with type parameter.
publicpublic staticstatic <E> voidvoid print(E[] arr)
{
forfor (E element : arr)
{
System.out.printf("%s ", element);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 125 of 185http://itec2150.gitlab.io/
6.10. Key Terms
A/%&?#?(8"J#B(F('#&#+5<(8"J#(A#5&'($J#<5G5#?(*$(*($%A8"J#(/G(*&/83#+(8"J#
%JJ#+(A/%&?#?(@50?<*+?(Ox\(#c8#&?$(byRB(A/%&?$(@583(%JJ#+(5&3#+58*&<#(</&$8+*5&8(A"(%$5&'(#8#&?$(,#"@/+?1
0/@#+(A/%&?(@50?<*+?(Ox\($%J#+(byRB(A/%&?$(5$(%$5&'(83#(@50?<*+?(<3*+*<8#+(O\R-(G/00/@5&'(A"(83#($%J#+(,#"@/+?(A"
58$(0/@#+(A/%&?1
%&A/%&?#?(@50?<*+?Ox\yRB(A/%&?$(@35<3(5$($J#<5G5#?(%$5&'(x\y1(835$(5$(<*00#?(%&,&/@&(8"J#1
+*@(8"J#B(*(&*>#(/G(*('#&#+5<(<0*$$(/+(5&8#+G*<#(@583/%8(*&"(8"J#(*+'%>#&8$1
8"J#(#+*$%+#B(83#(J+/<#$$(/G(8"J#(<3#<,5&'(/&0"(*8(</>J50#(85>#(*&?(?5$<*+?5&'(83#(#0#>#&8(8"J#(5&G/+>*85/&(*8
+%&85>#1
6.11. Exercises
6.11.1. Exercise 1 (Palindrome)
4+58#(*('#&#+5<(>#83/?(8/(</%&8(83#(&%>A#+(/G(J*05&?+/>#$(5&(*(</00#<85/&1(.8#J(CB(Z$#(*('#&#+5<(=;#$8#+(5&8#+G*<#
@583(*(>#83/?(&*>#(test(?#G5&#?(*$(G/00/@$B
J%A05<(5&8#+G*<#(=;#$8#+x;y(z(J%A05<(A//0#*&(8#$8O;(/ADRe(}
.8#J(EB(4+58#()*05&?+/>#(<0*$$(*&?(835$(<0*$$(5>J0#>#&8$('#&#+5<(=;#$8#+(5&8#+G*<#(*&?(?#G5&#(83#(8#$8(>#83/?(8/
8#$8(5G(83#(H*0%#(5$(*(J*05&?+/>#(/+(&/81
)*05&?+/># O388J$BPP@@@1>#++5*>Q@#A$8#+1</>P?5<85/&*+"PJ*05&?+/>#R(5$(*(@/+?-(H#+$#-(/+($#8#&<#(/+(*(&%>A#+(83*8(+#*?$
83#($*>#(A*<,@*+?(/+(G/+@*+?(O$%<3(*$(rl*<#(<*+r-rS*?*>(=m>(F?*>1r-rC[[Cr-rF>/+#-(l/>*r-r_5&'-(*+#("/%('0*?
"/%(*+#(,5&'\r(.8#J(IB(4+58#(*(7/%&8#+(<0*$$(*&?(?#G5&#(*('#&#+5<(>#83/?(&*>#?(countIf1(;35$(>#83/?(@500(</%&8
83#(&%>A#+(/G(#0#>#&8$(5&(*(</00#<85/&(83*8(3*H#(J*05&?+/>#$(*&?(*0$/(@500(5'&/+#(&/&Q*0J3*(<3*+*<8#+$(*&?(@358#
$J*<#$1
6.11.2. Exercise 2
4+58#(*('#&#+5<(>#83/?(8/($@*J(83#(J/$585/&$(/G(8@/(?5GG#+#&8(#0#>#&8$(5&(*&(*++*"1
By defaultdefault all generic types are replaced with type Object as you see follows.
Upon compilation, the compiler replaces the type paramter E with Object.
publicpublic staticstatic voidvoid print(Object[] arr)
{
forfor (Object element : arr)
{
System.out.printf("%s ", element);
}
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 126 of 185http://itec2150.gitlab.io/
6.11.3. Exercise 3
4+58#(*('#&#+5<(>#83/?(83*8(?5$J0*"$(*++*"(#0#>#&8
6.11.4. Exercise 4
4+58#(*('#&#+5<(>#83/?($#0#<85/&./+8(A*$#?(/&(83#($#0#<85/&./+8(>#83/?(*$(G/00/@$1(Z$#(83#('#&#+5<($@*J(>#83/?
G+/>(83#(bc#+<5$#(E1(Z$#(x;(#c8#&?$(7/>J*+*A0#x;yy(5&(83#(8"J#QJ*+*>#8#+($#<85/&(G/+(835$(>#83/?1(S*,#($%+#(%$#
</>J*+#;/(8/(</>J*+#(83#(/AD#<8$(/G(83#(8"J#(83*8(;(+#J+#$#&8$1
6.11.5. Exercise 5
4+58#(*(8#$8(J+/'+*>(83*8(5&J%8$-($/+8$-(*&?(/%8J%8$(*&(2/%A0#(*++*"(*&?(=&8#'#+(*++*"1(Z$#(83#('#&#+5<($@*J
>#83/?-('#&#+5<(?5$J0*"F++*"(>#83/?-(*&?('#&#+5<($#0#<85/&./+8(>#83/?1
!#+#(5$(83#($*>J0#(+%&B
6.12. References
`*#0?%&'1OETETR1(;3#(`*$85<$(/G(6*H*(q#&#+5<$1(l#8+5#H#?(G+/>(388J$BPP@@@1A*#0?%&'1</>PD*H*Q'#&#+5<$(6%&#*%-(61
OETCKR1(Generics: _How They Work and Why They Are Important-(l#8+5#H#?(388J$BPP@@@1/+*<0#1</>P8#<3&5<*0Q
+#$/%+<#$P*+85<0#$PD*H*PD%&#*%Q'#&#+5<$138>01
N5*&'-(21(OETC[R1(Introduction to Java: Programming and Data Structures(OCC83(#?1R1()#*+$/&
6.13. Issue Tracker/Comments
=$$%#(;+*<,#+ O388J$BPP'583%A1</>P3J*+,WP3#0JY?#$,P5$$%#$R
publicpublic staticstatic voidvoid selectionSort(doubledouble[] list)
{
forfor (intint i = 0; i < list.length - 1; i++)
{
intint smallest = i;
forfor (intint index = i + 1; index < list.length; index++)
ifif (list[index] < list[smallest])
smallest = index;
swap(list, i, smallest);
// swap smallest element into position
}
}
JAVA
Original Double array elements
[2.3, 45.6, 4.5, 11.6, 2.0]
Double array elements after the selection sort
[2.0, 2.3, 4.5, 11.6, 45.6]
Original Integer array elements
[3, 56, 1, 12, 7, 90, 45, 23]
Double array elements after the selection sort
[1, 3, 7, 12, 23, 45, 56, 90]
4/5/21, 10)50 PMIntermediate Programming
Page 127 of 185http://itec2150.gitlab.io/
7. Recursion
7.1. Learning Outcomes
Z&?#+$8*&?(83#(A*$5<$(/G(+#<%+$5/&
N#*+&(@3"(+#<%+$5/&(<*&(A#(%$#G%0
N#*+&(3/@(8/(*JJ0"(+#<%+$5/&(8/($/0H#($5>J0#(J+/A0#>$
N#*+&(@3"("/%($3/%0?(&/8(%$#(+#<%+$5/&(G/+(G5A/&*<<5(&%>A#+$(*&?(G*<8/+5*0$
7.2. Resources
7.2.1. Text and Tutorials
=&8+/?%<85/&(8/()+/'+*>>5&'(Z$5&'(6*H*Q(b5'383(b?585/&(A"(2*H5?(61(b<,B(73*J8#+(^
O388JBPP>*8313@$1#?%PD*H*&/8#$P<^P$C138>0R(73*J8#+(^(Q(N5&,#?(2*8*(.8+%<8%+#$(*&?(l#<%+$5/&
6*H*-(6*H*-(6*H*B(UAD#<8QU+5#&8#?()+/A0#>(./0H5&'(A"(l*0J3(S/+#005(*&?(l*0J3(4*0?#B
388JBPP@@@1<$18+5&</001#?%Pt+*>PDDDPDDDQ/$QETCWTMEL1J?G(73*J8#+(CE(l#<%+$5H#()+/A0#>(./0H5&'
7.2.2. Videos
6*H*(l#<%+$5/&(B(388J$BPP@@@1"/%8%A#1</>P@*8<3\H]&#%2%GY5[.'
F0'/+583>(l#<%+$5/&(B(388J$BPP@@@1"/%8%A#1</>P@*8<3\H]_bb_&WS#Q>$
7.3. Introduction
l#<%+$5/&(5$(*&(*<85/&(83*8(?#$<+5A#$(*(>#83/?(<*005&'(58$#0G1(;3#(G/00/@5&'(5$(*&($5>J0#(#c*>J0#(/G(+#<%+$5/&
@35<3(<*0<%0*8#$(83#(&Q83(G5A/&*<<5(&%>A#+
;3#(G5A/&&*<<5($#d%#&<#(?#G5&#$(*($#d%#&<#(/G(&%>A#+$(@3#+#(83#(&Q83(&%>A#+(5$(83#(*??585/&(/G(83#(O&QCR(83(*&?
O&QER(83(&%>A#+1(j/85<#(83*8(835$(+#<%+$5H#($/0%85/&(8/(<*0<%0*85&'(83#(G5A/&*<<5(&%>A#+(5$(H#+"($0/@1(=&(G*<8-(58(5$
>%<3($0/@#+(83*&(83#(58#+*85H#(O%$5&'(0//J$R(H#+$5/&1
;3#(+#*$/&(G/+(835$(5$(A#<*%$#(83#($*>#(>#83/?(<*00('#8$(<*00#?(/H#+(*&?(/H#+(*'*5&1(V/+(#c*>J0#-(<*0<%0*85&'(83#
L83(G5A/&&*<<5(&%>A#+(@/%0?(8*,#(Ln(<*00$(8/('#8V5AOR1
publicpublic intint getFib(intint num)
{
ifif(num == 1 || num == 2)
{
returnreturn 1;
}
elseelse
{
returnreturn getFib(num - 1) + getFib(num + 2);
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
4/5/21, 10)50 PMIntermediate Programming
Page 128 of 185http://itec2150.gitlab.io/
./0H5&'(J+/A0#>$(+#<%+$5H#0"(>*"(&/8(*0@*"$(+#$%08(5&(83#(>/$8(#GG5<5#&8($/0%85/&(A%8(83#(+#*$/&(+#<%+$5/&(5$
%$#G%0(5$(A#<*%$#(+#<%+$5/&(<*&(0#*?(8/($/0%85/&$(83*8(*+#(#*$"(8/(%&?#+$8*&?(*&?(5>J0#>#&81(O;35$(@500(A#
*JJ*+#&8(5&(8+*H#+$*0$(5&(A5&*+"(8+##$R
43#&($/0H5&'(J+/'+*>>5&'(+#<%+$5H#0"-("/%(>%$8(835&,(/G(83#(A*$#(<*$#(*&?(83#(+#<%+$5H#(<*$#1
Base caseB(83#($5>J0#$8(<*$#(8/(</&$5?#+
Recursive caseB(<*005&'(83#(+#<%+$5/&(>#83/?(@583(*($5>J05G5#?(O/+($>*00#+R(H*0%#
V/+(#c*>J0#-(</&$5?#+(83#(J+/A0#>(/G(G5&?5&'(83#(+#H#+$#(/G(*($8+5&'1
j/85<#(83*8(83#(A*$#(<*$#(3*&?0#$(@3#&(83#(0#&'83(/G(83#($8+5&'(5$(a#+/1(UG(</%+$#-(@3#&(83#(0#&'83(/G(83#($8+5&'(5$
a#+/-(83#(#>J8"($8+5&'(5$(+#8%+&#?1(=&(83#(+#<%+$5H#(<*$#-(83#($*>#(>#83/?(5$(<*00#?(*'*5&(@583(*(H*0%#(83*8(5$
r<0/$#+r(8/(83#(A*$#(<*$#1
!#+#(5$(*&/83#+(#c*>J0#(@3#+#(%$5&'(+#<%+$5/&(+#$%08$(5&(*($5>J0#+($/0%85/&(83*&(83#(58#+*85H#(H#+$5/&1(;3#
J+/A0#>(5$(J+5&85&'(*00(A5&*+"(&%>A#+$(@583(&Q?5'58$1
;3#(58#+*85H#(H#+$5/&(/G(J+5&85&'(*00(A5&*+"(&%>A#+$(5$(*(A58(>/+#(</>J05<*8#?(*&?(5&H/0H#$(%$5&'(*(N5$81
publicpublic String getReverseString(String str)
{
ifif (str.length() == 0)
{
returnreturn "";
}
elseelse
{
returnreturn str.substring(str.length()-1) + getReverseString(str.substring(0,str.length()-1));
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
publicpublic staticstatic voidvoid printBinaryNumber(String str, intint length)
{
ifif (length == 0)
{
System.out.println(str);
returnreturn;
}
printBinaryNumber(str + "0", length - 1);
printBinaryNumber(str + "1", length - 1);
}
JAVA
1
2
3
4
5
6
7
8
9
10
4/5/21, 10)50 PMIntermediate Programming
Page 129 of 185http://itec2150.gitlab.io/
;35$(500%$8+*8#$(83*8(+#<%+$5/&(<*&(0#*?(8/($/0%85/&$(83*8(*+#(#*$5#+(8/(%&?#+$8*&?1(4583(*(A58(/G(<+#*85H58"("/%(<*&
@+58#(#H#+"(58#+*85H#($/0%85/&(8/(*(+#<%+$5H#(/&#(*&?(H5<#QH#+$*1()0#*$#(@*8<3(83#(G/00/@5&'(H5?#/(*$(*(+#H5#@(/G(83#
A*$5<(</&<#J8$(/G(+#<%+$5/&B
Algorithms: Recursion
7.4. Permutations and Combinations
publicpublic staticstatic voidvoid printBinaryNumberIterative(intint length)
{
ArrayList<String> list = newnew ArrayList<String>();
list.add("0");
list.add("1");
intint max = (intint) Math.pow(2, length);
whilewhile (list.size() < max)
{
String temp = list.remove(0);
list.add(temp + "0");
list.add(temp + "1");
}
System.out.println(list);
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
4/5/21, 10)50 PMIntermediate Programming
Page 130 of 185http://itec2150.gitlab.io/
F(G*>/%$(*JJ05<*85/&(/G(+#<%+$5/&(5$(G/%&?(5&('#&#+*85&'(J#+>%8*85/&$1(V/+(#c*>J0#-('#&#+*85&'(*00(83#
J#+>%8*85/&$(/G(83#(@/+?(r<*+r(+#$%08$(5&(r<*+r-(r<+*r-(r*<+r-(r*+<r-(r+<*r-(*&?(r+*<r1(j/85<#(83*8(5G(83#+#(*+#(&Q
<3*+*<8#+$(5&(*(@/+?-(58(@500(+#$%08(5&(&n(J#+>%8*85/&$1(;3#(G/00/@5&'(5>*'#(500%$8+*8#$(3/@(*&(+#<%+$5/&('#&#+*8#$
83#(J#+>%8*85/&$1
!#+#(5$(*&(#c*>J0#(/G('#&#+*85&'(83#(</>A5&*85/&$(/G(*($8+5&'1(;35$(+#<%+$5H#($/0%85/&$(%$#$(*(3#0J#+(>#83/?1
publicpublic staticstatic ArrayList<String> permutations(String str)
{
ArrayList<String> result = newnew ArrayList<>();
ifif (str.length() == 1)
{
result.add(str);
returnreturn result;
}
forfor (intint i = 0; i < str.length(); i++)
{
String part = str.substring(0, i) + str.substring(i + 1);
ArrayList<String> permutes = permutations(part);
forfor (String s : permutes)
{
result.add(str.charAt(i) + s);
}
}
returnreturn result;
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
4/5/21, 10)50 PMIntermediate Programming
Page 131 of 185http://itec2150.gitlab.io/
7.5. Using Recursion in Mazes
publicpublic staticstatic ArrayList<String> combinations(String str)
{
ArrayList<String> combos = newnew ArrayList<String>();
forfor (intint i = 1; i <= str.length(); i++)
{
combinations(i, str, combos);
}
returnreturn combos;
}
publicpublic staticstatic ArrayList<String> combinations(intint length, String str, ArrayList<String> combos)
{
ifif (str.length() == length)
{
ifif (!combos.contains(str))
{
combos.add(str);
}
}
elseelse
{
forfor (intint i = 0; i < str.length(); i++)
{
String shortened = str.substring(0, i) + str.substring(i + 1);
combos = combinations(length, shortened, combos);
}
}
returnreturn combos;
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
4/5/21, 10)50 PMIntermediate Programming
Page 132 of 185http://itec2150.gitlab.io/
F&(5&8#+#$85&'(J+/A0#>(83*8(<*&(A#($/0H#?(@583(+#<%+$5/&(*+#(>*a#$1(S*a#$(<*&(A#($/0H#?(H#+"(&*8%+*00"(@583
+#<%+$5/&(*$(J0*"(58$#0G(>*J$(8/(+#<%+$5/&(H#+"(@#001(;3#(5?#*(5$(8/(8+"(/%8(*00(J*83$(*&?($#0#<8(83#(J*83(83*8(0#*?$(8/
83#(#c581(;3#(G/00/@5&'(</?#(500%$8+*8#$(3/@(+#<%+$5/&(<*&(A#(%$#?(8/(G5&?(83#($/0%85/&(8/(*(>*a#1
7.6. Summary
S*,#($%+#("/%(3*H#(*(A*$#(<*$#(*&?(<3#<,$(8/(*H/5?(5&G5&58#(+#<%+$5/&
Z&?#+$8*&?(83*8("/%(<*&(+#@+58#(*&"(+#<%+$5H#(>#83/?(58#+*85H#0"(*&?(H5<#QH#+$*
`#(*A0#(8/($/0H#($5>J0#(J+/A0#>$(%$5&'(+#<%+$5/&
7.7. Key Terms
+#<%+$5/&B(</>J%8*85/&(83*8(5&H/H0#$(*(G%&<85/&(O/+(>#83/?R(<*005&'(58$#0G
A*$#(<*$#B(83#($5>J0#$8(<*$#(5&(*(+#<%+$5H#($/0%85/&
+#<%+$5H#(<*$#B(>5++/+$(83#(/H#+*00($/0%85/&(A%8(@583($5>J05G5#?(5&J%8(H*0%#$
?5+#<8(+#<%+$5/&B(@3#&(83#($*>#(>#83/?(<*00$(58$#0G
5&?5+#<8(+#<%+$5/&B(@3#&(>/+#(83*&(/&#(>#83/?(5$(5&H/0H#?(5&(*(+#<%+$5/&
+#<%+$5H#(A*<,8+*<,5&'B(@3#&(+#<%+$5/&(5$(%$#?(8/(A%50?(*($#8(/G(<*&?5?*8#($/0%85/&$(*&?(*(<+58#+5*(5$(*JJ05#?(8/
$#0#<8(83#(+5'38(/&#$
7.8. Exercises
C1( 4+58#(*(+#<%+$5H#(>#83/?(83*8(+#H#+$#$(*($8+5&'1(V/+(#c*>J0#-("/%+(>#83/?($3/%0?(J+5&8(/%8(r+*<r('5H#&(83#
@/+?(r<*+r1
E1( =&H#$85'*8#(3/@(8/(?+*@(*(G+*<8*0(%$5&'(+#<%+$5/&1
I1( =&H#$85'*8#(83#(;/@#+(/G(!*&/5(J%aa0#(*&?($/0H#(58(%$5&'(+#<%+$5/&1
publicpublic booleanboolean findPath(Maze maze, Point position)
JAVA
1
4/5/21, 10)50 PMIntermediate Programming
Page 133 of 185http://itec2150.gitlab.io/
7.9. Issue Tracker/Comments
=$$%#(;+*<,#+ O388J$BPP'583%A1</>P3J*+,WP3#0JY?#$,P5$$%#$R
4/5/21, 10)50 PMIntermediate Programming
Page 134 of 185http://itec2150.gitlab.io/
8. Basic Data Structures and Sorting
8.1. Learning Objectives
.8%?#&8$(@500(A#(*A0#(8/B
2#G5&#(*&?(?#$<+5A#(G/%+(A*$5<(*A$8+*<8(?*8*($8+%<8%+#$B(N5$8-(.8*<,-(p%#%#(*&?()+5/+58"(p%#%#1
FJJ0"(83#(A*$5<(*A$8+*<8(?*8*($8+%<8%+#$(8/(?#H5$#(#GG5<5#&8(*0'/+583>$(G/+($/0H5&'(</>J%8*85/&*0(J+/A0#>$1
=>J0#>#&8(#GG5<5#&8(*0'/+583>$(5&H/0H5&'(83#(?*8*($8+%<8%+#$-(%$5&'(83#(<0*$$#$(*&?(>#83/?$(G/+(83#(?*8*
$8+%<8%+#$(G+/>(83#(6*H*(F)=$1
2#$<+5A#(*&?(5>J0#>#&8($5c($/+85&'(*0'/+583>$B(.#0#<85/&(./+8-(=&$#+85/&(./+8-(`%AA0#(./+8-(!#*J(./+8-(S#+'#
./+8(*&?(p%5<,(./+81
7/>J*+#(83#($/+85&'(*0'/+583>$(5&(8#+>$(/G(#GG5<5#&<"(*&?(/83#+(J+/J#+85#$($%<3(*$($8*A5058"1
FJJ0"(F++*"$1$/+8-(F++*"$1$/+8(*&?(7/00#<85/&$1$/+8(8/(J+/H5?#(#GG5<5#&8($/0%85/&$(8/(J+/A0#>$(@3#+#($/+85&'(5$
A#&#G5<5*01
8.2. Resources
8.2.1. Text
;3/>*$(7/+>#&-(73*0+#$(N#5$#+$/&-(l/&*0?(l5H#$8(*&?(705GG/+?(.8#5&1(=&8+/?%<85/&(8/(F0'/+583>$-(I+?(b?585/&1
;3#(S=;()+#$$1
2*&5#0(N5*&'1(=&8+/?%<85/&(8/(6*H*()+/'+*>>5&'(*&?(2*8*(.8+%<8%+#$-(CC83(b?585/&1()#*+$/&1
8.3. Introduction
=&(J+/'+*>>5&'-(*(?*8*($8+%<8%+#(5$(*(>#*&$(8/(/+'*&5a#(*(</00#<85/&(/G(?*8*1(F(?*8*($8+%<8%+#(&/8(/&0"($8/+#$(83#
?*8*-(A%8(*0$/(J+/H5?#$(>#83/?$(G/+(*<<#$$5&'(*&?(>*&5J%0*85&'(58$(#0#>#&8$1(43#&("/%(3*H#(*($5&'0#(/+(*(G#@
?*8*(58#>$-("/%(<*&($5>J0"(?#<0*+#(*(H*+5*A0#(/+(<+#*8#(*&(/AD#<8(8/(+#J+#$#&8(#*<3(58#>1(!/@#H#+-(?/5&'($/(5$
<0#*+0"(5&G#*$5A0#(@3#&("/%(3*H#(*(0*+'#(</00#<85/&-($*"(/G(*(>5005/&(?*8*(58#>$1(=&(835$($58%*85/&-(3/@(83#(?*8*(5$
+#J+#$#&8#?(*&?(/+'*&5a#?(5$(<+%<5*0(G/+(83#(</++#<8&#$$(*&?(#GG5<5#&<"(/G(83#(J+/'+*>1
;3#(G5+$8(?*8*($8+%<8%+#("/%(3*H#(0#*+&#?-(*&?(J#+3*J$(83#($5>J0#$8(/G(*00(?*8*($8+%<8%+#$-(5$(*&(array1(F&(*++*"(5$(*
$#d%#&<#(/G(#0#>#&8$(/G(83#($*>#(?*8*(8"J#(/<<%J"5&'(</&85'%/%$(0/<*85/&$(5&(83#(>#>/+"1(=8(5$(G%&?*>#&8*0(*&?
H#+"(</>>/&0"(%$#?-(?%#(8/(58$($5>J05<58"(*&?(83#(#GG5<5#&<"(5&(*<<#$$5&'(*&"(#0#>#&8(/G(83#(*++*"1(!/@#H#+-(*
>*D/+(?+*@A*<,(/G(*&(*++*"(5$(83*8(58(5$(statice(83*8(5$-(/&<#(*&(*++*"(5$(?#<0*+#?-(58$(0#&'83(5$(G5c#?-(*&?(/&#(<*&&/8
*??(*(&#@(#0#>#&8(8/(83#(*++*"(/+(+#>/H#(*&(#c5$85&'(#0#>#&8(G+/>(83#(*++*"1(V/+(83#(>*&"(*JJ05<*85/&$(@3#+#
83#(?*8*(&##?$(8/('+/@(O*&?($3+5&,R(?"&*>5<*00"-(@#(?#$5+#(*(dynamic(?*8*($8+%<8%+#(83*8($%JJ/+8$(#GG5<5#&8
/J#+*85/&$(G/+(5&$#+85/&(*&?(?#0#85/&-(*>/&'(/83#+(5>J/+8*&8(/J#+*85/&$1
=&(835$(<3*J8#+-(@#(5&8+/?%<#(G/%+(A*$5<(?"&*>5<(?*8*($8+%<8%+#$B(05$8$-($8*<,$-(d%#%#$(*&?(J+5/+58"(d%#%#$1(;3#
G/<%$(3#+#(5$(8/(?#G5&#(83#>(*&?(?#$<+5A#(83#5+(%$*'#1(=&(=;b7(ICLTB(F?H*&<#?()+/'+*>>5&'-("/%(@500(0#*+&(83#5+
5>J0#>#&8*85/&$(*$(@#00(*$(83#5+(#GG5<5#&<"1
4/5/21, 10)50 PMIntermediate Programming
Page 135 of 185http://itec2150.gitlab.io/
8.4. Lists
F,5&(8/(*&(*++*"-(*(05$8(5$(*(sequentially ordered(</00#<85/&(/G(OJ/$$5A0"(?%J05<*8#R(/AD#<8$1(F(G%&?*>#&8*0(?5GG#+#&<#
A#8@##&(*(05$8(*&?(*&(*++*"-(5$(83*8(*(05$8(5$(dynamic(5&(83#($#&$#(83*8(/&#(<*&(*??(&#@(#0#>#&8$(8/(*(05$8(*&?
+#>/H#(#c5$85&'(#0#>#&8$(G+/>(*(05$8-(@3#+#*$(*&(*++*"(5$($8*85<1(S/+#/H#+-(*?D*<#&8(#0#>#&8$(5&(*(05$8(>*"(not
+#$5?#(5&(</&85'%/%$(0/<*85/&$(5&(83#(>#>/+"-(*$(5&(83#(<*$#(/G(*(linked list(83*8(5$(?5$<%$$#?(A#0/@1
8.4.1. The Java List Interface
;3#(6*H*(List(5&8#+G*<#-(@35<3(5$(</&8*5&#?(5&(83#(java.util(J*<,*'#-(#c8#&?$(83#(Collection(5&8#+G*<#(*&?(J+/H5?#$
*&(*A$8+*<85/&(/G(*(05$8(?*8*($8+%<8%+#(?#$<+5A#?(*A/H#1(9/%(<*&(G5&?(*00(83#(>#83/?$(/G(83#(List(5&8#+G*<#(*$(@#00(*$
83#5+(?#8*50#?(?#$<+5J85/&$(*8(388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850PN5$8138>01
`#0/@(@#($%>>*+5a#($/>#(</>>/&0"(%$#?(>#83/?$(5&(83#(5&8#+G*<#(ListxEy-(@3#+#(E(5$(83#('#&#+5<(8"J#(/G(83#
05$8(#0#>#&8$($J#<5G5#?(*8(83#(<+#*85/&(/G(83#(05$81
A//0#*&(addOE(#R1(F??$(83#($J#<5G5#?(#0#>#&8(8/(83#(end(/G(83#(05$8-(*&?(+#8%+&$(true(5G(83#(#0#>#&8(5$
$%<<#$$G%00"(*??#?1
A//0#*&(addAllOCollectionx\(#c8#&?$(Ey(<R1(F??$(*00(83#(#0#>#&8$(/G(83#($J#<5G5#?(</00#<85/&(8/(83#(end(/G(83#
05$8(5&(83#(/+?#+(?#G5&#?(A"(83#(</00#<85/&m$(58#+*8/+-(*&?(+#8%+&$(true(5G(*00(83#(#0#>#&8$(*+#($%<<#$$G%00"(*??#?1
;3#(>#83/?(83+/@$(*(NullPointerException(5G(83#($J#<5G5#?(</00#<85/&(5$(null1
H/5?(addO5&8(5&?#c-(E(#R1(F??$(83#($J#<5G5#?(#0#>#&8(8/(83#(05$8(*8(83#($J#<5G5#?(5&?#c1(;3#(>#83/?(83+/@$(*&
IndexOutOfBoundException(5G(83#($J#<5G5#?(5&?#c(5$(/%8(/G(+*&'#-(51#1(5G(O5&?#c(x(T(~~(5&?#c(y($5a#ORR1
E(removeO5&8(5&?#cR1(l#>/H#$(*&?(+#8%+&$(83#(#0#>#&8(*8(83#($J#<5G5#?(5&?#c(G+/>(83#(05$81(;3#(>#83/?(83+/@$
*&(IndexOutOfBoundException(5G(83#($J#<5G5#?(5&?#c(5$(/%8(/G(+*&'#-(51#1(5G(O5&?#c(x(T(~~(5&?#c(y]($5a#ORR1
j/85<#(83#($05'38(?5GG#+#&<#(5&(83#(/%8Q/GQ+*&'#(</&?585/&(A#8@##&(add(*&?(remove1
A//0#*&(removeOObject(/R1(l#>/H#$(83#(G5+$8(/<<%++#&<#(/G(83#($J#<5G5#?(/AD#<8(G+/>(83#(05$8-(5G(83#(/AD#<8
#c5$8$1(;3#(>#83/?(+#8%+&$(true(5G(83#($J#<5G5#?(/AD#<8(5$(J+#$#&8(5&(83#(05$8-(*&?(false(/83#+@5$#1
E(getO5&8(5&?#cR1(l#8%+&$(83#(#0#>#&8(*8(83#($J#<5G5#?(5&?#c(5&(83#(05$81(;3#(>#83/?(83+/@$(*&
IndexOutOfBoundException(5G(83#($J#<5G5#?(5&?#c(5$(/%8(/G(+*&'#-(51#1(5G(O5&?#c(x(T(~~(5&?#c(y]($5a#ORR1
E(setO5&8(5&?#c-(E(#R1(l#J0*<#$(83#(#0#>#&8(*8(83#($J#<5G5#?(5&?#c(@583(83#($J#<5G5#?(#0#>#&8-(*&?(+#8%+&$(83#
#0#>#&8(J+#H5/%$0"($8/+#?(*8(83#($J#<5G5#?(5&?#c1(;3#(>#83/?(83+/@$(*&(IndexOutOfBoundException(5G(83#
$J#<5G5#?(5&?#c(5$(/%8(/G(+*&'#-(51#1(5G(O5&?#c(x(T(~~(5&?#c(y]($5a#ORR1
A//0#*&(containsOObject(/R1(l#8%+&$(true(5G(*&?(/&0"(5G(83#(05$8(</&8*5&$(83#($J#<5G5#?(/AD#<81
5&8(indexOfOObject(/R1(l#8%+&$(83#(5&?#c(/G(83#(G5+$8(/<<%++#&<#(/G(83#($J#<5G5#?(/AD#<8(5G(83#(/AD#<8(5$(5&(83#(05$8-
*&?(QC(/83#+@5$#1
5&8(lastIndexOfOObject(/R1(l#8%+&$(83#(5&?#c(/G(83#(0*$8(/<<%++#&<#(/G(83#($J#<5G5#?(/AD#<8(5G(83#(/AD#<8(5$(5&(83#
05$8-(*&?(QC(/83#+@5$#1
5&8(sizeOR1(l#8%+&$(83#($5a#(/G(83#(05$8-(51#1(83#(&%>A#+(/G(#0#>#&8$(5&(83#(05$81
A//0#*&(isEmptyOR1(l#8%+&$(true(5G(*&?(/&0"(5G(83#(05$8(5$(#>J8"-(51#1($5a#OR(]](T1
4/5/21, 10)50 PMIntermediate Programming
Page 136 of 185http://itec2150.gitlab.io/
H/5?(clearOR1(70#*+$(83#(05$81(j/8#(83*8(83#(5>J0#>#&8*85/&(/G(835$(>#83/?(&##?(not(+#>/H#(83#(#0#>#&8$(/G(83#
05$81(F00(58(&##?$(5$(8/(<+#*8#(*(&#@(#>J8"(05$81
A//0#*&(equalsOObject(/R1(l#8%+&$(true(5G(*&?(/&0"(5G(83#($J#<5G5#?(/AD#<8(5$(*(05$8(83*8(</&8*5&$(83#($*>#
#0#>#&8$(5&(83#($*>#(/+?#+(*$(835$(05$81
ListIteratorxEy(listIteratorOR1(l#8%+&$(*(list iterator($8*+85&'(*8(83#(A#'5&&5&'(/G(83#(05$8(83*8(*00/@$(/&#(8/
8+*H#+$#(83#(05$8(5&(A/83(?5+#<85/&$(*&?(>*&5J%0*8#(83#(05$8(#&(+/%8#1(4#(@500(?5$<%$$(83#(N5$8(=8#+*8/+(5&(>/+#
?#8*50$-(5&(83#($%A$#<85/&(?#H/8#?(8/(581
ListIteratorxEy(listIteratorO5&8(5&?#cR1(l#8%+&$(*(05$8(58#+*8/+($8*+85&'(*8(83#($J#<5G5#?(J/$585/&(5&(83#(05$81(;3#
>#83/?(83+/@$(*&(IndexOutOfBoundsException(5G(83#(5&?#c(5$(/%8(/G(+*&'#(O5&?#c(x(T(~~(5&?#c(y($5a#ORR1
8.4.2. The ArrayList and LinkedList Classes
ArrayList(*&?(LinkedList(*+#(83#(8@/(<0*$$#$(83*8(5>J0#>#&8(83#(List(5&8#+G*<#1
Array List
9/%(*0+#*?"(0#*+&#?(83#(ArrayList(<0*$$(5&(=;b7(ECKTB()+/'+*>>5&'(V%&?*>#&8*0$ O388JBPP58#<ECKT1'580*A15/PR1(9/%
0#*+&#?(3/@(8/(<+#*8#(*&(ArrayList(/AD#<8(*&?(3/@(8/(>*&5J%0*8#(*&(ArrayList(A"(<*005&'(58$(>#83/?$1(;/(*??(8/
"/%+(,&/@0#?'#-(@#(A+5#G0"(?#$<+5A#(3#+#(3/@(*&(*++*"(05$8(5$(5>J0#>#&8#?1
;3#(ArrayList(<0*$$(5>J0#>#&8$(*(05$8(@583(*(resizable array1(;3*8(5$-(83#(backing data structure(/G(*&(ArrayList(5$
*&(array1(;3#(A*<,5&'(*++*"(5$(*&(5&$8*&<#(H*+5*A0#(/G(*&(ArrayList(/AD#<8(*&?($8/+#$(*00(83#(#0#>#&8$(/G(83#(05$81
;3#+#G/+#(5&8#+&*00"-(*<<#$$5&'(*&(#0#>#&8(/G(83#(*++*"(05$8(A"(5&?#c-(@35<3(5$(+#d%5+#?(G/+(>#83/?$($%<3(*$(getO5&8
5&?#cR(*&?(setO5&8(5&?#c-(E(#R-(5$($5>J0"(*<<#$$5&'(83#(#0#>#&8(5&(83#(A*<,5&'(*++*"(A"(5&?#c1(43#&(*(&#@(#0#>#&8
5$(*??#?(8/(83#(05$8(*&?(83#(A*<,5&'(*++*"(5$(*A/%8(8/(A#(G%00-(83#(add(>#83/?(@/%0?(<+#*8#(*(new larger(*++*"
@3/$#(0#&'83(5$(C1L(85>#$(83#(0#&'83(/G(83#(O/0?R(A*<,5&'(*++*"-(</J"(*00(83#(#0#>#&8$(G+/>(83#(O/0?R(A*<,5&'(*++*"(*$
@#00(*$(83#(&#@(#0#>#&8(8/(83#(&#@(*++*"-(*&?(>*,#(83#(&#@(*++*"(83#(A*<,5&'(*++*"1(4350#(+#$5a5&'(5$(*&
#cJ#&$5H#(/J#+*85/&-($%<3(*&(#H#&8(/<<%+$(5&G+#d%#&80"1
Linked List
=&(*(linked list-(#H#+"(05$8(#0#>#&8-(%$%*00"(<*00#?(*(node-(3*$(*(?*8*(G5#0?(*&?(>*5&8*5&$(8@/(J/5&8#+$(O51#1
+#G#+#&<#$R(Q(/&#(J/5&85&'(8/(83#(J+#H5/%$(&/?#(*&?(83#(/83#+(J/5&85&'(8/(83#(&#c8(&/?#1(;3#+#(*+#(8@/($J#<5*0
&/?#$(Q(83#(head(@35<3(5$(83#(G5+$8(&/?#(/G(83#(05$8-(*&?(83#(tail(@35<3(5$(83#(0*$8(&/?#(/G(83#(05$81(;3#(&/?#(J+#H5/%$
8/(83#(3#*?(5$(null-($/(5$(83#(&/?#(&#c8(8/(83#(8*501(;3#(G/00/@5&'(G5'%+#(?#J5<8$(*(05&,#?(05$8(/G(5&8#'#+(#0#>#&8$1
4/5/21, 10)50 PMIntermediate Programming
Page 137 of 185http://itec2150.gitlab.io/
=&(83#(G5'%+#-(#*<3($d%*+#(+#J+#$#&8$(*(&/?#(/G(83#(05$81(;3#(0#G8>/$8(&/?#(5$(83#(3#*?(*&?(83#(+5'38>/$8(/&#(5$(83#
8*501(;3#(&%>A#+(5&$5?#(#*<3(&/?#(+#J+#$#&8$(83#(H*0%#(/G(58$(?*8*-(*&?(83#(&%>A#+(*A/H#(+#J+#$#&8$(58$(5&?#c1(F8
#*<3(&/?#-(83#(*++/@(J/5&85&'(8/(83#(+5'38(+#J+#$#&8$(83#(J/5&8#+(8/(83#(&#c8(&/?#-(*&?(83*8(J/5&85&'(8/(83#(0#G8
+#J+#$#&8$(83#(J/5&8#+(8/(83#(J+#H5/%$(&/?#1(V+/>(#*<3(&/?#(/&#(<*&(?5+#<80"(*<<#$$(A/83(&#5'3A/+$(A"(G/00/@5&'
83#(J/5&8#+$1(;3#(8@/(&#5'3A/+$(*+#(3/@#H#+(83#(only(&/?#$(83*8(*+#(?5+#<80"(*<<#$$5A0#(G+/>(*(&/?#-(A#<*%$#(83#
&/?#$(/G(*(05&,#?(05$8(</%0?(+#$5?#(5&(arbitrary(>#>/+"(0/<*85/&$-(*$(/JJ/$#?(8/(</&85'%/%$(>#>/+"(0/<*85/&$(*$(5&
83#(<*$#(/G(*&(*++*"(05$81(;3#+#G/+#-(*<<#$$5&'(#0#>#&8$(/G(*(05&,#?(05$8(A"(5&?#c-(@35<3(5$(+#d%5+#?(G/+(>#83/?$($%<3
*$(getO5&8(5&?#cR(*&?(setO5&8(5&?#c-(E(#R-(5$(*($0/@(/J#+*85/&-(*$(58(+#d%5+#$(8/(8+*H#+$#(83#(#&85+#(05$8(8/($#*+<3(G/+
83#(&/?#(*8(83#($J#<5G5#?(5&?#c1
=&(J+5&<5J0#-(*(05&,#?(05$8(<*&(A#(+#J+#$#&8#?(A"(#583#+(58$(3#*?(/+(58$(8*50-(A#<*%$#(G+/>(#583#+(#&?(/&#(<*&(8+*H#+$#
83#(#&85+#(05$81(V/+(</&H#&5#&<#(*&?(#GG5<5#&<"(3/@#H#+-(*($8*&?*+?(5>J0#>#&8*85/&-(*$(83#(/&#(G/+(83#(6*H*
LinkedList(<0*$$-(8+*<,$(A/83(83#(3#*?(*&?(83#(8*50(/G(*(05&,#?(05$8(/AD#<8(*$(58$(J+5H*8#(5&$8*&<#(H*+5*A0#$-(*$
500%$8+*8#?(A"(83#(8@/(A0%#(*++/@$(5&(83#(*A/H#(G5'%+#1
9/%(<*&(G5&?(*00(83#(>#83/?$(/G(83#(LinkedList(<0*$$(*$(@#00(*$(83#5+(?#8*50#?(?#$<+5J85/&$(*8
388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850PN5&,#?N5$8138>01
`#<*%$#(58(5$(#*$"(8/(*<<#$$(*&?(%J?*8#(A/83(#&?$(/G(*(05&,#?(05$8-(in addition to(83#(</>>/&(>#83/?$(/G(83#(List
5&8#+G*<#-(83#(LinkedList(<0*$$(J+/H5?#$(83#(G/00/@5&'(>#83/?$(involving just the head or the tail(/G(*
LinkedListxEy(/AD#<8-(@3#+#(E(5$(83#('#&#+5<(8"J#(/G(83#(05$8(#0#>#&8$1
H/5?(addFirstOE(#R1(F??$(83#($J#<5G5#?(#0#>#&8(8/(83#(head(/G(83#(05$81(FG8#+(835$(>#83/?-(*(&#@(3#*?(5$(<+#*8#?
*&?(J/5&8$(8/(83#(/0?(3#*?(*$(58$($%<<#$$/+1(;3#(&#@0"(*??#?(#0#>#&8(&/@(+#$5?#$(*8(83#(3#*?(/G(83#(05$81
H/5?(addLastOE(#R1(F??$(83#($J#<5G5#?(#0#>#&8(8/(83#(tail(/G(83#(05$81(.*>#(*$(83#(>#83/?(addOE(#R(O#c<#J8(G/+(83#
+#8%+&(8"J#R1(FG8#+(835$(>#83/?-(*(&#@(8*50(5$(<+#*8#?(*&?(J/5&8$(8/(83#(/0?(8*50(*$(58$(J+#?#<#$$/+1(;3#(&#@0"
*??#?(#0#>#&8(&/@(+#$5?#$(*8(83#(8*50(/G(83#(05$81
E(removeFirstOE(#R1(l#>/H#$(*&?(+#8%+&$(83#(#0#>#&8(*8(83#(head(/G(05$81(FG8#+(835$(>#83/?-(83#($%<<#$$/+(O5&
83#(/+5'5&*0(05$8R(8/(83#(+#>/H#?(3#*?(A#</>#$(83#(&#@(3#*?(/G(83#(05$81(;3#(>#83/?(83+/@$(*
NoSuchElementException(5G(83#(05$8(5$(#>J8"1
E(removeLastOE(#R1(l#>/H#$(*&?(+#8%+&$(83#(#0#>#&8(*8(83#(tail(/G(05$81(FG8#+(835$(>#83/?-(83#(J+#?#<#$$/+(O5&
83#(/+5'5&*0(05$8R(8/(83#(+#>/H#?(8*50(A#</>#$(83#(&#@(8*50(/G(83#(05$81(;3#(>#83/?(83+/@$(*
NoSuchElementException(5G(83#(05$8(5$(#>J8"1
E(getFirstOR1(l#8%+&$(83#(?*8*(*8(83#(head(/G(83#(05$81(;3#(>#83/?(83+/@$(*(NoSuchElementException(5G(83#(05$8
5$(#>J8"1
E(getLastOR1(l#8%+&$(83#(?*8*(*8(83#(tail(/G(83#(05$81(;3#(>#83/?(83+/@$(*(NoSuchElementException(5G(83#(05$8(5$
#>J8"1
F$(83#(ArrayListxEy(<0*$$-(83#(LinkedListxEy(<0*$$(3*$(83#(G/00/@5&'(8@/(</&$8+%<8/+$1
;3#(?#G*%08(</&$8+%<8/+(LinkedListOR(<+#*8#$(*&(#>J8"(05&,#?(05$81
4/5/21, 10)50 PMIntermediate Programming
Page 138 of 185http://itec2150.gitlab.io/
;3#(</&$8+%<8/+(LinkedListOCollectionx\(#c8#&?$(Ey(<R(<+#*8#$(*(05&,#?(05$8(83*8(</&8*5&$(83#(#0#>#&8$(/G(83#
$J#<5G5#?(</00#<85/&-(5&(83#($*>#(/+?#+(*$(?#G5&#?(A"(83#(</00#<85/&m$(58#+*8/+1
8.4.3. The List Iterator
;3#(ListIterator(5&8#+G*<#($J#<5G5#$(*&(58#+*8/+(83*8(*00/@$(/&#(8/(8+*H#+$#(*(05$8(5&(A/83(?5+#<85/&$(*&?(>*&5J%0*8#
83#(05$8(#&(+/%8#1(=8$(5>J0#>#&8*85/&(?#J#&?$(/&(83#(8"J#(/G(83#(%&?#+0"5&'(05$81(43#&(83#(05$8(5$(*&(*++*"(05$8-(58$
58#+*8/+(8+*H#+$#$(83#(05$8(%$5&'(83#(5&?5<#$(/G(83#(A*<,5&'(*++*"1(43#&(83#(05$8(5$(*(05&,#?(05$8-(58$(58#+*8/+(?/#$($/(A"
G/00/@5&'(83#(G/+@*+?(/+(A*<,@*+?(J/5&8#+$(/G(83#(05$8(&/?#$1(;3#(05$8(58#+*8/+(5$(<+%<5*0(*$(58(%&?#+05#$(#H#+"
>#83/?(83*8(+#d%5+#$(8/($#*+<3(83#(05$81
F8(388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850PN5$8=8#+*8/+138>0-("/%(<*&(G5&?(*(?#8*50#?(?#$<+5J85/&(/G(83#
ListIteratorxEy(5&8#+G*<#1(`#0/@(@#($%>>*+5a#(58$(>#83/?$1
A//0#*&(hasNextOR1(l#8%+&$(8+%#(5G(*&?(/&0"(5G(83#(05$8(3*$(>/+#(#0#>#&8$(5&(83#(G/+@*+?(?5+#<85/&(O51#1(83#
<%+$/+(5$(&/8(*8(83#(#&?(/G(83#(05$8R1
E(nextOR1(l#8%+&$(83#(&#c8(#0#>#&8(5&(83#(05$8(*&?(*?H*&<#$(83#(<%+$/+(G/+@*+?-(5G(83#(&#c8(#0#>#&8(#c5$8$(O51#1(5G
hasNextOR(@/%0?(+#8%+&(8+%#R1(U83#+@5$#(83#(>#83/?(83+/@$(*(NoSuchElementException1
5&8(nextIndexOR1(l#8%+&$(83#(5&?#c(/G(83#(&#c8(#0#>#&8(5&(83#(05$8(5G(83#(58(#c5$8$(O51#1(5G(hasNextOR(@/%0?(+#8%+&
8+%#Re(/83#+@5$#(+#8%+&$(83#($5a#(/G(83#(05$81
A//0#*&(hasPreviousOR1(l#8%+&$(8+%#(5G(*&?(/&0"(5G(83#(05$8(3*$(>/+#(#0#>#&8$(5&(83#(A*<,@*+?(?5+#<85/&(O51#1(83#
<%+$/+(5$(&/8(*8(83#(A#'5&&5&'(/G(83#(05$8R1
E(previousOR1(l#8%+&$(83#(J+#H5/%$(#0#>#&8(5&(83#(05$8(*&?(>/H#$(83#(<%+$/+(A*<,@*+?-(5G(83#(J+#H5/%$(#0#>#&8
#c5$8$(O51#1(5G(hasPreviousOR(@/%0?(+#8%+&(8+%#R1(U83#+@5$#(83#(>#83/?(83+/@$(*(NoSuchElementException1
5&8(previousIndexOR1(l#8%+&$(83#(5&?#c(/G(83#(J+#H5/%$(#0#>#&8(5&(83#(05$8(5G(83#(58(#c5$8$(O51#1(5G(hasPreviousOR
@/%0?(+#8%+&(8+%#Re(/83#+@5$#(+#8%+&$(QC1
H/5?(addOE(#R1(F??$(83#($J#<5G5#?(#0#>#&8(A#G/+#(83#(&#c8(#0#>#&8(*&?(*G8#+(83#(J+#H5/%$(#0#>#&8-(5G(83#"(A/83
#c5$81(;3#(#0#>#&8(5$(*??#?(8/(83#(A#'5&&5&'(O+#$J1(#&?R(/G(83#(05$8(5G(83#(<%+$/+(5$(*8(83#(A#'5&&5&'(O+#$J1(#&?R(/G
83#(05$81
H/5?(removeOR1(l#>/H#$(83#(0*$8(#0#>#&8(83*8(@*$(+#8%+&#?(A"(nextOR(/+(previousOR1(;3#(>#83/?(83+/@$(*&
IllegalStateException(5G(&#583#+(nextOR(&/+(previousOR(3*$(A##&(<*00#?1
H/5?(setOE(#R1(l#J0*<#$(83#(0*$8(#0#>#&8(+#8%+&#?(A"(nextOR(/+(previousOR(@583(83#($J#<5G5#?(#0#>#&81(;3#
>#83/?(83+/@$(*&(IllegalStateException(5G(&#583#+(nextOR(&/+(previousOR(3*$(A##&(<*00#?1
8.4.4. Putting Things Together By Examples
4#(&/@(%$#(*(G#@(</?#(#c*>J0#$(8/(500%$8+*8#(3/@(8/(>*&5J%0*8#(05$8$(@583($/>#(/G(83#(*G/+#>#&85/&#?(>#83/?$1
Basic list operations
=&(83#(G5+$8(#c*>J0#(A#0/@-(@#(?#>/&$8+*8#(3/@(8/(<+#*8#(*(05$8-(*??(#0#>#&8$(8/(*&?(+#>/H#(#0#>#&8$(G+/>(*(05$8-
*&?(8+*H#+$#(*(05$8(5&(A/83(?5+#<85/&$(%$5&'(58$(05$8(58#+*8/+1
4/5/21, 10)50 PMIntermediate Programming
Page 139 of 185http://itec2150.gitlab.io/
;3#(/%8J%8(/G(83#(J+/'+*>(5$
importimport java.util.*java.util.*;
publicpublic classclass ListDemo {
publicpublic staticstatic voidvoid main(String[] args) {
List<Integer> arrayList = newnew ArrayList<>();
forfor (intint i = 0; i < 10; i++) {
arrayList.add(i);
}
System.out.println("arrayList:");
System.out.println(arrayList);
arrayList.add(0, 10);
arrayList.add(3, 30);
System.out.println("\narrayList after add(0, 10) and add(3, 30):");
System.out.println(arrayList);
LinkedList<Object> linkedList = newnew LinkedList<>(arrayList);
System.out.println("\nlinkedList:");
System.out.println(linkedList);
linkedList.add(3, "hello");
linkedList.remove(4);
System.out.println("\nlinkedList after add(3, \"hello\") and remove(4):");
System.out.println(linkedList);
linkedList.removeFirst();
linkedList.removeLast();
linkedList.removeFirst();
linkedList.removeLast();
System.out.println("\nlinkedList after the removeFirst and removeLast calls:");
System.out.println(linkedList);
linkedList.addFirst("red");
linkedList.addFirst("green");
linkedList.addFirst("blue");
linkedList.addLast("red");
linkedList.addLast("green");
linkedList.addLast("blue");
System.out.println("\nlinkedList after the addFirst and addLast calls:");
System.out.println(linkedList);
System.out.println("\nDisplay the linked list forward:");
ListIterator<Object> listIterator = linkedList.listIterator();
whilewhile (listIterator.hasNext()) {
System.out.print(listIterator.next() + " ");
}
System.out.println("\n\nDisplay the linked list backward:");
listIterator = linkedList.listIterator(linkedList.size());
whilewhile (listIterator.hasPrevious()) {
System.out.print(listIterator.previous() + " ");
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
4/5/21, 10)50 PMIntermediate Programming
Page 140 of 185http://itec2150.gitlab.io/
4#(&/@(#cJ0*5&(83#(*A/H#(</?#(*&?(58$(/%8J%81(U&(N5&#$(M(Q(^-(83#(J+/'+*>(<+#*8#$(arrayList-(*&(#>J8"(*++*"(05$8
/G(5&8#'#+(#0#>#&8$(%$5&'(83#(?#G*%08(</&$8+%<8/+-(83#&(*??$(5&8#'#+$(T(8/(^(8/(83#(05$8(5&(835$(/+?#+1(;3#&(/&(N5&#$
CI(Q(CK-(58(<*00$(arrayList.addOT-(CTR(*&?(arrayList.addOI-(ITR(8/(*??(83#(&#@(#0#>#&8$(CT(*&?(IT(8/(83#(05$8(*8(83#
$J#<5G5#?(5&?5<#$1(j/8#(83*8(CT(5$(G5+$8(*??#?(8/(83#(A#'5&&5&'(O51#1(5&?#c(TR(/G(83#(05$8-(83#&(IT(5$(*??#?(*8(5&?#c(I(/G
83#(+#$%085&'(05$81(;3#(#0#>#&8(E-(@35<3(@*$(*8(5&?#c(I(A#G/+#(arrayList.addOI-(ITR(@*$(<*00#?-(&/@(>/H#$(8/(83#
&#c8(5&?#c(?/@&(83#(05$8-($/(?/#$(#H#+"(#0#>#&8(*G8#+(581
j#c8(/&(N5&#(C[-(83#(J+/'+*>(<+#*8#$(linkedList-(*(05&,#?(05$8(/G(UAD#<8$(G+/>(arrayList-(%$5&'(83#($#</&?
</&$8+%<8/+(LinkedListOCollectionx\(#c8#&?$(Ey(<R1(.5&<#(#H#+"(<0*$$(5$(*(?#$<#&?#&8(/G(83#(Object(<0*$$-(835$(05$8
*00/@$(*&"(/AD#<8(8/(A#(*??#?1(F8(835$(J/5&8-(linkedList(*&?(arrayList(</&8*5&(83#($*>#(#0#>#&8$(5&(83#($*>#
/+?#+-(#H#&(83/%'3(5&8#+&*00"(83#"(*+#(+#J+#$#&8#?(?5GG#+#&80"1(j/8#(83*8(@3#&("/%(%$#(System.out.println(8/
J+5&8(*(05$8-(58(5>J05<580"(8+*H#+$#(83#(05$8(%$5&'(58$(58#+*8/+1
j#c8(/&(N5&#$(EE(Q(EI-(83#(J+/'+*>(<*00$(linkedList.addOI-(r3#00/rR(*&?(linkedList.removeOKR(8/(*??(83#($8+5&'
r3#00/r(*8(5&?#c(I(/G(83#(05$8(*&?(83#&(+#>/H#(83#(#0#>#&8(*8(5&?#c(K(G+/>(83#(+#$%085&'(05$81(FG8#+(r3#00/r(5$(*??#?(*8
5&?#c(I-(83#(#0#>#&8(IT-(@35<3(@*$(*8(5&?#c(I(A#G/+#(linkedList.addOI-(r3#00/rR(@*$(<*00#?-(&/@(>/H#$(8/(5&?#c(K1
;3#+#G/+#-(83#($%A$#d%#&8(<*00(linkedList.removeOKR(+#>/H#$(83#(#0#>#&8(IT(G+/>(83#(05$81
F(<*+#G%0(+#*?#+(>*"(3*H#(&/85<#?(83*8(%&05,#(83#(/AD#<8(arrayList(@35<3(@*$(?#<0*+#?(/&(N5&#(M(*$(*(List-(83#
/AD#<8(linkedList(@*$(?#<0*+#?(/&(N5&#(C[(*$(*(LinkedList1(;35$(5$(A#<*%$#(@#(@*&8(8/(<*00(>#83/?$(/&(58(83*8(*+#
&/8($J#<5G5#?(5&(83#(List(5&8#+G*<#(A%8(J#+8*5&(/&0"(8/(83#(LinkedList(<0*$$1(j#c8(/&(N5&#$(EW(Q(IT-(83#(J+/'+*>(<*00$
linkedList.removeFirstOR(*&?(linkedList.removeLastOR-(83#&(+#J#*8$(83#>1(;35$($#d%#&<#(/G(<*00$(+#>/H#$(83#
G5+$8(8@/(#0#>#&8$(*&?(83#(0*$8(8@/(#0#>#&8$(/G(83#(05$81
arrayList:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
arrayList after add(0, 10) and add(3, 30):
[10, 0, 1, 30, 2, 3, 4, 5, 6, 7, 8, 9]
linkedList:
[10, 0, 1, 30, 2, 3, 4, 5, 6, 7, 8, 9]
linkedList after add(3, "hello") and remove(4):
[10, 0, 1, hello, 2, 3, 4, 5, 6, 7, 8, 9]
linkedList after the removeFirst and removeLast calls:
[1, hello, 2, 3, 4, 5, 6, 7]
linkedList after the addFirst and addLast calls:
[blue, green, red, 1, hello, 2, 3, 4, 5, 6, 7, red, green, blue]
Display the linked list forward:
blue green red 1 hello 2 3 4 5 6 7 red green blue
Display the linked list backward:
blue green red 7 6 5 4 3 2 hello 1 red green blue
4/5/21, 10)50 PMIntermediate Programming
Page 141 of 185http://itec2150.gitlab.io/
j#c8(/&(N5&#$(IK(Q(KT-(83#(J+/'+*>(<*00$(83#(>#83/?$(linkedList.addFirst(*&?(linkedList.addLast(8/(*??($8+5&'$
r+#?r-(r'+##&r(*&?(rA0%#r(8/(83#(G+/&8(*&?(A*<,(/G(83#(05$8(+#$J#<85H#0"1(j/85<#(83*8(addLast(*??$(83#(&#@(#0#>#&8$
8/(A*<,(5&(83#(/+?#+('5H#&-(@3#+#*$(addFirst(*??$(83#(#0#>#&8$(8/(83#(G+/&8(5&(83#(reverse(/+?#+1
V5&*00"(/&(N5&#$(KK(Q(LK-(83#(J+/'+*>(J+5&8$(linkedList(G/+@*+?(*&?(A*<,@*+?-(#cJ05<580"(%$5&'(58$(05$8(58#+*8/+(8/
8+*H#+$#(83#(05$8(5&(A/83(?5+#<85/&$1
Multiple ways to traverse a list
;3#(&#c8(#c*>J0#(5$(*($>5J0#(>#83/?(83*8(</>J%8#$(83#($%>(/G(83#(#0#>#&8$(/G(*(05$81
;3#(>#83/?(sum(#cJ05<580"(%$#$(83#(58#+*8/+(/G(83#('5H#&(05$8(8/(8+*H#+$#(83#(05$8(*&?($%>$(83#(#0#>#&8$(#&(+/%8#1
F08#+&*85H#0"(*&?(#d%5H*0#&80"-(/&#(<*&(@+58#(83#(>#83/?(%$5&'(*(foreach(0//J(*$(G/00/@$1
F083/%'3(83#(>#83/?(sum2(?/#$(&/8(#cJ05<580"(%$#(*(05$8(58#+*8/+-(83#(foreach(0//J
5>J05<580"(%$#$(83#(58#+*8/+(/G(83#('5H#&(05$8(8/(8+*H#+$#(83#(05$81(;3#+#G/+#-(#H#&(83/%'3(83#"(*+#(@+588#&(?5GG#+#&80"-
83#(>#83/?$(sum(*&?(sum2(*+#(5?#&85<*0(5&(8#+>$(/G(#c#<%85/&1
;3#(G/00/@5&'(5$("#8(*&/83#+(@*"(8/(@+58#(83#(>#83/?1
publicpublic staticstatic intint sum(List<Integer> list) {
intint sum = 0;
ListIterator<Integer> listIterator = list.listIterator();
whilewhile (listIterator.hasNext()) {
sum += listIterator.next();
}
returnreturn sum;
}
JAVA
1
2
3
4
5
6
7
8
9
publicpublic staticstatic intint sum2(List<Integer> list) {
intint sum = 0;
forfor (intint x : list) {
sum += x;
}
returnreturn sum;
}
JAVA
1
2
3
4
5
6
7
8
forfor (intint x : list) {
sum += x;
}
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 142 of 185http://itec2150.gitlab.io/
!/@#H#+-(@350#(G%&<85/&*00"(#d%5H*0#&8(8/(sum(*&?(sum2-(83#(>#83/?(sum3($3/%0?(not(A#(%$#?(A#<*%$#(58(5$
inefficient(@3#&(83#('5H#&(05$8(5$(*(linked list1(F$(>#&85/&#?(#*+05#+-(#0#>#&8$(/G(*(05&,#?(05$8(*+#(not(?5+#<80"
*<<#$$5A0#(A"(5&?#c1(;3#+#G/+#-(5&(83#(0//J(/G(sum3-(each(<*00(/G(list.getO5R(+#d%5+#$(*($#*+<3(/H#+(83#(entire(05$8(G/+
83#(#0#>#&8(*8(5&?#c(51(!#&<#(@3#&("/%(&##?(8/(58#+*8#(/H#+(*(05&,#?(05$8-(/+(*(05$8(5&('#&#+*0(@3/$#(8"J#(5$
%&,&/@&-("/%($3/%0?(never do so by indicese(5&$8#*?("/%($3/%0?(#583#+(#cJ05<580"(%$#(83#(iterator(/G(83#(05$8-(/+(%$#
*(foreach(0//J(83*8(@/%0?(%$#(83#(05$8(58#+*8/+(5>J05<580"1
F(foreach(0//J(5$(&5<#(A#<*%$#(58(5$(#GG5<5#&8(*&?(*0$/($5>J05G5#$(83#(</?#(@3#&(58(5$(*JJ05<*A0#1(!/@#H#+-(/&#
05>58*85/&(/G(*(foreach(0//J(5$(83*8(58(only(*00/@$("/%(8/(*<<#$$(*(?*8*($8+%<8%+#(A%8(?/#$(not(*00/@("/%(8/(>/?5G"(581
;3#+#G/+#-(8/(#GG5<5#&80"($#*+<3(*&?(%J?*8#(*&(#&85+#(05$8-("/%(>*"($8500(&##?(8/(%$#(58$(58#+*8/+(#cJ05<580"1
Using an iterator to add and remove - an illustration
;3#(&#c8(#c*>J0#(5$(*($5>J0#(>#83/?(83*8(+#>/H#$(*00(83#(/??(&%>A#+$(G+/>(*('5H#&(05$8(/G(5&8#'#+$1(;35$(>#83/?
>%$8(#cJ05<580"(%$#(83#(58#+*8/+(/G(83#(05$8(A#<*%$#(/G(83#(&##?(8/(>/?5G"(83#(05$81
=&(83#(0*$8(#c*>J0#(/G(83#($#<85/&-(@#(@+58#(*(>#83/?
83*8(*??$(83#($J#<5G5#?(#0#>#&8(*8(83#($J#<5G5#?(5&?#c(/G(83#('5H#&(05$81(;3#(#GG#<8(/G(addAtIndexOlist-(5&?#c-
#0#>#&8R(5$(5?#&85<*0(8/(83*8(/G(list.addO5&?#c-(#0#>#&8R1(!/@#H#+-(addAtIndexOlist-(5&?#c-(#0#>#&8R(?/#$(not(<*00
List.addO5&?#c-(#0#>#&8R(/+(%$#(*&"(>#83/?(/G(83#(05$8(/83#+(83*&(58$(05$8(58#+*8/+1(=8(/&0"(<*00$(83+##(>#83/?$(/G(83#
publicpublic staticstatic intint sum3(List<Integer> list) {
intint sum = 0;
forfor (intint i = 0; i < list.size(); i++) {
sum += list.get(i);
}
returnreturn sum;
}
JAVA
1
2
3
4
5
6
7
8
publicpublic staticstatic voidvoid removeOdds(List<Integer> list) {
ListIterator<Integer> listIterator = list.listIterator();
whilewhile (listIterator.hasNext()) {
intint x = listIterator.next();
ifif (x % 2 != 0) {
listIterator.remove();
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
<E> voidvoid addAtIndex(List<E> list, intint index, E element)
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 143 of 185http://itec2150.gitlab.io/
05$8(58#+*8/+B(nextIndexOR-(nextOR(*&?(addOR1(;3#+#G/+#-(83#(</?#(500%$8+*8#$(3/@(8/(5>J0#>#&8(83#(addO5&?#c-
#0#>#&8R(>#83/?(G/+(*(05$8(%$5&'(58$(58#+*8/+(/&0"1
;3#(>#83/?(83+/@$(*&(=&?#cU%8UG`/%&?$bc<#J85/&(5G(83#($J#<5G5#?(5&?#c(5$(/%8$5?#(83#(H*05?(+*&'#(/G(5&?5<#$1(=G
83#(5&?#c(5$(@5835&(83#(H*05?(+*&'#-(83#(@350#(0//J(*?H*&<#$(83#(<%+$/+(/G(83#(05$8(58#+*8/+(%&850(83#(J/$585/&
5>>#?5*8#0"(A#G/+#(83#(#0#>#&8(<%++#&80"(/<<%J"5&'(83#(5&?#c(5G(83#(5&?#c(x(05$81$5a#OR-(/+(8/(83#(#&?(/G(83#(05$8(5G
83#(5&?#c(#d%*0$(05$81$5a#OR1(U&<#(83#(>#83/?(#c58$(83#(0//J-(58(*??$(83#($J#<5G5#?(#0#>#&8(*8(83#(+5'38(J0*<#1
;3#(*A/H#(5>J0#>#&8*85/&(/G(83#(>#83/?(*0@*"$($8*+8$(83#(05$8(58#+*8/+(*8(83#(G+/&8(/G(83#(05$81(=8(5$($0/@(@3#&(83#
$J#<5G5#?(5&?#c(5$(&#*+(83#(A*<,(/G(83#(05$81(U&#(<*&(>*,#(*&(5>J+/H#>#&8(8/(83#(>#83/?(A"($8*+85&'(83#(58#+*8/+(*8
83#(#&?(83*8(5$(<0/$#+(8/(83#($J#<5G5#?(5&?#c1(4#(0#*H#(835$(*$(*&(#c#+<5$#1
8.4.5. Notes on Eciency: Array List vs. Linked List
4#(</&<0%?#(83#($#<85/&(/&(05$8$(@583($/>#(+#>*+,$(/&(83#(#GG5<5#&<"(/G(*++*"(05$8$(*&?(05&,#?(05$8$1(F<<#$$5&'
#0#>#&8$(/G(*&(*++*"(05$8(A"(5&?#c(5$(H#+"(#GG5<5#&8(A#<*%$#(58$(A*<,5&'(?*8*($8+%<8%+#(5$(*&(*++*"1(U&(83#(/83#+
3*&?-(*$(*0+#*?"(>#&85/&#?-(*<<#$$5&'(#0#>#&8$(/G(*(05&,#?(05$8(A"(5&?#c(5$(H#+"(5&#GG5<5#&8-(A#<*%$#(58(+#d%5+#$(8/
$#*+<3(83#(#&85+#(05$8(G/+(83#(5&?#c1(7/&$#d%#&80"-(G/+(*JJ05<*85/&$(83*8(+#d%5+#(>*&"(random accesses(8/(83#
#0#>#&8$(/G("/%+(05$8-(*&(*++*"(05$8(5$(J+#G#++#?1
U&(83#(/83#+(3*&?-(*(05&,#?(05$8(5$(>/+#(#GG5<5#&8(G/+(*??5&'(8/(/+(+#>/H5&'(G+/>(83#(two ends(/G(83#(05$81(V/+(*&
*++*"(05$8-(@350#(+#>/H5&'(G+/>(83#(A*<,(5$(G*$8(*&?(*??5&'(8/(83#(A*<,(5$(%$%*00"(G*$8-(*??5&'(8/(83#(G+/&8(/+
+#>/H5&'(G+/>(83#(G+/&8(5$(H#+"($0/@(*$(58(+#d%5+#$(8/($35G8(83#(#&85+#(05$81
N5$8$(*+#(83#($5>J0#$8('#&#+*0QJ%+J/$#(?*8*($8+%<8%+#$(83*8(*00/@(/&#(8/($#*+<3(G/+(*&?(%J?*8#(#0#>#&8$-(*??(&#@
#0#>#&8$(*&?(+#>/H#(#c5$85&'(#0#>#&8$1(!/@#H#+(all(83#(>#83/?$(/G(*(05$8(+#d%5+5&'(*($#*+<3(/G(*&(#0#>#&8-(*+#
inefficient-(@3#83#+(83#(%&?#+0"5&'(05$8(5$(*&(*++*"(05$8(/+(*(05&,#?(05$81(;3#$#(>#83/?$(5&<0%?#(containsOObject(/R-
removeOObject(/R-(indexOfOObject(/R-(*&?($#H#+*0(/83#+$1(=&(83#(@/+$8(<*$#(/&#(&##?$(8/($#*+<3(83#(#&85+#(05$8(G/+
83#('5H#&(/AD#<8(/G(5&8#+#$81(;3#+#G/+#-(05$8$(*+#(&/8(5?#*0(G/+(83#(>*&"(*JJ05<*85/&$(83*8(+#d%5+#(*(0*+'#(&%>A#+(/G
$#*+<3-(%J?*8#-(5&$#+85/&(*&?(?#0#85/&(/J#+*85/&$1(V/+($%<3(*JJ05<*85/&$("/%(@/%0?(?#$5+#(*&(#GG5<5#&8(?*8*
$8+%<8%+#($%<3(*$(*(hash map(/+(*(balanced binary search tree-(@35<3("/%(@500(0#*+&(5&(=;b7(ICLTB(F?H*&<#?
)+/'+*>>5&'1
8.5. Stacks
publicpublic staticstatic <E> voidvoid addAtIndex(List<E> list, intint index, E element) {
ifif (index < 0 || index > list.size()) {
throwthrow newnew IndexOutOfBoundsException("Index " + index + " is out of bound");
}
ListIterator<E> listIterator = list.listIterator();
whilewhile (listIterator.nextIndex() < index) {
listIterator.next();
}
listIterator.add(element);
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
4/5/21, 10)50 PMIntermediate Programming
Page 144 of 185http://itec2150.gitlab.io/
F($8*<,(5$(*(H#+"($J#<5*0(05$8(@583(/&#(#&?(r<0/$#?r-($/(83*8(58(5$(/&0"(*<<#$$5A0#(G+/>(83#(/83#+(#&?1(;/(H5$%*05a#(*
$8*<,-(@#(#&H5$5/&(*(H#+85<*0($8*<,(/G(J0*8#$-(@3#+#(*??5&'(/+(+#>/H5&'(5$(/&0"(J/$$5A0#(*8(83#(8/J1(.##(83#(G/00/@5&'
5>*'#(G+/>(45,5J#?5* O388J$BPP#&1@5,5J#?5*1/+'P@5,5P.8*<,YO*A$8+*<8Y?*8*Y8"J#RR1
F($8*<,(3*$(8@/(G%&?*>#&8*0(/J#+*85/&$B
push-(@35<3(*??$(*&(#0#>#&8(8/(83#(8/J(/G(83#($8*<,e(*&?
pop-(@35<3(+#>/H#$(83#(#0#>#&8(*8(83#(8/J(/G(83#($8*<,1
V/+(</&H#&5#&<#-(*(peek(/J#+*85/&(5$(5&8+/?%<#?(83*8(*00/@$(/&#(8/(+#*?(83#(#0#>#&8(*8(83#(8/J(@583/%8(+#>/H5&'
581
`#<*%$#(*($8*<,(5$(/&0"(*<<#$$5A0#(G+/>(83#(8/J-(58(3*$(83#(J+/J#+8"(,&/@&(*$(last in, first out(OLIFOR1(;3*8(5$-(83#
0*$8(#0#>#&8(83*8(#&8#+$(83#($8*<,(5$(83#(G5+$8(#0#>#&8(83*8(</>#$(/%81(=&(/83#+(@/+?$-(#H#+"(85>#(83#($8*<,(5$
J/JJ#?-(83#(#0#>#&8(83*8(</>#$(/%8(5$(83#(>/$8(+#<#&80"(*??#?(#0#>#&8(5&(83#($8*<,(*8(83*8(85>#1
8.5.1. The Stack Class
;3#(Stack(<0*$$(5$(>%<3($5>J0#+(83*&(83#(LinkedList(/+(83#(ArrayList(<0*$$1(9/%(<*&(G5&?(*(?#8*50#?(?#$<+5J85/&(/G
83#(StackxEy(<0*$$(*8(388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850P.8*<,138>01(`#0/@(@#($%>>*+5a#(83#
5>J/+8*&8(>#83/?$(/G(83#(<0*$$1
E(pushOE(#R1(F??$(83#($J#<5G5#?(#0#>#&8(8/(83#(8/J(/G(83#($8*<,(*&?(+#8%+&$(83#(*??#?(#0#>#&81
4/5/21, 10)50 PMIntermediate Programming
Page 145 of 185http://itec2150.gitlab.io/
E(popOE(#R1(l#>/H#$(*&?(+#8%+&$(83#(#0#>#&8(*8(83#(8/J(/G(83#($8*<,1(;3#(>#83/?(83+/@$(*&
EmptyStackException(5G(83#($8*<,(5$(#>J8"1
E(peekOE(#R1(l#8%+&$(83#(#0#>#&8(*8(83#(8/J(/G(83#($8*<,(@583/%8(+#>/H5&'(581(;3#(>#83/?(83+/@$(*&
EmptyStackException(5G(83#($8*<,(5$(#>J8"1
A//0#*&(addAllOCollectionx\(#c8#&?$(Ey(<R1(F??$(*00(83#(#0#>#&8$(/G(83#($J#<5G5#?(</00#<85/&(8/(83#($8*<,(5&(83#
/+?#+(?#G5&#?(A"(83#(</00#<85/&m$(58#+*8/+-(*&?(+#8%+&$(true(5G(*00(83#(#0#>#&8$(*+#($%<<#$$G%00"(*??#?1(;3#
>#83/?(83+/@$(*(NullPointerException(5G(83#($J#<5G5#?(</00#<85/&(5$(null1
5&8(sizeOR1(l#8%+&$(83#($5a#(/G(83#($8*<,-(51#1(83#(&%>A#+(/G(#0#>#&8$(5&(83#($8*<,1
A//0#*&(emptyOR1(l#8%+&$(true(5G(*&?(/&0"(5G(83#($8*<,(5$(#>J8"(O51#1($5a#OR(]](TR1
H/5?(clearOR1(70#*+$(83#($8*<,1
;3#(H#+"($5>J0#(</?#(A#0/@($3/@$(3/@(8/(<+#*8#(*(Stack(/AD#<8(*&?(%$#(58$(>#83/?$1
;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>-(@35<3(?#>/&$8+*8#$(83#(LIFO(&*8%+#(/G(*($8*<,
=&8#'#+$(C(8/(M(*+#(*??#?(8/(83#($8*<,(5&(835$(/+?#+1(;3#"(</>#(/%8(5&(83#(reverse(/+?#+(?%#(8/(83#(LIFO(&*8%+#(/G
83#($8*<,1(;3#(G/00/@5&'(5>*'#(G+/>(45,5J#?5* O388J$BPP#&1@5,5J#?5*1/+'P@5,5P.8*<,YO*A$8+*<8Y?*8*Y8"J#RR(?#>/&$8+*8#$(83#
$8#J$(5&(83#(#c#<%85/&(/G(83#(J+/'+*>1
importimport java.util.*java.util.*;
publicpublic classclass StackDemo {
publicpublic staticstatic voidvoid main(String[] args) {
Stack<Integer> stack = newnew Stack<>();
forfor (intint i = 1; i <= 6; i++) {
stack.push(i);
}
System.out.println("Stack after elements are added: ");
System.out.println(stack);
System.out.println();
System.out.println("Popping the stack: ");
whilewhile (!stack.empty()) {
System.out.print(stack.pop() + " ");
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Stack after elements are added:
[1, 2, 3, 4, 5, 6]
Popping the stack:
6 5 4 3 2 1
4/5/21, 10)50 PMIntermediate Programming
Page 146 of 185http://itec2150.gitlab.io/
8.5.2. Applications of Stacks
F(<%+5/%$(+#*?#+(>*"(*$,(*8(835$(J/5&8(@3"(@#(<*+#(*A/%8(83#(LIFO(J+/J#+8"(*&?(@3*8(&/&8+5H5*0(8*$,$(@#(<*&
*<</>J05$3(@583(*($8*<,1(=8(8%+&$(/%8(83*8($8*<,$(*+#($%+J+5$5&'0"(%$#G%0(*&?(J/@#+G%0(5&(J+/'+*>>5&'(?#$J58#(83#5+
$5>J05<58"1(!#+#(@#(H#+"(A+5#G0"(>#&85/&($/>#(5>J/+8*&8(*JJ05<*85/&$(/G($8*<,$(5&(&/&8#<3&5<*0(8#+>$1
F($8*<,(5$(%$#?(8/(5>J0#>#&8(83#(undo(G%&<85/&(/G(*JJ05<*85/&$($%<3(*$(S5<+/$/G8(4/+?1(;3#(*JJ(J%$3#$(*00(83#
*<85/&$(8/(*($8*<,(*$(83#"(/<<%+1(43#&(83#(%$#+(J+#$$#$(83#(undo(A%88/&-(83#(*JJ(J/J$(83#($8*<,(8/(%&?/(83#(>/$8
+#<#&8(*<85/&1
F(0#$$(/AH5/%$(*JJ05<*85/&(5$(J+/'+*>(#c#<%85/&1(43#&(*(J+/'+*>(5$(#c#<%8#?-(83#(+%&85>#($"$8#>(%$#$(*(call stack
8/(#c#<%8#(58$(G%&<85/&$(/+(>#83/?$1(l/%'30"($J#*,5&'-(83#(<*00($8*<,(>*5&8*5&$(*00(83#(*<85H#(>#83/?$(*@*585&'
#c#<%85/&1(;3#(>#83/?(*8(83#(8/J(/G(83#($8*<,(5$(83#(&#c8(/&#(8/(#c#<%8#1(43#&(*(>#83/?(O83#(callerR(5&(#c#<%85/&
<*00$(*&/83#+(>#83/?(O83#(calleeR-(83#(<*00#+(3*$(8/($%$J#&?(58$(#c#<%85/&(%&850(83#(<*00##(+#8%+&$1(;3#+#G/+#-(83#
+%&85>#($"$8#>(J%$3#$(83#(<*00#+(8/(83#(8/J(/G(83#($8*<,(*&?(#c#<%8#$(83#(<*00##1(43#&(83#(<*00##(+#8%+&$-(83#
$"$8#>(J/J$(83#($8*<,(*&?(+#$%>#$(83#(#c#<%85/&(/G(83#(<*00#+1
=&(73*J8#+(W("/%(0#*+&#?(83#(J/@#+G%0(8#<3&5d%#(/G(recursion(83*8($/0H#$(J+/A0#>$(@583(*(?5H5?#Q*&?Q</&d%#+
$8+*8#'"-(@3#+#(*(>#83/?(,##J$(<*005&'(58$#0G(%&850(*(A*$#(<*$#(5$(+#*<3#?1(V+/>(83#(J+#<#?5&'(J*+*'+*J3("/%(<*&
$##(83*8(*($8*<,(5$(#$$#&85*0(G/+(83#(#c#<%85/&(/G(*00(+#<%+$5H#(>#83/?$1
.8*<,$(*+#(</>>/&0"(%$#?(8/(5>J0#>#&8(*(G%&?*>#&8*0(*0'/+583>(,&/@&(*$(Depth First Search(O2V.R(83*8("/%(@500
0#*+&(5&(=;b7(ICLTB(F?H*&<#?()+/'+*>>5&'1(F>/&'(>*&"(/83#+(*JJ05<*85/&$-(2V.(5$(<+%<5*0(G/+(%&</H#+5&'
5>J/+8*&8(J+/J#+85#$(*&?($8+%<8%+#$(/G(0*+'#-($/J35$85<*8#?(</>J%8#+(/+($/<5*0(&#8@/+,$1(=8(5$(%$#?-(G/+(#c*>J0#-(A"
$#*+<3(#&'5&#$(8/(<+*@0(83#(4#A1
9#8(*&/83#+(*JJ05<*85/&(5$(J*+$5&'(5&(J+/'+*>(</>J50*85/&1(43#&(83#($/%+<#(</?#(/G(*(J+/'+*>(5$(</>J50#?-(83#
J*+$#+(/G(83#(</>J50#+(%$#$(*($8*<,(8/(J*+$#(83#(#cJ+#$$5/&$(*&?($8*8#>#&8$(5&(83#(</?#(G/+($"&8*c(#++/+$1
4/5/21, 10)50 PMIntermediate Programming
Page 147 of 185http://itec2150.gitlab.io/
;3#(*A/H#>#&85/&#?(*JJ05<*85/&$(*+#($/J35$85<*8#?(*&?(A#"/&?(83#($</J#(/G(835$(</%+$#1(j#c8(@#(J+#$#&8(*
<0*$$5<*0(J+/A0#>(83*8(5$($8500(&/&8+5H5*0(A%8(<*&(A#(+#*?50"($/0H#?(%$5&'(*($8*<,1
8.5.3. A Classical Application: Checking for Balanced Parentheses
4#(</&<0%?#(83#($#<85/&(/&($8*<,$(@583(*(<0*$$5<*0(J*+$5&'(J+/A0#>(83*8(5$(*>#&*A0#(8/(*($8*<,1
4#(</&$5?#+(3#+#(*($#d%#&<#(/G(J*+#&83#$#$(oOo(*&?(oRo1(;3#($*>#(*JJ05#$(8/(oo(*&?(oo-(/+(ozo(*&?(o}o1(F($#d%#&<#(/G
J*+#&83#$#$(5$(balanced(5G(#H#+"(oRo(<0/$#$(*(J+#<#?5&'(oOo-(*&?(</&H#+$#0"(#H#+"(oOo(5$(<0/$#?(A"(*($%A$#d%#&8(oRo(5&(83#
$#d%#&<#1(V/+(#c*>J0#-($#d%#&<#$(O(R-(O(RO(R-(O(O(R(R-(*&?(O(O(R(O(R(R(O(R(*+#(*00(A*0*&<#?-(@3#+#*$($#d%#&<#$(RO-(O(R(R-(O(O(R(*&?
O(RO(R(R(*+#(&/8(A*0*&<#?1(;3#(J+/A0#>(/G(5&8#+#$8(3#+#(5$(8/(?#<5?#(@3#83#+(*('5H#&($#d%#&<#(/G(J*+#&83#$#$(5$
A*0*&<#?1(;3#(J+/A0#>(</>#$(%J(5&(83#(J*+$5&'(/G(J+/'+*>$(G/+($"&8*c(#++/+$1(O7*&("/%($##(3/@(835$(5$(+#0#H*&8\R
;3#(J+/A0#>(?/#$(&/8(*JJ#*+(#*$"(*$(58(>*"($##>(3*+?(8/(G5&?(>*8<35&'(J*5+$(/G(oOo(*&?(oRo1(;3#(,#"(/A$#+H*85/&(5$(5G
*($#d%#&<#(5$(A*0*&<#?-(83#&(*$(@#(J+/<#$$(58(G+/>(0#G8(8/(+5'38-(#H#+"(oRo(<0/$#$(83#(last(J+#<#?5&'(oOo(83*8(+#>*5&$(8/
A#(<0/$#?1(;35$(5$(LIFOn(4583(835$(5?#*(5&(>5&?-(@#(3*H#(83#(G/00/@5&'($5>J0#(>#83/?(83*8($/0H#$(83#(J+/A0#>1
4#(*$$%>#(83*8(#H#+"(<3*+*<8#+(5&(83#(5&J%8($8+5&'(5$(#583#+(oOo(/+(oRo1(;3#(>#83/?(J+/<#$$#$(83#(5&J%8(G+/>(0#G8(8/
+5'381(ZJ/&($##5&'(#*<3(oOo-(83#(>#83/?(J%$3#$(58(8/(83#($8*<,1(ZJ/&($##5&'(*(oRo-(83#(>#83/?(*88#>J8$(8/(G5&?(*
>*8<35&'(oOo(A"(J/JJ5&'(83#($8*<,1(=G(*8(835$(J/5&8(83#($8*<,(5$(#>J8"(OA#G/+#(83#(@3/0#(5&J%8(5$(J+/<#$$#?R-(83#&(83#
<%++#&8(<3*+*<8#+(5$(*&(/%8$8*&?5&'(oRo(83*8(?/#$(&/8(<0/$#(*(J+#<#?5&'(oOo1(;3#+#G/+#-(83#(5&J%8(5$(&/8(A*0*&<#?(*&?
83#(>#83/?(+#8%+&$(G*0$#1(=G(83#(>#83/?(#c58$(83#(0//J(*G8#+(J+/<#$$5&'(83#(@3/0#(5&J%8(A%8(83#($8*<,(5$(not(#>J8"-
83#&(83#+#(5$($/>#(/%8$8*&?5&'(oOo(#*+0"(5&(83#(5&J%8(83*8(5$(&/8(<0/$#?(A"(*($%A$#d%#&8(oRo1(;3#+#G/+#-(83#(5&J%8(5$
A*0*&<#?(5G(*&?(/&0"(5G(83#($8*<,(5$(#>J8"(*G8#+(83#(@3/0#(5&J%8(5$(J+/<#$$#?1(;35$(5$(@3*8(83#(>#83/?(+#8%+&$1
=&(/&#(/G(83#(#c#+<5$#$-("/%(@500(A#(*$,#?(8/(%$#(*($8*<,(8/($/0H#(*(H*+5*&8(/G(83#(J+/A0#>-(@3#+#(oOo-(oRo-(o•o-(o€o-(ozo(*&?
o}o(</%0?(*00(A#(J+#$#&8(5&(83#(5&J%81(;35$(3*$(A##&(*(J/J%0*+(5&8#+H5#@(d%#$85/&$(G/+(J/$585/&$(5&(83#($/G8@*+#
5&?%$8+"n
8.6. Queues
publicpublic staticstatic booleanboolean isBalanced(String parentheses) {
Stack<Character> stack = newnew Stack<>();
forfor (charchar p : parentheses.toCharArray()) {
ifif (p == '(') {
stack.push(p);
}
elseelse {
// i.e. if p == ')'
ifif (stack.empty()) {
returnreturn falsefalse;
}
stack.pop();
}
}
returnreturn stack.empty();
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
4/5/21, 10)50 PMIntermediate Programming
Page 148 of 185http://itec2150.gitlab.io/
F(d%#%#(5$(*&/83#+(H#+"($J#<5*0(05$8(@3#+#(#0#>#&8$(*+#(*??#?(/&0"(8/(83#(A*<,(O/+(8*50R(*&?(+#>/H#?(/&0"(G+/>(83#
G+/&8(O/+(3#*?R1(;+*?585/&*00"-(83#(/J#+*85/&(/G(*??5&'(8/(83#(A*<,(/G(*(d%#%#(5$(<*00#?(enqueue-(*&?(83*8(/G
+#>/H5&'(G+/>(83#(G+/&8(/G(*(d%#%#(5$(<*00#?(dequeue1(=&(835$(@+585&'(3/@#H#+(@#(%$#(83#(>/+#(<*$%*0(8#+>$(add
*&?(remove1(;3#(G/00/@5&'(5>*'#(G+/>(45,5J#?5* O388J$BPP#&1@5,5J#?5*1/+'P@5,5Pp%#%#YO*A$8+*<8Y?*8*Y8"J#RR(?#J5<8$(*
d%#%#1
Dequeue
Enqueue
Back Front
=&(</&8+*$8(8/(*($8*<,-(*(d%#%#(3*$(83#(J+/J#+8"(,&/@&(*$(first in, first out(OFIFOR1(;3*8(5$-(83#(G5+$8(#0#>#&8(83*8
#&8#+$(83#(d%#%#(5$(83#(G5+$8(#0#>#&8(83*8(</>#$(/%81
8.6.1. The Queue Interface
;3#(Queue(5&8#+G*<#(#c8#&?$(83#(Collection(5&8#+G*<#(*&?(J+/H5?#$(*&(*A$8+*<85/&(/G(*(d%#%#(?*8*($8+%<8%+#1(j/8#
83*8(5&(6*H*(Queue(5$(not(*(<0*$$1(=8(5$(*&(5&8#+G*<#(@583($#H#+*0(5>J0#>#&8*85/&$1(S/$8(/G8#&-(*($8*&?*+?(V=VU
d%#%#(5$(5>J0#>#&8#?(A"(*(LinkedList1(;35$(5$(&/8($%+J+5$5&'(A#<*%$#(*(d%#%#(5$(*($J#<5*0(05&,#?(05$8(@3#+#(@#(*??
*8(/&#(#&?(*&?(+#>/H#(G+/>(83#(/83#+1
F8(388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850Pp%#%#138>0("/%(<*&(G5&?(*(?#8*50#?(?#$<+5J85/&(/G(83#
QueuexEy(5&8#+G*<#1(`#0/@(@#($%>>*+5a#(83#(5>J/+8*&8(>#83/?$(/G(83#(5&8#+G*<#1
A//0#*&(addOE(#R1(F??$(83#($J#<5G5#?(#0#>#&8(8/(83#(tail(/G(83#(d%#%#(*&?(+#8%+&$(8+%#(*$(0/&'(*$(83#(#0#>#&8(5$
$%<<#$$G%00"(*??#?1
A//0#*&(offerOE(#R1(.*>#(*$(addOE(#R1
E(removeOR1(l#>/H#$(*&?(+#8%+&$(83#(#0#>#&8(*8(83#(head(/G(83#(d%#%#1(;3#(>#83/?(83+/@$(*
NoSuchElementException(5G(83#(83#(d%#%#(5$(#>J8"1
E(pollOR1(.*>#(*$(removeOR-(A%8(+#8%+&$(null(5G(83#(83#(d%#%#(5$(#>J8"1
E(peekOR1(l#8%+&$(83#(#0#>#&8(*8(83#(head(/G(83#(d%#%#(@583/%8(+#>/H5&'(581(l#8%+&$(null(5G(83#(d%#%#(5$(#>J8"1
E(elementOR1(.*>#(*$(peekOR-(A%8(83+/@$(*(NoSuchElementException(5G(83#(83#(d%#%#(5$(#>J8"1
4/5/21, 10)50 PMIntermediate Programming
Page 149 of 185http://itec2150.gitlab.io/
A//0#*&(addAllOCollectionx\(#c8#&?$(Ey(<R1(F??$(*00(83#(#0#>#&8$(/G(83#($J#<5G5#?(</00#<85/&(8/(83#(d%#%#(5&(83#
/+?#+(?#G5&#?(A"(83#(</00#<85/&m$(58#+*8/+-(*&?(+#8%+&$(true(5G(*00(83#(#0#>#&8$(*+#($%<<#$$G%00"(*??#?1(;3#
>#83/?(83+/@$(*(NullPointerException(5G(83#($J#<5G5#?(</00#<85/&(5$(null1
5&8(sizeOR1(l#8%+&$(83#($5a#(/G(83#(d%#%#-(51#1(83#(&%>A#+(/G(#0#>#&8$(5&(83#(d%#%#1
A//0#*&(isEmptyOR1(l#8%+&$(true(5G(*&?(/&0"(5G(83#(d%#%#(5$(#>J8"(O51#1($5a#OR(]](TR1
H/5?(clearOR1(70#*+$(83#(d%#%#1
;3#(H#+"($5>J0#(</?#(A#0/@($3/@$(3/@(8/(<+#*8#(*(d%#%#(*&?(%$#(58$(>#83/?$1
j/85<#(83*8(5&(83#(J+/'+*>(83#(d%#%#(5$(<+#*8#?(*$(*(05&,#?(05$81(;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>-(@35<3
?#>/&$8+*8#$(83#(FIFO(&*8%+#(/G(*(d%#%#1
;3#(#0#>#&8$(0#*H#(83#(d%#%#(5&(83#($*>#(/+?#+(*$(83#"(#&8#+(83#(d%#%#1
8.6.2. Applications of Queues
N5,#($8*<,$-(d%#%#$(3*H#(>*&"(5>J/+8*&8(*JJ05<*85/&$(?#$J58#(83#5+($5>J05<58"1(2%#(8/(83#5+(FIFO(&*8%+#-(d%#%#$
*+#(%$#?(8/(5>J0#>#&8($<3#?%0#+$(/G(+#$/%+<#$(83*8(*+#($3*+#?(*>/&'(>*&"(<%$8/>#+$-(@3#+#(83#(<%$8/>#+$(*+#
$#+H#?(/&(*(first come, first serve(A*$5$1
importimport java.util.*java.util.*;
publicpublic classclass QueueDemo {
publicpublic staticstatic voidvoid main(String[] args) {
Queue<Integer> queue = newnew LinkedList<>();
forfor (intint i = 1; i <= 6; i++) {
queue.add(i);
}
System.out.println("Queue after elements are added: ");
System.out.println(queue);
System.out.println();
System.out.println("Emptying the queue: ");
whilewhile (!queue.isEmpty()) {
System.out.print(queue.remove() + " ");
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Queue after elements are added:
[1, 2, 3, 4, 5, 6]
Emptying the queue:
1 2 3 4 5 6
4/5/21, 10)50 PMIntermediate Programming
Page 150 of 185http://itec2150.gitlab.io/
p%#%#$(*+#(83#(?*8*($8+%<8%+#$(G/+(5>J0#>#&85&'(*&/83#+(G%&?*>#&8*0(*0'/+583>(,&/@&(*$(Breadth First Search
O`V.R(83*8("/%(@500(0#*+&(5&(=;b7(ICLTB(F?H*&<#?()+/'+*>>5&'1(F0/&'(@583(2V.-(`V.(J0*"$(*(<+%<5*0(+/0#(5&(83#
?5$</H#+"(/G(5>J/+8*&8(J+/J#+85#$(*&?($8+%<8%+#$(/G(0*+'#(&#8@/+,$1
8.7. Priority Queues
F(J+5/+58"(d%#%#(5$(*(</00#<85/&(/G(#0#>#&8$(83*8(*+#(*$$/<5*8#?(@583(*(rJ+5/+58"r(H*0%#1(Z&05,#(*(V=VU(d%#%#(@3#+#
83#(#*+05#$8(*++5H5&'(#0#>#&8(5$(G5+$8($#+H#?-(5&(*(J+5/+58"(d%#%#(83#(#0#>#&8(@583(83#("highest priority"(5$(G5+$8
$#+H#?1(;/('5H#(*(</&<+#8#(#c*>J0#-(0#8m$(</&$5?#+(83#(#>#+'#&<"(+//>(/G(*(3/$J58*0(@3#+#(>*&"(J*85#&8$(@*58(8/
A#(*?>588#?1(=?#*00"(83#(>/$8(500(J*85#&8($3/%0?(A#(G5+$8($#+H#?1(U&#(5?#*(8/(5>J0#>#&8(835$(5$(8/(*$$5'&(*(rJ+5/+58"r
8/(#*<3(J*85#&8(A*$#?(/&(83#($#H#+58"(/G(35$P3#+(500&#$$1(43#&(*(J*85#&8(+//>(5$(*H*50*A0#-(83#(bl(*?>58$(83#(@*585&'
J*85#&8(@583(83#(r35'3#$8(J+5/+58"r1(;35$(5$(*(+#*0Q@/+0?(#c*>J0#(/G(*(J+5/+58"(d%#%#1
`%8(#c*<80"(@3*8(5$(J+5/+58"\(!/@(?/(@#($"$8#>*85<*00"(+#J+#$#&8(*&?(5>J0#>#&8(J+5/+58"(5&(/%+(J+/'+*>$\(4#
>%$8(<*+#G%00"(*&$@#+(83#$#(d%#$85/&$(5&(/+?#+(8/(J+#<5$#0"(?#G5&#(*(J+5/+58"(d%#%#1
!#+#(@#(*$$%>#(83*8(83#(#0#>#&8$(/G(/%+(</00#<85/&(*+#(/AD#<8$(G+/>(*(<0*$$(83*8(3*$(*&(order1(;35$(>#*&$(83*8(@#
<*&(</>J*+#(*&"(8@/(/AD#<8$(x(*&?(y(/G(83#(<0*$$-(*&?(83#(+#$%08(/G(83#(</>J*+5$/&(5$(/&#(/G(83#(83+##(J/$$5A0#
/%8</>#$B
x(5$($>*00#+(83*&(ye(/+
x(#d%*0$(ye(/+
x(5$('+#*8#+(83*&(y(O51#1(y(5$($>*00#+(83*&(xR1
./>#(?*8*(8"J#$(*&?(<0*$$#$(3*H#(*(natural ordering1(;3#($5>J0#$8(#c*>J0#(5$(5&8#'#+$-(@35<3(*+#(/+?#+#?(A"(83#5+
&%>#+5<*0(H*0%#$1(F&/83#+(#c*>J0#(5$(83#(<0*$$(/G($8+5&'$-(@35<3(*+#(/+?#+#?(*0J3*A#85<*00"1(S/+#('#&#+*00"-(@#
<*&(8*,#(*&"(<0*$$(83*8(5>J0#>#&8$(83#(Comparable(5&8#+G*<#e(5&(835$(<*$#(*&(/+?#+(5$(?#G5&#?(A"(83#(compareTo
>#83/?(/G(83#(<0*$$1
./>#85>#$(58(5$(&/8(#&/%'3(8/(+#0"(/&(83#(&*8%+*0(/+?#+5&'(/G(*(<0*$$-(/+(83#(/+?#+(J+#Q?#G5&#?(A"(83#(compareTo
>#83/?(/G(*(<0*$$1(4#(>*"(&##?(8/(8*,#(*(J+#H5/%$0"(%&/+?#+#?(<0*$$(*&?(impose an order-(/+(8*,#(*(<0*$$(@583(*&
*0+#*?"(#c5$85&'(/+?#+(A%8(define a new order(/&(?#>*&?(G/+(83#(<0*$$1(V/+8%&*8#0"-(6*H*(*00/@$(%$(8/(?/($/(A"
?#G5&5&'(/%+(/@&(<%$8/>(</>J*+*8/+$-(%$5&'(83#(Comparator(5&8#+G*<#(83*8(@#(@500(?5$<%$$($3/+80"1
4#(<*&(&/@(?#G5&#(*(J+5/+58"(d%#%#(*$(*(</00#<85/&(/G(/AD#<8$-(#*<3(*$$/<5*8#?(@583(*(J+5/+58"(83*8(5$(?#8#+>5&#?(A"
*('5H#&(/+?#+5&'-(@3#83#+(&*8%+*0(/+(<%$8/>1(;3#(/AD#<8(@583(83#(r35'3#$8r(J+5/+58"(5$(G5+$8($#+H#?1(;3#+#(*+#(8@/
8"J#$(J+5/+58"(d%#%#$-(min priority queues(*&?(max priority queues-(83*8(3*H#(/JJ/$58#(&/85/&$(/G(J+5/+58"1(=&(*(min
J+5/+58"(d%#%#-(smaller(#0#>#&8$(3*H#(35'3#+(J+5/+585#$1(=&(</&8+*$8-(5&(*(max(J+5/+58"(d%#%#-(larger(#0#>#&8$(3*H#
35'3#+(J+5/+585#$1(!#+#($>*00#+(/+(0*+'#+(5$(@583(+#$J#<8(8/(*($J#<5G5#?(/+?#+5&'1(V/+(#c*>J0#-(5&(*(>5&(J+5/+58"
d%#%#(/G(5&8#'#+$(@583(83#(&*8%+*0(/+?#+5&'-(C(@/%0?(3*H#(*(35'3#+(J+5/+58"(83*&(Ee(@3#+#*$(5&(*(>*c(J+5/+58"
d%#%#-(E(@/%0?(3*H#(*(35'3#+(J+5/+58"(83*&(C1(=&(6*H*-(*(J+5/+58"(d%#%#(5$(by default a min priority queue1
8.7.1. The PriorityQueue Class
4/5/21, 10)50 PMIntermediate Programming
Page 151 of 185http://itec2150.gitlab.io/
F8(388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850P)+5/+58"p%#%#138>0("/%(<*&(G5&?(*(?#8*50#?(?#$<+5J85/&(/G(83#
PriorityQueuexEy(<0*$$1(`#0/@(@#($%>>*+5a#(83#(5>J/+8*&8(>#83/?$(/G(83#(<0*$$1
A//0#*&(addOE(#R1(F??$(83#($J#<5G5#?(#0#>#&8(8/(83#(J+5/+58"(d%#%#(*&?(+#8%+&$(8+%#(*$(0/&'(*$(83#(#0#>#&8(5$
$%<<#$$G%00"(*??#?1(;3#(>#83/?(83+/@$(*(ClassCastException(5G(*&(/+?#+(5$(&/8(?#G5&#?(G/+(83#(<0*$$(E-(*&?(*
NullPointerException(5G(83#(#0#>#&8(5$(null1
A//0#*&(offerOE(#R1(.*>#(*$(addOE(#R1
E(removeOR1(l#>/H#$(*&?(+#8%+&$(83#(#0#>#&8(@583(83#(highest priority(5&(83#(d%#%#1(;3#(>#83/?(83+/@$(*
j/.%<3b0#>#&8bc<#J85/&(5G(83#(83#(d%#%#(5$(#>J8"1
E(pollOR1(.*>#(*$(removeOR-(A%8(+#8%+&$(null(5G(83#(83#(d%#%#(5$(#>J8"1
E(peekOR1(l#8%+&$(83#(#0#>#&8(*8(83#(head(/G(83#(d%#%#(@583/%8(+#>/H5&'(581(l#8%+&$(null(5G(83#(d%#%#(5$(#>J8"1
E(elementOR1(.*>#(*$(peekOR-(A%8(83+/@$(*(NoSuchElementException(5G(83#(83#(d%#%#(5$(#>J8"1
A//0#*&(addAllOCollectionx\(#c8#&?$(Ey(<R1(F??$(*00(83#(#0#>#&8$(/G(83#($J#<5G5#?(</00#<85/&(8/(83#(J+5/+58"
d%#%#(5&(83#(/+?#+(?#G5&#?(A"(83#(</00#<85/&m$(58#+*8/+-(*&?(+#8%+&$(true(5G(*00(83#(#0#>#&8$(*+#($%<<#$$G%00"
*??#?1(;3#(>#83/?(83+/@$(*(NullPointerException(5G(83#($J#<5G5#?(</00#<85/&(5$(null(/+(*&"(#0#>#&8(/G(83#
</00#<85/&(5$(null1
5&8(sizeOR1(l#8%+&$(83#($5a#(/G(83#(d%#%#-(51#1(83#(&%>A#+(/G(#0#>#&8$(5&(83#(d%#%#1
A//0#*&(isEmptyOR1(l#8%+&$(true(5G(*&?(/&0"(5G(83#(d%#%#(5$(#>J8"(O51#1($5a#OR(]](TR1
H/5?(clearOR1(70#*+$(83#(J+5/+58"(d%#%#1
;3#(PriorityQueue(<0*$$(3*$(>*&"(</&$8+%<85/&$1(!#+#(@#(?5$<%$$(83+##(/G(83#>1(;3#(G5+$8(8@/(</&$8+%<8/+$(<+#*8#
J+5/+58"(d%#%#$(G/+(<0*$$#$(83*8(3*H#(*(&*8%+*0(/+?#+5&'(?#G5&#?1(;3#(835+?(</&$8+%<8/+(<+#*8#$(*(J+5/+58"(d%#%#(G/+
*&(*+A58+*+"(<0*$$(Q(58(8*,#$(*(<%$8/>(</>J*+*8/+(G/+(83#(<0*$$(*$(*(J*+*>#8#+-(*&?(<+#*8#$(*(J+5/+58"(d%#%#(83*8
/+?#+$(83#(#0#>#&8$(/G(83#(<0*$$(*<</+?5&'(8/(83#('5H#&(</>J*+*8/+1
PriorityQueueOR1(7+#*8#$(*&(#>J8"(J+5/+58"(d%#%#(83*8(/+?#+$(58$(#0#>#&8$(*<</+?5&'(8/(83#(&*8%+*0(/+?#+5&'(/G
83#('#&#+5<(<0*$$(E1(=G(*&(/+?#+5&'(5$(&/8(?#G5&#?(G/+(83#(<0*$$-(83#(</&$8+%<8/+(@500($8500(<+#*8#(*&(#>J8"(d%#%#e
3/@#H#+(5&(835$(<*$#(@3#&(/&#(*88#>J8$(8/(*??(*&(/AD#<8(/G(83#(<0*$$(8/(83#(d%#%#-(83#(add(>#83/?(@500(83+/@(*
ClassCastException1
PriorityQueueOCollectionx\(#c8#&?$(Ey(<R1(7+#*8#$(*(J+5/+58"(d%#%#(83*8(</&$5$8$(/G(83#(#0#>#&8$(5&(83#
$J#<5G5#?(</00#<85/&(*&?(/+?#+$(58$(#0#>#&8$(*<</+?5&'(8/(83#(&*8%+*0(/+?#+5&'(/G(83#('#&#+5<(<0*$$1(;3#
</&$8+%<8/+(83+/@$(*(ClassCastExeption(5G(*&(/+?#+5&'(5$(&/8(?#G5&#?(G/+(83#(<0*$$-(*&?(*
NullPointerException(5G(*&"(#0#>#&8(/G(83#(</00#<85/&(5$(null1
PriorityQueueOComparatorx\($%J#+(Ey(</>J*+*8/+R1(7+#*8#$(*&(#>J8"(J+5/+58"(d%#%#(83*8(/+?#+$(58$(#0#>#&8$
*<</+?5&'(8/(83#($J#<5G5#?(</>J*+*8/+(G/+(83#('#&#+5<(<0*$$1(;35$(</&$8+%<8/+(*00/@$("/%(8/(?#G5&#(*(<%$8/>
</>J*+*8/+(8/(/+?#+(83#(#0#>#&8$(/G(83#(J+5/+58"(d%#%#1(=&(6*H*-(*(J+5/+58"(d%#%#(5$(A"(?#G*%08(<+#*8#?(*$(*(min
J+5/+58"(d%#%#1(=G(83#('#&#+5<(<0*$$(3*$(*(&*8%+*0(/+?#+5&'-("/%(<*&(%$#(835$(</&8+%<8/+(8/(<+#*8#(*(max(J+5/+58"
d%#%#(A"(J*$$5&'(Collections.reverseOrder()(*$(83#(</>J*+*8/+1
4/5/21, 10)50 PMIntermediate Programming
Page 152 of 185http://itec2150.gitlab.io/
4#(&/@('5H#(*($5>J0#(#c*>J0#(83*8(?#>/&$8+*8#$(3/@(8/(%$#(83#(</&$8+%<8/+$(*&?(>#83/?$(/G(83#(PriorityQueue
<0*$$1
;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>1
;3#(J+/'+*>(G5+$8(<+#*8#$(*(O>5&R(J+5/+58"(d%#%#(pq1(/G(5&8#'#+$(%$5&'(83#(?#G*%08(</&$8+%<8/+-(83#&(*??$(L-(I-(E-(M-
K-(C(8/(83#(d%#%#1(43#&(83#(#0#>#&8$(*+#(+#>/H#?(pq1-(83#"(</>#(/%8(5&(*$<#&?5&'(/+?#+1(j#c8(83#(J+/'+*>(%$#$
83#(835+?(</&$8+%<8/+(*A/H#(8/(<+#*8#(*(>*c(J+5/+58"(d%#%#(pq2(/G(5&8#'#+$-(@583(Collections.reverseOrder()(*$
83#(</>J*+*8/+-(83#&(*??$(83#($*>#(&%>A#+$(8/(83#(d%#%#1(43#&(83#(#0#>#&8$(*+#(+#>/H#?(pq2-(83#"(</>#(/%8(5&
?#$<#&?5&'(/+?#+1(V5&*00"-(83#(J+/'+*>(%$#$(83#($#</&?(</>J*+*8/+(*A/H#(8/(<+#*8#(*(O>5&R(J+5/+58"(d%#%#(/G
$8+5&'$(G+/>(*&(#c5$85&'(</00#<85/&-(@35<3(5$(83#(05$8(•r.3*&'3*5r-(r`/$8/&r-(rj#@(9/+,r-(r:%+5<3r-(rF80*&8*r€1(43#&
83#(#0#>#&8$(*+#(+#>/H#?(pq3-(83#"(</>#(/%8(5&(*$<#&?5&'(*0J3*A#85<(/+?#+1
8.7.2. The Comparator Interface
importimport java.util.*java.util.*;
publicpublic classclass PQDemo {
publicpublic staticstatic voidvoid main(String[] args) {
PriorityQueue<Integer> pq1 = newnew PriorityQueue<>();
pq1.add(5); pq1.add(3); pq1.add(2);
pq1.add(6); pq1.add(4); pq1.add(1);
System.out.print("Emptying pq1: ");
whilewhile (!pq1.isEmpty()) {
System.out.print(pq1.remove() + " ");
}
PriorityQueue<Integer> pq2 = newnew PriorityQueue<>(Collections.reverseOrder());
pq2.add(5); pq2.add(3); pq2.add(2);
pq2.add(6); pq2.add(4); pq2.add(1);
System.out.print("\n\nEmptying pq2: ");
whilewhile (!pq2.isEmpty()) {
System.out.print(pq2.remove() + " ");
}
PriorityQueue<String> pq3 = newnew PriorityQueue<>(
Arrays.asList("Shanghai", "Boston", "New York", "Zurich", "Atlanta"));
System.out.print("\n\nEmptying pq3: ");
whilewhile (!pq3.isEmpty()) {
System.out.print(pq3.remove() + " ");
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Emptying pq1: 1 2 3 4 5 6
Emptying pq2: 6 5 4 3 2 1
Emptying pq3: Atlanta Boston New York Shanghai Zurich
4/5/21, 10)50 PMIntermediate Programming
Page 153 of 185http://itec2150.gitlab.io/
;3#(Comparator(5&8#+G*<#(*00/@$(/&#(8/(?#G5&#(*(<%$8/>(</>J*+*8/+(G/+(*&"(<0*$$(A"(5>J0#>#&85&'(83#(compare
>#83/?1(F8(388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850P7/>J*+*8/+138>0X</>J*+#Q;Q;Q("/%(<*&(G5&?(*00
?#8*50$(/G(83#(5&8#+G*<#(ComparatorxTy-(@3#+#(T(5$(*('#&#+5<(<0*$$(8/(?#G5&#(*(</>J*+*8/+(G/+1(!#+#(@#(/&0"
?5$<%$$(83#(>#83/?
@35<3(5$(83#(>#83/?(8/(5>J0#>#&8(5&(/+?#+(8/(?#G5&#("/%+(</>J*+*8/+(/&(83#(<0*$$(T1(;3#(>#83/?(8*,#$(8@/(/AD#<8$
o1(*&?(o2(/G(83#(<0*$$(T(*$(J*+*>#8#+$-(*&?(+#8%+&$(*&(5&8#'#+(@35<3(5$(5&8#+J+#8#?(*$(G/00/@$B
=G(83#(+#8%+&#?(H*0%#(5$(negative-(83#&(o1(5$(smaller(83*&(o21
=G(83#(+#8%+&#?(H*0%#(5$(zero-(83#&(o1(*&?(o2(*+#(#d%*01
=G(83#(+#8%+&#?(H*0%#(5$(positive-(83#&(o1(5$(greater(83*&(o21
4#(500%$8+*8#(3/@(8/(5>J0#>#&8(83#(compare(>#83/?(@583(*(G#@(#c*>J0#$(*$(G/00/@$1
4#(,&/@(83*8(83#(String(<0*$$(*0+#*?"(3*$(*(&*8%+*0(/+?#+5&'(@35<3(5$(83#(*0J3*A#85<(/+?#+5&'1(`%8($%JJ/$#(83*8(5&
*&(*JJ05<*85/&(@#(&##?(8/(5>J/$#(*(?5GG#+#&8(/+?#+5&'(83*8(/+?#+$($8+5&'$(first by length1(=G(8@/($8+5&'$(3*H#(83#
$*>#(0#&'83-(83#&(@#(/+?#+(83#>(*0J3*A#85<*00"1(;3#+#(*+#($#H#+*0(@*"$(8/(5>J0#>#&8(835$(</>J*+*8/+1(;3#
8+*?585/&*0(@*"(5$(8/(#cJ05<580"(?#G5&#(*(</>J*+*8/+(<0*$$(*$(G/00/@$1
=&(83#(</?#(*A/H#-(@#(?#G5&#(*(<0*$$(StringLenComparator(83*8(5>J0#>#&8$(83#(Comparator(5&8#+G*<#(G/+(String1
=&(83#(<0*$$(*00(@#(&##?(8/(?/(5$(8/(5>J0#>#&8(83#(compare(>#83/?1(=G(83#(J*+*>#8#+$(s1(*&?(s2(3*H#(different
0#&'83$-(83#&(83#(compare(>#83/?(?#8#+>5&#$(83#5+(/+?#+(A"(0#&'831(=&(835$(<*$#(83#(>#83/?(+#8%+&$
j/85<#(83*8(5G(s1(5$($3/+8#+(83*&(s2-(83#&(83#(+#8%+&#?(H*0%#(s1.length() - s2.length()(5$(&#'*85H#-(83#+#G/+#(s1
J+#<#?#$(s2(5&(83#(&#@(/+?#+1(7/&H#+$#0"-(5G(s1(5$(0/&'#+(83*&(s2-(83#&(83#(+#8%+&#?(H*0%#(s1.length() - s2.length()
5$(J/$585H#-(83#+#G/+#(s2(J+#<#?#$(s1(5&(83#(&#@(/+?#+1
=G(s1(*&?(s2(3*H#(83#($*>#(0#&'83-(83#&(83#(compare(>#83/?(+#8%+&$
publicpublic intint compare(T o1, To2)
JAVA
classclass StringLenComparator implementsimplements Comparator<String> {
publicpublic intint compare(String s1, String s2) {
ifif (s1.length() != s2.length()) {
returnreturn s1.length() - s2.length();
}
elseelse {
returnreturn s1.compareTo(s2);
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
s1.length() - s2.length()
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 154 of 185http://itec2150.gitlab.io/
@35<3(/+?#+$(83#(8@/($8+5&'$(A"(83#(&*8%+*0(/+?#+5&'(/G(83#(String(<0*$$1
U&<#(@#(?#G5&#(83#(StringLenComparator-(@#(<*&(%$#(58(8/(<+#*8#(*(J+5/+58"(d%#%#(*$(G/00/@$1
;3#(J+/'+*>(<+#*8#$(*(J+5/+58"(d%#%#(pq(/G($8+5&'$(83*8(/+?#+$(#0#>#&8$(*<</+?5&'(8/(83#(&#@(/+?#+5&'(5>J/$#?(A"
StringLenComparator1(=8(?/#$($/(A"(J*$$5&'(8/(83#(</&$8+%<8/+(*&(/AD#<8(/G(83#(StringLenComparator(*$(83#
</>J*+*8/+1(FG8#+(83*8-(83#(J+/'+*>(*??$(83#(#0#>#&8$(/G(*(05$8(8/(pq(*&?(83#&(#>J8"(pq1(V/+(</&8+*$8-(83#(J+/'+*>
*0$/(<+#*8#$-(%$5&'(83#(?#G*%08(</&$8+%<8/+-(*(J+5/+58"(d%#%#(pq2(83*8(/+?#+$(#0#>#&8$(A"(83#(&*8%+*0(*0J3*A#85<
/+?#+5&'-(*&?(+#J#*8$(83#(*A/H#(G/+(pq21(;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>1
;3#($8+5&'(rl#?r-(@35<3(5$(/G(0#&'83(I-(5$(83#($3/+8#$8(/G(*00(83#($8+5&'$(5&(83#(05$81(;3#+#G/+#-(rl#?r(*JJ#*+$(G5+$8(*$
#0#>#&8$(*+#(+#>/H#?(G+/>(83#(J+5/+58"(d%#%#(pq1(=8(5$(G/00/@#?(A"(r`0%#r-(r7"*&r(*&?(rq/0?r-(#*<3(/G(0#&'83(K1
`#<*%$#(83#"(3*H#(83#($*>#(0#&'83-(83#"(*+#(/+?#+(*0J3*A#85<*00"(*>/&'(83#>1(;3#"(*+#(G/00/@#?(A"(rq+##&r
@35<3(5$(/G(0#&'83(L1(;3#($8+5&'(rq+##&r(5$(G/00/@#?(A"(r=&?5'/r-(rU+*&'#r-(r)%+J0#r-(rh5/0#8r(*&?(r9#00/@r-(#*<3(/G
@35<3(5$(/G(0#&'83(M1(F'*5&(A#<*%$#(83#$#(G5H#(*+#(/G(83#($*>#(0#&'83-(83#"(*+#(/+?#+(*0J3*A#85<*00"(*>/&'(83#>1(=&
83#(>#*&85>#-("/%($##(83*8(83#($*>#($8+5&'$(</>#(/%8(5&(83#(*0J3*A#85<(/+?#+(*$(83#"(0#*H#(83#(J+5/+58"(d%#%#(pq21
F08#+&*85H#0"-(@#(<*&(%$#(*(lambda expression(8/(?#G5&#(83#(</>J*+*8/+(*&?($5>J05G"(83#(*A/H#(</?#-(*$(G/00/@$1
s1.compareTo(s2)
JAVA
publicpublic classclass ComparatorDemo1 {
publicpublic staticstatic voidvoid main(String[] args) {
List<String> list = Arrays.asList(
"Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red", "Cyan", "Gold", "Purple");
PriorityQueue<String> pq = newnew PriorityQueue<>(newnew StringLenComparator());
pq.addAll(list);
System.out.println("Order imposed by the new comparator: ");
whilewhile (!pq.isEmpty()) {
System.out.print(pq.remove() + " ");
}
System.out.println("\n\nNatural order: ");
PriorityQueue<String> pq2 = newnew PriorityQueue<>();
pq2.addAll(list);
whilewhile (!pq2.isEmpty()) {
System.out.print(pq2.remove() + " ");
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Order imposed by the new comparator:
Red Blue Cyan Gold Green Indigo Orange Purple Violet Yellow
Natural order:
Blue Cyan Gold Green Indigo Orange Purple Red Violet Yellow
4/5/21, 10)50 PMIntermediate Programming
Page 155 of 185http://itec2150.gitlab.io/
4#(&/@(+#8%+&(8/(83#(#>#+'#&<"(+//>(#c*>J0#(83*8(@*$(?5$<%$$#?(*8(83#(A#'5&&5&'(/G(83#($#<85/&1(.%JJ/$#(83*8
83#(G/00/@5&'(<0*$$(3*$(A##&(<+#*8#?(8/(+#J+#$#&8(*(J*85#&81
!#+#(83#(*88+5A%8#(severity(d%*&85G5#$(83#($#H#+58"(/G(83#(500&#$$(/G(*(J*85#&81(;3#(0*+'#+(83#(H*0%#(/G(severity-(83#
>/+#($#H#+#(83#(500&#$$(/G(83#(J*85#&81(;3#(*88+5A%8#(arrival(+#J+#$#&8$(83#(J*85#&8m$(*++5H*0(85>#1(;3#($>*00#+(83#
H*0%#(/G(arrival-(83#(#*+05#+(83#(J*85#&8(*++5H#?1
4#(&/@(?#G5&#(*&(/+?#+5&'(/G(J*85#&8$(5&(/+?#+(8/(?#<5?#(83#(/+?#+(8/(*?>58(83#>1(4#(first order patients by
severity in descending order1(;3*8(5$-(J*85#&8$(@583(*(35'3#+($#H#+58"(H*0%#($3/%0?(A#($##&(#*+05#+1(=G(8@/(J*85#&8$
3*H#(83#($*>#(severity(H*0%#-(@#(then order them by arrival in ascending order1(;3*8(5$-(J*85#&8$(@583(83#($*>#
importimport java.util.*java.util.*;
publicpublic classclass ComparatorDemo2 {
publicpublic staticstatic voidvoid main(String[] args) {
List<String> list = Arrays.asList(
"Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red", "Cyan", "Gold", "Purple");
Comparator<String> cmp = (s1, s2) -> {
ifif (s1.length() != s2.length()) {
returnreturn s1.length() - s2.length();
}
elseelse {
returnreturn s1.compareTo(s2);
}
};
PriorityQueue<String> pq = newnew PriorityQueue<>(cmp);
pq.addAll(list);
System.out.println("Order imposed by the new comparator: ");
whilewhile (!pq.isEmpty()) {
System.out.print(pq.remove() + " ");
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
classclass Patient {
String name;
intint arrival;
intint severity;
publicpublic Patient(String name, intint arrival, intint severity) {
thisthis.name = name;
thisthis.arrival = arrival;
thisthis.severity = severity;
}
publicpublic String toString() {
returnreturn String.format("(%s, %d, %d)", name, arrival, severity);
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
4/5/21, 10)50 PMIntermediate Programming
Page 156 of 185http://itec2150.gitlab.io/
0#H#0(/G($#H#+58"(*+#($##&(/&(83#(G5+$8(</>#-(G5+$8($#+H#(A*$5$1(;3#(G/00/@5&'(J+/'+*>(5>J0#>#&8$(835$(/+?#+5&'1
;3#(J+/'+*>(<+#*8#$(*(</>J*+*8/+(cmp(83*8(</>J*+#$(J*85#&8$(A"(83#(/+?#+5&'($J#<5G5#?(*A/H#1(j/85<#(83*8(@3#&
J*85#&8$(p1(*&?(p2(3*H#(?5GG#+#&8(H*0%#$(G/+(severity-(83#(</>J*+*8/+(+#8%+&$(p2.severity - p1.severity-(not
JC1$#H#+58"(Q(JE1$#H#+58"1(;35$(5$(A#<*%$#(83#(J*85#&8$(*+#(8/(A#(/+?#+#?(G5+$8(5&(?#$<#&?5&'(/+?#+(/G(severity1(=G
83#&
83#+#G/+#(+#8%+&5&'(p2.severity - p1.severity(@500(>*,#(p1(J+#<#?#(p21
;3#(G/00/@5&'(5$(83#(/%8J%8(/G(83#(J+/'+*>1
importimport java.util.*java.util.*;
publicpublic classclass ER {
publicpublic staticstatic voidvoid main(String[] args) {
List<Patient> list = Arrays.asList(
newnew Patient("Alice", 1, 6), newnew Patient("Bob", 2, 9), newnew Patient("Carol", 3, 8),
newnew Patient("Frank", 4, 7), newnew Patient("Eve", 5, 8), newnew Patient("Dan", 6, 7));
Comparator<Patient> cmp = (p1, p2) -> {
ifif (p1.severity != p2.severity) {
returnreturn p2.severity - p1.severity;
}
elseelse {
returnreturn p1.arrival - p2.arrival;
}
};
PriorityQueue<Patient> pq = newnew PriorityQueue<>(cmp);
pq.addAll(list);
System.out.println("Order to admit the patients: ");
whilewhile (!pq.isEmpty()) {
System.out.print(pq.remove() + " ");
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
p1.severity > p2.severity,
JAVA
p2.severity - p1.severity < 0,
JAVA
Order to admit the patients:
(Bob, 2, 9) (Carol, 3, 8) (Eve, 5, 8) (Frank, 4, 7) (Dan, 6, 7) (Alice, 1, 6)
4/5/21, 10)50 PMIntermediate Programming
Page 157 of 185http://itec2150.gitlab.io/
`/A(*JJ#*+$(G5+$8(A#<*%$#(3#(3*$(83#(35'3#$8($#H#+58"(H*0%#(^1(!#(5$(G/00/@#?(A"(7*+/0(*&?(bH#(@3/(A/83(3*H#(83#
$*>#($#H#+58"(H*0%#([1(7*+/0(J+#<#?#$(bH#(A#<*%$#(7*+/0(*++5H#?(#*+05#+1(;3#"(*+#(G/00/@#?(A"(2*&(*&?(V+*&,(@3/
A/83(3*H#(83#($*>#($#H#+58"(H*0%#(W1(V+*&,(J+#<#?#$(2*&(A#<*%$#(V+*&,(*++5H#?(#*+05#+1(V5&*00"(</>#$(F05<#-(@3/
3*$(83#(0/@#$8($#H#+58"(H*0%#(M1
8.7.3. Applications of Priority Queues
;3#(>/$8(?5+#<8(*JJ05<*85/&(/G(J+5/+58"(d%#%#$(5$(sorting-(@35<3(5$(83#(J+/A0#>(/G(*++*&'5&'(*(</00#<85/&(/G
#0#>#&8$(*<</+?5&'(8/(*($J#<5G5<(/+?#+1(=&?##?-(*$("/%($##(G+/>(83#(#c*>J0#$(/G(83#($#<85/&-(*G8#+(A%50?5&'(*
J+5/+58"(d%#%#(</&8*5&5&'(83#(#0#>#&8$(/G(*(</00#<85/&-(83#(#0#>#&8$(</>#(/%8(5&(83#($/+8#?(/+?#+(*$(83#"(*+#
+#>/H#?(G+/>(83#(d%#%#1(4#(@500(+#H5$58(835$(>#83/?(/G($/+85&'(5&(83#(&#c8($#<85/&(@35<3(5$(?#H/8#?(8/($/+85&'1
)+5/+58"(d%#%#$(*+#(%$#?(8/(5>J0#>#&8(J+5/+58"QA*$#?(+#$/%+<#($<3#?%05&'-(@3#+#(*(+#$/%+<#(5$($3*+#?(*>/&'
>*&"(<%$8/>#+$(@583(?5GG#+#&8(J+5/+585#$1(;3#(#>#+'#&<"(+//>(#c*>J0#('5H#&(5&(835$($#<85/&(5$(/&#($%<3(#c*>J0#1
F&/83#+(#c*>J0#(5$(7)Z($<3#?%05&'-(@3#+#(83#(7)Z(/G(*(</>J%8#+(5$($3*+#?(A"(>*&"(J+/<#$$#$(@583(?5GG#+#&8
J+5/+585#$-(*&?(83#(/J#+*85&'($"$8#>$(>*5&8*5&$(*(J+5/+58"(d%#%#(/G(*<85H#(J+/<#$$#$(@*585&'(8/(A#(#c#<%8#?1
)+5/+58"(d%#%#$(3*H#(>*&"(/83#+(*JJ05<*85/&$1(V/+(#c*>J0#-(*(J+5/+58"(d%#%#(%&?#+05#$(83#(G*>/%$(Dijkstra’s
algorithm-(*&(#GG5<5#&8(*0'/+583>(83*8(</>J%8#$($5&'0#Q$/%+<#($3/+8#$8(J*83$(5&(*('+*J3(*&?(3*$(>*?#(?5'58*0
&*H5'*85/&(*(+#*058"1(h*+5*&8$(/G(25D,$8+*m$(*0'/+583>(*+#(</+&#+($8/&#$(G/+(&*H5'*85/&$($"$8#>$($%<3(*$(q//'0#
S*J$-(*&?(83#(UJ#&(.3/+8#$8()*83(V5+$8(OU.)VR(J+/8/</0(G/+(+/%85&'(J*<,#8$(/&(83#(=&8#+&#81
)+5/+58"(p%#%#$(*+#(*0$/(%$#?(5&(Huffman codes(@35<3(*+#(%$#?(8/(</>J+#$$(S)I(*&?(6)bq(G50#$1
8.8. Sorting
./+85&'(5$(83#(J+/A0#>(/G(*++*&'5&'(*&(*++*"(/G(#0#>#&8$(*<</+?5&'(8/(*($J#<5G5#?(/+?#+1(=8(5$(/&#(/G(83#(>/$8
G%&?*>#&8*0(J+/A0#>$(5&(</>J%85&'(*&?(5$(5&3#+#&8(5&(>*&"(*JJ05<*85/&$-(8//(>*&"(8/(#&%>#+*8#1(=8(5$(*0$/(/&#(/G
83#(A#$8($8%?5#?(J+/A0#>$(5&(</>J%85&'-(*&?($/+85&'(*0'/+583>$(*A/%&?1(=&(835$($#<85/&-(@#(J+#$#&8($5c($/+85&'
*0'/+583>$1(4#(G5+$8(?#$<+5A#(83+##($5>J0#(*0'/+583>$(,&/@&(*$(Selection Sort-(Insertion Sort(*&?(Bubble Sort1
73*&<#$(*+#(83*8("/%(3*H#(5&*?H#+8#&80"(%$#?(*8(0#*$8(/&#(/G(83#>(@3#&("/%($/+8(*($>*00(</00#<85/&(/G(&%>A#+$1
4350#(83#"(*+#(5&8%585H#(*&?($8+*5'38G/+@*+?-(83#"(*+#(H#+"($0/@(@3#&(83#(</00#<85/&(8/($/+8(5$(0*+'#1(4#(83#&
?5$<%$$(83+##(#GG5<5#&8(*0'/+583>$(,&/@&(*$(Heap Sort-(Merge Sort(*&?(Quick Sort1(;3#$#(83+##(*0'/+583>$(*+#
>/+#($%A80#(*&?(<0#H#+(83*&(83#(*G/+#>#&85/&#?(/&#$-(*&?(*+#(H#+"(G*$8(#H#&(@3#&(83#(5&J%8(5$(0*+'#1
V/+($5>J05<58"(*&?(@583/%8(0/$$(/G('#&#+*058"-(@#(?#$<+5A#(*&?(?#>/&$8+*8#(*00(83#(*0'/+583>$(5&(835$($#<85/&(G/+
$/+85&'(*&(*++*"(/G(5&8#'#+$(5&(83#(*$<#&?5&'(/+?#+1(;3#(*0'/+583>$(#c8#&?(+#*?50"(G/+(*++*"$(/G(*&"('#&#+5<(8"J#
83*8(#583#+(5>J0#>#&8$(83#(7/>J*+*A0#(5&8#+G*<#-(/+(5$($%JJ05#?(@583(*(<%$8/>(</>J*+*8/+1(=&(?#$<+5A5&'(83#
*0'/+583>$(@#(*0$/(#&H5$5/&(83*8(83#(5&J%8(*++*"(5$(0*5?(/%8(3/+5a/&8*00"-(@583(83#(0/@(5&?#c(T(/&(83#(G*+(0#G8(#&?
*&?(83#(35'3(5&?#c-(@35<3(5$(83#(0#&'83(/G(83#(*++*"(>5&%$(C-(/&(83#(G*+(+5'38(#&?1
8.8.1. Naive Iterative Algorithms
Selection Sort
4/5/21, 10)50 PMIntermediate Programming
Page 158 of 185http://itec2150.gitlab.io/
.#0#<85/&(./+8(58#+*85H#0"($/+8$(*&(*++*"(A"(>*5&8*5&5&'(*($/+8#?($%A*++*"(/&(83#(0#G8(*&?(*&(%&$/+8#?($%A*++*"(/&
83#(+5'381(=&585*00"(83#($/+8#?($%A*++*"(5$(#>J8"(*&?(83#(%&$/+8#?($%A*++*"(</&$5$8$(/G(83#(#&85+#(5&J%8(*++*"1(O!#+#
@#(*$$%>#(83*8(83#(5&J%8(*++*"(5$(%&$/+8#?1R(=&(#*<3(58#+*85/&-(83#(*0'/+583>(G5&?$(83#($>*00#$8(#0#>#&8(/G(83#
%&$/+8#?($%A*++*"-($@*J$(58(@583(83#(0#G8>/$8(#0#>#&8(/G(83#(%&$/+8#?($%A*++*"-(*&?(>/H#$(A/%&?*+"(A#8@##&(83#
$/+8#?(*&?(%&$/+8#?($%A*++*"$(/&#(J/$585/&(8/(83#(+5'381(j/85<#(83*8(#H#+"(#0#>#&8(/G(83#(%&$/+8#?($%A*++*"(5$
*0@*"$('+#*8#+(83*&(/+(#d%*0(8/(#H#+"(#0#>#&8(/G(83#($/+8#?($%A*++*"(?%+5&'(83#(#c#<%85/&(/G(83#(*0'/+583>1(`"(83#
#&?(/G(83#(*0'/+583>-(83#($/+8#?($%A*++*"(</&$5$8$(/G(*00(#0#>#&8$(/G(83#(#&85+#(*++*"(5&(83#($/+8#?(/+?#+-(@350#(83#
%&$/+8#?($%A*++*"(A#</>#$(#>J8"1(;3#(G/00/@5&'(G5'%+#(G+/>(2*&5#0(N5*&'m$(8#c8A//,-(=&8+/?%<85/&(8/(6*H*
)+/'+*>>5&'(*&?(2*8*(.8+%<8%+#$-(500%$8+*8#$(#*<3(58#+*85/&(/G(83#(*0'/+583>(@3#&(83#(5&J%8(*++*"(5$(•E-(^-(L-(K-([-(C-
M€1(=&(83#(G5'%+#-(#*<3(05&#(+#J+#$#&8$(*&(58#+*85/&(/G(83#(*0'/+583>1(=&(#*<3(58#+*85/&-(83#(#0#>#&8$(</0/+#?(A0%#(/&
83#(0#G8(*+#(#0#>#&8$(/G(83#($/+8#?($%A*++*"-(@350#(83#(+#>*5&5&'(#0#>#&8$(8/(83#(+5'38(*+#(#0#>#&8$(/G(83#
%&$/+8#?($%A*++*"1
(
4/5/21, 10)50 PMIntermediate Programming
Page 159 of 185http://itec2150.gitlab.io/
;3#(G/00/@5&'(</?#(5>J0#>#&8$(.#0#<85/&(./+81
Insertion Sort
N5,#(.#0#<85/&(./+8-(=&$#+85/&(./+8(>*5&8*5&$(*($/+8#?($%A*++*"(/&(83#(0#G8(@35<3(5$(5&585*00"(#>J8"(*&?(#H#&8%*00"
</&$5$8$(/G(83#(#&85+#(*++*"(5&(83#($/+8#?(/+?#+-(*&?(*&(%&$/+8#?($%A*++*"(/&(83#(+5'38(@35<3(5&585*00"(</&$5$8$(/G
83#(#&85+#(5&J%8(*++*"(*&?(#H#&8%*00"(A#</>#$(#>J8"1(!/@#H#+-(%&05,#(5&(.#0#<85/&(./+8-(#0#>#&8$(/G(83#(%&$/+8#?
$%A*++*"(>*"(A#($>*00#+(83*&(#0#>#&8$(/G(83#($/+8#?($%A*++*"(?%+5&'(83#(#c#<%85/&(/G(=&$#+85/&(./+81(=&(#H#+"
58#+*85/&-(=&$#+85/&(./+8(5&$#+8$(83#(0#G8>/$8(#0#>#&8(/G(83#(%&$/+8#?($%A*++*"(5&8/(83#($/+8#?($%A*++*"(*8(83#
</++#<8(J/$585/&-(A"(</>J*+5&'(58(@583(*&?($35G85&'(#0#>#&8$(/G(83#($/+8#?(*++*"($8*+85&'(*8(83#(A*<,1(;3#(G/00/@5&'
G5'%+#(G+/>(2*&5#0(N5*&'m$(8#c8A//,-(=&8+/?%<85/&(8/(6*H*()+/'+*>>5&'(*&?(2*8*(.8+%<8%+#$-(500%$8+*8#$(#*<3
58#+*85/&(/G(83#(*0'/+583>(@3#&(83#(5&J%8(*++*"(5$(•E-(^-(L-(K-([-(C-(M€1(=&(83#(G5'%+#-(#*<3(05&#(+#J+#$#&8$(*&(58#+*85/&
/G(83#(*0'/+583>1(=&(#*<3(58#+*85/&-(83#(#0#>#&8$(</0/+#?(+#?(/&(83#(0#G8(*+#(#0#>#&8$(/G(83#($/+8#?($%A*++*"-(@350#
83#(+#>*5&5&'(#0#>#&8$(8/(83#(+5'38(*+#(#0#>#&8$(/G(83#(%&$/+8#?($%A*++*"1
publicpublic staticstatic voidvoid selectionSort(intint[] A) {
/**
* In each iteration, i is the index of the leftmost element
* of the unsorted subarray
*/
forfor (intint i = 0; i < A.length - 1; i++) {
intint minIndex = i;
/**
* Find the index of the smallest element of the unsorted subarray
*/
forfor (intint j = i + 1; j < A.length; j++) {
ifif (A[j] < A[minIndex]) {
minIndex = j;
}
}
/**
* Swap the leftmost element and the smallest element of the
* unsorted subarray
*/
ifif (minIndex != i) {
swap(A, i, minIndex);
}
}
}
publicpublic staticstatic voidvoid swap(intint[] A, intint i, intint j) {
intint temp = A[i];
A[i] = A[j];
A[j] = temp;
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
4/5/21, 10)50 PMIntermediate Programming
Page 160 of 185http://itec2150.gitlab.io/
(
;3#(G/00/@5&'(</?#(5>J0#>#&8$(=&$#+85/&(./+81
4/5/21, 10)50 PMIntermediate Programming
Page 161 of 185http://itec2150.gitlab.io/
Selection Sort vs. Insertion Sort
4350#(.#0#<85/&(./+8(*&?(=&$#+85/&(./+8(*+#(A/83(H#+"($5>J0#(*&?(5&8%585H#-(=&$#+85/&(./+8(5$(%$%*00"(G*$8#+(83*&
.#0#<85/&(./+8(G/+(83#(G/00/@5&'(+#*$/&1(=&(#H#+"(J*$$(/G(=&$#+85/&($/+8-(83#(5&&#+(0//J(<*&($8/J(*$($//&(*$(58(G5&?$
83#(</++#<8(J/$585/&(G/+(83#(r<%++#&8r(#0#>#&8(8/(5&$#+8(5&8/(83#($/+8#?($%A*++*"1(=&(</&8+*$8-(5&(#H#+"(J*$$(/G
.#0#<85/&(./+8-(83#(5&&#+(0//J(>%$8(#c3*%$8(83#(#&85+#(%&$/+8#?($%A*++*"(8/(G5&?(83#(>5&5>%>1(=&(J*+85<%0*+-(@3#&
83#(5&J%8(*++*"(5$(&#*+0"($/+8#?-(=&$#+85/&(./+8(5$(*(0/8(G*$8#+(83*&(.#0#<85/&(./+81
Bubble Sort
publicpublic staticstatic voidvoid insertionSort(intint[] A) {
forfor (intint i = 1; i < A.length; i++) {
/**
* The current element to insert into the sorted subarray
*/
intint current = A[i];
intint j = i - 1;
// Start at the back of the sorted subarray
/**
* Shift elements of the sorted subarray one position to the right
* until an element less than or equal to the current element is found
* or the beginning of the sorted subarray is passed
*/
whilewhile (j >= 0 && A[j] > current) {
A[j+1] = A[j];
j--;
}
/**
* Insert the current element at the correct position
*/
A[j+1] = current;
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
4/5/21, 10)50 PMIntermediate Programming
Page 162 of 185http://itec2150.gitlab.io/
`%AA0#(./+8(58#+*85H#0"($/+8$(*&(*++*"(A"(>*5&8*5&5&'(*&(%&$/+8#?($%A*++*"(/&(83#(0#G8(@35<3(5&585*00"(</&$5$8$(/G
83#(#&85+#(5&J%8(*++*"(*&?(#H#&8%*00"(A#</>#$(#>J8"-(*&?(*($/+8#?($%A*++*"(/&(83#(+5'38(@35<3(5$(5&585*00"(#>J8"
*&?(#H#&8%*00"(</&$5$8$(/G(83#(#&85+#(*++*"(5&(83#($/+8#?(/+?#+1(2%+5&'(83#(#c#<%85/&(/G(83#(*0'/+583>-(#H#+"
#0#>#&8(/G(83#(%&$/+8#?($%A*++*"(5$($>*00#+(83*&(/+(#d%*0(8/(#H#+"(#0#>#&8(/G(83#($/+8#?($%A*++*"1(=&(#H#+"(J*$$-
83#(*0'/+583>(>/H#$(83#(0*+'#$8(#0#>#&8(/G(83#(%&$/+8#?($%A*++*"(8/(83#(</++#<8(J/$585/&(A"(</>J*+5&'(*&?
$@*JJ5&'(*?D*<#&8(#0#>#&8$1(;3#(G/00/@5&'(G5'%+#(G+/>(2*&5#0(N5*&'m$(8#c8A//,-(=&8+/?%<85/&(8/(6*H*()+/'+*>>5&'
*&?(2*8*(.8+%<8%+#$-(500%$8+*8#$(#H#+"(J*$$(/G(83#(*0'/+583>(@3#&(83#(5&J%8(*++*"(5$(•E-(^-(L-(K-([-(C€1
q#&#+*05a5&'(83#(5?#*(5&(83#(#c*>J0#(*A/H#(+#$%08$(5&(*&(*0'/+583>(83*8($/+8$(*&(*++*"(5&(#c*<80"(&QC(J*$$#$-(@3#+#
&(5$(83#(0#&'83(/G(83#(*++*"1(;35$(3/@#H#+(</%0?(A#(@*$8#G%0(A#<*%$#(83#(*++*"(>*"(A#($/+8#?(5&(>%<3(G#@#+(J*$$#$1
V/+(#c*>J0#-(5G(83#(5&J%8(*++*"(5$(M-(C-(E-(I-(K-(L€-(83#&(58(5$(#*$"(8/(<3#<,(83*8(83#(*++*"(@/%0?(A#($/+8#?(5&(D%$8(/&#
J*$$-(83#+#G/+#(83#(&#c8(J*$$#$(A#</>#(%&&#<#$$*+"1(4#(<*&(5>J+/H#(83#(*0'/+583>(A"($8/JJ5&'(58$(#c#<%85/&(D%$8
@3#&(83#(*++*"(5$($/+8#?-(%$5&'(83#(G/00/@5&'(5?#*1(=&(#*<3(J*$$-(@#(3*H#(83#(*0'/+583>(<3#<,(@3#83#+(*($@*J
/<<%+$(A#8@##&(*&"(J*5+(/G(*?D*<#&8(#0#>#&8$(/G(83#(%&$/+8#?($%A*++*"1(=G(&/($@*J(/<<%+$(?%+5&'(83#(#&85+#(J*$$-
83#&(83#(*++*"(5$(*0+#*?"($/+8#?-($/(83#(&#c8(J*$$#$(*+#(&/8(&##?#?1
;3#(G/00/@5&'(</?#(5>J0#>#&8$(`%AA0#(./+81
4/5/21, 10)50 PMIntermediate Programming
Page 163 of 185http://itec2150.gitlab.io/
8.8.2. Ecient Algorithms
;3#(83+##($/+85&'(*0'/+583>$(?5$<%$$#?($/(G*+-(&*>#0"(.#0#<85/&(./+8-(=&$#+85/&(./+8(*&?(`%AA0#(./+8-(*+#(*00(H#+"
$5>J0#(*&?(/AH5/%$0"(</++#<81(;3#"(@/+,(G5&#(@3#&(83#(5&J%8(*++*"(5$($>*00(OG/+(#c*>J0#(@3#&(83#(0#&'83(/G(83#
5&J%8(*++*"(5$(5&(83#(/+?#+(/G(CT-TTT(/+(0#$$R1(!/@#H#+-(83#"(*+#(H#+"($0/@(@3#&(83#(5&J%8(*++*"(5$(0*+'#1(S/+#
J+#<5$#0"-(83#"(#*<3(3*H#(85>#(</>J0#c58"(UO&•ER1(9/%+(@500($8%?"(85>#(</>J0#c58"(/G(*0'/+583>$(*&?(83#(`5'(U
&/8*85/&(5&(=;b7(ICLTB(F?H*&<#?()+/'+*>>5&'1(l/%'30"($J#*,5&'-(835$(>#*&$(83*8(G/+($/+85&'(*&(*++*"(/G(0#&'83(&-
publicpublic staticstatic voidvoid bubbleSort(intint[] A) {
forfor (intint pass = 1; pass < A.length; pass++) {
booleanboolean noSwap = truetrue;
/*
* i = 0 to A.length - pass are indices of the
* unsorted subarray
*/
forfor (intint i = 0; i < A.length - pass; i++) {
/**
* If an element is larger than the next element, then
* swap them and set the noSwap flag to false
*/
ifif (A[i] > A[i+1]) {
swap(A, i, i+1);
noSwap = falsefalse;
}
}
/**
* If no swap occurs during the entire pass, then the array is
* already sorted, so terminate the execution of the algorithm
*/
ifif (noSwap) {
breakbreak;
}
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
4/5/21, 10)50 PMIntermediate Programming
Page 164 of 185http://itec2150.gitlab.io/
83#(&%>A#+(/G(/J#+*85/&$(#*<3(/G(83#(83+##(*0'/+583>$(%$#$(5&(83#(@/+$8(<*$#-(5$(J+/J/+85/&(8/(&•E1(V/+(#c*>J0#-(8/
$/+8(*&(*++*"(/G(CT(>5005/&(#0#>#&8$(O51#1(@3#&(&(](CT-TTT-TTT(](CT•WR-(.#0#<85/&(./+8-(=&$#+85/&(./+8(*&?(`%AA0#(./+8
#*<3(8*,#(*JJ+/c5>*8#0"(CT•CK(/J#+*85/&$(5&(83#(@/+$8(<*$#1(F(>/?#+&(</>>/?58"()7(#c#<%8#$(*A/%8(/&#(A5005/&
OCT•^R(/J#+*85/&$(J#+($#</&?1(;3#+#G/+#-(/&($%<3(*(J0*8G/+>-(58(8*,#$(#*<3(/G(83#(83+##(&*5H#(*0'/+583>$(*A/%8
CT•CK(P(CT•^(](CTT-TTT($#</&?$-(@35<3(5$(*A/%8(28 hours-(8/($/+8(*&(*++*"(/G(CT(>5005/&(#0#>#&8$1
=&(835$($%A$#<85/&-(@#(J+#$#&8(83+##(#GG5<5#&8($/+85&'(*0'/+583>$(Q(Heap Sort-(Merge Sort(*&?(Quick Sort-(@35<3
*+#(H#+"(G*$8(#H#&(@3#&(83#(5&J%8(5$(0*+'#1(S/+#(J+#<5$#0"-(83#"(#*<3(3*H#(85>#(</>J0#c58"(/&0"(UO&(0/'(&R-(@3#+#
83#(0/'*+583>(5$(/G(A*$#(E1(j/85<#(83*8(@350#(CT(>5005/&(>*"(A#(0*+'#-(83#(A*$#QE(0/'*+583>(/G(CT(>5005/&(5$(0#$$(83*&
EK-(@35<3(5$($>*00n(;3#+#G/+#-(8/($/+8(*&(*++*"(/G(CT(>5005/&(#0#>#&8$-(!#*J(./+8-(S#+'#(./+8(*&?(p%5<,(./+8(#*<3
8*,#(*A/%8(/&0"(E1K(s(CT•[(/J#+*85/&$-(*$(/JJ/$#?(8/(CT•CK(5&(83#(<*$#(/G(83#(83+##(&*5H#(*0'/+583>$1(U&(*(J0*8G/+>
83*8(#c#<%8#$(/&#(A5005/&(/J#+*85/&$(J#+($#</&?-(58(8*,#$(#*<3(/G(!#*J(./+8-(S#+'#(./+8(*&?(p%5<,(./+8(/&0"(*A/%8
0.3 second(8/($/+8(*&(*++*"(/G(CT(>5005/&(#0#>#&8$n
Heap Sort
F$(@#($*@(G+/>(83#($#<85/&(/&()+5/+58"(p%#%#$-(#H#+"(J+5/+58"(d%#%#('5H#$(+5$#(8/(*($/+85&'(*0'/+583>(*$(G/00/@$B
C1( 7/&$8+%<8(*(J+5/+58"(d%#%#(</&$5$85&'(/G(*00(#0#>#&8$(/G(83#(5&J%8(*++*"-(@3#+#(83#(J+5/+58"(5$(?#G5&#?(A"(#583#+
83#(&*8%+*0(/+?#+5&'(/G(83#(#0#>#&8$-(/+(83#(/+?#+5&'(5>J/$#?(A"(*(<%$8/>(</>J*+*8/+1
E1( l#>/H#(/&#(#0#>#&8(*8(*(85>#(G+/>(83#(J+5/+58"(d%#%#(%&850(58(5$(#>J8"1
`"(83#(J+5/+58"(d%#%#(J+/J#+8"-(83#(#0#>#&8$(*JJ#*+(5&(83#($/+8#?(/+?#+(*$(83#"(#c58(83#(d%#%#1(;35$(*0'/+583>(5$
,&/@&(*$(!#*J(./+8(@3#&(83#(J+5/+58"(d%#%#(5$(5>J0#>#&8#?(%$5&'(*(heap-(*(?*8*($8+%<8%+#(83*8("/%(@500(0#*+&(5&
=;b7(ICLTB(F?H*&<#?(J+/'+*>>5&'1(=&?##?-(*(6*H*(J+5/+58"(d%#%#(5$(3#*JQA*$#?1(;3#(G/00/@5&'(</?#(5>J0#>#&8$
!#*J(./+81
Merge Sort
publicpublic staticstatic voidvoid heapSort(Integer[] A) {
/**
* Construct a priority queue consisting of all elements of the
* input array. Using this constructor is more efficient than using
* the default constructor to create an empty priority queue and then
* adding each element of the array to the queue.
*/
PriorityQueue<Integer> pq = newnew PriorityQueue<>(Arrays.asList(A));
/**
* Remove one element at a time from the priority queue and copy it
* to the array until the priority queue is empty.
*/
forfor (intint i = 0; i < A.length; i++) {
A[i] = pq.remove();
}
}
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4/5/21, 10)50 PMIntermediate Programming
Page 165 of 185http://itec2150.gitlab.io/
S#+'#(./+8(5$(*&(#GG5<5#&8(*0'/+583>(83*8($/+8$(*&(*++*"(A"(recursion-(*(J/@#+G%0(8#<3&5d%#("/%(0#*+&#?(5&(73*J8#+
W1(=&(83#(A*$#(<*$#(@3#+#(83#(*++*"(5$(/G(0#&'83(/&#-(83#(*0'/+583>(?/#$(&/835&'-(*$(*($5&'0#8/&(5$($/+8#?1(=&('#&#+*0-
83#(*0'/+583>(?5H5?#$(83#(*++*"(5&8/(8@/(3*0H#$(Q(*(0#G8(3*0G(</&$5$85&'(/G(#0#>#&8$(G+/>(83#($8*+8(8/(83#(>5??0#
5&?#c(/G(83#(*++*"(*&?(*(+5'38(3*0G(</&$5$85&'(/G(83#(+#>*5&5&'(#0#>#&8$-(83#&(recursively($/+8$(83#(8@/(3*0H#$-(*&?
G5&*00"(>#+'#$(83#(8@/($/+8#?(3*0H#$(5&8/(*(@3/0#($/+8#?(*++*"1(;35$(5$(*&(5&$8*&<#(/G(*('#&#+*0(*0'/+583>(?#$5'&
J*+*?5'>(,&/@&(*$(divide-and-conquer1(;3#(G/00/@5&'(G5'%+#(G+/>(2*&5#0(N5*&'m$(8#c8A//,-(=&8+/?%<85/&(8/(6*H*
)+/'+*>>5&'(*&?(2*8*(.8+%<8%+#$-(500%$8+*8#$(83#(#c#<%85/&(/G(83#(*0'/+583>(@3#&(83#(5&J%8(*++*"(5$(•E-(^-(L-(K-([-(C-
M-(W€1
(
F$("/%(<*&($##-(83#(*<8%*0($/+85&'(3*JJ#&$(@3#&($%A*++*"$(*+#(>#+'#?1(;3#(G/00/@5&'(</?#(5>J0#>#&8$(S#+'#
./+8(*$(@#00(*$(83#(>#83/?(G/+(>#+'5&'(8@/($/+8#?(*++*"$(5&8/(*($5&'0#(/&#1
publicpublic staticstatic voidvoid mergeSort(intint[] A) {
/**
* Base Case: If the array is of length 1, then do nothing
*/
ifif (A.length == 1) {
returnreturn;
}
JAVA
1
2
3
4
5
6
7
8
9
4/5/21, 10)50 PMIntermediate Programming
Page 166 of 185http://itec2150.gitlab.io/
Quick Sort
intint n = A.length;
intint m = n / 2;
//Find the middle index of the array
/**
* Divide the array into two halves: left and right
*/
intint[] left = newnew intint[m];
intint[] right = newnew intint[n-m];
forfor (intint i = 0; i < m; i++) {
left[i] = A[i];
}
forfor (intint i = 0; i < n-m; i++) {
right[i] = A[m+i];
}
/**
* Recursively sort the left and right halves
*/
mergeSort(left);
mergeSort(right);
/**
* Merge the two sorted subarrays into a whole sorted array
*/
merge(left, right, A);
}
/**
* Merges two sorted arrays into a single sorted array
*
* @param left a sorted array
* @param right another sorted array
* @param A the destination to store the merged array
*/
publicpublic staticstatic voidvoid merge(intint[] left, intint[] right, intint[] A) {
intint curL = 0;
intint curR = 0;
intint cur = 0;
whilewhile (curL < left.length && curR < right.length) {
ifif (left[curL] < right[curR]) {
A[cur++] = left[curL++];
}
elseelse {
A[cur++] = right[curR++];
}
}
whilewhile (curL < left.length) {
A[cur++] = left[curL++];
}
whilewhile (curR < right.length) {
A[cur++] = right[curR++];
}
}
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
4/5/21, 10)50 PMIntermediate Programming
Page 167 of 185http://itec2150.gitlab.io/
p%5<,(./+8(5$(*&/83#+(#GG5<5#&8(?5H5?#Q*&?Q</&d%#+($/+85&'(*0'/+583>1(=&(83#(A*$#(<*$#(@3#+#(83#(*++*"(5$(/G(0#&'83
a#+/(/+(/&#-(83#+#(5$(&/835&'(8/(?/1(=&('#&#+*0-(83#(*0'/+583>(J5<,$(*(pivot(#0#>#&8(G+/>(83#(*++*"-(83#&(partitions
83#(*++*"(*+/%&?(J5H/8(5&8/(8@/($%A*++*"$(Q(*(0#G8($%A*++*"(@3#+#(#H#+"(#0#>#&8(5$(&/('+#*8#+(83*&(83#(J5H/8-(*&?(*
+5'38($%A*++*"(@3#+#(#H#+"(#0#>#&8(5$('+#*8#+(83*&(83#(J5H/81(FG8#+(83#(J*+8585/&-(83#(*0'/+583>(recursively($/+8$
83#(0#G8($%A*++*"(*&?(83#(+5'38($%A*++*"1(;3#(J5H/8(#0#>#&8(5$(%$%*00"(<3/$#&(+*&?/>0"(*>/&'(*00(*++*"(#0#>#&8$1
=8(5$(<0#*+(83*8(J*+8585/&5&'(83#(*++*"(*+/%&?(83#(J5H/8(5$(83#(#$$#&85*0(J*+8(/G(83#(*0'/+583>-(@3#+#($/+85&'(*<8%*00"
8*,#$(J0*<#1(F&(/AH5/%$(@*"(8/(5>J0#>#&8(83*8(5$(8/(<+#*8#(8@/(*%c505*+"(*++*"$(G/+(83#(0#G8(*&?(+5'38($%A*++*"$
+#$J#<85H#0"1(U&<#(83#(J5H/8(5$(<3/$#&-(/&#(<*&($5>J0"(58#+*8#(/H#+(83#(*++*"(*&?(</>J*+#(#*<3(#0#>#&8(@583(83#
J5H/81(=G(*&(#0#>#&8(5$($>*00#+(83*&(/+(#d%*0(8/(83#(J5H/8-(83#&(</J"(58(8/(83#(0#G8($%A*++*"e(/83#+@5$#(</J"(58(8/(83#
+5'38($%A*++*"1(=8(5$(3/@#H#+(@*$8#G%0(8/(<+#*8#(83#(#c8+*($J*<#(*&?(</J"(#0#>#&8$(A*<,(*&?(G/+83(A#8@##&(83#
/+5'5&*0(*&?(*%c505*+"(*++*"$(@3#&#H#+(@#(<*&(*H/5?(581(=8(8%+&$(/%8(83*8(J*+8585/&5&'(<*&(A#(?/&#(in place-(83*8(5$-
5&(83#(/+5'5&*0(*++*"(58$#0G(@583/%8(*&"(*%c505*+"(?*8*($8+%<8%+#-(*$(500%$8+*8#?(5&(83#(G/00/@5&'(G5'%+#(G+/>(2*&5#0
N5*&'m$(8#c8A//,-(=&8+/?%<85/&(8/(6*H*()+/'+*>>5&'(*&?(2*8*(.8+%<8%+#$-(G/+(83#(5&J%8(*++*"(•L-(E-(^-(I-([-(K-(T-(C-(M-
W€1
4/5/21, 10)50 PMIntermediate Programming
Page 168 of 185http://itec2150.gitlab.io/
(
4#(&/@(A+5#G0"(#cJ0*5&(83#(5&QJ0*<#(J*+8585/&(*0'/+583>1(;3#(500%$8+*85/&(*A/H#(*$$%>#$(83*8(83#(G5+$8(#0#>#&8(/G
83#(5&J%8(*++*"(5$(<3/$#&(*$(83#(J5H/81(=&('#&#+*0-(@3#&(83#(J5H/8(5$(&/8(83#(G5+$8(#0#>#&8-(/&#(<*&($@*J(58(@583(83#
G5+$8(#0#>#&8(*&?(83#&(J+/<##?(@583(83#($*>#(>#83/?1(;3#(*0'/+583>(>*5&8*5&$(8@/(J/5&8#+$-(*(0/@(J/5&8#+(83*8
$8*+8$(*8($#</&?(5&?#c(/G(83#(5&J%8(*++*"-(*&?(*(35'3(J/5&8#+(83*8(5$(5&585*00"(*8(83#(G*+Q+5'38(#&?(/G(83#(*++*"1(;3#
*0'/+583>(+#J#*8#?0"(*?H*&<#$(83#(0/@(J/5&8#+(8/(83#(+5'38(%&850(*&(#0#>#&8('+#*8#+(83*&(83#(J5H/8(5$(G/%&?-
*?H*&<#$(83#(35'3(J/5&8#+(8/(83#(0#G8(%&850(*&(#0#>#&8($>*00#+(83*&(/+(#d%*0(8/(83#(J5H/8(5$(G/%&?-(*&?($@*J$(83#
8@/(#0#>#&8$-(until the two pointers cross each other1(F8(835$(J/5&8-(*00(#0#>#&8$(8/(83#(0#G8(/G(83#(0/@(J/5&8#+(*+#
$>*00#+(83*&(/+(#d%*0(8/(83#(J5H/8-(@350#(*00(#0#>#&8$(8/(83#(+5'38(/G(83#(35'3(J/5&8#+(*+#('+#*8#+(83*&(83#(J5H/81(;3#
*0'/+583>(</>J0#8#$(83#(J*+8585/&(A"($@*JJ5&'(83#(J5H/8(@35<3(@*$(*8(83#(A#'5&&5&'(/G(83#(5&J%8(*++*"-(@583(83#
#0#>#&8(*8(83#(35'3(J/5&8#+(@35<3(5$(&/@(8/(83#(5>>#?5*8#(0#G8(/G(83#(0/@(J/5&8#+1(FG8#+(83#($@*J-(*00(#0#>#&8$(8/
83#(0#G8(/G(83#(J5H/8(*+#($>*00#+(83*&(/+(#d%*0(8/(83#(J5H/8-(*&?(*00(#0#>#&8$(8/(83#(+5'38(/G(83#(J5H/8(*+#('+#*8#+(83*&
83#(J5H/81(;3#(G/00/@5&'(</?#(5>J0#>#&8$(p%5<,(./+8(*$(@#00(*$(83#(J*+8585/&(>#83/?1
4/5/21, 10)50 PMIntermediate Programming
Page 169 of 185http://itec2150.gitlab.io/
publicpublic staticstatic voidvoid quickSort(intint[] A) {
quickSortHelper(A, 0, A.length, newnew Random());
}
/**
* The recursive helper method for Quick Sort that sorts in place a section of
* an array with a start index (inclusive) and an end index (exclusive)
*
* @param A an array
* @param start the start index of the section to sort
* @param end the end index of the section to sort
* @param rand the Random object for picking a pivot element
*/
publicpublic staticstatic voidvoid quickSortHelper(intint[] A, intint start, intint end, Random rand) {
/**
* Base Case: If the array is of length 0 or 1, then do nothing
*/
intint n = end - start;
ifif (n <= 1) {
returnreturn;
}
/**
* Pick a random index between start (inclusive) and end (exclusive),
* choose the element at this index as the pivot element, and
* swap the pivot with the element at start
*/
intint pivotIdx = rand.nextInt(n) + start;
swap(A, start, pivotIdx);
/**
* Partition the section of A between start and end
* around the pivot
*/
pivotIdx = partition(A, start, end);
/**
* Recursively sort the left subarray and the right subarray
*/
quickSortHelper(A, start, pivotIdx, rand);
quickSortHelper(A, pivotIdx+1, end, rand);
}
/**
* Partitions the section of an array between a start index and an end index
* around a pivot element which is initially at the start index
*
* @param A an array
* @param start the start index of the section
* @param end the end index of the section
* @return the index of the pivot after the partition
*/
publicpublic staticstatic intint partition(intint[] A, intint start, intint end) {
JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4/5/21, 10)50 PMIntermediate Programming
Page 170 of 185http://itec2150.gitlab.io/
A Comparison of Heap Sort, Merge Sort and Quick Sort
`/83(!#*J(./+8(*&?(S#+'#(./+8(3*H#(@/+$8Q<*$#(85>#(</>J0#c58"(UO&(0/'(&R1(!/@#H#+-(?%#(8/(83#(</$8(8/(>*5&8*5&(*
3#*J-(!#*J(./+8(5$(%$%*00"($0/@#+(83*&(S#+'#(./+81
intint pivot = A[start];
intint low = start + 1;
intint high = end - 1;
/**
* Repeat until the low pointer and high pointer cross each other
*/
whilewhile (low <= high) {
/**
* Advance the low pointer to the right until
* an element greater than the pivot is found
*/
whilewhile (low <= high && A[low] <= pivot) {
low += 1;
}
/**
* Advance the high pointer to the left until an element
* smaller than or equal to the pivot is found
*/
whilewhile (low <= high && A[high] > pivot) {
high -= 1;
}
/**
* Swap the elements at the low and high indices
*/
ifif (low < high) {
swap(A, low++, high--);
}
}
/**
* Swap the pivot which was at the start index with
* the element at the high index. After the swap the
* partition is complete. The pivot is now at the
* high index. All elements in A[start], ..., A[high-1]
* are smaller than or equal to the pivot. All elements
* in A[high+1], ..., A[end-1] are greater than the pivot.
*/
swap(A, start, high);
/**
* Return the high index which is now the pivot index
*/
returnreturn high;
}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
4/5/21, 10)50 PMIntermediate Programming
Page 171 of 185http://itec2150.gitlab.io/
=&(83#(@/+$8(<*$#-(p%5<,(./+8(</%0?(3*H#(85>#(</>J0#c58"(UO&•ER1(;35$(3*JJ#&$(@3#&(A*?(J5H/8(#0#>#&8$(*+#
<3/$#&(?%+5&'(83#(#c#<%85/&(/G(83#(*0'/+583>(83*8(+#$%08(5&(%&A*0*&<#?(J*+8585/&$1(F&(#c8+#>#(<*$#(5$(@3#+#(5&
#H#+"(+#<%+$5H#(<*00(83#(*0'/+583>(J5<,$(83#($>*00#$8(/+(83#(0*+'#$8(#0#>#&8(/G(*($%A*++*"(*$(83#(J5H/8-($/(83*8(*00
#0#>#&8$(/G(83#($%A*++*"(*JJ#*+(/&(/&#($5?#(/G(83#(J5H/8(*G8#+(83#(J*+8585/&1(.%<3(@/+$8Q<*$#($<#&*+5/$(*+#
3/@#H#+(#c8+#>#0"(%&05,#0"(5G(83#(J5H/8(5$(<3/$#&(randomly(*$(@#(?/(5&(/%+(5>J0#>#&8*85/&(*A/H#1(U&(*H#+*'#
*&?(5&(83#(/H#+@3#0>5&'(>*D/+58"(/G(*00(<*$#$-(p%5<,(./+8(3*$(85>#(</>J0#c58"(UO&(0/'(&R1(S/+#/H#+-(5&(J+*<85<#
Quick Sort is usually faster than Merge Sort-(*(J+5>*+"(+#*$/&(A#5&'(83*8(p%5<,(./+8(5$(in place1(=&(</&8+*$8-(S#+'#
./+8(&##?$(8/(*00/<*8#(*%c505*+"(>#>/+"($J*<#(*&?(</J"(#0#>#&8$(A#8@##&(83#(5&J%8(*&?(*%c505*+"($J*<#(8/(>#+'#
$/+8#?($%A*++*"$1(bH#&(/&(*(>/?#+&(</>J%8#+-(>#>/+"(/J#+*85/&$(*+#($8500($5'&5G5<*&80"($0/@#+(83*&(*+583>#85<
/J#+*85/&$1(43#&($J##?(5$(/%+(J+5>*+"(</&<#+&-(p%5<,(./+8(5$(%$%*00"(83#($/+85&'(*0'/+583>(/G(<3/5<#1
!/@#H#+-(S#+'#(./+8(3*$(*(&5<#(J+/J#+8"(,&/@&(*$(stability(83*8(>*,#$(58(?#$5+*A0#(G/+($/+85&'(complex objects1(F
$/+85&'(*0'/+583>(5$(stable(5G(58(+#$J#<8$(83#(/+?#+(/G(83#(#0#>#&8$(5&(83#(/+5'5&*0(5&J%8e(83*8(5$-(G/+(#0#>#&8$(/G(83#
same priority-(83#(*0'/+583>(preserves their order in the original input1(;35$(&/85/&(/G($8*A5058"(>*,#$(&/($#&$#(G/+
#0#>#&8$(/G(*(J+5>585H#(8"J#1(!/@#H#+-(58(5$(>#*&5&'G%0(@3#&(@#($/+8(/AD#<8$(/G(*(<0*$$1(4#(?#>/&$8+*8#(83#
>#*&5&'(*&?(%$#G%0&#$$(/G($8*A5058"(%$5&'(83#(#c*>J0#('5H#&(*8(835$($58#
O388J$BPP<*G#1#03*+/1</>PJ+/'+*>>5&'PD*H*QJ+/'+*>>5&'P@3"QD*H*Q%850Q*++*"$Q%$#$Q8@/Q$/+85&'Q*0'/+583>$PR1(7/&$5?#+(83#
G/00/@5&'(<0*$$(83*8(+#J+#$#&8$($8%?#&8$(8*,5&'(*(</%+$#(@583($#H#+*0($#<85/&$1
.%JJ/$#(83*8(@#(*+#('5H#&(*&(5&J%8(*++*"(/G(.8%?#&8(/AD#<8$(83*8(*+#(*0+#*?"($/+8#?(A"(&*>#-(*$(G/00/@$B
.%JJ/$#(83*8(&/@(@#($/+8(83#($8%?#&8$(A"($#<85/&1(FG8#+($/+85&'(83#('5H#&(*++*"(%$5&'(S#+'#(./+8(O/+(*&"($8*A0#
$/+85&'(*0'/+583>R-(83#(*++*"(A#</>#$
classclass Student {
String lastName;
String firstName;
intint section;
}
JAVA
1
2
3
4
5
John Alisson 2
Nabeel Aronowitz 3
Joe Jones 2
James Ledbetter 2
Ilya Lessing 1
Betty Lipschitz 2
Betty Neubacher 2
John Neubacher 3
Katie Senya 1
Jim Smith 3
Ping Yi 1
JAVA
1
2
3
4
5
6
7
8
9
10
11
4/5/21, 10)50 PMIntermediate Programming
Page 172 of 185http://itec2150.gitlab.io/
j/85<#(83*8(83#($8%?#&8$(*+#(&/8(/&0"($/+8#?(A"($#<85/&(Q($8%?#&8$(@5835&(#*<3($#<85/&(*+#(*0$/($/+8#?(A"(&*>#1(;35$
5$(A#<*%$#(J+5/+(8/(83#($/+85&'(A"($#<85/&-(83#($8%?#&8$(@#+#(*0+#*?"($/+8#?(A"(&*>#1(`#<*%$#(S#+'#(./+8(5$($8*A0#-
*$(58($/+8$(83#($8%?#&8$(A"($#<85/&-(58(J+#$#+H#$(83#(/+5'5&*0(/+?#+(OA"(&*>#R(/G(83#($8%?#&8$(@583(83#($*>#($#<85/&
&%>A#+1
=&(</&8+*$8-(p%5<,(./+8(5$(instable-(A#<*%$#(83#(5&QJ0*<#(J*+8585/&(*0'/+583>(5$(&/8(/+?#+(J+#$#+H5&'1(;3#(G/00/@5&'(5$
/&#(J/$$5A0#(/%8</>#(@3#&(@#(*JJ0"(p%5<,(./+8(8/($/+8(83#($*>#(5&J%8(A"($#<85/&B
;3#+#G/+#-(G/+($/+85&'(</>J0#c(/AD#<8$-(@3#&($8*A5058"(5$(+#d%5+#?-(S#+'#(./+8(5$(%$%*00"(83#(J+#G#++#?(*0'/+583>1
Arrays.sort, Collections.sort and Arrays.parallelSort
./+85&'(5$($/(G%&?*>#&8*0(83*8(#H#+"(>/?#+&(J+/'+*>>5&'(0*&'%*'#(J+/H5?#$(#GG5<5#&8('#&#+5<(>#83/?$(/+
G%&<85/&$(G/+($/+85&'(5&(83#5+(F)=$1(6*H*(J+/H5?#$(8@/($%<3(>#83/?$(Q(Arrays.sort(G/+($/+85&'(*++*"$-(*&?
Collections.sort(G/+($/+85&'(05$8$1(F00(83#(?#8*50$(/G(83#(8@/(>#83/?$(<*&(A#(G/%&?(3#+#
O388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850PF++*"$138>0R(*&?(3#+#
O388J$BPP?/<$1/+*<0#1</>PD*H*$#P[P?/<$P*J5PD*H*P%850P7/00#<85/&$138>0R1(43#&($/+85&'(*(05$8-(7/00#<85/&$1$/+8(G5+$8(</J5#$(83#(05$8
#0#>#&8$(8/(*&(*++*"-(83#&($/+8$(83#(*++*"(%$5&'(F++*"$1$/+8-(*&?(G5&*00"(</J5#$(83#(#0#>#&8$(G+/>(83#($/+8#?(*++*"
A*<,(8/(83#(05$81(`#0/@(@#(#0*A/+*8#(/&(F++*"$1$/+81
Ilya Lessing 1
Katie Senya 1
Ping Yi 1
John Alisson 2
Joe Jones 2
James Ledbetter 2
Betty Lipschitz 2
Betty Neubacher 2
Nabeel Aronowitz 3
John Neubacher 3
Jim Smith 3
JAVA
1
2
3
4
5
6
7
8
9
10
11
Ilya Lessing 1
Ping Yi 1
Katie Senya 1
Betty Lipschitz 2
Betty Neubacher 2
Joe Jones 2
James Ledbetter 2
John Alisson 2
John Neubacher 3
Jim Smith 3
Nabeel Aronowitz 3
JAVA
1
2
3
4
5
6
7
8
9
10
11
4/5/21, 10)50 PMIntermediate Programming
Page 173 of 185http://itec2150.gitlab.io/
F++*"$1$/+8(5$(?#G5&#?(G/+(*++*"$(/G(*00(J+5>585H#(8"J#$(@583(*(&*8%+*0(/+?#+5&'(O*00(&%>#+5<*0(8"J#$(*&?(<3*+*<8#+$R-
*$(@#00(*$(*++*"$(/G(/AD#<8$1(V/+($/+85&'(/AD#<8$-(F++*"$1$/+8(*JJ05#$(8/(#H#+"(<0*$$(83*8(5>J0#>#&8$(83#(7/>J*+*A0#
5&8#+G*<#-(*&?(*&(*+A58+*+"(<0*$$(@3#&($%JJ05#?(@583(*(<%$8/>(</>J*+*8/+(G/+(83#(<0*$$1(V/+(#c*>J0#-(5G(F(5$(*&
*++*"(/G(/AD#<8$(/G(*(<0*$$(7(83*8(5>J0#>#&8$(7/>J*+*A0#-(83#&
$/+8$(F(*<</+?5&'(8/(83#(/+?#+5&'(?#G5&#?(A"(83#(compareTo(>#83/?(/G(70*$$(71
=G(F(5$(*&(*++*"(/G(/AD#<8$(/G(*&(*+A58+*+"(<0*$$(*&?(<>J(5$(*(<%$8/>(</>J*+*8/+(?#G5&#?(G/+(83#(<0*$$-(83#&
$/+8$(F(*<</+?5&'(8/(83#(/+?#+5&'($J#<5G5#?(A"(83#(</>J*+*8/+(<>J1
F++*"$1$/+8(#>J0/"$(two($/+85&'(*0'/+583>$(Q(58(%$#$(*(G*$8(H#+$5/&(/G(Quick Sort(,&/@&(*$(Dual Pivot Quicksort(8/
$/+8(J+5>585H#$-(*&?(%$#$(*(G*$8(H*+5*&8(/G(Merge Sort(,&/@&(*$(Timsort(8/($/+8(/AD#<8$1
4#(G5&*00"(>#&85/&(83*8(5&(6*H*([(*&/83#+($/+85&'(>#83/?-(Arrays.parallelSort-(@*$(5&8+/?%<#?(@35<3(5>J+/H#$
83#(J#+G/+>*&<#(/G(F++*"$1$/+8(A"(multithreading-(*(8/J5<("/%(@500(0#*+&(5&(=;b7(ICLTB(F?H*&<#?()+/'+*>>5&'1
8.9. Exercises
8.9.1. Exercise 1
9/%(*+#(not(*00/@#?(8/(%$#(83#(Collections.reverse(>#83/?(G/+(835$(#c#+<5$#1
O*R(4+58#(*('#&#+5<(>#83/?
83*8(+#H#+$#$(83#(/+?#+(/G(83#(#0#>#&8$(/G(*('5H#&(*++*"(05$8(in place-(83*8(5$-(5&(83#(/+5'5&*0(05$8(58$#0G(@583/%8(%$5&'
*&"(*%c505*+"(?*8*($8+%<8%+#1(.5&<#(83#(5&J%8(05$8(5$(*&(array(05$8-(58(5$(efficient(8/(*<<#$$(58$(#0#>#&8$(A"(5&?#c1(V/+(835$
>#83/?-("/%(*+#(*00/@#?(8/(58#+*8#(/H#+(83#(5&J%8(05$8(A"(5&?#c-(*&?(%$#(83#(get(*&?(set(>#83/?$(/G(83#(05$8(8/(+#*?
*&?(>/?5G"(83#(#0#>#&8(*8(*&"(5&?#c1
OAR(4+58#(*('#&#+5<(>#83/?
83*8(/&(5&J%8(*(05&,#?(05$8-(+#8%+&$(*&/83#+(05&,#?(05$8(83*8(</&8*5&$(83#(#0#>#&8$(/G(83#(/+5'5&*0(05$8(5&(83#(+#H#+$#
/+?#+1(.5&<#(83#(5&J%8(05$8(5$(*(linked(05$8-(58(5$(inefficient(8/(*<<#$$(58$(#0#>#&8$(A"(5&?#c1(V/+(835$(>#83/?-("/%(*+#(not
*00/@#?(8/(*<<#$$(83#(#0#>#&8$(/G(83#(5&J%8(05$8(A"(5&?#c1(=&$8#*?-("/%(must(%$#(83#(58#+*8/+(/G(83#(5&J%8(05$8(8/
Arrays.sort(A)
JAVA
Arrays.sort(A, cmp)
JAVA
publicpublic staticstatic <E> voidvoid reverseArrayListInPlace(ArrayList<E> list)
JAVA
publicpublic staticstatic <E> LinkedList<E> reverseLinkedList(LinkedList<E> list)
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 174 of 185http://itec2150.gitlab.io/
58#+*8#(/H#+(83#(05$8(D%$8(/&<#(5&(#583#+(?5+#<85/&-(*&?(#&(+/%8#(*??(#*<3(#0#>#&8(8/(*&(/%8J%8(05$8(%$5&'(*(J+/J#+
add(>#83/?1
O<R(4+58#(*('#&#+5<(>#83/?
83*8(+#H#+$#$(83#(/+?#+(/G(83#(#0#>#&8$(/G(*('5H#&(05&,#?(05$8(in place1(F$(5&()*+8(OAR-("/%(*+#(not(*00/@#?(8/(*<<#$$
83#(#0#>#&8$(/G(83#(5&J%8(05$8(A"(5&?#c1(=&$8#*?-("/%(must(58#+*8#(/H#+(83#(05$8(D%$8(/&<#(%$5&'(58$(05$8(58#+*8/+1(OHintB
Z$#(8@/(58#+*8/+$-(/&#($8*+85&'(*8(83#(3#*?(*&?(83#(/83#+($8*+85&'(*8(83#(8*50(/G(83#(5&J%8(05$81R
8.9.2. Exercise 2
=>J0#>#&8(83#(G/00/@5&'('#&#+5<(>#83/?$(using the list iterator(s) of the input list(s)1(V/+(#*<3(>#83/?(5&H/0H5&'(*&
5&?#c-($8*+8(83#(58#+*8/+(*8(83#(#&?(/G(83#(05$8(83*8(5$(<0/$#+(8/(83#(5&?#c1
O*R
;35$(>#83/?(*??$(83#('5H#&(#0#>#&8(8/(83#('5H#&(05$8(*8(83#($J#<5G5#?(5&?#c1(;3#(>#83/?($3/%0?(83+/@(*&
IndexOutOfBoundException(5G(83#($J#<5G5#?(5&?#c(5$(/%8(/G(+*&'#-(51#1(5G(O5&?#c(x(T(~~(5&?#c(y(05$81$5a#ORR1(9/%(*+#
not(*00/@#?(8/(%$#(83#(addO5&8(5&?#c-(b(#R(>#83/?(/G(83#(5&J%8(05$81
OAR
;35$(>#83/?(+#>/H#$(83#(#0#>#&8(*8(83#($J#<5G5#?(5&?#c(G+/>(83#('5H#&(05$81(;3#(>#83/?($3/%0?(83+/@(*&
IndexOutOfBoundException(5G(83#($J#<5G5#?(5&?#c(5$(/%8(/G(+*&'#-(51#1(5G(O5&?#c(x(T(~~(5&?#c(y](05$81$5a#ORR1(9/%(*+#
not(*00/@#?(8/(%$#(83#(removeO5&8(5&?#cR(>#83/?(/G(83#(5&J%8(05$81
O<R
;35$(>#83/?(+#8%+&$(83#(#0#>#&8(/G(83#('5H#&(05$8(*8(83#($J#<5G5#?(5&?#c1(;3#(>#83/?($3/%0?(83+/@(*&
IndexOutOfBoundException(5G(83#($J#<5G5#?(5&?#c(5$(/%8(/G(+*&'#-(51#1(5G(O5&?#c(x(T(~~(5&?#c(y](05$81$5a#ORR1(9/%(*+#
not(*00/@#?(8/(%$#(83#(getO5&8(5&?#cR(>#83/?(/G(83#(5&J%8(05$81
O?R
publicpublic staticstatic <E> LinkedList<E> reverseLinkedList(LinkedList<E> list)
JAVA
publicpublic staticstatic <E> voidvoid add(List<E> list, intint index, E element)
JAVA
publicpublic staticstatic <E> E remove(List<E> list, intint index)
JAVA
publicpublic staticstatic <E> E get(List<E> list, intint index)
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 175 of 185http://itec2150.gitlab.io/
;35$(>#83/?(+#J0*<#$(83#(#0#>#&8(/G(83#('5H#&(05$8(*8(83#($J#<5G5#?(5&?#c(@583(83#('5H#&(#0#>#&81(;3#(>#83/?
$3/%0?(83+/@(*&(IndexOutOfBoundException(5G(83#($J#<5G5#?(5&?#c(5$(/%8(/G(+*&'#-(51#1(5G(O5&?#c(x(T(~~(5&?#c(y]
05$81$5a#ORR1(9/%(*+#(not(*00/@#?(8/(%$#(83#(setO5&8(5&?#c-(b(#0#>#&8R(>#83/?(/G(83#(5&J%8(05$81
O#R
;35$(>#83/?(+#8%+&$(83#(5&?#c(/G(83#(G5+$8(/<<%++#&<#(/G(83#($J#<5G5#?(#0#>#&8(5&(83#('5H#&(05$8(5G(83#(#0#>#&8(5$(5&
83#(05$8-(*&?(QC(/83#+@5$#1(9/%(*+#(not(*00/@#?(8/(%$#(83#(indexOfOUAD#<8(/R(>#83/?(/G(83#(5&J%8(05$81
OGR
;35$(>#83/?(+#8%+&$(83#(5&?#c(/G(83#(0*$8(/<<%++#&<#(/G(83#($J#<5G5#?(#0#>#&8(5&(83#('5H#&(05$8(5G(83#(#0#>#&8(5$(5&
83#(05$8-(*&?(QC(/83#+@5$#1(9/%(*+#(not(*00/@#?(8/(%$#(83#(lastIndexOfOUAD#<8(/R(>#83/?(/G(83#(5&J%8(05$81
O'R
;35$(>#83/?(+#>/H#$(83#(G5+$8(/<<%++#&<#(/G(83#($J#<5G5#?(#0#>#&8(G+/>(83#('5H#&(05$8-(5G(83#(#0#>#&8(#c5$8$1(;3#
>#83/?(+#8%+&$(8+%#(5G(83#($J#<5G5#?(#0#>#&8(5$(J+#$#&8(5&(83#(05$8-(*&?(G*0$#(/83#+@5$#1(9/%(*+#(not(*00/@#?(8/(%$#
83#(removeOUAD#<8(/R(>#83/?(/G(83#(5&J%8(05$81
O3R
;35$(>#83/?(+#8%+&$(8+%#(5G(83#(8@/('5H#&(05$8(</&8*5&$(83#($*>#(#0#>#&8$(5&(83#($*>#(/+?#+1(9/%(*+#(not(*00/@#?
8/(%$#(83#(equalsOUAD#<8(/R(>#83/?(/G(#583#+(5&J%8(05$81
8.9.3. Exercise 3
4#(>#&85/&#?(83*8(*($8*<,(5$(*(H#+"($J#<5*0(05$8(@583(/&#(#&?(<0/$#?1(=&(835$(#c#+<5$#("/%(*+#(8/(5>J0#>#&8(*($8*<,
%$5&'(*(linked list1(7+#*8#(*(J%A05<('#&#+5<(<0*$$(MyStackxEy1(;3#(<0*$$($3/%0?(3*H#(*(A*<,5&'(05&,#?(05$8(*$(*
J+5H*8#(5&$8*&<#(H*+5*A0#B
publicpublic staticstatic <E> voidvoid set(List<E> list, intint index, E element)
JAVA
publicpublic staticstatic <E> intint indexOf(List<E> list, E element)
JAVA
publicpublic staticstatic <E> intint lastIndexOf(List<E> list, E element)
JAVA
publicpublic staticstatic <E> booleanboolean remove(List<E> list, E element)
JAVA
publicpublic staticstatic <E> booleanboolean equals(List<E> list1, List<E> list2)
JAVA
privateprivate LinkedList<E> backingList;
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 176 of 185http://itec2150.gitlab.io/
;3#(A*<,5&'(05$8(%&?#+05#$(83#($8*<,("/%(*+#(*A/%8(8/(5>J0#>#&81(9/%(<*&(%$#(#583#+(#&?(/G(83#(A*<,5&'(05$8(*$(83#
8/J(/G(83#($8*<,1
=>J0#>#&8(83#(G/00/@5&'(>#83/?$(G/+(83#(MyStackxEy(<0*$$-(%$5&'(83#(J+/J#+(>#83/?$(/G(83#(A*<,5&'(05$8B
F(</&$8+%<8/+(83*8(8*,#$(&/(J*+*>#8#+(*&?(<+#*8#$(*(&#@-(#>J8"($8*<,1
public void pushOE(#R1(;35$(>#83/?(*??$(83#($J#<5G5#?(#0#>#&8(8/(83#(8/J(/G(83#($8*<,1
public E popOE(#R1(;35$(>#83/?(+#>/H#$(*&?(+#8%+&$(83#(#0#>#&8(*8(83#(8/J(/G(83#($8*<,1(=8(83+/@$(*&
EmptyStackException(5G(83#($8*<,(5$(#>J8"1
public E peekOE(#R1(;35$(>#83/?(+#8%+&$(83#(#0#>#&8(*8(83#(8/J(/G(83#($8*<,(@583/%8(+#>/H5&'(581(=8(83+/@$(*&
EmptyStackException(5G(83#($8*<,(5$(#>J8"1
bc<#J8(@3#&(<3#<,5&'(G/+(*&(#>J8"($8*<,-(83#(push-(pop(*&?(peek(>#83/?$($3/%0?(only(%$#(83#(addFirst-
removeFirst(*&?(getFirst(>#83/?$-(/+(83#(addLast-(removeLast(*&?(getLast(>#83/?$(/G(83#(A*<,5&'(05$8-
?#J#&?5&'(/&(@35<3(#&?(/G(83#(05$8("/%(%$#(*$(83#(8/J(/G(83#($8*<,1
8.9.4. Exercise 4
4#(>#&85/&#?(83*8(*(d%#%#(5$(*0$/(*(H#+"($J#<5*0(05$8(@3#+#(#0#>#&8$(*+#(/&0"(*??(*8(/&#(#&?(*&?(+#>/H#?(*8(83#
/83#+(#&?(/G(83#(05$81(=&(835$(#c#+<5$#("/%(*+#(8/(5>J0#>#&8(*(d%#%#(%$5&'(*(linked list1(7+#*8#(*(J%A05<('#&#+5<(<0*$$
MyQueuexEy1(;3#(<0*$$($3/%0?(3*H#(*(A*<,5&'(05&,#?(05$8(*$(*(J+5H*8#(5&$8*&<#(H*+5*A0#B
;3#(A*<,5&'(05$8(%&?#+05#$(83#(d%#%#("/%(*+#(*A/%8(8/(5>J0#>#&81
=>J0#>#&8(83#(G/00/@5&'(>#83/?$(G/+(83#(MyQueuexEy(<0*$$-(%$5&'(83#(J+/J#+(>#83/?$(/G(83#(A*<,5&'(05$8B
F(</&$8+%<8/+(83*8(8*,#$(&/(J*+*>#8#+(*&?(<+#*8#$(*(&#@-(#>J8"(d%#%#1
public void addOE(#R1(;35$(>#83/?(*??$(83#($J#<5G5#?(#0#>#&8(8/(83#(tail(/G(83#(d%#%#1
public E removeOR1(;35$(>#83/?(+#>/H#$(*&?(+#8%+&$(83#(#0#>#&8(*8(83#(head(/G(83#(d%#%#1(=8(83+/@$(*
NoSuchElementException(5G(83#(d%#%#(5$(#>J8"1
public E peekHeadOR1(;35$(>#83/?(+#8%+&$(83#(#0#>#&8(*8(83#(head(/G(83#(d%#%#(@583/%8(+#>/H5&'(581(=8(83+/@$
*(NoSuchElementException(5G(83#(d%#%#(5$(#>J8"1
public E peekTailOR1(;35$(>#83/?(+#8%+&$(83#(#0#>#&8(*8(83#(tail(/G(83#(d%#%#(@583/%8(+#>/H5&'(581(=8(83+/@$(*
NoSuchElementException(5G(83#(d%#%#(5$(#>J8"1
bc<#J8(@3#&(<3#<,5&'(G/+(*&(#>J8"(d%#%#-(83#(add-(remove-(peakHead(*&?(peakTail(>#83/?$($3/%0?(only(%$#
83#(>#83/?$(G/+(*<<#$$5&'(83#(8@/(#&?$(/G(83#(A*<,5&'(05$81
8.9.5. Exercise 5
privateprivate LinkedList<E> backingList;
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 177 of 185http://itec2150.gitlab.io/
=&(835$(#c#+<5$#-("/%(*+#(8/($/0H#(H*+5*&8(/G(83#(A*0*&<#?(J*+#&83#$#$(J+/A0#>(@583(83+##(?5GG#+#&8(8"J#$(/G
J*+#&83#$#$-(@3#+#(oOo-(oRo-(o•o-(o€o-(ozo(*&?(o}o(</%0?(*00(A#(J+#$#&8(5&(83#(5&J%81(.%<3(*($#d%#&<#(/G(J*+#&83#$#$(5$
A*0*&<#?(5G(#H#+"(/J#&(J*+#&83#$5$(5$(<0/$#?(A"(*(<0/$5&'(J*+#&83#$5$(/G(83#(same type(*&?(#H#+"(<0/$5&'
J*+#&83#$5$(<0/$#$(*&(/J#&5&'(J*+#&83#$5$(/G(83#(same type(5&(83#(correct order1(V/+(#c*>J0#-(rzO•€R}r(*&?(rOR•€z}r
*+#(A*0*&<#?-(@3#+#*$(rO€r(*&?(r•O€Rr(*+#(&/8(A*0*&<#?1
Z$5&'(*(stack-(@+58#(*(>#83/?
83*8(/&(*&(5&J%8($8+5&'(</&$5$85&'(/G(oOo-(oRo-(o•o-(o€o-(ozo(*&?(o}o-(?#<5?#$(@3#83#+(/+(&/8(83#(5&J%8(5$(*(A*0*&<#?($#d%#&<#
/G(J*+#&83#$#$1
8.9.6. Exercise 6
Z$5&'(*(priority queue-(@+58#(*('#&#+5<(>#83/?
83*8(/&(5&J%8(*(05$8(F(/G(/AD#<8$(/G(83#('#&#+5<(<0*$$(E-(*&(5&8#'#+(,(x(F1$5a#OR-(*&?(*(</>J*+*8/+(<>J(/&(83#(<0*$$(E-
+#8%+&$(*(05$8(</&$5$85&'(/G(83#(,($>*00#$8(#0#>#&8$(5&(83#(/+?#+(83*8(5$(?#G5&#?(A"(<>J1(9/%(*+#(8/(@+58#(83#(>#83/?
G/00/@5&'(83#($8#J$(A#0/@B
C1( 7+#*8#(*(J+5/+58"(d%#%#(G/+(/AD#<8$(/G(83#('#&#+5<(<0*$$(E-(%$5&'(83#('5H#&(</>J*+*8/+(<>J1
E1( F??(*00(#0#>#&8$(/G(83#(5&J%8(05$8(F(8/(83#(J+5/+58"(d%#%#1
I1( 7*00(*(J+/J#+(>#83/?(8/(+#>/H#(G+/>(83#(J+5/+58"(d%#%#(,(85>#$1(b*<3(85>#(*&(#0#>#&8(5$(+#>/H#?(G+/>(83#
J+5/+58"(d%#%#-(*??(58(8/(*&(/%8J%8(05$81
K1( l#8%+&(83#(/%8J%8(05$81
8.9.7. Exercise 7
;3#(Point(<0*$$(?#G5&#?(A#0/@(+#J+#$#&8$(J/5&8$(5&(*(8@/Q?5>#&$5/&*0(J0*&#1
publicpublic staticstatic booleanboolean isBalanced2(String parentheses)
JAVA
publicpublic staticstatic <E> List<E> kSmallest(List<E> A, intint k, Comparator<E> cmp)
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 178 of 185http://itec2150.gitlab.io/
!#+#(83#(x(*&?(y(5&$8*&<#(H*+5*A0#$(+#J+#$#&8(83#(x(*&?(yQ<//+?5&*8#$(/G(*(J/5&8(5&(83#(J0*&#1
4+58#(*('#&#+5<(>#83/?
83*8(/&(5&J%8(*(05$8(F(/G(J/5&8$(*&?(*&(5&8#'#+(,(x(F1$5a#OR-(+#8%+&$(*(05$8(</&$5$85&'(/G(83#(,(<0/$#$8(J/5&8$(8/(83#
/+5'5&(OT-(TR1
HintB
C1( 2#G5&#(*(</>J*+*8/+(G/+(83#(Point(<0*$$(83*8('5H#$(35'3#+(J+5/+585#$(8/(J/5&8$(<0/$#+(8/(83#(/+5'5&1
E1( 7*00(83#(>#83/?(kSmallest(G+/>(83#(J+#H5/%$(#c#+<5$#1
8.9.8. Exercise 8
V/+(8@/(&%>A#+$(c(*&?("-(@#(?#G5&#(83#5+(distance(8/(A#(~c(Q("~-(51#1(83#(*A$/0%8#(H*0%#(/G(83#(?5GG#+#&<#(A#8@##&(c
*&?("1(=&(835$(#c#+<5$#-("/%(*+#(8/(@+58#(8@/(?5GG#+#&8(>#83/?$(8/($/0H#(83#(G/00/@5&'(J+/A0#>B(q5H#&(*&(*++*"(/G
5&8#'#+$-(G5&?(83#(?5$8*&<#(A#8@##&(83#(closest pair(/G(#0#>#&8$-(51#1(G5&?(83#(minimum distance(*>/&'(*00(J*5+$(/G
#0#>#&8$(5&(83#(*++*"1(V/+(#c*>J0#-(5G(83#(5&J%8(*++*"(5$(•[-(CE-(C^-(I-(CL€-(83#&("/%+(>#83/?$($3/%0?(+#8%+&(I-
A#<*%$#(83#(<0/$#$8(J*5+(/G(#0#>#&8$(5&(83#(*++*"(5$(•CE-(CL€(*&?(83#5+(?5$8*&<#(5$(~CE(Q(CL~(](I1
O*R(4+58#(*(>#83/?
83*8(G5&?$(83#(minimum distance(*>/&'(*00(J*5+$(/G(#0#>#&8$(5&(83#(5&J%8(*++*"(F-(A"(exhaustively searching for the
minimum through all pairs of elements using a nested loop1
publicpublic classclass Point {
privateprivate doubledouble x;
privateprivate doubledouble y;
publicpublic Point(doubledouble x, doubledouble y) {
thisthis.x = x;
thisthis.y = y;
}
publicpublic doubledouble getX() {
returnreturn x;
}
publicpublic doubledouble getY() {
returnreturn y;
}
}
JAVA
publicpublic staticstatic List<Point> kClosestToOrigin(List<Point> A, intint k)
JAVA
publicpublic staticstatic intint closestPairBruteForce(intint[] A)
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 179 of 185http://itec2150.gitlab.io/
OAR(=&(835$(J*+8-("/%(*+#(8/(@+58#(*(>%<3(>/+#(#GG5<5#&8(>#83/?(G/+($/0H5&'(83#($*>#(J+/A0#>(83*&(83#(A+%8#QG/+<#
>#83/?("/%(@+/8#(5&()*+8(O*R1(4+58#(*(>#83/?
83*8(G5&?$(83#(minimum distance(*>/&'(*00(J*5+$(/G(#0#>#&8$(5&(83#(5&J%8(*++*"(F-(A"(first sorting(83#(5&J%8(*++*"
%$5&'(Arrays.sort(/+(Arrays.parallelSort-(83#&(G5&?5&'(83#(>5&5>%>(?5$8*&<#(*>/&'(/&0"(83#(adjacent pairs(/G
#0#>#&8$(5&(83#(sorted(*++*"1
O<R(4+58#(*(>*5&(>#83/?(8/(8#$8(*&?(</>J*+#("/%+(</?#(G/+(closestPairBruteForce(*&?(closestPairSort-(*$
G/00/@$B
V5+$8('#&#+*8#(*($>*00(O$*"(/G(0#&'83(CTR(*++*"(/G(random(5&8#'#+$-(<*00(A/83(>#83/?$(/&(83#($*>#(5&J%8(*++*"
*&?(A#($%+#(83*8(83#"(A/83('5H#(83#($*>#(</++#<8(*&$@#+1(l#J#*8(835$($#H#+*0(85>#$1
j/@(8+"(83#(G/00/@5&'(5&J%8($5a#$B(CTTT-(CT-TTT-(CTT-TTT(*&?(CTTT-TTT1(V/+(#*<3(5&J%8($5a#-('#&#+*8#(*&(*++*"(/G
83*8($5a#(</&$5$85&'(/G(random(5&8#'#+$-(<*00(A/83(>#83/?$(/&(83#($*>#(5&J%8(*++*"(*&?(time your methods1
l#J#*8(835$($#H#+*0(85>#$(G/+(#*<3(5&J%8($5a#1(43*8(?/("/%(/A$#+H#\(=G("/%+(5>J0#>#&8*85/&$(*+#(</++#<8-("/%
$3/%0?(/A$#+H#(83*8(closestPairSort(5$(>%<3(G*$8#+(closestPairBruteForce(*$(83#(5&J%8($5a#(5&<+#*$#$1(7*&("/%
#cJ0*5&(835$(J3#&/>#&/&-(%$5&'(time complexity(83*8(5$(A+5#G0"(?5$<%$$#?(5&(835$(<3*J8#+\
8.9.9. Exercise 9
=>J0#>#&8(*(generic(H#+$5/&(/G(.#0#<85/&(./+8-(=&$#+85/&(./+8-(`%AA0#(./+8-(!#*J(./+8-(S#+'#(./+8(*&?(p%5<,(./+8
G/+($/+85&'(*&(*++*"(/G(/AD#<8$(G+/>(*(generic class that implements(Comparable1(;3#(3#*?#+$(/G(83#(>#83/?$
$3/%0?(A#(*$(G/00/@$B
publicpublic staticstatic intint closestPairSort(intint[] A)
JAVA
publicpublic staticstatic <T extendsextends Comparable<? supersuper T>> voidvoid selectionSort(T[] A)
JAVA
publicpublic staticstatic <T extendsextends Comparable<? supersuper T>> voidvoid insertionSort(T[] A)
JAVA
publicpublic staticstatic <T extendsextends Comparable<? supersuper T>> voidvoid bubbleSort(T[] A)
JAVA
publicpublic staticstatic <T extendsextends Comparable<? supersuper T>> voidvoid heapSort(T[] A)
JAVA
publicpublic staticstatic <T extendsextends Comparable<? supersuper T>> voidvoid mergeSort(T[] A)
JAVA
publicpublic staticstatic <T extendsextends Comparable<? supersuper T>> voidvoid quickSort(T[] A)
JAVA
4/5/21, 10)50 PMIntermediate Programming
Page 180 of 185http://itec2150.gitlab.io/
F0$/(@+58#(*(>*5&(>#83/?(8/(8#$8(83#(*A/H#($5c(>#83/?$(*$(G/00/@$1(7+#*8#(*($>*00(*++*"(/G($8+5&'$(/+(/AD#<8$(G+/>
*&"(<0*$$(83*8(5>J0#>#&8$(7/>J*+*A0#1(7*00(#*<3(/G(83#($5c(>#83/?$(*A/H#-(*$(@#00(*$(Arrays.sort(/+
Arrays.parallelSort-(8/($/+8(83#($*>#(5&J%8(*++*"(*&?(H#+5G"(83*8(83#"(*00('5H#(83#(</++#<8(+#$%081
8.9.10. Exercise 10
O*R(=>J0#>#&8(*(generic(H#+$5/&(/G(.#0#<85/&(./+8-(=&$#+85/&(./+8-(`%AA0#(./+8-(!#*J(./+8-(S#+'#(./+8(*&?(p%5<,
./+8(G/+($/+85&'(*&(*++*"(/G(/AD#<8$(G+/>(*(generic class when supplied with a custom(comparator1(;3#(3#*?#+$(/G
83#(>#83/?$($3/%0?(A#(*$(G/00/@$B
;3#(J*+8$(A#0/@(*+#(G/+(8#$85&'(*&?(</>J*+5&'("/%+(>#83/?$(5&()*+8(O*R1
OAR(7+#*8#(83#(G/00/@5&'(<0*$$(83*8(+#J+#$#&8$($8%?#&8$(8*,5&'(*(</%+$#(@583($#H#+*0($#<85/&$B
4+58#(*(</&$8+%<8/+(*&?(*(8/.8+5&'(>#83/?(G/+(83#(<0*$$1
2#G5&#(*(</>J*+*8/+(cmp(G/+(83#(Student(<0*$$(83*8(/+?#+$($8%?#&8$(G5+$8(A"(0*$8(&*>#(5&(*0J3*A#85<(/+?#+-(83#&(A"
G5+$8(&*>#(5&(*0J3*A#85<(/+?#+(@3#&(8@/($8%?#&8$(3*H#(83#($*>#(0*$8(&*>#1
O<R(4+58#(*(>*5&(>#83/?(8/(8#$8("/%+(>#83/?$(G+/>()*+8(O*R(*$(G/00/@$1
7+#*8#(*&(*++*"(</&$5$85&'(/G(83#(G/00/@5&'(.8%?#&8(/AD#<8$(5&(83#('5H#&(/+?#+B
publicpublic staticstatic <T> voidvoid selectSort(T[] A, Comparator<T> cmp)
JAVA
publicpublic staticstatic <T> voidvoid insertionSort(T[] A, Comparator<T> cmp)
JAVA
publicpublic staticstatic <T> voidvoid bubbleSort(T[] A, Comparator<T> cmp)
JAVA
publicpublic staticstatic <T> voidvoid heapSort(T[] A, Comparator<T> cmp)
JAVA
publicpublic staticstatic <T> voidvoid mergeSort(T[] A, Comparator<T> cmp)
JAVA
publicpublic staticstatic <T> voidvoid quickSort(T[] A, Comparator<T> cmp)
JAVA
classclass Student {
String lastName;
String firstName;
intint section;
}
JAVA
1
2
3
4
5
4/5/21, 10)50 PMIntermediate Programming
Page 181 of 185http://itec2150.gitlab.io/
7*00(#*<3(/G(83#($5c(>#83/?$(G+/>()*+8(O*R-(*$(@#00(*$(Arrays.sort(/+(Arrays.parallelSort-(8/($/+8(83#(*++*"(%$5&'
83#(</>J*+*8/+(cmp(G+/>()*+8(OAR1(F00(83#($#H#&(>#83/?$($3/%0?('5H#
*$(83#(/%8</>#1
O?R(2#G5&#(*&/83#+(</>J*+*8/+(cmp2(83*8(/+?#+$(.8%?#&8(/AD#<8$(A"($#<85/&(5&(*$<#&?5&'(/+?#+1(=&(83#(>*5&
>#83/?(<*00(#*<3(/G(83#($5c(>#83/?$(G+/>()*+8(O*R-(*$(@#00(*$(Arrays.sort(/+(Arrays.parallelSort-(8/($/+8(83#
+#$%085&'(*++*"(G+/>()*+8(O<R-(835$(85>#(%$5&'(cmp2(*$(83#(</>J*+*8/+1(7/>J*+#(83#(+#$%08$(/G(83#($#H#&(>#83/?$1
43*8(?/("/%(/A$#+H#\(435<3(>#83/?$(*+#($8*A0#\
8.10. Issue Tracker/Comments
=$$%#(;+*<,#+ O388J$BPP'583%A1</>P3J*+,WP3#0JY?#$,P5$$%#$R
John Neubacher 3
Ilya Lessing 1
Nabeel Aronowitz 3
Joe Jones 2
Katie Senya 1
John Alisson 2
Betty Neubacher 2
James Ledbetter 2
Betty Lipschitz 2
Ping Yi 1
Jim Smith 3
JAVA
1
2
3
4
5
6
7
8
9
10
11
John Alisson 2
Nabeel Aronowitz 3
Joe Jones 2
James Ledbetter 2
Ilya Lessing 1
Betty Lipschitz 2
Betty Neubacher 2
John Neubacher 3
Katie Senya 1
Jim Smith 3
Ping Yi 1
JAVA
1
2
3
4
5
6
7
8
9
10
11
4/5/21, 10)50 PMIntermediate Programming
Page 182 of 185http://itec2150.gitlab.io/
9. Glossary
Behavior
*<85/&$(/G(*&(/AD#<8e(+#J+#$#&8#?(A"(83#(>#83/?$(/G(*&(/AD#<81
Class
*(A0%#J+5&8(83*8(?#G5&#$(*&(/AD#<81
Heap memory
?"&*>5<*00"(*00/<*8#?(>#>/+"(%$#?(8/($8/+#(/AD#<8$1
Object
+#J+#$#&8$(*&(#&858"(5&(83#(+#*0(@/+0?(*&?(3*$($8*8#(*&?(A#3*H5/+e(*&(5&$8*&<#(/G(*(<0*$$1
Object-oriented programming
*(@*"(/G(/+'*&5a5&'(</?#(*+/%&?(/AD#<8$-(5&$8#*?(/G(*<85/&$1
Reference
>#>/+"(*??+#$$(/G(@3#+#(83#(/AD#<8(5$(0/<*8#?1
Reference type
*(<0*$$e(H*+5*A0#(/G(835$(8"J#(<*&(+#G#+#&<#(*&(/AD#<8(/G(*(<0*$$1
Reference variable
H*+5*A0#(/G(*(<0*$$(8"J#-(@35<3(</&8*5&$(*(+#G#+#&<#(8/(83#(/AD#<8(/G(83*8(<0*$$1
Stack memory
$8/+#$(0/<*0(H*+5*A0#$(/G(J+5>585H#(8"J#(*&?(+#G#+#&<#(H*+5*A0#$e(>#>/+"(5$(*<<#$$#?(5&(V5+$8(=&(N*$8(U%8(/+?#+1
State
+#J+#$#&8#?(A"(?*8*(G5#0?$(/+(*88+5A%8#$(/G(83#(/AD#<81
Association
*('#&#+*0(A5&*+"(+#0*85/&(A#8@##&(8@/($#J*+*8#(<0*$$#$1(V/+(#c*>J0#-(*($8%?#&8(8*,5&'(*(</%+$#(5$(*&(*$$/<5*85/&
A#8@##&(83#(.8%?#&8(<0*$$(*&?(83#(7/%+$#(<0*$$1
Aggregation
*&(*$$/<5*85/&(@3#&(/&#(/AD#<8(%$#$(*&/83#+(/AD#<81
Composition
*&(*$$/<5*85/&(@3#&(/&#(/AD#<8(/@&$(/83#+(<0*$$(*&?(/83#+(<0*$$(<*&&/8(>#*&5&'G%00"(#c5$81(7/>J/$585/&(5$
$8+/&'#+(83*&(*''+#'*85/&1
4/5/21, 10)50 PMIntermediate Programming
Page 183 of 185http://itec2150.gitlab.io/
Overriding method
43#&(*(>#83/?(5&(*($%A<0*$$(3*$(83#($*>#(&*>#-($*>#(J*+*>#8#+$(/+($5'&*8%+#-(*&?($*>#(+#8%+&(8"J#$O/+($%AQ
8"J#R(*$(*(>#83/?(5&(58$($%J#+<0*$$1
Overloading method
*(<0*$$(5$(*00/@#?(8/(3*H#(>/+#(83*&(/&#(>#83/?(3*H5&'(83#($*>#(&*>#(*$(0/&'(*$(83#5+(J*+*>#8#+(05$8$(*+#
?5GG#+#&81
Inheritance
*(>#<3*&5$>(8/(?#G5&#(&#@(<0*$$#$(G+/>(#c5$85&'(<0*$$#$1(=&(D*H*-(<0*$$#$(<*&(5&3#+58(83#(J+/J#+85#$(*&?(>#83/?$
/G($%J#+<0*$$1
Superclass
*('#&#+*0(<0*$$(@35<3(*(>#83/?O$R(8/(*($%A<0*$$1(U+(83#(<0*$$(A#5&'(5&3#+58#?(G+/>1
Subclass
;3#(?#+5H#?(<0*$$(83*8(5$(?#+5H#?(G+/>($%J#+<0*$$1
Exception
F&(#++/&#/%$(/+(*&/>*0/%$(</&?585/&(83*8(</>#$(%J(@3#&(*(J+/'+*>(5$(+%&&5&'1
Exception Handling
F&(*JJ+/*<3(83*8($#J*+*8#$(*(J+/'+*>m$(&/+>*0(</?#(G+/>(58m$(#++/+Q3*&?05&'(</?#1
Throw
;/(83+/@(*&(#c<#J85/&(5$(8/(<+#*8#(*&(#c<#J85/&(/AD#<8(*&?(J*$$(58(/GG(8/(83#(+%&Q85>#(#&H5+/&>#&81(;35$(5$(?/&#
#cJ05<58"(5&(</?#(%$5&'(83#(throw(,#"@/+?1
Stack Trace
F($8*<,(8+*<#(5$($/>#85>#$(<*00#?(*($8*<,(A*<,8+*<#(/+(#H#&(D%$8(*(A*<,8+*<#1(;3#($8*<,(8+*<#5$(*(05$8(/G($8*<,
G+*>#$1(F($8*<,(G+*>#(5&?5<*8#$(*(>/>#&8(?%+5&'(*&(*JJ05<*85/&m$(#c#<%85/&(@3#&(*(>#83/?(5$(<*00#?1(F($8*<,
G+*>#(</&8*5&$(5&G/+>*85/&(*A/%8(@3#+#(83#(>#83/?(@*$(<*00#?(G+/>(5&(83#(6*H*($/%+<#(</?#1(./(83#(6*H*($8*<,
8+*<#('#&#+*8#?(@3#&(*&(#c<#J85/&(5$(<*00#?(5$(*(05$8(/G(G+*>#$(83*8($8*+8$(*8(05&#(5&(83#(>#83/?(83#(#c<#J85/&
/<<%++#?(*&?(#c8#&?$(A*<,(8/(@3#&(83#(J+/'+*>($8*+8#?1
File
F(+#$/%+<#(%$#?(8/($8/+#(*(</00#<85/&(/G(?*8*(/&(*(</>J%8#+($8/+*'#(?#H5<#1
Text File
F(</>J%8#+(G50#(</&$5$85&'(/G(3%>*&(+#*?*A0#(Z&5</?#(<3*+*<8#+$1(;"J5<*00"(+#*?(%$5&'(*(8#c8(#?58/+(05,#(83#(/&#
5&(>/$8(=2bm$1(7/&$5?#+#?(3%>*&(+#*?*A0#1
4/5/21, 10)50 PMIntermediate Programming
Page 184 of 185http://itec2150.gitlab.io/
Binary File
F(</>J%8#+(G50#($8/+#?(5&(83#(&*85H#(A5&*+"(</?#(/G(83#(</>J%8#+1(j/8(</&$5?#+#?(3%>*&(+#*?*A0#1
Input
=&G/+>*85/&(/+(?*8*(G+/>(*&(#c8#+&*0($/%+<#(+#*?(5&8/(*(6*H*(J+/'+*>1
Output
=&G/+>*85/&(/+(?*8*(G+/>(*(6*H*(J+/'+*>(@+588#&(8/(*&(#c8#+&*0($/%+<#1
Open a File
7+#*8#(*($8+#*>(/G(?*8*(8/(/+(G+/>(*(</>J%8#+(G50#1
File Stream
F(/&#(@*"(d%#%#(/G(?*8*(#583#+(8/(/+(G+/>(*(G50#1(;3#(/+?#+(/G(?*8*(5&(83#(d%#%#(+#J+#$#&8$(83#(/+?#+(/G(83#(?*8*
5&(83#(G50#1
Close a File
V0%$3(*&?(<0/$#(*($8+#*>(/G(?*8*(8/(/+(G+/>(*(G50#1(43#&(@+585&'(*(G50#-(G/+<#$(83#(J+/'+*>(8/(@*58(%&850(*00(?*8*(5&
83#($8+#*>(3*$(A##&(@+588#&(8/(83#(G50#1(43#&(+#*?5&'(*(G50#-(8#+>5&*8#$(*&"(G%+83#+(?*8*(</>5&'(G+/>(83#(G50#1
Delimiter
2#05>58#+$(*+#(@358#$J*<#(<3*+*<8#+$(%$#?(8/($#J*+*8#(H*+5/%$(J5#<#$(/G(?*8*(5&(*(8#c8(G50#1(bc*>J0#$(*+#(*(A0*&,
$J*<#-(8*A-(/+(#&?(/G(05&#(<3*+*<8#+$(@35<3(?/(&/8($3/@(%J(*$(J+5&8(5&(*(8#c8(G50#1
bounded type
F('#&#+5<(8"J#(A#5&'($J#<5G5#?(*$(*($%A8"J#(/G(*&/83#+(8"J#
upper bounded wildcard (<? extends E>)
A/%&?$(@583(%JJ#+(5&3#+58*&<#(</&$8+*5&8(A"(%$5&'(#8#&?$(,#"@/+?1
lower bound wildcard (<? super E>)
A/%&?$(5$(%$5&'(83#(@50?<*+?(<3*+*<8#+(O\R-(G/00/@5&'(A"(83#($%J#+(,#"@/+?(A"(58$(0/@#+(A/%&?1
unbounded wildcard(<?>)
A/%&?$(@35<3(5$($J#<5G5#?(%$5&'(x\y1(835$(5$(<*00#?(%&,&/@&(8"J#1
raw type
*(&*>#(/G(*('#&#+5<(<0*$$(/+(5&8#+G*<#(@583/%8(*&"(8"J#(*+'%>#&8$1
type erasure
83#(J+/<#$$(/G(8"J#(<3#<,5&'(/&0"(*8(</>J50#(85>#(*&?(?5$<*+?5&'(83#(#0#>#&8(8"J#(5&G/+>*85/&(*8(+%&85>#1
4/5/21, 10)50 PMIntermediate Programming
Page 185 of 185http://itec2150.gitlab.io/
recursion
</>J%8*85/&(83*8(5&H/H0#$(*(G%&<85/&(O/+(>#83/?R(<*005&'(58$#0G
base case
83#($5>J0#$8(<*$#(5&(*(+#<%+$5H#($/0%85/&
recursive case
>5++/+$(83#(/H#+*00($/0%85/&(A%8(@583($5>J05G5#?(5&J%8(H*0%#$
direct recursion
@3#&(83#($*>#(>#83/?(<*00$(58$#0G
indirect recursion
@3#&(>/+#(83*&(/&#(>#83/?(5$(5&H/0H#?(5&(*(+#<%+$5/&
recursive backtracking
@3#&(+#<%+$5/&(5$(%$#?(8/(A%50?(*($#8(/G(<*&?5?*8#($/0%85/&$(*&?(*(<+58#+5*(5$(*JJ05#?(8/($#0#<8(83#(+5'38(/&#$