You are on page 1of 4

Pr

ogr
ammi
ngi
nJAVA
Assi
gnment 2 Duedat
e:
30.
09.2019

1.Createaclassnamed' Student'wi
thStringvari
able'
name'andint
eger
vari
able'r
oll
_no'.Assi
gnt hevalueofrol
l_noas'2'andthatofnameas"J
ohn"by
creat
inganobjec tofthec l
assStudent.

2. 
Ass
ignandpr
intt
her
ollnumber
,phonenumberandaddr
essoft
wo
st
udentshavi
ngnames"
Sam"and"
John"r
espec
tiv
elybyc
reat
ingt
woobj
ect
s
ofcl
ass'St
udent
'.

3.Wr
iteapr
ogr
am t
opr
intt
hear
eaandper
imet
erofat
riangl
ehav
ings
idesof3,
4and5unit
sbyc
reat
ingac
las
snamed'
Tri
angl
e'wi
thoutanypar
amet
eri
nit
s
cons
truc
tor
.

4.Wr
iteapr
ogr
am t
opr
intt
hear
eaoft
wor
ect
angl
eshav
ings
ides(
4,5)and(
5,8)
respec
tiv
elybycreat
ingacl
assnamed'
Rectangl
e'wit
hamethodnamed'
Area'
whichretur
nstheareaandl
engthandbr
eadthpassedaspar
amet
erstoi
ts
constr
uctor.

5.Addt
wodi
stanc
esi
ninc
h-f
eetbyc
reat
ingac
las
snamed'
AddDi
stanc
e'.

6.Wr
iteaJ
avacl
ass
 Aut
hor
 wi
thf
oll
owi
ngf
eat
ures
:

 I
nst
anc
evar
iabl
es:
o f
irs
tName 
fort
heaut
hor
’sf
irs
tnameoft
ypeSt
ring.
o l
ast
Name 
fort
heaut
hor
’sl
astnameoft
ypeSt
ring.
 Cons
truc
tor
:
o publ
icAut
hor(
Str
ingf
irs
tName,St
ringl
ast
Name)
:Ac
ons
truc
torwi
thpar
amet
ers
,
itcr
eatestheAuthorobjec
tbys et
tingthetwof i
eldstothepassedv al
ues
.
 Ins
tancemethods:
o publi
cvoidsetFir
stName(Stringfi
rstName):Usedtos etthefi
rstnameofauthor.
o publi
cvoidsetLastName(Str
inglastName):Usedt osetthelas
tnameofaut hor.
o publi
cdoublegetFirst
Name():Thi
smet hodreturnsthefir
stnameoft heaut
hor.
o publ
icdoubl
eget
Las
tName(
):Thi
smet
hodr
etur
nst
hel
astnameoft
heaut
hor
.
o publ
icSt
ringt
oSt
ring(
):Thi
smet
hodpr
int
edoutaut
hor
’snamet
othes
creen
Wr
iteaJ
avac
las
s Book 
wit
hfol
lowi
ngf
eat
ures
:

 I
nst
anc
evar
iabl
es:
o t
itl
e f
ort
het
itl
eofbookoft
ypeSt
ring.
o aut
hor
 fort
heaut
hor
’snameoft
ypeSt
ring.
o pr
ice 
fort
hebookpr
iceoft
ypedoubl
e.
 Cons
truc
tor
:
o publ
icBook(
Str
ingt
itl
e,Aut
horname,doubl
epr
ice)
:Ac
ons
truc
torwi
th
paramet ers,i
tc reatestheAut horobj ectbys et t
ingt hethef i
eldstothepas s
ed
values.
 Instanc emet hods:
o pu bl i
cvoids etTitl
e(St r
ingtitl
e):Us edt os ett het itl
eofbook.
o pu bl i
cvoids etAut hor(Stri
ngaut hor ):Us edt os ett henameofaut horofbook.
o pu bl i
cvoids etPrice(doubleprice):Us edt os ett hepr i
ceofbook.
o pu bl i
cdoubl eget Titl
e():Thismet hodr etur nst het itl
eofbook.
o pu bl i
cdoubl eget Aut hor()
:Thismet hodr etur nst heaut hor’
snameofbook.
o pu bl i
cSt r
ingt oString():Thi
smet hodpr intedoutbook’ sdet ail
stothescreen
Wr i
teas eparatec l
ass BookDemo  witha mai n() met hodc reatesaBookt itl
ed“Dev el
opi
ng
JavaSoftwar e”withaut horsRus selWi nder andpr i
c e79. 75.Print
st heBook’sst
ring
repres
ent ati
ont os t
andar dout put(usingSy stem. out .
println)
.

