0% found this document useful (0 votes)
2 views

JAVA LAB RECORD nihal

The document outlines a series of programming tasks in Core Java for a student named Nihal, with a focus on various concepts such as finding integers, string manipulation, and class creation. It includes both Part A and Part B, detailing specific programming assignments along with their page numbers. Each task requires the implementation of different Java functionalities, including user input handling and mathematical operations.

Uploaded by

vyntraq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA LAB RECORD nihal

The document outlines a series of programming tasks in Core Java for a student named Nihal, with a focus on various concepts such as finding integers, string manipulation, and class creation. It includes both Part A and Part B, detailing specific programming assignments along with their page numbers. Each task requires the implementation of different Java functionalities, including user input handling and mathematical operations.

Uploaded by

vyntraq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

LABONPROGRAMMI

NGI
NCOREJAVA
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
I
NDEX

SI
.NO PROGRAM NAME PAGENUMBER

PARTA
1 Wr
it
eapr ogr
am t
ofi
ndt henumberandsum ofal
l i
nteger
s 3
gr
eatert
han100andlessthan200thatar
edivi
sibl
eby7.

2 Wri
teaprogram t
hataskst heusertoentertwowords.The 4
wordswil
lbesepar atedbyenoughdot ssothatt
hetotall
i
ne
lengthis30.
3 Wri
teaprogram t
hataskst heuserforlowandhighint
egerin 6
arangeofint
eger s.Thepr ogram t
henaskstheuserfor
integerst obeaddedup.
4 Wr
iteaprogram t
oconcat enatetwostri
ngusingcommand 8
l
inear guments.

5 Writ
eapr ogr
am toextr
actapor t
ionofcharacterstri
ngand 9
printt
heextract
edstring.Assumet hati
nchar act
erar
e
extr
actedstar
tingwi t
hthenthchar acter.
6 Wri
teaprogram tocreat
eav ect
ort ost
oretheshoppi ngl
istof 11
i
tems.

7 Wr
it
eapr
ogr
am tosortal
i
stofnumberst
hatar
eaccept
ed 15
t
hroughcommandli
neargument
s.

8 Wr
it
eapr
ogr
am t
omul
ti
plyt
womat
ri
ces. 17

PARTB
1 Writeapr ogr am t odi spl ayamenuwi thanopt ionadd 21
subtract,mul ti
plyanddi visionbyi mpl ement ingcl asses.Do
theappr opr i
at eact i
ononsel ection.Usessepar atemet hodf or
calcul ati
on.
2 Wr i
teapr ogr am t ocr eatest udentcl asswi t
hf i
elds.Thet ot al 25
ofthesubj ect smustbecal culatedonl ywhent hest udent
passesi nal l 3subj ectsot her wisedecl aret ot al i
s0, using
const ructorcr eat eanar r
ayof‘ n’student s.
3 Cr eateabasecl assboxwi thhei ght, dept handwi dth.Adda 28
met hodt ocal culatev olume.Wr i
teal lthepossi ble
const ructor s.Cr eateader ivedcl asswi thaddi tional dat a
member swei ghtandcol or.Makeusaget hekey wor dsuper .
4 Dev elopapr ogr am t opr oducepaysl i
pofaname, codeand 30
designation.Addanot herbasecl assconsi st i
ngofdat a
member saccountnumber ,dateofj oi ni
ngandbasi cpay .The
derivedcl assconsi stsofdat amember sofot herear ning( pf,
lic,tax) .(I
mpl ementusi ngi nterface) .
5 Cr eateapackagemypackage, whichhascl asst or epr esenta 35
bankaccount ,includesf ollowi ngdat amember s.Nameoft he
deposi tor,accountno, typeofaccountandbal anceamount .
6 Wr i
teaj avapr ogr am todemonst r
at et heusageoff eat ure 38
package.Cr eat epackaget ocov erttemper atur ei nCent i
gr ade
i
nto
Fahr enheitandonemor epackaget ocal culatesi mpl ei nterest .
Impl ementbot hpackagesi nmai n.
7 Program t odemonst rat emul ti
threadi ngusi ngr unnabl e 42
interface.Def inet hreedi fferentt hreadst ocal culat esquar eof

1
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
fi
rstnint
egers,
anothert
ocalcul
atecubeoff
ir
stnint
egers
andt hi
rdt
hreadtofi
ndthesquarerootoft
hefi
rstni
nteger
s.

PARTA

2
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 1:
Wri
teaprogr
am t
ofi
ndt
henumberandsum ofal
li
nteger
sgr
eat
ert
han100andl
esst
han200t
hatar
e
di
vi
sibl
eby7.

i
mpor
tjav
a.i
o.*
;

cl
assdi
vi
de

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

i
nti
,
sum=0;

Sy
stem.
out
.pr
int
ln(
"TheNumber
sbet
ween100and200t
hatar
edi
vi
sibl
eby7ar
e:"
);

f
or(
i=101;
i
<200;
i
++)

i
f(
i%7==0)

Sy
stem.
out
.pr
int
ln(
i)
;sum=sum+i
;

Sy
stem.
out
.pr
int
ln(
"TheSum ofNumber
sdi
vi
sibl
eby7i
s:"
+sum)
;

OUTPUT:

