JAVA - Individual .Assignment
JAVA - Individual .Assignment
VERSI
TY
Col
legeofBusi
nessandEconomi
c
Depar
tmentofMI
S
JavaI
ndi
vidualassi
gnment
Eveni
ngst
udent
NameBi
kil
aTar
iku
I
d,0519/
14
Expl
1, ainhowt
ocr
eat
eCl
assandObj
ectusi
ngJav
acode
I
nJav
a,acl
assi
sabl
uepr
intf
orcr
eat
ingobj
ect
s,andanobj
ecti
s
ani
nst
anceofacl
ass.Her
e'sast
epbyst
epexpl
anat
ionofhow
t
ocr
eat
eacl
assandanobj
ecti
nJav
a,al
ongwi
thexampl
ecode.
St
ep1:
Def
ineaCl
ass
Tocr
eat
eacl
ass,
uset
hecl
asskey
wor
dfol
l
owedbyt
hecl
ass
name.Thecl
asscancont
ainf
iel
ds(
var
iabl
es)andmet
hods
(
funct
ions)t
hatdef
inei
tspr
oper
ti
esandbehav
ior
s.
Ex
ampl
eCl
ass:
Car
publ
i
ccl
assCar{
/
/Fi
elds(
Att
ri
but
es)
pr
ivat
eSt
ri
ngcol
or;
pr
ivat
eSt
ri
ngmodel
;
pr
ivat
einty
ear
;
/
/Const
ruct
or
publ
i
cCar
(St
ri
ngcol
or,
Str
ingmodel
,i
nty
ear
){
t
his.
col
or=col
or;
t
his.
model
=model
;
t
his.
year=y
ear
;
/
/Met
hodt
odi
spl
aycardet
ail
s
publ
i
cvoi
ddi
spl
ayDet
ail
s(){
Sy
stem.
out
.pr
int
ln(
"CarModel
:"+model
);
Sy
stem.
out
.pr
int
ln(
"CarCol
or:
"+col
or)
;
Sy
stem.
out
.pr
int
ln(
"CarYear
:"+y
ear
);
}
St
ep2:
Creat
eanObj
ect
Tocr
eat
eanobj
ectoft
hecl
ass,
uset
henewkey
wor
dfol
l
owedby
t
hecl
assconst
ruct
or.Youcant
henaccesst
heobj
ect
'smet
hods
andf
iel
dsusi
ngt
hedot(
.)oper
ator
.
Ex
ampl
eofCr
eat
inganObj
ect
:
publ
i
ccl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/Cr
eat
inganobj
ectoft
heCarcl
ass
Carmy
Car=newCar
("Red"
,"Toy
otaCamr
y",
2021)
;
/
/Cal
l
ingmet
hodt
odi
spl
aycardet
ail
s
my
Car
.di
spl
ayDet
ail
s()
;
Compl
eteExampl
eCode
Her
e’showt
hecompl
etecodel
ookswheny
ouputbot
hthecl
ass
andt
heobj
ectcr
eat
iont
oget
her
:
/
/Car
.j
ava
publ
i
ccl
assCar{
/
/Fi
elds(
Att
ri
but
es)
pr
ivat
eSt
ri
ngcol
or;
pr
ivat
eSt
ri
ngmodel
;
pr
ivat
einty
ear
;
/
/Const
ruct
or
publ
i
cCar
(St
ri
ngcol
or,
Str
ingmodel
,i
nty
ear
){
t
his.
col
or=col
or;
t
his.
model
=model
;
t
his.
year=y
ear
;
/
/Met
hodt
odi
spl
aycardet
ail
s
publ
i
cvoi
ddi
spl
ayDet
ail
s(){
Sy
stem.
out
.pr
int
ln(
"CarModel
:"+model
);
Sy
stem.
out
.pr
int
ln(
"CarCol
or:
"+col
or)
;
Sy
stem.
out
.pr
int
ln(
"CarYear
:"+y
ear
);
}
/
/Mai
n.j
ava
publ
i
ccl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/Cr
eat
inganobj
ectoft
heCarcl
ass
Carmy
Car=newCar
("Red"
,"Toy
otaCamr
y",
2021)
;
/
/Cal
l
ingmet
hodt
odi
spl
aycardet
ail
s
my
Car
.di
spl
ayDet
ail
s()
;
Ex
planat
ionofKeyComponent
s:
1.Cl
assDef
ini
ti
on:
•publ
i
ccl
assCar
:Thi
sdef
inesapubl
i
ccl
assnamedCar
.
2.Fi
elds:
•pr
ivat
eSt
ri
ngcol
or;
:Thesear
eat
tri
but
esoft
heCarcl
ass.
Theyar
epr
ivat
e,meani
ngt
heycannotbeaccesseddi
rect
lyf
rom
out
sidet
hecl
ass.
3.Const
ruct
or:
•publ
i
cCar
(St
ri
ngcol
or,
Str
ingmodel
,i
nty
ear
):Thi
sisaspeci
al
met
hodusedt
oini
ti
ali
zenewobj
ect
s.I
thast
hesamenameas
t
hecl
ass.
4.Met
hods:
•publ
i
cvoi
ddi
spl
ayDet
ail
s()
:Thi
smet
hodpr
int
soutt
hedet
ail
s
oft
hecar
.
5.Cr
eat
inganObj
ect
:
•Carmy
Car=newCar
("Red"
,"Toy
otaCamr
y",
2021)
;:Thi
sli
ne
cr
eat
esanewi
nst
anceoft
heCarcl
ass.
6.Cal
l
ingMet
hods:
•my
Car
.di
spl
ayDet
ail
s()
;:Thi
scal
l
sthemet
hodt
opr
intt
hecar
's
det
ail
s.
2,
Wri
teJav
acode,
thatcr
eat
eMet
hodandConst
ruct
or,
inv
oke
cr
eat
edmet
hodandConst
ruct
oranddi
spl
ay“
Thi
sisMet
hod”f
or
Met
hodand“
Thi
sisConst
ruct
or”Message.
Cer
tai
nly
!Bel
owi
sasi
mpl
eJav
apr
ogr
am t
hatdef
inesacl
ass
wi
thaconst
ruct
orandamet
hod.Theconst
ruct
ordi
spl
ayst
he
message"
Thi
sisConst
ruct
or"whenanobj
ectoft
hecl
assi
s
cr
eat
ed,
andt
hemet
hoddi
spl
ays"
Thi
sisMet
hod"wheni
nvoked.
publ
i
ccl
assMy
Class{
/
/Const
ruct
or
publ
i
cMy
Class(
){
Sy
stem.
out
.pr
int
ln(
"Thi
sisConst
ruct
or"
);
/
/Met
hod
publ
i
cvoi
dmy
Met
hod(
){
Sy
stem.
out
.pr
int
ln(
"Thi
sisMet
hod"
);
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/Cr
eat
eanobj
ectofMy
Class,
whi
chi
nvokest
he
const
ruct
or
My
Classobj
=newMy
Class(
);
/
/Inv
oket
hemet
hod
obj
.my
Met
hod(
);
Ex
planat
ion:
1.Const
ruct
or:
TheMy
Classconst
ruct
orpr
int
s"Thi
sis
Const
ruct
or"whenani
nst
anceofMy
Classi
scr
eat
ed.
2.Met
hod:
Themy
Met
hod(
)met
hodpr
int
s"Thi
sisMet
hod"when
i
tiscal
l
ed.
3.Mai
nMet
hod:
Int
hemai
nmet
hod,
wecr
eat
eani
nst
anceof
My
Class,
whi
chcal
l
stheconst
ruct
or,
andt
henwecal
l
my
Met
hod(
)todi
spl
ayt
her
espect
ivemessages.
Howt
oRun:
1.Copyt
hecodei
ntoaf
il
enamedMy
Class.
jav
a.
2.Compi
l
ethecodeusi
ngj
avacMy
Class.
jav
a.
3.Runt
hecompi
l
edcl
assusi
ngj
avaMy
Class.
Youshoul
dseet
hef
oll
owi
ngout
put
:
Thi
sisConst
ruct
or
Thi
sisMet
hod
3,
Expl
ainmet
hodOv
erl
oadi
ngandmet
hodOv
err
idi
ng,
showwi
th
ex
ampl
eCer
tai
nly
!Met
hodOv
erl
oadi
ngandMet
hodOv
err
idi
ngar
e
t
woi
mpor
tantconcept
sinObj
ectOr
ient
edPr
ogr
ammi
ng(
OOP)
.
Theyhel
pachi
evepol
ymor
phi
sm,
all
owi
ngmet
hodst
obehav
e
di
ff
erent
lybasedont
hei
rcont
ext
.
Met
hodOv
erl
oadi
ng
Def
ini
ti
on:
Met
hodOv
erl
oadi
ngoccur
swhent
woormor
e
met
hodsi
nthesamecl
asshav
ethe
samenamebutdi
ff
erentpar
amet
ers(
dif
fer
entt
ype,
number
,or
bot
h).I
tal
l
owsacl
asst
oper
for
m si
mil
aroper
ati
onswi
th
di
ff
erentt
ypesofi
nput
.
Ex
ampl
e:
cl
assMat
hOper
ati
ons{
/
/Met
hodt
oaddt
woi
nteger
s
i
ntadd(
inta,
intb){
r
etur
na+b;
/
/Met
hodt
oaddt
hreei
nteger
s
i
ntadd(
inta,
intb,
intc){
r
etur
na+b+c;
/
/Met
hodt
oaddt
wodoubl
eval
ues
doubl
eadd(
doubl
ea,
doubl
eb){
r
etur
na+b;
publ
i
ccl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Mat
hOper
ati
onsmat
h=newMat
hOper
ati
ons(
);
Sy
stem.
out
.pr
int
ln(
"Sum of2and3:
"+mat
h.add(
2,3)
);/
/
Cal
l
sthef
ir
stmet
hod
Sy
stem.
out
.pr
int
ln(
"Sum of1,
2,and3:
"+mat
h.add(
1,2,
3))
;
/
/Cal
l
sthesecondmet
hod
Sy
stem.
out
.pr
int
ln(
"Sum of2.
5and3.
5:"+mat
h.add(
2.5,
3.
5))
;//Cal
l
sthet
hir
dmet
hod
Met
hodOv
err
idi
ng
Def
ini
ti
on:
Met
hodOv
err
idi
ngoccur
swhenasubcl
asspr
ovi
desa
speci
fi
cimpl
ement
ati
onofamet
hodt
hati
sal
readydef
inedi
nit
s
super
class.I
tal
l
owsasubcl
asst
omodi
fyt
hebehav
iorofan
i
nher
it
edmet
hod.
Ex
ampl
e:
cl
assAni
mal
{
v
oidsound(
){
Sy
stem.
out
.pr
int
ln(
"Ani
mal
makesasound"
);
}
cl
assDogext
endsAni
mal
{
Ov
err
ide
v
oidsound(
){
Sy
stem.
out
.pr
int
ln(
"Dogbar
ks"
);
cl
assCatext
endsAni
mal
{
Ov
err
ide
v
oidsound(
){
Sy
stem.
out
.pr
int
ln(
"Catmeows"
);
publ
i
ccl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Ani
mal
myDog=newDog(
);
Ani
mal
myCat=newCat
();
my
Dog.
sound(
);/
/Cal
l
sDog'
sov
err
iddenmet
hod
my
Cat
.sound(
);/
/Cal
l
sCat
'sov
err
iddenmet
hod
}
4,
Expl
ainmaj
ordi
ff
erencesandsi
mil
ari
tybet
weenConst
ruct
or
andMet
hodi
nJav
a
I
nJav
a,bot
hconst
ruct
orsandmet
hodsar
eessent
ial
component
sofcl
asses,
butt
heyser
vedi
ff
erentpur
posesand
hav
edi
sti
nctchar
act
eri
sti
cs.Her
ear
ethemaj
ordi
ff
erencesand
si
mil
ari
ti
esbet
weent
hem:
Maj
orDi
ff
erences
1.Pur
pose:
•Const
ruct
or:
Aconst
ruct
ori
susedt
oini
ti
ali
zeanobj
ectwhen
i
tiscr
eat
ed.I
tset
supt
hei
nit
ial
stat
eoft
heobj
ect
.
•Met
hod:
Amet
hodi
susedt
odef
inebehav
ior
soract
ionst
hat
anobj
ectcanper
for
m af
teri
thasbeencr
eat
ed.
2.Name:
•Const
ruct
or:
Thenameofaconst
ruct
ormustbet
hesameas
t
hecl
assname.
•Met
hod:
Amet
hodcanhav
eanyname,
asl
ongasi
tfol
l
ows
Jav
a'snami
ngconv
ent
ions.
3.Ret
urnTy
pe:
•Const
ruct
or:
Const
ruct
orsdonothav
ear
etur
nty
pe,
notev
en
v
oid.
•Met
hod:
Met
hodsmusthav
ear
etur
nty
pe(
whi
chcanbeany
dat
aty
pe,
incl
udi
ngv
oidi
fnov
aluei
sret
urned)
.
4.I
nvocat
ion:
•Const
ruct
or:
Aconst
ruct
ori
scal
l
edaut
omat
ical
l
ywhenan
obj
ectoft
hecl
assi
sinst
ant
iat
edusi
ngt
henewkey
wor
d.
•Met
hod:
Amet
hodmustbecal
l
edexpl
i
cit
lyonanobj
ectaf
ter
i
thasbeencr
eat
ed.
5.Ov
erl
oadi
ng:
•Const
ruct
or:
Const
ruct
orscanbeov
erl
oaded,
meani
ngy
ou
canhav
emul
ti
pleconst
ruct
orsi
nacl
asswi
thdi
ff
erentpar
amet
er
l
i
sts.
•Met
hod:
Met
hodscanal
sobeov
erl
oadedi
nthesameway
.
6.I
nher
it
ance:
•Const
ruct
or:
Const
ruct
orsar
enoti
nher
it
ed.Eachcl
assmust
def
inei
tsownconst
ruct
ors.
•Met
hod:
Met
hodscanbei
nher
it
edf
rom par
entcl
assesand
canbeov
err
iddeni
nchi
l
dcl
asses.
Si
mil
ari
ti
es
1.Bel
ongi
ngt
oCl
asses:
Bot
hconst
ruct
orsandmet
hodsar
e
def
inedwi
thi
nacl
assandar
eusedt
ooper
ateonobj
ect
soft
hat
cl
ass.
2.CanBeOv
erl
oaded:
Bot
hcanbeov
erl
oaded,
all
owi
ngmul
ti
ple
v
ersi
onswi
thdi
ff
erentpar
amet
erswi
thi
nthesamecl
ass.
3.AccessModi
fi
ers:
Bot
hconst
ruct
orsandmet
hodscanhav
e
accessmodi
fi
ers(
li
kepubl
i
c,pr
ivat
e,pr
otect
ed,
etc.
)thatcont
rol
t
hei
rvi
sibi
l
ity
.
4.Par
amet
ers:
Bot
hcanacceptpar
amet
ers,
all
owi
ngf
ordy
nami
c
behav
iorbasedoni
nputv
alues.
5.Execut
ionCont
ext
:Bot
hoper
atei
nthecont
extofani
nst
ance
ofacl
ass(
orast
ati
ccont
extf
orst
ati
cmet
hods)
,al
l
owi
ngt
hem
t
oaccessi
nst
ancev
ari
abl
esandot
hermet
hods
5,
Wri
teapr
ogr
am t
hatdi
spl
ayt
heNameofSt
udentandGr
ade
f
rom t
hest
udentcl
assbycr
eat
ingt
hepar
amet
eri
zedconst
ruct
or
t
hathol
dtwov
aluesandcr
eat
e“Di
spl
ay”met
hodt
odi
spl
ay
v
alues.
Cer
tai
nly
!Bel
owi
sasi
mpl
ePy
thonpr
ogr
am t
hatdef
inesa
St
udentcl
asswi
thapar
amet
eri
zedconst
ruct
ort
ohol
dthename
andgr
adeofast
udent
.Ital
soi
ncl
udesaDi
spl
aymet
hodt
oshow
t
hesev
alues.
cl
assSt
udent
:
def_
_ini
t__
(sel
f,name,
gra
e)
:
"
""
Par
amet
eri
zedconst
ruct
ort
oini
ti
ali
zenameandgr
ade.
""
"
sel
f.
name=name
sel
f.
grade=gr
ade
defDi
spl
ay(
sel
f)
:
"
""
Met
hodt
odi
spl
ayt
hest
udent
'
snameandgr
ade.
""
"
pr
int
(f"
Name:
{sel
f.
name}
,Gr
ade:
{sel
f.
grade}
")
#Exampl
eusage:
i
f__
name_
_=="
__mai
n__
":
#Cr
eat
einst
ancesoft
heSt
udentcl
ass
st
udent
1=St
udent
("
Ali
ce"
,"A"
)
st
udent
2=St
udent
("
Bob"
,"B"
)
#Di
spl
ayt
hei
nfor
mat
ionofeachst
udent
st
udent
1.Di
spl
ay(
)
st
udent
2.Di
spl
ay(
)
Ex
planat
ion:
1.Cl
assDef
ini
ti
on:
Wedef
ineaSt
udentcl
ass.
2.Const
ruct
or:
The_
_ini
t__met
hodi
stheconst
ruct
ort
hat
i
nit
ial
i
zest
henameandgr
adeat
tri
but
eswhenanewobj
ectof
St
udenti
scr
eat
ed.
3.Di
spl
ayMet
hod:
TheDi
spl
aymet
hodpr
int
sthest
udent
'sname
andgr
adei
naf
ormat
tedmanner
.
4.Exampl
eUsage:
Int
hemai
nbl
ock,
wecr
eat
einst
ancesoft
he
St
udentcl
assandcal
ltheDi
spl
aymet
hodt
oshowt
hei
r
i
nfor
mat
ion.
Howt
oRun:
Youcancopyt
hiscodei
ntoaPy
thonenv
ironment(
li
keI
DLE,
Py
Char
m,oranyonl
i
nePy
thoncompi
l
er)andr
uni
ttoseet
he
out
put
.Theexpect
edout
putwi
l
lbe:
Name:
Ali
ce,
Grade:
A
Name:
Bob,
Grade:
B
6,
Whati
swr
ongwi
tht
hef
oll
owi
ngsampl
ecode?Rewr
it
esampl
e
codecor
rect
ly?
cl
assAni
mal
{
voi
deat
(){
Syst
em.
out
.pr
int
ln(
"eat
ing.
..
")
;}
cl
assDogext
endsAni
mal
{
voi
dbar
k()
{Syst
em.
out
.pr
int
ln(
"bar
king.
..
")
;}
cl
assTest
Inher
it
ance{
publ
icst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
Dogd=newDog(
);
d.
bar
k()
;
d.
eat
();
}
}