Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

r,alP is iJJustrated beJow

J ," " Jass ddplus


f't,fiC C
)
I . tatic void main(String args[J
f'i,JJC S

I .
• t jJ; A m1"t'' ' 11 B·ikash11,11Chand11,"Dili "}
11

JJI [ J[ 1 = (

p '
5ttit18 rn II "F
f II11E_s.h a,; . ,
" arJ ~" , "Girish",11Harish"}
1Lishu"} }·
{ lsh an 'Ja sh 1 '"Kamesh",1
11 M . '
,. _
0 ut. pri ntl n( Th e ele me nts of the 3* 4 atn x are:");
sys.. r 111 .
,.1· ll·j<J:1++)
fon 1:::v '
l . )
for(j::::Oj<4 ;J++

. t(m["JU
l system.out.prin 1
J +II II) ;

~ysrem.out .println();

3 *4 Ma tri x are
output : The elements of the
fbe Amit Bikash Chand Dilip
Esha Farjan Girish Harish
Ishan Jashi Kamesh Lishu

Bv Using BlueJ System the tim e of executing the progra m. It may


s are enter ed at
In.this method, rhe data value ile input
ing type. Yo u ne ed no t use any Java Library package wh
It an integer or a Str re the
reove r, the bu ffer is also automatically activated to sto
mrough Bluel system. Mo
data items. the time of
type: Th e da ta va lue s are entered from the console at
(a) For integer stem.
vis ed to giv e a rel evan t co mment while using Bluel Sy
execution. However, it is ad
int m[ ][] )
Syntax: public static void main(
sy ste m wi ll cre ate un fixed number of rows and columns
F.mpty brackets show that the which is
form. Ea ch da ta va lue is separated with a comma,
ro store data in matrix Do uble Dimensional Array.
rep res en t a row in the
~losed within curly braces {} to nsional
to ac ce pt da ta in Bl ue l Sy stem by using Double Dime
'~ -~ Ie te program
·- , 11 illustrated below:

Arrays (Si ngl e Dllnens;onal


and Double rnmensional) I
.>
205
• • t:ntl·r ~:h,.r,1.:ra, .,nll d1~·l <)l in ~ir, 1
, 1 .•
SltP .,1 · · ., .,. c .. 11 ·•'•t•· t
,1., j '\ f, .

•· -...__ _
..... , c~. ,:. ,. .
I:! . ·•r •
C j I
k ~ II ~.

r, 'i r •

(cl /(,, .,rrini: 1_, 11, : In thi, ") 't.:m. 1h~·


tinu: of c,ccution .
A rl'll.'.\Jt.l commi:111 \hould he 11,c1:11 <•i,,'<l •., h'.:
s,~1cm.
, It m:.ihc, 1!1L· profr:,mmcr mC'rc 1nl,·n::~,! .. :·
S~nta,: ru~l.. ,1..:1, ,, ... ! 1:·.
It mean, th;it thi: ,y,tcm <rl.'..Jc, u::!1\~,J !,\_,,::.· .,
r11lu111n,) to qor c Stnnf' · L;dl J:.:.1 1 .. !t,: 1, c:,·l,•,,"' \ l 1\ ..I :•
\\tth a uHnrn:i. v.hid1 1~ l111Jii,1· c-r.,i .,,J 1,::!1 1:1 '"'\•·
.. '',.
:.i l)ouhk D1mm,ic1nal An.1:
A c11111plctt: p1,1fr;1111 11,ing tit: B!t:.:J \, ,·, ;·. :·: I'
i, illi1,11 :itcd helow:
II I\ fllll)'l;tlll to 11lu,11ute ll lud ~~'t,1:1 Ill f

public l l:1,, S:.1111:I.

puhl,c ,1.111c 1,,1d 1n.:n:r\1r111_'.' 111l l1 ]1

int 1.1.
S) ,(,'Ill 11lll 1111111l111 l 1,.• I. :I' '
I I

t,ll(I -O ,t,.\11 t I

\~ ~lt'lll , 1u1 f'l 11.tl!l\ 1;

C~mpilalion and E\t'l'Ulion


Sttp I •• ('.0111p1k ;111J d >s. ll .
\ ~ I~ l'lllp ,1111
click on the icon Sa mp le and select vo. .
t id mam(String( ](] m) .
. __ _ _

in Me th0 .
' r names (string) and click Ok d Ca ll window. Output of th!!
, eote J1l is thus obtained.
f' J, pfOgra

rix are :
The nam es in the mat
Gop al Mohan Kai lash
Bho la Lok esh Jite ndr a

Cancel

are entered as:


. us in the Method Call Window "
,n,e 5tr1n~ " "K ai·1 ash" } ,{"Bh o1a , "Lokesh", "Jitendra"}}
I" "Mohan , - --------- --
,roopa, - - - - - - - - -
J''

utStreamReader Class
/
gram. Th~y
·11g Inpalso, the data are to be entered during execution of the pro
Bf ~SI ethod
-~JS rf)
/o u• of any type. con~ok at
rype: Th e da ta values are entered one by one from the
maY i,e mr . er .. ge by u~ing the
) for egcution . However, It IS ne cessary to give a relevant messa
(a_ of exe
1Jie ume nt to enable the Input editor. /
t stateme wh ich me ans it creates 12 cells in the m~mory
/.llpu •. = new int [3] [4] ;
t m[ ][]
·
syntaX•din4 columns) to store 12 rnteger type numbers. ing
le Dimensional Array by w,
13;;;;lete
0
program to
accept the numbers in Doub
fuputStreamClass is illustrated
as:
. . *·
import 1ava.10. ,
public class Sample
I
public static void main(String
args []) throws IOException

l treamReader(System.in);
mR eader rea d = ne w Inp utS
InputStrea
redReader(read);
BufferedReader in = new Buffe
mt ij;
and Double Dimensional) I
209
Arr ays (Single Dimensional
-- --- - -- ----- __
.,

~~~:=:: :
} ~= .,. .,. --= -~-.,.,.,,,~-,;...._.,,..,....,,.,,.~-
T .. . •

Step 3 •• r n am es (s tr in g) an U <:\kf-. ()\_ ·


E n te
p ro g ra m is th u s a b ta\.neU.
..
i/ l fl I/. , ., , 11111 · l,y ,,,,, · 1.
/ ,u·11 "'
1111111 1I
II
,,,, '"' ,,,,,,111( '' / ·llll .,
. 1 .111 p11/ I
.,
.
\ ' '
I I, , I , i I I I
II,, (I 1

,,,,11 (/./· l.i I I I


·/111 1i11.11·, 11 // ,i11d )) ;
/1Jll ')' l 'I / '• " ·"
111/1/ II I

") ·,
. · 1/ll' n- II -~ :ire:
/ . "'/'Ill' r lt-11 1('11 1\ Ill
S.I ~/4'/II 1111 / .J>I 111 /lll(

/itl( / - 11;1 ,.l; il +)

I
Ii,, (i :.O:j-.J.i ++)

. .//J'/+" ") '·


•, ll' l/1 ./lfll ./Jf llll ( III/ I
.'i ) ·
I
, 1ri11tl11 () :
Sys ll'lll-' 1111·

. . and 1-:xcc ution gram.


Comp ion . . . d clo se the pro
ilat _.! .
Stt·p I : Comp1k an

''
.. jtl . . Ill/ ,
.,_ • ,... . IY~ I Jllt'•W • •Jt,,IJ

;-:-;~•;;,:=.-:•. - •. ,r••...,.,-"'••• .. '''


1 1

... ,.,,
.;.: ~\ ·;~..·~·,!~.': ·._,.,_ ,., '11'1 .,,

IH tt•J , J•l1,• •I

'•,1! ,,., .. , .... ,.., .. ,.,,,


• •• .11."""''''
I

:, • • - ,,•r••
.,._ , J' I, ., .. • l- •' I 111 I l l ~ • ll llt' , •,,
ZE IS •

., ... .... ,., ,11 .,1 ,11, •• .,,

'

args).
-
ht click on the icon Sam ple and select void main(S1ri11g[}
Sttp 2 : Rig
Call Window.
Sttp J : Click Ok in the Method

~ 210 I Understandin c eJ-X


' ' I omputer Applications with Blu
\
l

.
r<-t,hc static void tnain(String arg, []) th

I
lnputStrea1nReader react ===new In S row, IOE.xception

BufferedReader in == new Bufferectn


Put trearnReade
d r (S Y~tern.in)~
int ij; ,
1'-ea er(read)·
charm[ ]( ]=new char[3Jl4L

System.out.println( " Enter the characters all at a time :"">:


for(i=O;i<3;i++)
{
for(j=Oj<4;j++)
{
m[i][j]=(char)(in.read()) ~
}
}
S.)'Slem.out.println( "The characters in the cells are :" L
a(i=O;i<3;i++)
l
D"mensiona\) \ 2\ \ ~
. le Dimensional an d Double I
Arrays (Sing
;
._.,.,.....~ •-,,·++)
l·,,1\1-' \
.,.. " ")·•
I rinf(nililLi
. :-I.,,1 (ltll-P
,,·
.. ~

I rinrln( );
s,. :,1(111.l'ur.r
~

I ... ---· -· -- · ·· -- - ------------- ----------


rers all al a time
--.........
..........., ,
- -- t: Enrcr th·c__charac "
5a111plt Jnpu ailcJ~fihrJkl d
b C
Output : a h
e f g
k I
J
--- ----
' ____.. -- --- ------ -- - - trin"s should be entered one by one f
. ' r,
(c) frr 5rr111.~ : / ,c: The s o rorn the co
f ~u11on. b .
rhe rime Cl e1. • ro give a relevant message y usino the I 050!e
· ·
Ho\\ e,er. al ts ncrcssJI)
.- 0 nput Staten,
ln ill~~ ~
enatile P . I I[ I= nl?w Srring!Jlf4];
S taX' Sinn•· Ill S .
)D · • :- using the Input talement rn arrays is illustrat
A complere pn,grant · ed beJo\i•:
impon ja,·a.io. •;
public dass ddname

;ublic static \'oid main(String args []) throws IOException


I
lnputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int ij;
String m[ J[ )=new String[3)[4);
for(i=O;i<J;i++)
I
for(j=O:j<4;j++)
I
System.out.println("Enter your names one by one");
m{i}Li)=(in.readline() );

}
Systcm.out.println("The names in the matrix arc: ");
for(i=O;i<J;i++)
(
for(j=0~<4;j++)
(
Systcm.out.print(m[i]ij]+" ");
4'..,
~ u......._..,_
- - - . --11'111
r---., JI._.,__.
..... ......uons
.
wtth BlueJ-X
I our.prinrln();
St;tefll-
7
nnrr Class
~fflf: sea, cnls in .a Double
. . .
.
Dimensional Array use the 1"oil owing 'itep.;;:
,· II" flC e en1

e..I~11111 I SCJflflL:·r class 1s a member of Java.util package. Hence • linpOrt h
l e packao-
fl' J : • class declaration as shown below: -=e
~1eP ticf()fl,; .
· . ()rl java.uul. *;
unP an objecl of scanner class as:
. create ' .
·ieP Z • r in == new Scanner(System.m);
~ s~anne . . .
· d
a Double D1mens10nal Array of the specific data type ' statinoO requ ire
crea te
srcP 3 : number of rows and columns.
for n:ample,
• for int data type: .
int arr[ ][ J == new mt[2][3];
• for character data type:
char arr[ ][ J == new char[2][3];
• for String data type:
string arr[ ][ J == new String[2][3];
. use two loops such that the outer loop ranges for the number of fO\\:;
4. Now,
Step • whereas, the inner loop _ranges for the ~umber of columns of the arr:iy. u~t!
outer and inner loop vanables representmg row and column subscripts of th~
array respectively. The snippet is as shown below:
• for int data type:
for(i:::O;i<2;i++)
(
for(j:::0;j<3;j++) To store the elements in double
dimensional array row wise, the
arr[iJUJ=in.nextint( ); input stater :mt 1s:
J arr[i] [JI = in.nextlnt( );
To store the elements column
• For character data type:
wise, the input statement can be
for(i::O;i<2;i++) used as:
{ arrU] (1) = in.nextlnt( );

for(j::O;j<3;j++)
arr[i]U]=in.next( ).charAt(O);
}
•For String data type:
for(i==O;i<2;i++)
(
,.·- •-•ti<J;j++)
1
1Ul=in.next( );
1
.
/)iJ1l'J'1.' . l : I I . . f) ou hl ~ D im en '11 111-
'J'Ih_• . • • ,,,.. \\ l ·t·n -~ In ,....fr Hm11: 11' io 1• ·il .\ rr n .r an d
..
il/ \
s b · bl e Dimen,,·onc1J A tr
. dilfrr en c~ 1 Si '" n sion3J A m 1y and D ou
rr1, ,
gI\' cn
> helow · ' e Ween np. t: 1men !
.

1. JI is rcprcscn
rcJ along X-ax;~ anu r..
J. Jt is· ,
h' llf l' ' ln ll 'l
f :ilong X-:ixis. ~-
"'
",_. 1'hc variables with• .,<1me nJ •11 C h-~vt

npls reprc~ Cntin . 1•
· ve single typcs ol sub-;c
-"l . ·, hl· ,.•1r ·i;1 hi cs w11 h sa111c name ha
n
mn number 1: ,.

,u h~ni pl. number and colu


t .. _
double sub\ crip rcc ' dO · L
iprcJ variable.
J. It is called as ,.,
.!,!lc suhscr
3. II is also c11lcJ ~in

SOLVED PROBLEMS ON S IN G L E D
IM E N S IO N A

ff er en t nu m be rs
L ARRAYS

in a Sin gl e Dime
(SDA)

.
.
to accept 20 di m be rs w hi ch ar
ior,~
e di v,· ~bslq
Prog. 1 Write a pr og ra m o f al l th e nu SJ .
isplay the sum !
Array (SDA). D
,
ei th er 3 or 5.
3 or 5
m be rs w hi ch are di vi si bl e by
sum o f nu
// To display rhe
import java.util
. *;
public class Sum
/ [])
main(String args
public static void
(
Scanner(Syst em.in);
Scanner in=new
int i,s=O;
0];
int mf]=new int[2
for(i=0;i<20;i++)
{
the cell :");
1.out.p ri nt("E nt er the number in
Sy~1e~
mf1J=rn.nextlnt();
}
for(i=0;i<20;i++)
(
m[i]%5==0)
if(m[i]%3==0 II
s=s+m[i];
s);
.out.printJn("The s nu m be rs di vi si ble by 3 or 5:"+
System um o f
J

ing c eJ -X
~ 214 I Understand omputer Applica
tions With B lu
'~
- th e .,~
&nta r
Snt•rth• n r i n t he
Enter th e UIQb•.r J.. n the c•.1. 1 : 33
n Wllbez; ce.1. 1
ltnte r the n'-'-~ - in the c ' : 44
&nter t he
- -...r
llUJnb
i
n
•...,1
t h• <::e1
l
: 2
er i n t 1 : 55
Enter the n'lDlb h e ce
11 : 31
•r ~n
Enter t h e n ~ the c •l l
r in th : g1
Enter the llUIQb • ce11
er in th : 93
Enter th e n'lDlb e ce11 .
er i n th .77
Enter the nUIQb e ce1 1 .
er in th . 53
The • wn o r n\llllbe r . e ce11 : 78
• di"i•ibl
e by 3
Or 5 : 1 79

prol· 2 Write a program to accept .


10
Array (SDA ). Di splay the o different nurnbers i .
elements. e reatest anct the srn all e~ a Sin gle Dirnen .
st nurnb ~ ''lln.11
Sa mple Input: [IC er~ of the arr~\
SE "'()()7 ,
n[OJ n[IJ n[2J n[JJ ' · ~fl\7\
n[4] n[5] n(6] 17)
43 45 92 87 64 n n\8\
76 81 11\'1\
Sample Output: 65 12
3\
Greatest element: 92
SmaJiest element: 12

11 To display the greatest a nd the smallest num bers in SDA


impart java.util. *;
public class Max_Min
(
public static void main(String args[])
(
Scanner in=new Scanner(System.i n);
int i,min,max;
int m[J=new intf 10];
for(i=O;i<J O;i++)
(
System.out.print("Enter the no. m the cell :");
mfiJ=in.nextlnt();
l
l!laX=mfOJ;min=m[OJ;
for(~ JO;i++)
l
~

Arrays (Single Dimensional and Dou


ble Dimensional)\ 215 ~
/,
rni11=11,1 i 1:

=" t 111:rx):
1crn.out.p ri111l11(" Tlll' grc:11c:-.1 nf rite arr:1y ck111cn,..,
Sy~ J:
nf llil ' :11ray ck111c11h =.:" +111in
-.1cm.,)lll.pri111l11(''Tlll' s111:ilh..·:-.t
Sy:
}

marl,.-. in l'hy:-.k-.,· Ch L·, 11 " 1I\ "J


Pro~ . 3 \Vritc a pro!.!r.tm 10 an:cpr rite1 · a ·s·111 ~ 1l' , )1111L'1l\i11n.i1· ···· '-:
~ mk nls ,lf a l: a:-.,
111
Sl.'l'llrcJ hy -W :-.t .\ ,r ·· i
i:_.
. 11 o,H.n~ :
. play till' In
and dis
l!! ~0'.r a11J .,b o\ c in .1~~ I::"" , .:-•II-:
L'·•
1• Number of stll<..knrs ~ccuril !~rl'~ -:l:
11. Nu mb er of ~lu tkn h
~ccuring 3..i r:i :.111d hd ow in :1_!,

Jl·nrs wir h a a~g rcf :tll ' marf.-


~
II To dis pla y the nu mh a of ~ru
import ja\·a.uril. *:
class An~ragc

pu blic static ,·oi J m:.1in()

IJ.in ):
Scanner in = new Sc.:anncr(S) ,ll'I
int i.j.c.: I =0 .c2 =0 :
inr ph) I l = new inrf-rnl :
int d1l'mf I = new in1j-WJ:
int ma th., ! j = nl'w intl-10] ;
for ( i=O:i< -+0:i ++ )

nm:d 111 Ph~ , 1~·, "):


S) ... 1crn .out.pri111l11( "J\f ;u i...., :-c
ph )lil = in .11l'xlln1():
·tm ·,I 111 C h~·11 1i,1 1) "):
Sy ,ll' rn .ou1.prin1ln ('',"'.l;11b '>L'L
chem Ii I = in nn1l111 ( ):
.ll " " 'l'- tm ·d i 11 .\ l .111J
.., " ):
sy~ ll' /ll.( llll. pt i111l11( "~1
m:11h:-lil = in .11c,1ln1():

for( i=O:i< -W :i+ +)

3 () >= ~())
if((phyfiJ+dlL'lllliJ+111 ;11h,j ij)/
cl+ + :
() <= 3 ➔ )
if((ph)lil+d1l·rnliJ+m.11h,/iP/3
c.::!++ :

of ,1uJc11i,; ~d li, 1,, M) ', arid Jh1 >\l. '', r' '+.._· 11.
Sy))fl'lll .ou1 .pri111ln( ''~u111ll\."r
of ,111J~ 11 1-. gl' llin : J4' ; .in,J hch w. \r" ♦ t· 2 i;
S) ~1crn.ou1.pri111ln( ''N um ha ,
I

<.
~

l'J •-·X
216 1 Und<'r,1.ind111J: Com
J'ul n AJlp lu atton.!I "Ith Ulu
l
WrilC a p ro g ra111 111 a vn· ,,1 I !J .
. );\ ) . N. o w, l' lll e r ,. , 11111
j\rraY (SI d11f t· 11· I
1il wr II. 11111111 11 · 1 ... 111"
. ~, ,, ., I,· I) 11
•11uc
c~cnl or nol rn lhl" l i~I
''1 ,.,,:,y (·I~·11 w 1,1..,
. .i 11d '-l':, 11 Ii ' 11rw1, .1r :,il
1,y w. lll'll wr Iii•·- 1,r111,l 11. r 1
rr
(CC "1 • ' 1t II'. I ,11- w ·,r '
11 -.. 111•• '
, h,plc
11
npul: ·· ••·,n cl ,
111 I
5
nl I l 111 21 nl J J 11 l"i l
nlOI 1117 1
56 62 77 <,7
74 ~rJ

sample Ourpul:
Enter a number lo be searched : 4 2
The number is present

earc a number in SDA by using Linear ·Search tc c·hn1quc


.
11
i/fJ ,dava.ull·1 .*''
5

101f'. lass search


pti~h' C
I . void rnain(String args[])
l
·c stauc
rub I
I jn::::new Scanner(System.in);
5(aoner
inl i.k:::0,ns;
inl rn[J::::new int[ IO J;
forti::O;i< IO;i++)
I
system.out.print("Enter the number in the cell :");
m[i]=in.nextlntO;
J
System.out.print("Enter the number to be searched :");
ns=in.nextlnt();
for(i::O;i<l O;i++)
I
if(m(iJ=ns)
k=l;
l
if(k=:J)
elseSystem.out•println("The number 1s
. present");

s .
I ntln("The number is not present");
c, \ in ·1 s·
.:Cl't'pl 20
:
inll'r!LT fll //l lh •. ing/i.; /)·111,cn,
Wrirc ,J pr, 1/ f.l lll 111
Prog. 7 1/J,• /11/hn,111i,! :
,d d,,pl.i) ' •r,~
.-\ 1r.1) I ·111d .11
I ~.
11I t'f f/ l lll lll il ll'
I. .\ 1111 1/i('f
1111111l11·1\
11. ,\ u, ul ,l· r 1,/ 11dd
JJI. ,\1111ilw1 ,,1 11111/1,pln 11/ -1.
11111/tip ln 111 •I
. odd .111d
1111111k1 11/ nl 'II
:. l11 d, , pl.11
1 1 il 1.11.1111d
'
lll lf'

l l.1,, ,\ i1 1i lk f

lll .i11 );
:: Ill'\\ Sr;11111t·1(Sy:-.ll'
Sl ,ll lll lT i11
O;
i111 i._j.l'/.:O.l·~=-11.c.L
1/ IO/:
inf .,/ I = 1JC11 111
2(1 lll lll llk 'f. ~ ") ;
s~ ,1c111.n111.1•11111 /11( T n ll' f
/111 ( i::: 0. i<:O.i
++ )
11():
.1/1/ :::: i11 .11n1/1
hl l( i=0.i<20.i++)

1r'(.1/Wr2 == OJ
d + +:
i((a[i]'r2 != 0)
c2++:
if(:i/i]<:"c4 == 0)
d++:
1);
1ln("N um ber of even numbers\t"+c
S) ~rem .o u1.p rin
bers\t "+c2);
em .o ur .prin rln ("N umber of odd num
Sy~r 4\t"+c3);
.o ur.p rin1l n ("N umber of multiples of
Sys1em
}
mensional
a set of IO integers in a Single Di
program to accept nding order by us
ing the 'Bubble
Prog. 8 Write a bers in as ce
Array (SDA). Sort
the nu m
array. [ICSE 2005 , 2013/
isplay the so rted
Sort' technique. D
technique
th e nu m bers by using Bubble Sort
II To arrange
import java.util. *;
ding
public class Ascen
t
main(String args[])
public static void
t
J- X
. tions with Bl ue
• -fll'W .\l';rllllt· r( Sv,1v 111 , ;, I >·,
,•( 111- •
/j~·)l1~ .
·/11 iJ-'• •\\' inf( I Uf ;
I W"'''
I JII · ., f{l,i++)
I/I ,..jl;I._

I•
·,rt,,..
I , ,111 .p1i1ll ( '' / · 11k1 1111111'11· 1, i11 1111.: l"l'/1 :") ,·
. ,,,.,11,1
:,,11 ·1; 111 flL'\ f/111 ( ),
fll I

I . o i ,1 l
f,'n
•' ,...
f,;.. •

,.,,,t . I,,..
. ('1-i):jH )
.- (

'
I_ tl:;,1111.itl/)
,u,111
I
1:::J11UI:
flllil:::1111.i+ I 1:
·+IJ:::I:
,11 L'

I prinrln(''Thc numhcrs arranged in ascendin g order are :")·


'' ~,ern.t1u . . I ,
:· ·:::tl;i<JO;i++)
,,,ni · In(mf 1' I)·,
· . .., our.pnnl
5~ ~,e, ...

In a class. rhc Mathematics teacher gave an assignm ent to her students


to find rhe area of a scalene triangle. She writes a program taking the
Jength of the sides as input and calculates its area. Further, she enters the
answers obtained by all 40 students in a Single Dimensional Array. The
program compares the area calculated by the teacher and the answer~
stored in the array to display the number of ~tudents who have given the
correct answer. Write the program in Java what the teacher would write
,a solve the purpose .

Todld the answer with Teacher's answer

new Scanner(System.in);
.area;

Arrays (Single Dimensional and Double Dimensional) 1221 >


J
int i: =O.i;
l l: ,l .... 11
thl ll 11 • , II :· lll' W 1h111hkl •III\.
Sy~ll'lll.1111\.{)I .-Ill 1l 1l " h1 h' I thl ' '. llk 'I 111 1111 ,,
I · · 111:11q•k 1:
'I ::; ill .lll'\11 ), 111hk( 1;
s~ = in .n~,1D,111hkl );
sJ = in .1w\tD,111hk( L
Sr tl. 'll \.lllll.pii111h1l"
F111,·1 ''" ' all ',\Vl' I'.,
11I ,\(I ,,1111k1il ·,'' ,.
fN(i =lU <~l);i t +)
ans\ i\=in .n~xl l), iuhkl );
il\(sl-ts~) >s3 & & (s2•
s3) >s l ,"'18:. h i l \ .\) ,•,'n

s=(s \ +s2+s3 )/2 .0;


arl.'a =Math .sqrt(s*(s-s
I)· ' (s-s'2.)~' (s-sJ ));
for(i=O;i<40 ;i++)
if(ans\i\ == area)
c++;
System .out.print\n("Num
hcr of stmknts who 1•,a
w t 01 1n I :1 11·,•11,
) 1 i" q,
1
else

System.out.print\n("Tria
nglc fon11alion is 1101 pm
) ,!>i hl,·'' J;

Prog. 10 Write a program to ac


cept a !,c\ of 20 inlq.!,t:
Array. Using menu Jrivc l'.'-t in a Si11,.k 1)1111111 ,1,, I
n approach display the 1
choice: l'nllo wi111• , a\ p1r 11
11
i. Al\ the perfect numb
ers storeJ in the ;may
ii. All the buzz numb .
ers stored in the array
(A number whose sum .
of factors (including I
itselt) is the same is and i;xrl11dinr till: 1111111!1 •
said to be a pcrfc<.:t 11u
divisible by 7 or has the 111hcr. A m1111h1 r 1li.1t 1
la'.'-tt digit as 7 is '.'laid
to ht: a 111111. 11111 11ltrr I
// To display perfect nu
mbers and bun. numbers
import java.util. *~
class Number

public static void main(


)
\
Scanner in = new Scan
ner(Systcm.in);
int al] = new int[20};
(
,J): · 1. +)
~ .,,,.,·J·"al1 :J+
f11rW •
I !ilrH :::::: O)
jfl.l I •

s::: s + J:

\(S :::::: a[ ii)


1
systeJll,out.println(ali]);

I
break;
,ase 2:
for(i::O;i<lO;i++)

I
if(a[i]o/o 7 == 0 II a[i1% l O == 7)
System.out.pri ntln( a[i 1);

break;
default:
System.out.println(" Invalid choice");

l
l

Arrays (Single Dimensional and Double Dimensional) I 223 }


. . prl 11r:1111 10 :1cn•pl roll numb. . -:,
1
JI ' · h)' 40 ,111Jcn1S of"a' -i'a,. l d the
11·n1< ',.,,1P1'1ie311""'
,..,. (llftlpUs' ,rt th<" ,n:rrk< (lhlaincJ in des "c ''" ir, a s~l1~tk
.\rfJ'°· l
,h'
•· 1· . ccmhno , in 0 1\
· O'· ,unth<' anJ. niar•' o each ,1udci11 .,n So"Med OrdCr. 11
~ u ~,,,~
,• ",,
,.,,
11, . in Jc.:cnJ,ng orJcr "'de •11) \
. I , iii' n1ilf"' r. . i ·1
,rr, J,>r •> . •. '.'
·;1,·;1.ulll, •
jn1r,r1 J
c1.1~' ~umtx·r
I . ·1•tic niiJ rnainO
pU~hC ~ ..
I . _new scanner(System.in):
s,~ner 1n -
jpl ij.l:
int rollll :: new int[40];
int marksll :: new int[40]:
,.m.ou1.prindn(En,er roll nu01ber and marks");
51
for( i:::O:i<40:i++)
I
rolllil :: in.nextlnt();
marks[il :: in.nex!lnt():

I
for(i:::O;i<39:i++)
I
for(j:::0:j<(39-i):j++)
I
if(marksUl<marksu+ I))
{
l=rollU);
rollU]=rollLi+ I];
roll U+ I]=t;
l=marksU);
marksUJ=marksLi+ I].
marksLi+ I]=t· '
} '
}
I
Systcm.outprintln("
S...;,<41)',i+t) Roll No·\t"+"Marks");
fol(r-"··

I
,__out.println(roll(i]+
. "\t" +marks[i]);

Wilb Blue.J-x
., pf10GRAMS ON DOUBLE DIMENSIONAL. ARRAYS

~"'"p I"',~r.,m in_J•''." '" ,t:•r, th,· 'I""''"''' in " l '4 nia_tri, in a O,iuhlc
£

t
.J
r
j
\\rilL ,. ,
•11'1' 11
ni1'''
' . ,.,y I
h.
J1'r I. 1111'111 :
,;.,1111' L 1,1·1~ ,,r
• , 1111111
l
, 1 ,\n.1) . I 111 J ,h~ :-um ol a\\ thi.: m11\llwr~ ol the matnx and
1

'
.
, 11111 11~111 b
.
,Ill rnput ~l.lll' ll\l'nl

1h1· 111 .111 i, arl.'


.

., , I I.\ 1-1
111~
I, ~I ~I -' ·'
1J '\{) ~I)
I II ·
(l ,. ()utl'\11 :
S·11111'\:lll ,1f thl.' l.'kllll.'l\tS in 1h1' matrix is 340
1111.' ~
ill ,,f 1h1· matrix ckml.'nts
111.' ~11
1,
,1,1~ ♦ •
\',. Ji"' 1,,.1- 111\ • •
-~J· . J,l~ll 111
·.' ' ~1,1•'
, .-:, . n1;1i11(St1in~ ar~s \ I)
.. ,.,,,,1
. ,pll•

. •\\' S1·a11111·r(Systl.'m.in);
! ·r io::::lll •
. ;1\11'
:-- . .,:::{l:
J::::oi:w int\ 31 I41;
.,:: 1•.1-~·
ll nrintln(Entl.'r the numbers of the matrix")·
:ii 111 \
'. ~~-'. '
~) -c::1fr:J:1++)
1.in'~ ,

I -~)j-'4j++)
f,irtJ- tl t()·
i11[i]LiJ::::in.nex n ,

/ient.l1ut.~rintln("The numbers of the matrix are:");


: ·dJ·id;1++)
111111-'

I . )
f0rtj:::Oj< 4:J++
I
system.out.print(m[i)Li1+" ");
I
System.out.println();
I
for(i::O;i<l;i++)
I
for(j=Oj<4;j++)
{
s=s+mli]Lil;

''
System.out.println("The sum of the elements in the matrix is "+s);
I
I
Wrire a ~gra'.11 in J.tva to More lhc
Double D1men1o1onill Arrny. Find lh , t . lltt111hl',- .
C 11 "h ' \ 111
die matn~ by usmg an input Ma1cnicnt C\I iln<J lh ~ 1t
• • •

Sample Input: · ~ l(11i, ~ 1 .


The numbers of thc marnx . arc l\ t ,11,, 1
11
1 1
111 \
12 21 JJ 14
24 -11 .s1 .n
61 II 30 29
59 82 41 76
Sample Output:
The lowest numhcr in the array is 11
The greatest number in the array is 82

11 To find the ma.'<. and min. of the matrix clements


import java.util. •;
public class ddMa.'<_Min
l
public static void main(String args fl)
I
Scanner in=new Scanner(System.in);
int ij.min,max;
min::O;max::0;
int ml 1( ]=new intf4][4J;
System.out.println("Enter the numbers of the matrix");
for(i::O;i<4;i++)
{
for(j::0j<4 ;j++)
mfiJU]=in.nextlnt();
J
System.out.println("The numbers of the matrix are:");
for(i::O;i<4;i++)
l
for(j::0j<4j++)
l
System.out.print(m[i]UJ+" ");
J
System.out.printJn();
J
min=m{OJ(OJ;
Dlal=m{OJ(OJ;
f0l(i=O;i<4~++)
f
fOl(i=O-j<4a++>
{

<-,
u...-,._ ........ """lllaoHc
I . itn(lhc hml'.'-1 nu1nhi.:r 1111 ~ . . _
,rttl . l~ ,I IT.t \ I\ " ,
l ,.,,,11 I . tln('Thc h1ght:,t nu1nhl•r in ll , . . +r,1111 '·
,,11 pflll ll: ,Ht,1 \ h '
•'. ,I .,,,.,,111 - · ~ 111.1\ I,
.r•
•'

. ,, 1·te a program in fa,a 10


,~r . 'ore
1 thc
l}l,uble Dimcm.ional Array. Find thc nun~hn, in ,1 •• ! I'. .. :r1, i·1 .1
a . 11'11t
an ,s1gm
.. 1.: ., t:llknt , ·n1e tr·an~r11~elrJn,
ltat ' r,u,1.,• Pt d,!',, !'1 •'n, h\ l .
of ' , , , '" '. , ,:·.::
ha n"c the row clement\ into Cc)\ ~ 1.1.; ,ri , h, !'t.:i:·.. ,1 '\ ;, .,, ..
c e,le In\)Ut: · uinn and u,,u:nn1
ck •~ 1, .. 1, •.•• ,' ,• •' " '
Samp 1I
' I , I • •'I '

'fhe numbers of the matri x are:


12 21 13 14
•"4 41 51 33
61 11 30 29
59 82 41 76
sample Output:
The transpose of the matrix is:
12 24 61 59
21 41 11 82
13 51 30 41
14 33 29 76

· find the transpose of a matrix


, ro ,. ·ava.utl·1 •*·,
"1~)11 1
,,·,b1.tC class ddtranspose
I'

~blic static void main(String arg~[ l)


I

I
Scanner in=new Scanner(System.in);
int ij;
intm[ ]( ]=new int[4][4] ;
int n[ )[ ]=new int[ 4][ 4];
System.out.println("Enter the numbers of thl! matrix");
flll{i=O;i<4;i++)
I
fll(j::()j<4j++)
m[ilU)=in.nextlnt();
I
S~println("The numbers of the matrix arc:");
I
' 1llI • ~
~, ,,~·m , ,,11 t'' 11~h n~ 11, '' )•

s~ ',:~·m \l~;t pn ::t: n(' 111'-· Ir.in,("-"--' ,,f Ih. .• 111.11 ri \ i~: "):
(,,:,I =1 l.1,-t1+--+)
I

'
f,lf\l =\ I._1...:. J .J +--♦)
I
s~ ,t,·m ,•ut rnnttnl illll~ .. "l:

S~ :,,.IC'm ,,ut printlnl );

\\"rik a pro!!ram in to ~h)r1.• th'-· nu111ha~ 111 .1 ➔ 4 ,. •


JJ\J
Doubk D~n'\k,nal ArrJ)'. Fi~J th'-· ~um ,)f 11~1.' nu 11 ,hl·t, ,/ :~~-~
..t ~ ~ o f t ~ num~rs lll ~~d• \.·,)lumn 01 thl.' m.nrn h l.•

uapul ~ D I . .
Seq,k Input
1'e •mbers of~ matrix are:
ll 21 IJ 14
)I ,1 , • .u
61 II JD 29
JIG4176
SF ,,, a.a-.
4-lit-fll• •-• of each row:
. :1t.tf ti r■Ells of Ist row = 60
·;lu rlfl of 2nd row= 149
ol Jnt row= 131
row= 258
I '·

1 n1,1lll \ ~I III\\' .ii)'' \\1


' \ ~ ,1,

I 11,,intln("ThL'
,•111.llll .
1H11nh1.·1.., ,,t thl· 111 ,,, 11 , ,
,,
\ \ 11111. • )

·· '.{l,i•:J:i++
1,•:11

I . -~)jd :j++ I
1,111J~ .