3
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 2:
Wri
teapr
ogr
am thataskst
heusert
oent
ert
wowor
ds.Thewor
dswi
l
lbesepar
atedbyenoughdot
ssot
hat
t
hetot
all
i
nelengt
his30.

i
mpor
tjav
a.i
o.*
;

cl
assdot
s

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

Dat
aInput
Str
eam b=newDat
aInput
Str
eam(
Syst
em.
i
n);

St
ri
ngs1,
s2;

i
ntsum,
i
;

t
ry

Sy
stem.
out
.pr
int
ln(
"Ent
ert
hef
ir
stst
ri
ng"
);

s1=b.
readLi
ne(
);

Sy
stem.
out
.pr
int
ln(
"Ent
ert
hesecondst
ri
ng"
);

s2=b.
readLi
ne(
);

sum=s1.
l
engt
h()
+s2.
lengt
h()
;

Sy
stem.
out
.pr
int
(s1)
;

f
or(
i=0;
i
<=(
30-
sum)
;i
++)

Sy
stem.
out
.pr
int
("
."
);

Sy
stem.
out
.pr
int
ln(
s2)
;

cat
ch(
Except
ione)

{
}

4
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
}

OUTPUT:

5
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 3:
Wri
teapr
ogr
am thataskst
heuserf
orl
owandhi
ghi
ntegeri
nar
angeofi
nteger
s.Thepr
ogr
am t
henasks
t
heuserf
ori
nteger
stobeaddedup.

i
mpor
tjav
a.i
o.*
;

cl
assBounds

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

i
ntl
b,
ub,
i
sum=0,
osum=0,
i
;

Dat
aInput
Str
eam b=new

Dat
aInput
Str
eam(
Syst
em.
in)
;

t
ry

Sy
stem.
out
.pr
int
ln(
"Ent
erl
owerBound:
")
;

l
b=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));

Sy
stem.
out
.pr
int
ln(
"Ent
erUpperBound:
")
;

ub=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));

do

Sy
stem.
out
.pr
int
ln(
"Ent
erNUmber
sandt
ype0t
ost
op"
);

i
=Int
eger
.par
seI
nt(
b.r
eadLi
ne(
));

i
f(
i>l
b&&i
<ub)

i
sum=i
sum+i
;

whi
l
e(i
!=0)
;

i
f(
i==0)

Sy
stem.
out
.pr
int
ln(
"Thesum wi
thi
nther
angei
s:"
+isum)
;

6
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
}

cat
ch(
Except
ione)

{
}

OUTPUT:

7
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 4:
Wr
it
eapr
ogr
am t
oconcat
enat
etwost
ri
ngusi
ngcommandl
i
near
gument
s.

i
mpor
tjav
a.i
o.*
;

cl
assconcat

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

i
nti
,
j,
n;

n=ar
gs[
0].
lengt
h()
+ar
gs[
1].
lengt
h()
;

charc[
]=newchar
[n]
;

f
or(
i=0;
i
<ar
gs[
0].
l
engt
h()
;i
++)

c[
i]
=ar
gs[
0].
char
At(
i)
;

j
=i;
for
(i
=0;
i
<ar
gs[
1].
lengt
h()
;i
++)

c[
j]
=ar
gs[
1].
char
At(
i)
;j
++;

Sy
stem.
out
.pr
int
ln(
"TheConcat
enat
edSt
ri
ngi
s:"
);

Sy
stem.
out
.pr
int
ln(
c);

OUTPUT:

8
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 5:
Writ
eaprogram t
oextr
actapor
ti
onofchar
acterstr
ingandpr
intt
heext
ract
edst
ri
ng.Assumet
hati
n
char
act
erareextr
act
edstar
ti
ngwitht
henthcharact
er.

i
mpor
tjav
a.i
o.*
;

cl
assext
ract

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

St
ri
ngst
r;

i
nti
,
n,m;

t
ry

Dat
aInput
Str
eam b=new

Dat
aInput
Str
eam (
Syst
em.
in)
;

Sy
stem.
out
.pr
int
ln(
"Ent
ert
hest
ri
ng:
")
;

st
r=b.
readLi
ne(
);

Sy
stem.
out
.pr
int
ln(
"Ent
ert
heposi
ti
onf
rom wher
ethest
ri
ngneedst
obeext
ract
ed:
")
;

n=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));

Sy
stem.
out
.pr
int
ln(
"Ent
ert
henumberofchar
act
erst
obeext
ract
ed:
")
;

m=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));

Sy
stem.
out
.pr
int
ln(
"Theext
act
edst
ri
ngi
s:
")
;

f
or(
i=n-
1;i
<n+m-
1;i
++)

Sy
stem.
out
.pr
int
(st
r.
char
At(
i)
);

Sy
stem.
out
.pr
int
ln(
);

cat
ch(
Except
ione)

{
}

9
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
}
}

OUTPUT:

10
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 6:
Wr
it
eapr
ogr
am t
ocr
eat
eav
ect
ort
ost
oret
heshoppi
ngl
i
stofi
tems.

i
mpor
tjav
a.i
o.*
;

i
mpor
tjav
a.ut
il
.*
;

cl
assshop

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

t
ry

Vect
orl
i
st=newVect
or(
);

St
ri
ngst
r;

i
nti
,
n,pos,
ch=0;

Dat
aInput
Str
eam b=new

