Professional Documents
Culture Documents
JavaPractice 3
JavaPractice 3
JavaPractice 3
Description
In this session you will ractice Java !ollections API and learn a"out #enerics.
Documentation
1. $. %a&talin, Ph. 'adler. Java Generics and Collections. ()*eilly, 2++, 2. htt -../ava.sun.co0./2se.1.5. d&.1enerics2tutorial. d&
3o ic 1- Data structures
Goal: 4nderstand the need &or collections. 4nderstand co00on data structures. Tasks: 1. !reate an Array class that will e0ulate the "ehavior o& an array o& values. !lass 0ust i0 le0ent &ollowin1 0ethodspublic class Array { public Array(int size) {} //constructor public int getSize() {} //returns size public Object getElement(int index){} //i returns null public boolean setElement(int index$ Object element){}# //i } index is !it"in array boundary$ places speci ied Element at# alse speci ied position and returns true$ ot"er!ise returns index is !it"in# array boundary$ returns Element at speci ied index$ ot"er!ise#
2. !reate a %ector class to e0ulate "ehavior o& 5ector. !lass 0ust i0 le0ent &ollowin1 0ethodspublic class %ector { public %ector(int initial&apacity) {} //creates a %ector !it"# speci ied initial capacity public int get&urrentSize() {} //returns current number o in t"is %ector public 'oid addElement(Object element) {} //adds an element to t"e# end o t"is %ector$ increasing %ector(s capacity i needed public boolean remo'eAt(int index) {} //i boundary$ remo'es element at speci ied index public boolean insertAt(int index$ Object element) {} //i index is# !it"in array boundary$ inserts an element at speci ied index and returns# Version 2.0 index is !it"in array# elements#
array boundary$ returns Element at speci ied index$ ot"er!ise# public boolean setElement(int index$ Object element){} # //i } index is !it"in array boundary$ places speci ied Element at# alse speci ied position and returns true$ ot"er!ise returns
3. !reate a )in*ed)ist class to e0ulate "ehavior o& 7in8ed 7ist. !lass 0ust i0 le0ent &ollowin1 0ethodspublic class )in*ed)ist { public 'oid add)ast(Object element) {} //adds an element at t"e end# o t"is )ist public 'oid add+irst(Object element) {} //adds an element at t"e# beginning o t"is )ist irst Element o t"e )ist i # public Object get+irst() {} //returns
Hint: 9or e&&icient i0 le0entation o& lin8ed list, create a nested class that would re resent ele0ent o& the list.
t"e )ist is not empty$ ot"er!ise returns null public Object get)ast() {} //returns last Element o t"e )ist is not empty$ ot"er!ise returns null public boolean remo'eAt(int index) {} //i boundary$ remo'es element at speci ied index public boolean insertAt(int index$ Object element) {} //i true$ ot"er!ise returns alse index is !it"in# index is# !it"in array boundary$ inserts an element at speci ied index and returns# public Object getElement(int index){} //i returns null public boolean setElement(int index$ Object element){} # //i index is !it"in array boundary$ places speci ied Element at# alse public %ector to%ector() {} //returns a %ector containing# elements o } t"is )ist speci ied position and returns true$ ot"er!ise returns index is !it"in array# t"e )ist i #
6. !reate a Stac* class to e0ulate "ehavior o& Stac8. !lass 0ust i0 le0ent &ollowin1 0ethodspublic class Stac* { public 'oid pus"(Object element) {} //adds an element at t"e top# o t"is Stac* public Object pop() {} //i element o returns null public Object pee*() {} //i topmost# element o } t"e Stac*$ ot"er!ise returns null Stac* is not empty$ returns t"e Stac* is not empty$ returns t"e topmost# rom t"e Stac*$ ot"er!ise# t"e Stac* and remo'es it
5. !reate a ,ueue class to e0ulate "ehavior o& :ueue. !lass 0ust i0 le0ent &ollowin1 0ethodspublic class ,ueue { public 'oid put(Object element) {} //puts an element at t"e end# o t"is ,ueue public Object get() {} //i element element } public Object pee*() {} //i t"e ,ueue is not empty$ gets t"e t"e ,ueue is not empty$ gets t"e irst# irst# rom t"is ,ueue and remo'es it$ ot"er!ise returns null rom t"is ,ueue$ ot"er!ise returns null
,. !reate an A%)-ree class to e0ulate "ehavior o& Sel&2"alancin1 ;inary Search 3ree usin1 <Adelson25els8y and 7andis= al1orith0. !lass 0ust i0 le0ent &ollowin1 0ethodspublic class A%)-ree { public 'oid put(Object element) {} //puts an element# into t"e -ree$ maintaing order and "eig"t conditions public %ector to%ector() {} //returns a %ector containing# elements o } t"is -ree in t"eir searc" order
>. !reate a .as"table class to e0ulate "ehavior o& ?ashta"le with chained con&lict resolution. !lass 0ust i0 le0ent &ollowin1 0ethodspublic class .as"table { public Object put(Object *ey$ Object 'alue) {} //associates# a speci ied 'alue !it" speci ied *ey$ i t"e *ey !as pre'iously# associated !it" anot"er 'alue$ returns it$ ot"er!ise returns null public Object get(Object *ey) {} //gets a 'alue$ associated !it"# speci ied *ey$ returns null i *ey is not associated !it" any 'alue t"e# public Object remo'e(Object *ey) {} //remo'es association o speci ied *ey$ and returns pre'iously associated 'alue or null i # *ey !as not associated !it" any 'alue }
3o ic 2- Understanding Generics
Goal: ;eco0e &a0iliari@ed with /enerics eading: A1B Part 1, A2B Tasks: 1. #eneric 0ethoda. In your Array class create a 1eneric utility random CstaticD, which, &or any ty e - eEtracts ele0ent &ro0 an array. ". %ow test your 1eneric 0ethod in main 0ethod Array class F create an array o& 0ntegers eEtract rando0 ele0ent. %ote that ty e or returned &ro0 the 0ethod is 0nteger. 2. #eneric classHint: Java 1enerics are si0ilar to te0 lates in !HH, however, Java 1enerics are de&ined "y erasure, whereas !HH te0 lates are de&ined "y eE ansion. In !HH te0 lates, each instance o& a te0 late at a new ty e is co0 iled se arately. I& you use a list o& inte1ers, a list o& strin1s, and a list o& lists o& strin1, there will "e three versions o& the code. I& you use lists o& a hundred di&&erent ty es, there will "e a hundred versions o& the code F a ro"le0 8nown as code "loat. In Java, no 0atter how 0any ty es o& lists you use, there is always one version o& the code, so "loat does not occur.
a. !reate class .older that would hold the value &or any 1iven ty e -. 3he class 0ust contain a value o& ty e -G have a constructor that ta8es an o"/ect o& ty e - and a get%alue 0ethod that returns value o& ty e -. ". !reate class 1in1ax+inder that will "e a"le to &ind 0ini0u0 and 0aEi0u0 value in an array. 3his class 0ust contain an array o& .older o"/ects Cinitiali@ed throu1h constructor F use varar1s &eature hereD. !reate 0ethods min() and max() that will deter0ine 0ini0u0 and 0aEi0u0 "y co0 arin1 values o& the array ele0ents Cuse get%alue() 0ethod o& class .olderD. %ote that values contained in the array 0ust i0 le0ent ja'a2lang2&omparable inter&ace to allow this. 3. *e&actor your classes &ro0 3o ic 1 to su ort #enerics &eature.
d. (ut ut your Set to see i& it reserves the order or ele0ents Cit should notD
e. !reate another Set instance and add 1+ instances o& class .older30nteger4 to it, where .older2'alue runs &ro0 1 to 1+ &. 4nite the two Sets Cdisjunction o erationD
1. !hec8 the si@e o& the united Set and out ut its contents. Does it 0atch your eE ectationJ I& no, why notJ h. !lear contents o& the "oth Sets and reinitiali@e the0, re eatin1 ste s a e a"ove. i. /. 8. l. $odi&y .older class so that Set can correctly do lo1ical o erations *e eat ste s f and g
Iterate
In a si0ilar way 0a8e a conjunction o& your two sets Cchec8 si@e and contents a&ter thatD
0. In a si0ilar way 0a8e a difference o& your sets Cchec8 si@e and contents a&ter thatD 2. In your 0ain 0ethoda. !reate a Set instance o& ty e -reeSet Cthis is used to contain ordered set o& ele0entsD ". Add 1+ instances o& class .older30nteger4 to your Set, with .older2'alue runnin1 &ro0 + to I c. $odi&y .older class to 0a8e -reeSet wor8in1 and re eat ste b
d. Iterate throu1h your Set to "e sure ele0ents are 8e t in correct order
d. Sort your )ist in ascendin1 order and out ut the results. 3his can "e done with a sin1le call to one o& the utility 0ethods in class ja'a2util2&ollections e. Sort your )ist ele0ents in a descendin1 order and out ut the results &. !reate an array o& 1+ .older instances. !onvert this array into )ist usin1 a sin1le call to one o& utility 0ethods o& Arrays class
h. Associate 1+ instances o& class .older Cwith .older2'alue initiali@ed "y rando0 nu0"er within ran1e 122+D usin1 sKuared value o& .older2'alue as 1ap)s entry value i.
Iterate throu1h nu0"ers 1 to 2+ and chec8 eEistence o& corres ondin1 1ap entry usin1 "oth get(Object *ey) and contains5ey(Object *ey)
>