..t~t~•,n.llut. pri11t (111l i 11.i I+ " "):


/ 1~,n.out .println( ):
'•

s,·,I11.•111.,mt.pnnt
· I11( "·r·1ll' :-.11111 ut the L'lr111l J1h ,,t , . • :1 !

1,;rtidl:i<.i:i++ )
I
r-0:
for(j:{):j<•tj++)
I
r=r+mli)Lil :
I
System.out.println("Thl' ~u111 ot 1li1.· ~·k111,·nt-. l 't •11~lt• 111., •11

I
System.out.println("Tlu: ~um of till.' l'k1111.·11h 11t L',1,h L·1•\11m:~ · 1:
fixti~~k4~i++)
I
C::0;
f(lfja();j<4j++)
1.':::l' tlllljll1 l: • ., 1 ,I " I Ii t I) t " n, l1 111111
Il J.
• ,. 11 11 I 11
1
' l 11 f I111. I
s~ :-ll'lll.llllt.pri111l11(''lhl' :-,11111
I
1
_ th~ 111111il)l'f':-. in •I ·1 tll:ilti .
,,11L' I X 111 ,
. J ·1\;1 ,,, " "f rite 1111111 ,en, of Ilic I ,11>1,
(II ,11 • I ...11111 . l'f I I 111'11 1
1
Pro)!. 16 \\'rit~ a.pn'.~!r;•
, , .-.,y. ,:;nil . ·.; ,,1 I1it.:
1 1L : . dghl d1a go11;d of Ilic n, ·
, 1 rt. x Ii
t 1;11,' 11.,