7.Consi
dert
hef
oll
owi
ngc
las
s:

publi
cclassI
denti
fyMy
Par
ts{
publ
icstat
ici
ntx=7;
publ
icinty=3;
}

a. Whatar
ethec
las
svar
iabl
es?
b. Whatar
ethei
nst
anc
evar
iabl
es?
c
. Whati
stheout
putf
rom t
hef
oll
owi
ngc
ode:

Ident
ify
MyPar
tsa=newI
dent
ify
MyPar
ts(
);
Ident
ify
MyPar
tsb=newI
dent
ifyMy
Par
ts(
);
a.y=5;
b.y=6;
a.x=1;
b.x=2;
Sy
stem.
out
.pr
int
ln(
"a.y="+a.y
);
Sy
stem.
out
.pr
int
ln(
"b.y="+b.y)
;
Sy
stem.
out
.pr
int
ln(
"a.x="+a.x
);
Sy
stem.
out
.pr
int
ln(
"b.x="+b.x)
;
Sy
stem.
out
.pr
int
ln(
"Ident
ify
MyPart
s.x="+I
dent
ify
MyPar
ts.
x);

8.Compl
etet
heI
mpl
ement
ati
onofgi
venFr
act
ionc
las
s

cl
assFr
act
ion
{
 
pr
ivat
eintn,d;/
/dat
amember
stos
tor
ethenumer
atoranddenomi
nat
or 
publ
icFr
act
ion(
intx,i
nty)/
/cons
truc
tort
oini
ti
ali
zeFr
act
ionobj
ect
s
{
/
/ini
ti
ali
zen&dus
ingx&y.
 
/
/ift
heval
ueofyi
s0,di
ssett
o1.
}
publ
icvoi
ddi
spl
ay(
)//
met
hodt
odi
spl
ayt
heval
ueofaf
ract
ion
{
/
/di
spl
ayt
heval
ueofaf
ract
ioni
nn/
dfor
mat
.
}
publ
icbool
eani
sPr
oper
()/
/met
hodt
ochec
kwhet
heraf
ract
ioni
spr
operornot
{
/
/Af
ract
ioni
spr
operi
fit
sn<d.
}
publ
icbool
eani
sEqui
val
ent
(Fr
act
ionf
)//
met
hodt
ochec
kwhet
hert
wof
rac
tionsar
eequi
val
ent
ornot
{
/
/Twof
ract
ionsar
eequi
val
enti
ftheyr
epr
esents
amev
aluee.
g1/
2and2/
4ar
eequi
val
ent
f
ract
ions
.
}
}

9.Compl
etet
hei
mpl
ement
ati
onofAc
countc
las
s
cl
assAcc
ount

pri
v ateSt ri
ngname;/ /dat amembert os tor et henameofanac count.
pri
v atedoubl ebal anc e;/ /dat amembert os torethebal anc eofanac count.
publ i
cAc count (Stri
ngn,doubl eb)/ /cons t ruc tortoiniti
alizeAc countobj
ects
{
//i
ni t
ial
izet hedat amember soft hec urrentAc countobj ectus i
ngt heparameters

}
publ i
cv oiddisplay (
)/ /met hodt odi splayt hedet ail
sofanAc countobject
{
//Ac countdet ailsshoul dbedi splayedi nt hef oll
owingf ormat :
//Name:Ac c ountName
//Balanc e:Ac countBal anc e
}
publ i
cdoubl eget Bal anc e()/ /met hodt or et urnt hev alueofbal anceofanAc count
object
{
//Valueoft hebal anc eoft heinv okingAc c ountobj ec tisreturned.
}
publ i
cv oiddepos i
t(doubl eamount )// met hodt odepos itamountt oanaccount
{
//I
ft heamounti snotnegat i
ve,Itisaddedt ot hebal ance. 
}
publ i
cv oidwi t
hdr aw( doubl eamount )/ /
met hodt owi thdrawamountf rom anaccount
{
//I
ft heamounti s≤bal anc e,Itiss ubtract edf rom thebal ance. 
}
}

10.
Expl
aint
ypesofI
nher
itanc
ewi
thpr
ogr
ammi
ngex
ampl
e.

You might also like