Download - Wrapper Classes,Collection Frmework,Generics
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
1/21
Wrapper Classes, Collection Framework, Generics Programs
Wrapper Classes Programes:-
package wrapperclasses;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class bte! "
#$$
$ %aut&or 'aks&man
$
$#
public static void main(String)* args+ t&rows IOException "
BufferedReader br,new BufferedReader(newInputStreamReader(Sstem.in++;
Sstem.out.println(-enter t&e bte no-+;
String s!,br.read'ine(+;
Bte b!,new Bte(s!+;
Sstem.out.println(-enter t&e bte no-+;
String s/,br.read'ine(+;
Page 1of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
2/21
Wrapper Classes, Collection Framework, Generics Programs
Bte b/,new Bte(s/+;
int n,b!.compare0o(b/+;
if (n,,1+
Sstem.out.println(-bot& btes are same-+;
else if(n21+Sstem.out.println(b!3 -is less-+;
else Sstem.out.println(b/3 -is less-+;
4
4
package wrapperclasses;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class c&aracter! "
#$$
$ %aut&or 'aks&man
$
$#
Page 2of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
3/21
Wrapper Classes, Collection Framework, Generics Programs
public static void main(String)* args+ t&rows IOException "
c&ar c&;
BufferedReader br,new BufferedReader(newInputStreamReader(Sstem.in++;
w&ile(true+"
Sstem.out.println(-enter a c&ar-+;
c&,(c&ar+br.read(+;
Sstem.out.println(-u enetered-+;
if (5&aracter.is6igit(c&++
Sstem.out.println(-a digit-+;
else if(5&aracter.is7pper5ase(c&++
Sstem.out.println(-an uppercase letter-+;
else if(5&aracter.is'ower5ase(c&++
Sstem.out.println(-an lowercase letter-+;
else if(5&aracter.isSpace5&ar(c&++
Sstem.out.println(-an spacebar c&ar-+;
else if(5&aracter.is8&itespace(c&++"
Sstem.out.println(-an w&itespace c&ar-+;
return;
4
else
Sstem.out.println(-sorr9 i dn:t know t&at enter ke-+;
br.skip(/+;
Page 3of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
4/21
Wrapper Classes, Collection Framework, Generics Programs
4
4
4
package wrapperclasses;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class convertions "
#$$
$ %aut&or 'aks&man
$
$#
public static void main(String)* args+ t&rows IOException "
BufferedReader br,new BufferedReader(new
InputStreamReader(Sstem.in++;
Sstem.out.println(-enter a integer-+;
String str,br.read'ine(+;
Page 4of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
5/21
Wrapper Classes, Collection Framework, Generics Programs
int i,Integer.parseInt(str+;
Sstem.out.println(-in decimal-3i+;
str,Integer.toBinarString(i+;
Sstem.out.println(-in binar-3str+;
str,Integer.toexString(i+;
Sstem.out.println(-in &exdecimal-3str+;
str,Integer.toOctalString(i+;
Sstem.out.println(-in octal-3str+;
4
4
packagewrapperclasses;
publicclassrandomno "
#$$
$ @author'aks&man$
$#publicstaticvoidmain(String)* args+ throwsInterruptedException "
Sstem.out.println(-random no:s b#w 1 to !1-+;while(true+"
doubled,!1$
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
6/21
Wrapper Classes, Collection Framework, Generics Programs
Collection Framework Programes:-
packagecollections;
importjava.util.$;
publicclasslinkedlist! "#$$
$ @author'aks&man$
$#
publicstaticvoidmain(String)* args+ "
'inked'ist ll,new'inked'ist (+;Sstem.out.println (-5O=0E=0S O> l! , -3ll+;
Sstem.out.println(-SI?E , -3ll.si@e (++;
ll.add(new Integer(!1++; ll.add (new Integer (/1++;
ll.add (new Integer (A1++; ll.add (new Integer (1++;
Sstem.out.println (-5O=0E=0S O> ll , -3ll+; Sstem.out.println (-SI?E , -3ll.si@e (++;
## retrieving data of ll using toCrra (+ Object obj )*,ll.toCrra (+;
ints,1; for(inti,1; i2obj.lengt&; i33+
" Integer io, (Integer+ obj )i*;
intx,io.intDalue (+; s,s3x;
4
Sstem.out.println (-S7< 7SI= toCrra (+ , -3s+;
ll.add>irst (new Integer (F++; ll.add>irst (new Integer (G++;
Sstem.out.println (-5O=0E=0S O> ll , -3ll+; Sstem.out.println (-SI?E , -3ll.si@e (++;
## retrieving data of ll using iterator (+ Iterator itr,ll.iterator (+;
ints!,1; while(itr.&as=ext (++
" Object obj!,itr.next (+;
Integer io!, (Integer+ obj!; intx!,io!.intDalue (+;
s!,s!3x!; 4
Sstem.out.println (-S7< 7SI= iterator (+ , -3s!+;
Page 6of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
7/21
Wrapper Classes, Collection Framework, Generics Programs
44
package collections;
##staticall add variables to -as&map- class obj;
##0&is program aut&orlaks&man date!1=ov/1!!
import java.util.$;
class as&map
"
#$$
$ %aut&or 'aks&man
$
$#
public static void main (String )* args+
"
as& &m , -3&m.si@e (++;
&m.put (new Integer (!1+9 new >loat(!/H.Hf++;
&m.put (new Integer (!+9 new >loat(!A.HAf++;
&m.put (new Integer (!11+9 new >loat(HH.Jf++;
Page 7of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
8/21
Wrapper Classes, Collection Framework, Generics Programs
Sstem.out.println (-5O=0E=0S O> &m , -3&m+;
Sstem.out.println (-SI?E O> &m , -3&m.si@e (++;
Set s,&m.entrSet (+;
Iterator itr,s.iterator (+;
w&ile (itr.&as=ext (++
"
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
9/21
Wrapper Classes, Collection Framework, Generics Programs
#$$
$ %aut&or 'aks&man
$
$#
public static void main(String args)*+"
as&
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
10/21
Wrapper Classes, Collection Framework, Generics Programs
##0&is program aut&orlaks&man date!1=ov/1!!
import java.util.$;
class as&set
"
#$$
$ %aut&or 'aks&man
$
$#
public static void main (String )* args+
"
as&Set &s,new as&Set (+;
Sstem.out.println (-5O=0E=0S O> &s , -3&s+;
Sstem.out.println (-SI?E O> &s , -3&s.si@e (++;
&s.add (new Integer (!++;
&s.add (new Integer (!JJ++;
&s.add (new Integer (L!++;
&s.add (new Integer (/1++;
&s.add (new Integer (/11++;
&s.add (new Integer (!++;
Sstem.out.println (-5O=0E=0S O> &s , -3&s+;
Sstem.out.println (-SI?E O> &s , -3&s.si@e (++;
Iterator itr,&s.iterator (+;
Page 10of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
11/21
Wrapper Classes, Collection Framework, Generics Programs
w&ile (itr.&as=ext (++
"
Object obj,itr.next (+;
Sstem.out.println (obj+;
4
4
4
package collections;
##staticall add variables to -as&set- class obj;
##0&is program aut&orlaks&man date!1=ov/1!!
import java.util.Iterator;
import java.util.0reeSet;
public class 0set "
#$$
$ %aut&or 'aks&man
$
$#
public static void main(String)* args+ "
## 0O6O CutoLgenerated met&od stub
0reeSet ts,new 0reeSet (+;
Page 11of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
12/21
Wrapper Classes, Collection Framework, Generics Programs
Sstem.out.println (-5O=0E=0S O> ts , -3ts+;
Sstem.out.println (-SI?E O> ts , -3ts.si@e (++;
ts.add (new Integer (!++;
ts.add (new Integer (!JJ++;
ts.add (new Integer (L!++;
ts.add (new Integer (/1++;
ts.add (new Integer (/11++;
ts.add (new Integer (!++;
Sstem.out.println (-5O=0E=0S O> ts , -3ts+;
Sstem.out.println (-SI?E O> ts , -3ts.si@e (++;
Iterator itr,ts.iterator (+;
w&ile (itr.&as=ext (++
"
Object obj,itr.next (+;
Sstem.out.println (obj+;
4
4
4
package collections;
Page 12of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
13/21
Wrapper Classes, Collection Framework, Generics Programs
##staticall add variables to -0reemap- class obj;
##0&is program aut&orlaks&man date!1=ov/1!!
import java.util.$;
class 0reemap
"
#$$
$ %aut&or 'aks&man
$
$#
public static void main (String )* args+
"
0ree tm , -3tm.si@e (++;
tm.put (new Integer (!1+9 new >loat(!/H.Hf++;
tm.put (new Integer (!+9 new >loat(!A.HAf++;
tm.put (new Integer (!11+9 new >loat(HH.Jf++;
Sstem.out.println (-5O=0E=0S O> tm , -3tm+;
Sstem.out.println (-SI?E O> tm , -3tm.si@e (++;
Set s,tm.entrSet (+;
Page 13of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
14/21
Wrapper Classes, Collection Framework, Generics Programs
Iterator itr,s.iterator (+;
w&ile (itr.&as=ext (++
"
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
15/21
Wrapper Classes, Collection Framework, Generics Programs
public static void main (String )* args+
"
Dector v,new Dector (+;
v.addElement (new Integer (!1++;
v.addElement (new >loat (!11.Af++;
v.addElement (new Boolean (true++;
v.addElement (-'aks&man-+;
Sstem.out.println (-SI?E , -3v.si@e (++;
Sstem.out.println (-5O=0E=0S , -3v+;
Enumeration en,v.elements(+;
w&ile (en.&as
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
16/21
Wrapper Classes, Collection Framework, Generics Programs
##staticall add variables to -&as&table- class obj;
##0&is program aut&orlaks&man date!1=ov/1!!
import java.util.$;
class &as&table
"
#$$
$ %aut&or 'aks&man
$
$#
public static void main (String )* args+
"
as&table &t,new as&table (+;
&t.put (-C-9-d-+;
&t.put (-Orissa-9-B&uvanes&war-+;
&t.put (-Karnatake-9-Bng-+;
&t.put (-0=-9-5&ennai-+;
&t.put (-Bi&ar-9-atna-+;
Sstem.out.println (&t+;
Page 16of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
17/21
Wrapper Classes, Collection Framework, Generics Programs
Enumeration en,&t.kes (+;
w&ile (en.&as
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
18/21
Wrapper Classes, Collection Framework, Generics Programs
4
4
4
4
packagecollections;
importjava.util.$;publicclassDector6emo "
#$$
$ @author'aks&man$
$#publicstaticvoidmain(String args)*+ "
Dector2StringM v!,newDector2StringM(+;
v!.add(-raju-+;v!.add(-ravi-+;v!.add(-sita-+;
v!.add(-geet&a-+;Sstem.out.println(-si@e is -3v!.si@e(++;
#$for(int i,1;i2v!.si@e(+;i33+"##Sstem.out.println(-vector values are -3v!+;
String s,v!.elementCt(i+;4$#
##en&anced for loop
for(String sv!+"Sstem.out.println(s+;
4
44
Generics Programes:-
packagegenerics;
#$$$ @author'aks&man
$$#
classgen20M "
0 obj;
gen(0 obj+"this.obj,obj;
4
0 getobj(+"
returnobj;4
Page 18of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
19/21
Wrapper Classes, Collection Framework, Generics Programs
4classgen!"
publicstaticvoidmain(String)* args+ "
Integer i,!/;##same as Integer i,new Integer(!/+;
gen2IntegerM obj,newgen2IntegerM(i+; Sstem.out.println(-u stored -3obj.getobj(++;
>loat f,!/.!/Af;##same as >loat i,new >loat(!/.!/Af+;
gen2>loatM obj!,newgen2>loatM(f+; Sstem.out.println(-u stored -3obj!.getobj(++;
gen2StringM obj/,newgen2StringM(-ai
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
20/21
Wrapper Classes, Collection Framework, Generics Programs
packagegenerics;#$$
$ @author'aks&man$
$#
interface>ruit20M "voidtell0aste(0 fruit+;
4
classCn>ruit20M implements>ruit20M"
publicvoidtell0aste(0 fruit+"
String fruitname,fruit.get5lass(+.get=ame(+;
if(fruitname.eNuals(-Banana-++
Sstem.out.println(-Banana is sweet-+;
elseif(fruitname.eNuals(-orange-++;Sstem.out.println(-orange is sour-+;
4 4
classBanana"
4classOrange
"
4
classen"publicstaticvoidmain(String args)*+"
Banana b,newBanana(+;Cn>ruit2BananaM fruit!,newCn>ruit2BananaM(+;
fruit!.tell0aste(b+;
Orange o,newOrange(+;
Cn>ruit2OrangeM fruit/,newCn>ruit2OrangeM(+;fruit/.tell0aste(o+;
44
packagegenerics;
#$$$ @author'aks&man
$$#
importjava.util.as&table;
Page 20of 21
-
7/26/2019 Wrapper Classes,Collection Frmework,Generics
21/21
Wrapper Classes, Collection Framework, Generics Programs
publicclasst! "
#$$$ @paramargs
$#publicstaticvoidmain(String)* args+ "
## T!CutoLgenerated met&od stub
as&table &t,newas&table(+;## &ere not taken generic tpe
&t.put(-laxman-9 new Integer(1++;##auto boxing is not done &ere&t.put(-sac&in-9 new Integer(F1++;
&t.put(-d&oni-9 new Integer(G1++;
String s,-sac&in-; Integer score,(Integer+&t.get(s+;##&ere tpe casing is done
Sstem.out.println(-score,-3score+;
4
4
packagegenerics;
#$$$ @author'aks&man
$$#
importjava.util.as&table;
publicclasst/ "
publicstaticvoidmain(String)* args+ "## T!CutoLgenerated met&od stub
as&table2String9IntegerM &t,newas&table2String9IntegerM(+;## &ere
taken generic tpe
&t.put(-laks&man-9 1+;##auto boxing is done &ere&t.put(-sac&in-9 F1+;
&t.put(-d&oni-9 G1+;
String s,-sac&in-;
Integer score,&t.get(s+;##no tpe casing &ere
Sstem.out.println(-score,-3score+;4
4
Page 21of 21