Dat
aInput
Str
eam(
Syst
em.
i
n);

do

Sy
stem.
out
.pr
int
ln(
"Menu"
);

Sy
stem.
out
.pr
int
ln(
"1.
Creat
e\n2.
Add\
n3.
Del
ete\
n4.
Displ
ay\
n5.
Exi
t"
);

Sy
stem.
out
.pr
int
ln(
"Ent
ery
ourchoi
ce:
")
;

ch=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));

swi
tch(
ch)

case1:

Sy
stem.
out
.pr
int
ln(
"Ent
ert
hei
tem t
obeaddedt
oli
st:
")
;

st
r=b.
readLi
ne(
);

l
i
st.
addEl
ement
(st
r);

11
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
Sy
stem.
out
.pr
int
ln(
"Cr
eat
ingal
i
st.
..
")
;

br
eak;

case2:

Sy
stem.
out
.pr
int
ln(
"Ent
erani
tem"
);

st
r=b.
readLi
ne(
);

Sy
stem.
out
.pr
int
ln(
"Ent
erposi
ti
on"
);

pos=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));

l
i
st.
inser
tEl
ement
At(
str
,pos-
1);

br
eak;

case3:

n=l
i
st.
size(
);

i
f(
n==0)

Sy
stem.
out
.pr
int
ln(
"Li
sti
sempt
y")
;

el
se

Sy
stem.
out
.pr
int
ln(
"Ent
ert
hei
tem t
obedel
eted:
")
;

st
r=b.
readLi
ne(
);

l
i
st.
remov
eEl
ement
(st
r);

br
eak;

case4:

i
ntl
=li
st.
size(
);

St
ri
ngs[
]=newSt
ri
ng[
l]
;

12
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
i
f(
l==0)

Sy
stem.
out
.pr
int
ln(
"Li
sti
sempt
y")
;

el
se

f
or(
i=0;
i
<l;
i
++)

l
i
st.
copy
Int
o(s)
;

Sy
stem.
out
.pr
int
ln(
"Thei
temsi
nthel
i
star
e:"
);

f
or(
i=0;
i
<l;
i
++)

Sy
stem.
out
.pr
int
ln(
s[i
])
;

br
eak;

case5:

Sy
stem.
exi
t(
0);

def
aul
t:

Sy
stem.
out
.pr
int
ln(
"I
nval
i
dchoi
ce:
")
;

whi
l
e(ch<5)
;

cat
ch(
Except
ione)
{}

13
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

OUTPUT:

14
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 7:
Wr
it
eapr
ogr
am t
osor
tal
i
stofnumber
sthatar
eaccept
edt
hroughcommandl
i
near
gument
s.

i
mpor
tjav
a.i
o.*
;

cl
assSor
t

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

i
nta[
]=newi
nt[
10]
;

i
nti
,
j,
n,
temp;

n=ar
gs.
lengt
h;

f
or(
i=0;
i
<n;
i
++)

a[
i]
=Int
eger
.val
ueOf
(ar
gs[
i]
);

Sy
stem.
out
.pr
int
ln(
"Theel
ement
sinunsor
tedor
derar
e")
;

f
or(
i=0;
i
<n;
i
++)

Sy
stem.
out
.pr
int
(a[
i]
+"\
t")
;

f
or(
i=0;
i
<n;
i
++)

f
or(
j=0;
j
<n-
i-
1;j
++)

i
f(
a[j
]<a[
j+1]
)

t
emp=a[
j]
;

a[
j]
=a[
j+1]
;

a[
j+1]
=temp;

Sy
stem.
out
.pr
int
("
\nTheel
ement
sindecendi
ngor
derar
e\n"
);

f
or(
i=0;
i
<n;
i
++)

Sy
stem.
out
.pr
int
(a[
i]
+"\
t")
;

15
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
Sy
stem.
out
.pr
int
ln(
);

OUTPUT:

16
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 8:
Wr
it
eapr
ogr
am t
omul
ti
plyt
womat
ri
ces.

i
mpor
tjav
a.i
o.*
;

i
mpor
tjav
a.ut
il
.Scanner
;

publ
i
ccl
assmat
ri
xmul
ti
ply
1

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

i
ntr
ow1,
col
1,r
ow2,
col
2;

Scanners=newScanner
(Sy
stem.
in)
;

Sy
stem.
out
.pr
int
("
Ent
ert
henumberofr
owsi
nthef
ir
stmat
ri
x:
")
;

r
ow1=s.
next
Int
();

Sy
stem.
out
.pr
int
("
Ent
ert
henumberofcol
umnsi
nthef
ir
stmat
ri
x:
")
;

col
1=s.
next
Int
();

Sy
stem.
out
.pr
int
("
Ent
ert
henumberofr
owsi
nthesecondmat
ri
x:
")
;

r
ow2=s.
next
Int
();

Sy
stem.
out
.pr
int
("
Ent
ert
henumberofcol
umnsi
nthesecondmat
ri
x:
")
;

col
2=s.
next
Int
();

i
f(
col
1!=r
ow2)

Sy
stem.
out
.pr
int
ln(
"Mat
ri
xmul
ti
pli
cat
ioni
snotpossi
ble"
);

r
etur
n;

i
nta[
][
]=newi
nt[
row1]
[col
1];

i
ntb[
][
]=newi
nt[
row2]
[col
2];

17
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
i
ntc[
][
]=newi
nt[
row1]
[col
2];

Sy
stem.
out
.pr
int
ln(
"Ent
erv
aluesf
ormat
ri
x1:
")
;

f
or(
inti
=0;
i
<row1;
i
++)

f
or(
intj
=0;
j
<col
1;j
++)

a[
i]
[j
]=s.
nex
tInt
();

Sy
stem.
out
.pr
int
ln(
"Ent
erv
aluesf
ormat
ri
x2:
")
;

f
or(
inti
=0;
i
<row2;
i
++)

f
or(
intj
=0;
j
<col
2;j
++)

b[
i]
[j
]=s.
next
Int
();

Sy
stem.
out
.pr
int
ln(
"mat
ri
xmul
ti
pli
cat
ion:
")
;

f
or(
inti
=0;
i
<row1;
i
++)

f
or(
intj
=0;
j
<col
2;j
++)

c[
i]
[j
]=0;

f
or(
intk=0;
k<col
1;k++)
{

c[
i]
[j
]+=a[
i]
[k]
*b[
k][
j]
;

Sy
stem.
out
.pr
int
(c[
i]
[j
]+""
);

Sy
stem.
out
.pr
int
ln(
);

18
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

OUTPUT:

19
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PARTB

20
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

PROGRAM 1:
Wri
teapr ogr
am t
odispl
ayamenuwi t
hanoptionaddsubtr
act,
mul
ti
plyanddi
visionbyi
mpl
ement
ing
cl
asses.Dotheappr
opri
ateact
iononsel
ect
ion.Usessepar
atemet
hodforcal
culati
on.

i
mpor
tjav
a.i
o.*
;
cl
asscal
cul
ator
{
i
ntx,
y;
v
oidget
dat
a(i
nta,
intb)
{
x=a;
y
=b;
}
i
ntsum(
)
{
r
etur
n(x+y
);
}
i
ntdi
ff
erence(
)
{
r
etur
n(x-
y);
}
i
ntpr
oduct
()
{
r
etur
n(x*
y);
}
i
ntquot
ient
()
{
r
etur
n(x/
y);

21
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
}
}
cl
assoper
ati
on
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
i
ntn1,
n2,
ch;
Dat
aInput
Str
eam b=new
Dat
aInput
Str
eam(
Syst
em.
in)
;
cal
cul
atorc=newcal
cul
ator
();
t
ry
{
Sy
stem.
out
.pr
int
ln(
"Ent
ert
hef
ir
stnumber
:"
);
n1=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));
Sy
stem.
out
.pr
int
ln(
"Ent
ert
hesecondnumber
:"
);
n2=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));
c.
get
dat
a(n1,
n2)
;

do
{
Sy
stem.
out
.pr
int
ln(
"Menuf
orOper
ati
on"
);
Sy
stem.
out
.pr
int
ln(
"1.
Add"
);
Sy
stem.
out
.pr
int
ln(
"2.
Subt
ract
");
Sy
stem.
out
.pr
int
ln(
"3.
Mul
ti
ply
");
Sy
stem.
out
.pr
int
ln(
"4.
Div
isi
on"
);
Sy
stem.
out
.pr
int
ln(
"5.
Exi
t"
);
Sy
stem.
out
.pr
int
ln(
"Ent
ery
ourchoi
ce:
")
;
ch=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));

22
NAME:NIHAL
REGI
STERNUMBER:3SU21SA100
swi
tch(
ch)
{
case1:
{
Sy
stem.
out
.pr
int
ln(
"Thesum i
s:"
+c.
sum(
));
br
eak;
}
case2:
{
Sy
stem.
out
.pr
int
ln(
"Thedi
ff
erencei
s:"
+c.
dif
fer
ence(
));
br
eak;
}
case3:
{
Sy
stem.
out
.pr
int
ln(
"Thepr
oducti
s:"
+c.
product
())
;
br
eak;
}
case4:
{
Sy
stem.
out
.pr
int
ln(
"Thedi
vi
sioni
s:"
+c.
quot
ient
())
;
br
eak;
}
case5:
{
Sy
stem.
exi
t(
0);
br
eak;
}
def
aul
t:

23
NAME:NIHAL
REGI
STERNUMBER:3SU21SA100
{
Sy
stem.
out
.pr
int
ln(
"I
nval
i
dchoi
ce"
);
}}
}
whi
l
e(ch<=5)
;}
cat
ch(
Except
ione)
{}}
}
OUTPUT:

24
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
PROGRAM 2:
Wr i
teaprogram tocreat
estudentcl
asswithfi
elds.Thetot
alofthesubject
smustbecal
cul
atedonl
ywhen
thestudentpassesinall3subj
ectsother
wisedeclaret
otali
s0,usingconstr
uct
orcr
eat
eanar r
ayof‘
n’
students.

i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.l
ang.
*;
cl
assSt
udent
{
i
ntr
egno,
mar
k[]
=newi
nt[
3],
tot
al=0,
i
;
St
ri
ngname;
St
udent
(i
ntr
,St
ri
ngst
r,
intm[
])
{
r
egno=r
;
name=st
r;
f
or(
i=0;
i
<3;
i
++)
{
mar
k[i
]=m[
i]
;
i
f(
mar
k[i
]<35)
{
t
otal
=0;
br
eak;
}
el
se
{
t
otal
=tot
al+mar
k[i
];
}
}
}
v
oiddi
spl
ay(
)

25
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
{
Sy
stem.
out
.pr
int
ln(
"Det
ail
soft
hest
udentar
e:"
);
Sy
stem.
out
.pr
int
ln(
"Regnonamet
otal
")
;
Sy
stem.
out
.pr
int
ln(
regno+""
+name+""
+tot
al)
;
}
}
cl
assst
udent
det
ail
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
St
udents[
];
i
ntno,
scor
e[]
,
tot
al,
i
,j
;
scor
e=newi
nt[
3];
Dat
aInput
Str
eam b=new
Dat
aInput
Str
eam(
Syst
em.
in)
;
t
ry
{
Sy
stem.
out
.pr
int
ln(
"Ent
ert
henumberofst
udent
");
i
ntn=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));
s=newSt
udent
[n]
;
Sy
stem.
out
.pr
int
ln(
"Ent
ert
heSt
udentdet
ail
s")
;
f
or(
i=0;
i
<n;
i
++)
{
Sy
stem.
out
.pr
int
ln(
"Ent
erRegi
sterNumber
");
no=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));
Sy
stem.
out
.pr
int
ln(
"Ent
erName"
);
St
ri
ngnm=b.
readLi
ne(
);
Sy
stem.
out
.pr
int
ln(
"Ent
ermar
ksi
nthr
eesubj
ect
s")
;

26
NAME:NI HAL
REGISTERNUMBER:3SU21SA100
for
(j
=0;j
<3;
j++)
scor
e[j
]=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));
s[
i]
=newSt
udent
(no,
nm,
scor
e);
}
Sy
stem.
out
.pr
int
ln(
"Thedet
ail
sofst
udent
sar
e")
;
Sy
stem.
out
.pr
int
ln(
"Regnonamet
otal
")
;
f
or(
i=0;
i
<n;
i
++)
s[
i]
.di
spl
ay(
);
}
cat
ch(
Except
ione)
{}
}
}
OUTPUT:

27
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
PROGRAM 3:
Creat
eabasecl assboxwit
hhei
ght,depthandwidt
h.Addamet hodt
ocal
cul
atevol
ume.Writ
eallt
he
possi
bleconstr
uctor
s.Cr
eat
eaderi
vedclasswit
haddit
ional
dat
amemberswei
ghtandcol
or.Makeusage
thekeywordsuper.

i
mpor
tjav
a.ut
il
.*
;
cl
assbox{
doubl
ewi
dth,
hei
ght
,dept
h;
box(
doubl
ewi
d,doubl
ehei
,
doubl
edep)
{
t
his.
widt
h=wi
d;
t
his.
hei
ght
=hei
;
t
his.
dept
h=dep;
}
publ
i
cvoi
dvol
ume(
){
Sy
stem.
out
.pr
int
ln(
"Thev
olume="
+(wi
dth*
hei
ght
*dept
h))
;
}
}
cl
assshapeext
endsbox{
St
ri
ngcol
or;
doubl
ewei
ght
;
shape(
doubl
ewi
d,doubl
ehei
,
doubl
edep,
doubl
ewei
,
Str
ingcol
){
super
(wi
d,hei
,
dep)
;
t
his.
col
or=col
;
t
his.
wei
ght
=wei
;
}
publ
i
cvoi
dshapei
nfo(
){
Sy
stem.
out
.pr
int
ln(
"Wei
ght
="+wei
ght
);
Sy
stem.
out
.pr
int
ln(
"COl
or="
+col
or)
;
v
olume(
);

28
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
}
}
publ
i
ccl
assshapeoper
ati
ons
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
doubl
eh,
w,wei
,
d;
St
ri
ngcol
our
;
Scannerob=newScanner
(Sy
stem.
in)
;
shapes;
Sy
stem.
out
.pr
int
("
Ent
ert
hehei
ght
:"
);
h=ob.
next
Int
();
Sy
stem.
out
.pr
int
("
Ent
ert
hewi
dth:
")
;
w=ob.
next
Int
();
Sy
stem.
out
.pr
int
("
Ent
ert
hedept
h")
;
d=ob.
next
Int
();
Sy
stem.
out
.pr
int
("
Ent
ert
hewei
ght
");
wei
=ob.
next
Int
();
Sy
stem.
out
.pr
int
("
Ent
ert
hecol
or:
")
;
col
our
=ob.
next
();
s=newshape(
w,h,
d,
wei
,
col
our
);
s.
shapei
nfo(
);
ob.
close(
);
}}
OUTPUT:

29
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
PROGRAM 4:
Devel
opaprogram t
oproducepaysli
pofaname, codeanddesignati
on.Addanot
herbasecl
ass
consi
sti
ngofdatamembersaccountnumber,dat
eofjoi
ningandbasicpay.Thederi
vedcl
assconsi
stsof
datamembersofotherear
ning(
pf,l
i
c,tax)
.(I
mplementusingi
nter
face).

i
mpor
tjav
a.i
o.*
;
cl
assemp1
{
St
ri
ngname,
desi
g;
i
ntcode;
v
oidget
emp1(
)
{
t
ry
{
Dat
aInput
Str
eam i
n=newDat
aInput
Str
eam(
Syst
em.
in)
;
Sy
stem.
out
.pr
int
ln(
"ent
ert
heempl
oyeename:
")
;
name=i
n.r
eadLi
ne(
);
Sy
stem.
out
.pr
int
ln(
"ent
ert
hedesi
gnat
ion:
")
;
desi
g=i
n.r
eadLi
ne(
);
Sy
stem.
out
.pr
int
ln(
"ent
ert
heempl
oyeecode:
")
;
code=I
nteger
.par
seI
nt(
in.
readLi
ne(
));
}
cat
ch(
Except
ione)
{
}
}
v
oiddi
semp1(
)
{
Sy
stem.
out
.pr
int
ln(
"empl
oyeename:
"+name)
;
Sy
stem.
out
.pr
int
ln(
"desi
gnat
ion:
"+desi
g);
Sy
stem.
out
.pr
int
ln(
"code:
"+code)
;

30
NAME:NIHAL
REGI
STERNUMBER:3SU21SA100
}
}
cl
assemp2ext
endsemp1
{
i
ntacc_
no,
basi
c_pay
;
St
ri
ngdoj
;
v
oidget
emp2(
)
{
t
ry
{
Dat
aInput
Str
eam i
n=newDat
aInput
Str
eam(
Syst
em.
in)
;
Sy
stem.
out
.pr
int
ln(
"ent
ert
heaccountnumber
:"
);
acc_
no=I
nteger
.par
seI
nt(
in.
readLi
ne(
));
Sy
stem.
out
.pr
int
ln(
"ent
ert
hedat
eofj
oini
ng:
")
;
doj
=in.
readLi
ne(
);
Sy
stem.
out
.pr
int
ln(
"ent
ert
hebasi
cpay
:")
;
basi
c_pay
=Int
eger
.par
seI
nt(
in.
readLi
ne(
));
}
cat
ch(
Except
ione)
{
}
}
v
oiddi
semp2(
)
{
Sy
stem.
out
.pr
int
ln(
"accountnumber
:"
+acc_
no)
;
Sy
stem.
out
.pr
int
ln(
"dat
eofj
oini
ng:
"+doj
);
Sy
stem.
out
.pr
int
ln(
"basi
c_pay
:"+basi
c_pay
);
}
}

31
NAME:NIHAL
REGI
STERNUMBER:3SU21SA100
i
nter
facededucat
ion
{
st
ati
cfi
nal
intpf
=500;
st
ati
cfi
nal
intl
i
c=700;
st
ati
cfi
nal
intt
ax=250;
publ
i
cvoi
dget
emp3(
);
publ
i
cvoi
ddi
semp3(
);
publ
i
cvoi
dsal
ary
();
}
cl
assemp3ext
endsemp2i
mpl
ement
sdeducat
ion
{
i
ntda,
hra,
cca;
publ
i
cvoi
dget
emp3(
)
{
t
ry
{
Dat
aInput
Str
eam i
n=newDat
aInput
Str
eam(
Syst
em.
in)
;
Sy
stem.
out
.pr
int
ln(
"ent
ert
heda:
")
;
da=I
nteger
.par
seI
nt(
in.
readLi
ne(
));
Sy
stem.
out
.pr
int
ln(
"ent
ert
hehr
a:"
);
hr
a=I
nteger
.par
seI
nt(
in.
readLi
ne(
));
Sy
stem.
out
.pr
int
ln(
"ent
ert
hecca:
")
;
cca=I
nteger
.par
seI
nt(
in.
readLi
ne(
));
}
cat
ch(
Except
ione)
{
}
}
publ
i
cvoi
ddi
semp3(
)

32
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
{
Sy
stem.
out
.pr
int
ln(
"dai
s:"
+da)
;
Sy
stem.
out
.pr
int
ln(
"hr
ais:
"+hr
a);
Sy
stem.
out
.pr
int
ln(
"ccai
s:"
+cca)
;
}
publ
i
cvoi
dsal
ary
()
{
i
ntgr
oss_
sal
=basi
c_pay
+hr
a+da+cca;
i
ntdeduce=pf
+tax
+li
c;
i
ntnet
_sal
=gr
oss_
sal
-deduce;
Sy
stem.
out
.pr
int
ln(
"l
ici
s:"
+li
c);
Sy
stem.
out
.pr
int
ln(
"pfi
s:"
+pf
);
Sy
stem.
out
.pr
int
ln(
"t
axi
s:"
+tax)
;
Sy
stem.
out
.pr
int
ln(
"empl
oyeegr
osssal
ary
:"+gr
oss_
sal
);
Sy
stem.
out
.pr
int
ln(
"deducat
ioni
s:"
+deduce)
;
Sy
stem.
out
.pr
int
ln(
"empl
oyeesal
ary
:"+net
_sal
);
}
}
publ
i
ccl
assemp
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)
{
emp3t
=newemp3(
);
t
.get
emp1(
);
t
.get
emp2(
);
t
.get
emp3(
);
Sy
stem.
out
.pr
int
ln(
"empl
oyeedet
ail
s")
;
t
.di
semp1(
);

33
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
t.
disemp2(
);
t
.di
semp3(
);
t
.sal
ary
();
}
}
OUTPUT:

34
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
PROGRAM 5:
Cr
eat
eapackagemypackage,
whichhascl
asstorepr
esentabankaccount
,incl
udesf
oll
owi
ngdat
a
member
s.Nameoft
hedeposit
or,
accountno,
typeofaccountandbal
anceamount.

PACKAGE:

packageMy
PAck;
publ
i
ccl
assbank{
l
ongacc_
no;
doubl
ebal
ance;
St
ri
ngname;
publ
i
cbank(
longa,
Str
ingn,
doubl
ebal
){
acc_
no=a;
name=n;
bal
ance=bal
;
}
publ
i
cvoi
ddeposi
t(
doubl
eamt
){
bal
ance=bal
ance+amt
;
Sy
stem.
out
.pr
int
ln(
"YourBal
ancei
s:"
+bal
ance)
;
}
publ
i
cvoi
dwi
thdr
aw(
doubl
eamt
)
{

i
f(
bal
ance-
amt
>1000) {
bal
ance=bal
ance-
amt
;
Sy
stem.
out
.pr
int
ln(
"Yourbal
ance:
"+bal
ance)
;
}
el
se
Sy
stem.
out
.pr
int
ln(
"I
nsuf
fi
cientbal
ance"
);
}

35
NAME:NIHAL
REGISTERNUMBER:3SU21SA100
publ
i
cv oi
ddi
spl
ay(
){
Sy
stem.
out
.pr
int
ln(
"AccountNo:
"+acc_
no)
;
Sy
stem.
out
.pr
int
ln(
"Nameoft
hecust
omer
:"
+name)
;
Sy
stem.
out
.pr
int
ln(
"Bal
ance:
"+bal
ance)
;
}}
MAI
N:

i
mpor
tMy
PAck.
*;
i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.ut
il
.Scanner
;

cl
assaccount
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
Scannersc=newScanner
(Sy
stem.
i
n);
i
ntchoi
ce=0;
l
ongac;
St
ri
ngnm;
doubl
eamt
,i
d;
Sy
stem.
out
.pr
int
ln(
"Ent
era/
cno,
nameandi
nit
ial
deposi
t:
")
;
ac=sc.
next
Long(
);
nm=sc.
next
();
i
d=sc.
next
Doubl
e()
;
bankb1=newbank(
ac,
nm,
i
d);

whi
l
e(choi
ce<=3)
{
Sy
stem.
out
.pr
int
ln(
"Menu"
);
Sy
stem.
out
.pr
int
ln(
"1.
Deposi
t\
n2.
Wit
hdr
aw\
n3.
Displ
ay\
n4.
Exi
t"
);
Sy
stem.
out
.pr
int
ln(
"Ent
ery
ourchoi
ce:
")
;
choi
ce=sc.
next
Int
();

36
NAME:NI HAL
REGISTERNUMBER:3SU21SA100
swi
tch(choi
ce)
{
case1:
Syst
em.
out
.pr
int
ln(
"Ent
ert
heamountt
obedeposi
ted:
")
;
amt
=sc.
next
Int
();
b1.
deposi
t(
amt
);
br
eak;
case2:
Syst
em.
out
.pr
int
ln(
"Ent
ert
heamountt
obewi
thdr
awn:
")
;
amt
=sc.
next
Int
();
b1.
wit
hdr
aw(
amt
);
br
eak;
case3:
b1.
displ
ay(
);
}}
}}
OUTPUT:

37
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
PROGRAM 6:
Wri
teajavapr
ogram t
odemonst r
atet
heusageoffeat
urepackage.Cr
eat
epackagetocov
ertt
emperature
i
nCenti
gradei
ntoFahrenhei
tandonemorepackagetocal
culat
esimplei
nter
est
.Impl
ementbothpackages
i
nmain.

TEMPCONVERTPACKAGE:
packaget
empconv
ert
;
i
mpor
tjav
a.i
o.*
;
publ
i
ccl
assconv
ert
or
{
publ
i
cvoi
dcent
_fahr
en(
)
{
doubl
ef=0;
doubl
ec=0;
Dat
aInput
Str
eam b=newDat
aInput
Str
eam(
Syst
em.
in)
;

t
ry
{
Sy
stem.
out
.pr
int
("
Ent
ert
het
emper
atur
eincent
igr
ade:
")
;
c=Doubl
e.par
seDoubl
e(b.
readLi
ne(
));
}
cat
ch(
Except
ione)
{}
f
=(c*
9)/
5+32;
Sy
stem.
out
.pr
int
ln(
"Thet
emper
atur
einf
arenhei
tis:
"+f
);
}
publ
i
cvoi
dfah_
cent
()
{
doubl
ef=0;
doubl
ec=0;
Dat
aInput
Str
eam b=newDat
aInput
Str
eam(
Syst
em.
in)
;

38
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
tr
y
{
Sy
stem.
out
.pr
int
("
Ent
ert
het
emper
atur
einf
arenhei
t:
")
;
f
=Doubl
e.par
seDoubl
e(b.
readLi
ne(
));
}
cat
ch(
Except
ione)
{}
c=(
f-
32)
*5/
9;
Sy
stem.
out
.pr
int
ln(
"Thet
emper
atur
eincent
igr
adei
s:"
+c)
;
}
}
SI
MPLEI
NTERESTPACKAGE:
packagecal
cul
ate;
i
mpor
tjav
a.i
o.*
;
publ
i
ccl
asssi
mpl
eint
erest
{
f
loatp,
n,
r,
si
;
publ
i
cvoi
dget
dat
a()
{
Dat
aInput
Str
eam b=newDat
aInput
Str
eam(
Syst
em.
in)
;
t
ry
{
Sy
stem.
out
.pr
int
("
Ent
erpr
inci
pal
:
");
p=Fl
oat
.par
seFl
oat
(b.
readLi
ne(
));
Sy
stem.
out
.pr
int
("
Ent
erTi
mei
nYear
s:"
);
n=Fl
oat
.par
seFl
oat
(b.
readLi
ne(
));
Sy
stem.
out
.pr
int
("
Ent
ert
her
ateofi
nter
est
:"
);
r
=Fl
oat
.par
seFl
oat
(b.
readLi
ne(
));
}

39
NAME:NI HAL
REGISTERNUMBER:3SU21SA100
cat
ch(Except
ione)
{
}
}
publ
i
cvoi
dpr
ocess(
)
{
si
=(p*
n*r
)/100;
}
publ
i
cvoi
dput
dat
a()
{
Sy
stem.
out
.pr
int
ln(
"Thepr
inci
pal
amount
:"
+p)
;
Sy
stem.
out
.pr
int
ln(
"Thenumberofy
ear
sis:
"+n)
;
Sy
stem.
out
.pr
int
ln(
"Ther
ateofi
nter
esti
s:"
+r)
;
Sy
stem.
out
.pr
int
ln(
"Si
mpl
eint
eresti
s:"
+si
);
}
}
MAI
N:
i
mpor
ttempconv
ert
.*;
i
mpor
tcal
cul
ate.
*;
i
mpor
tjav
a.i
o.*
;
cl
asscal
cul
ati
on
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
conv
ert
orc=newconv
ert
or(
);
si
mpl
eint
erests=newsi
mpl
eint
erest
();
Sy
stem.
out
.pr
int
ln(
"Out
putf
ort
emper
atur
e:"
);
c.
cent
_fahr
en(
);
c.
fah_
cent
();

40
NAME:NIHAL
REGISTERNUMBER:3SU21SA100
Syst
em.out
.pr
int
ln(
"Out
putf
orsi
mplei
nter
est
:"
);
s.
get
dat
a()
;
s.
process(
);
s.
put
dat
a()
;
}
}

OUTPUT:

41
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
PROGRAM 7:
Program t
odemonstrat
emul ti
threadingusingrunnablei
nter
face.Def
inethr
eediff
erentthreadst
o
calcul
atesquar
eoffi
rstnintegers,anothertocalcul
atecubeoffir
stnint
egersandthir
dt hreadt
ofi
ndt
he
squarerootoft
hefi
rstnintegers.

i
mpor
tjav
a.i
o.*
;
cl
assSquar
eimpl
ement
sRunnabl
e
{
i
ntn,
i
;
Squar
e(i
ntno)
{
n=no;
}
publ
i
cvoi
drun(
)
{
f
or(
i=1;
i
<=n;
i
++)
Sy
stem.
out
.pr
int
ln(
"Squar
eof"
+i+"i
s"+i
*i
);
}
}
cl
assCubei
mpl
ement
sRunnabl
e
{
i
ntn,
j
;
Cube(
intno)
{
n=no;
}
publ
i
cvoi
drun(
)
{
f
or(
j=1;
j
<=n;
j
++)
Sy
stem.
out
.pr
int
ln(
"Cubeof"
+j+"i
s"+j
*j
*j
);

42
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100
}
}
cl
assSquar
eRooti
mpl
ement
sRunnabl
e
{
i
ntn,
k;
Squar
eRoot
(i
ntno)
{
n=no;
}
publ
i
cvoi
drun(
)
{
f
or(
k=1;
k<=n;
k++)
Sy
stem.
out
.pr
int
ln(
"Squar
eRootof"
+k+"i
s"+Mat
h.sqr
t(k)
);
}
}
cl
assRoot
Thr
ead
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
t
ry
{
Dat
aInput
Str
eam b=newDat
aInput
Str
eam(
Syst
em.
in)
;
Sy
stem.
out
.pr
int
("
Ent
erAnyNumber
:")
;
i
ntn=I
nteger
.par
seI
nt(
b.r
eadLi
ne(
));
Squar
es=newSquar
e(n)
;
Cubec=newCube(
n);
Squar
eRootq=newSquar
eRoot
(n)
;
Thr
eadt
1=newThr
ead(
s);

43
NAME:NI HAL
REGISTERNUMBER:3SU21SA100
Threadt2=newThr
ead(
c);
Thr
eadt
3=newThr
ead(
q);
Sy
stem.
out
.pr
int
ln(
"St
artThr
eadt
1")
;
t
1.st
art
();
Sy
stem.
out
.pr
int
ln(
"St
artThr
eadt
2")
;
t
2.st
art
();
Sy
stem.
out
.pr
int
ln(
"St
artThr
eadt
3")
;
t
3.st
art
();
}

cat
ch(
Except
ione)

{
}
}
}
OUTPUT:

44
NAME:NI
HAL
REGI
STERNUMBER:3SU21SA100

45

You might also like