l)i111cn .. 111 11•11 ' f 1rhL' •• 1,,,11" ·


1
Y 11 ,,11,
anJ th' """. I "•11IL'lll. ·
;tit inrlll .. l,I L •
,111 : -'x :in--'•
san1rk I111.,., ,,1. ,1ti: ,11;111 •
Till' ,111111 IIL •
1
12 21 13 "
"" ... , ,, JJ
(!I 11 3() :Y
,;I} S2 .t I 76
Samrl' ()utr•" ·O ,liag<'""' elc111cnts " I 59
fhr: sunt of 1ht' I~ hi dia1!onal clclllClllS :::-. 135
fhc sunt of rhc rrg ~
' d ri hi diagonal clements
// To rinJ rhe sulll of !di an g
inipt>r1 jara.u1il. *;
public class dJdiagonals
/ .
public swric roid main(Scnng args[J)

/
Scanner in=new Scanner(Sys1em.in);
int i.j.ld.rd.k;ld=0:rd=O;
int m/ J( J=new inrf4Jf4J;
Sysrem.ou1.prin1ln("Enter the numbers of the matrix:");
for(i=0:i<4:i++)
I
for(j=O:j<4:j++)
mf iJUJ=in.nexllnt();
J
System.out.println("The number .
for(i=0;i<4;i++) s of the matrix are:");

(
for(j=OJ<4J++)
l
}System.out.pnnt(m[lJ[jJ+"
. . ");

System.out.println()·
23 '
o/ Understanding Comp
uter Appli .
cations With BlueJ-X
I . () ,.. I .I I I )
(I · .
11 11
.f,Hnil d/il :

~-'. .
t~l
I •111
,111 I'' 111tl11 ('' I lie
t
, 11111 111
"' '" ).

I ~ 1,
" - · - ti.I"•' l . I ♦ I I
,·,,n •
I 111' i II f.. I,
I ,I I I 11

~ · ~ f;
I ,
1111
1111(""lfa· , 11 111 11 1' Ilic right di.1~1111.il d r mrnh =-"+rd) ·
. t •Ill I I I 111
. •
~~ .. 4

\\'nil' a pn 1,1!1-:t111 i11 J,l\·:r ll1 :--lorl' 1l1L' m 1111bcrs in l\.\.o diJfl:n:nl D1111hle
J)itn'-·n,i(lnal Arr,iys 111H I 1-1 I and 111-11 /4 J. Fi11J Ilic sum of the 1111111hcr:--
nf th'-' "·01n·:--p1111Ji11~ ckll lL'IIIS pf' lhl' 1wo arrays 111 and n anJ :--1,,rc them
in th~ amt)' pf -1 J HI by usi ng .111 lnpul stalc111cn1. Oi:--pl;,iy the d..:rncnts
of amt)' p in a 111a1ri~ form.
s:unrk Input : Sample Output :
Array n Array p
Array 111
8 22 3I 15 20 45 64 37
23 JJ 22
12
11 16 30 26 31 47 74 36
JI 4➔ 10
20
17 25 33 20 42 39 41
:s 14 8 JO
JO
24 21 32 53 76 45 44
24 12 12
~I 52

,. fo tinJ ihc sum of the corresponding elements of the 1wo matrices


. •
. a.10.
:~ipvrt Ja\ •·
fJhlic class dd~um

p.iblic static void main(String args f J) throws JOException


I
lnputStreamReader read = new Inp utS1rcamReaJ cr(Sys1cm.in);
BufferedReader in = new Bufferec.JReader(read);
int ij;
int m{ 1l }=new int[4Jl4] ;
mt n( 1l }=new int(4][4];
Pl 1l )=new int[4J[4J;
1111

S>stern.out ·
fnr1· • pnntJn( "Enter the numbers of the first matrix");
-\i::o-J<4;.i++)
I
~•a++>
Arrays (Single Dimensional and Do uble Dimc nw,n,1/) f 2:1 I
F
>
n,! i 11 jl=-111tcgcr.p.1r:-dnt(in .rc.1dlinc() ):
I
.. . ,
.. l.nll'r . 1· I , • · rnd matri, "):
S),tL·111.11t1t p1111tl11( the 1111111hl'I!'> n t 11.: :-ul
f111( 1=0 .i<-1:i H)

fl11q ·c(l._j<-1:j H·)


11I • IUI:.: I nll'fl'r.par:-dnt( in.n:adLinl.'() ):
I
S:.!',tl'll1.nut.pri11tl11("S um uf the arr::iy ek111l!nts is:"):
fpr( i=O:i<-ki++)

for(j=O:j<~ :j ++)
I
rl i JU ]=ml i lli J+nliJLil:
System.out.pri nt(pl i]Li]+" "):
}
System.out. println();
}

ProJ?. 18 Write a program to store a day, its maximum tempaature, its minimum
temperature in a Double Dimensional Array of 7*3. The program acc,'pb
a particular day of the week and displays the maximum and minimum
temperature of that day. Use an Input statement for the same.

II To display the maximum and minimum temp of the day


import java.io. *;
public class dd_temp
(
public static void mai n(String args[)) throws IOException
I
InputStrcamReader read = new InputStrearnReader(System.in);
BufferedReader in = new BufferedReader(read);
int i,j;
String day;
String m[ I{ J=new String[7][3J;
for(i=0:i<7;i++)
(
for(j=O:j<3:j++)
{
Systcm.out.println("Enter da , .
m[i)LiJ-in dL'me(); }, maximum temp, minimum temp:"):

.
- .rea

<
J

232 I Understanding Computer A 1. .


. PP •cations with BlueJ-X

You might also like