Assignment Answers
Assignment Answers
--
--
--
---
DAY1
--
--
--
---
--
-
1.cl
assTest er{
publicstaticv oi
dmai n(
Str
ing[
]ar
gs){
Sy stem. out
.pr
int
ln(
"My name is SonakshiGupt
a.\
n My desi
gnat
ion i
s Sy
stem
Engineer."
);
//I mplementy ourcodehere
}
}
2.cl
assTest er{
publ
i
cst at
icv oi
dmai n(
Str
ing[
]ar
gs){
Sy stem.out.pri
ntln(
"Houseno:761\ nWar dNo.37\
nCi
ty:Jammu\
nPi
ncode:
180007")
;
//I mplementy ourcodehere
}
}
-
---
--
--
--
-
DAY2
-
---
--
--
--
-
1.cl
assTest er{
publ
i
cst at
icv oi
dmai n(
Str
ing[
]ar
gs){
intpr i
ncipal=5000;
floatrate=10. 0f;
by teti
me=5;
floatinterest=0.0f;
Sy stem.out.pri
ntln(
(pri
nci
pal
*rat
e*t
ime)
/100);
//I mplementy ourcodehere
}
}
2.cl
assTester{
publ
icst at
icv oi
dmai n( Str
ing[
]args){
i
ntpr i
ncipal=5000;
fl
oatrate=10. 0f;
byteti
me=5;
fl
oatinterest=0.0f ;
System.out.pri
ntln((pr
inci
pal*r
ate*
time)
/100)
;
i
ntpr i
n=3250;
fl
oatr=7. 0f;
bytet=3;
fl
oatSinterest=0. 0f;
System.out.pri
ntln((pr
in*r
*t)
/100);
/
/Impl
ementy
ourcodeher
e
}
}
3.cl
assTester{
publ
icst at
icvoidmai n(Str
ing[]ar
gs){
byterad=4;
fl
oatarea=(fl
oat)(3.14*rad*r
ad);
System.out.
printl
n(area);
byteradi
us=10;
fl
oatar=(f
loat)(3.14*radi
us*radi
us)
;
System.out.
printl
n(ar);
/
/Impl
ementy
ourcodeher
e
}
}
4.cl
assTester{
publ
icstaticvoi
dmai n(Str
ing[]ar
gs){
fl
oatfahr=32;
fl
oatfar=50;
fl
oatcels=(fl
oat)
(((f
ahr-32)/
9)*5)
;
fl
oatcel=(fl
oat)
(((
far-32)/
9)*
5);
System.out.
pri
ntl
n(cels);
System.out.
pri
ntl
n(cel);
}
}
-
---
--
--
--
-
DAY3
-
---
--
--
-
1.cl
assTest er{
publ
icst ati
cvoidmai n(Str
ing[]ar
gs){
i
ntnum1=3;
i
ntnum2=4;
i
ntnum3=1;
i
f(num1>num2)
{
i
f(num1>num3)
{System.out.
print
ln(num1+"i sthemaxi
mum number
");
}
else
{System.out.
print
ln(num3+"i sthemaxi
mum number
");
}
}
else{
{System.out.
printl
n(num2+"isthemax
imum number"
);
}
}
}
}
2.REVERSEOFANUMBER
--
--
--
--
-
cl
assTest er{
publ
i
cst ati
cvoidmain(
Str
ing[
]ar
gs){
intinput
Number=7865;
intsumOf Di
git
s=0;
inttemp=0;
whi
l
e( i
nput
Number>0){
temp=inputNumber%10;
sumOfDigi
ts=sumOfDi
git
s*10+t
emp;
i
nputNumber=input
Number/10;
}
Sy
stem.
out
.pr
int
ln(
"Sum ofdi
git
sar
e:"+sumOf
Digi
ts)
;
}
}
whi
l
e( i
nput
Number>0){
temp=inputNumber%10;
sumOfDigi
ts=sumOfDi
git
s*10+t
emp;
i
nputNumber=input
Number/10;
}
Sy
stem.
out
.pr
int
ln(
"Sum ofdi
git
sar
e:"+sumOf
Digi
ts)
;
}
}
4.INFINI
TEWHI LELOOP
--
--
--
--
-
cl
assTest er{
publ
icstat
icvoidmain(St
ri
ng[
]args){
inttot
alCost=0;
charwant ToAddFoodI
tem ='
Y';
intunit
Pri
ce=10;
intquanti
ty=1;
whil
e(wantToAddFoodItem ==' Y'
){
tot
alCost=totalCost+( quanti
ty*unitPri
ce) ;
System.out.
pri
ntln(
"Orderplacedsuccessf ull
y")
;
System.out.
pri
ntln(
"Totalcost:"+totalCost);
System.out.
pri
ntln(
"Doy ouwantt oaddonemor efoodit
em t
otheor
der?"
);
}
Syst
em.out.
print
ln(
"Thanky ouforor der
ingthef ood!Itwi
ll
reachy
oushor
tly
..
."
);
}
}
--
--
--
--
-
TREATINGI NINITEWHI LELOOP
--
--
--
--
--
-
cl
assTest er{
publ
i
cst aticvoidmain(St
ri
ng[
]args){
inttotalCost=0;
charwant ToAddFoodI
tem ='
Y';
intunitPri
ce=10;
intquant i
ty=1;
whil
e(wantToAddFoodI t
em ==' Y'
){
tot
alCost=totalCost+( quanti
ty*unitPri
ce) ;
System.out.
pri
ntln("
Orderplacedsuccessf ull
y")
;
System.out.
pri
ntln("
Totalcost:"+totalCost);
System.out.
pri
ntln("
Doy ouwantt oaddonemor efoodit
em t
otheor
der?"
);
wantToAddFoodI tem ='N';
}
Syst
em.out.
print
ln(
"Thanky ouforor der
ingthef ood!Itwi
ll
reachy
oushor
tly
..
."
);
}
}
do{
temp=inputNumber%10;
sumOfDigi
ts+=temp;
i
nputNumber=input
Number/10;
}whi
le(i
nput
Number>0) ;
Sy
stem.
out
.pr
int
ln(
"Sum ofdi
git
s:"+sumOf
Digi
ts)
;
}
}
6.FORLOOPEXECUTI ON
--
--
--
--
-
cl
assCust omer{
publ
i
cst aticvoidmain(Stri
ng[]args){
//Thebel owcodegener atescustomer Id
inttotalNoOfCustomer s=12;
Stri
ngcust omerI
d=" ";
for(intcounter=1; counter<=t ot
alNoOfCust
omers;
counter++){
i
f(counter<=9)
customerI
d=" C0"+count er;
el
se
customerI
d=" C"+count er ;
System.out.
pri
ntln(
"CustomerI dforcust
omer"+counter+"is"
+customerId);
}
}
}
7.NESTEDFORLOOP| |PATTERNFORMATI ON
--
--
--
--
-
cl
assTester{
publ
i
cstati
cv oidmai n(
Stri
ng[ ]args){
for(
introw=1; r
ow<=4; row++){
for( i
ntvalue=1; value<=5; value++){
System.out.
print(value+"");
}
Sy stem.out.
print
ln();
}
}
}
--
--
--
--
--
--
--
cl
assTest er{
publ
icstati
cvoidmain(Str
ing[ ]args){
for(intr
ow=1; row<=4; row++){
for(i
ntval
ue=1; value<=r ow;
value++){
Syst
em. out
.print(value+"");
}
Sy
stem.
out
.pr
int
ln(
);
}
}
}
--
--
--
--
--
--
--
--
-
8.FACTORI ALOFANUMBER
--
--
--
--
--
--
cl
assTest er{
publ
icstat i
cv oi
dmai n(Str
ing[
]ar
gs){
inta=5;
inttemp=1;
whi le(a!=0)
{
temp=t emp* a;
a--;
}
Sy stem. out
.pr
int
ln(temp);
//I mpl ementyourcodeher e
}
}
9.GEOMETRI CSEQUENCE
--
--
--
--
--
--
--
--
----
-
cl
assTest er{
publ
icstat i
cv oi
dmai n(
Stri
ng[
]args){
inta=1, r=2,n=5;
intz =1;
for(inti=1;i
<=n;
i++)
{
Sy stem. out.
pri
ntl
n(z)
;
intx=(int)
Math.pow(
r,
i)
;
z=a* x ;
}
}
}
-
---
--
--
--
---
---
--
---
--
--
--
-
*SELECTI ONCONTROLSTRUCTUREASSI GNMENT*
-
---
--
--
--
---
--
1.cl
assTest er{
publi
cst ati
cvoidmain(
Str
ing[
]ar
gs){
inta=5,b=5;
intsum=0;
if(
a==b)
{sum=a+b;
}
el
se{
sum=2* (
a+b);
}
System.out.pr
int
ln(
sum);
}
}
-
---
--
--
--
--
--
--
--
-
2.cl
assTester{
publ
icstat
icv
oidmai
n(St
ri
ng[
]ar
gs){
i
nta=1, b=4, c=6;
fl
oatdis=0f;
fl
oatx1=0f ;
fl
oatx2=0f ;
di
s=( fl
oat)((
b* b)
-4*a*c);
Syst
em. out.print
ln(
dis);
i
f(di
s==0)
{
System. out.
pri
ntln("
thevalueofrootsareequal:"
);
}
el
sei f
(dis>0)
{
System. out.
pri
ntln("
thevalueofrootsareunequal:
");
}
el
se{
System. out.
pri
ntln("
thevalueofrootsarenotreal
:")
;
}
x1=(f
loat
)((
-b+dis)
/(2*a)
);
x2=(f
loat
)((
-b-
dis)/
(2*a))
;
Syst
em.out.
print
ln(
x1);
Syst
em.out.
print
ln(
x2);
}
}
-
---
--
--
--
--
--
3.cl
assTest er{
publ
icstaticvoidmain(Str
ing[
]ar
gs){
inta=2, b=6,c=7;
intx=0;
if(
a==7)
{
x=b*c;
}
elsei f
(b==7)
{
x=c;
}
elsei f
(c==7)
{
x=-1;
}
else
{
x=a*b*c;
}
Sy stem.out.
print
ln(
x);
}
}
-
---
--
--
---
--
-
4.cl
assTest er{
publi
cstati
cvoidmain(
Str
ing[
]ar
gs){
charfoodType='
N';
i
ntquan=0;
i
ntdist=1;
i
ntm=di st
;
i
ntout put =0;
i
ntt otal=0;
i
ntex tr
a=0;
i
ntex tr
a1=1;
i
ntex tr
a2=2;
i
ntcost =0;
i
ntchar ge=0;
i
ntsona=0;
i
f(foodTy pe==' N')
{
cost =15;
}
el
se{
cost =12;
}
i
f(quan>=1)
{
total=cost *quan;
Syst em. out.pr
intln("
tot al
costi s$"+tot al
);
}
el
se{
Syst em. out.pr
intln("
Inv al
idselecti
on");
output=- 1;
Syst em. out.pr
intln("
tot al
bi l
lamountis:"+out put);
Syst em. out.pr
intln("
or dercannotbepl aced") ;
}
i
f(dist>=1)
{
i
f(di st<=3)
{
Syst em. out.pr
intln("
nodel i
verycharges");
}
el
sei f(
dist<=6)
{
System. out.pr i
ntl
n("Additional deli
ver
ychar gesf or$1perkm" )
;
extra=di st *
ext ra1;
}
el
sei f(
dist<=m)
{
System. out.pr i
ntl
n("Additional deli
ver
ychar gesof$3and$2perkm" )
;
extra=3+( (
di st-
6)*extra2);
}
output =t otal+ex tr
a;
System. out.pr i
ntl
n("youror derhasbeensuccessf ull
yplacedwort
h$"+out
put
);
}
el
se
{
Syst em. out.pr
intln("
inv al
idselecti
on"+"or dercannotbeplaced"
);
}
}
}
-
---
--
--
--
--
--
--
--
--
5.cl
assTest er{
publ
icstati
cvoi
dmai n(
Str
ing[
]ar
gs){
intaccnmbr=1001;
i
ntaccbal =250000;
i
ntsalary=40000;
St
ri
ngl oant y
pe=" Car"
;
i
ntloanAmEx pected=300000;
i
ntemi s=30;
i
ntcount =0;
i
nteli
gibleloanAmount =500000;
i
nteli
gibleEmi s=36;
i
nta=accnmbr ;
whi
le(a!=0)
{
a=a/ 10;
count++;
}
i
f(count ==4)
{
intfirstdi git=accnmbr /1000;
if(fi
rst digit
==1)
{
Sy st em. out .
println("Youcanpr oceedf urther");
}
}
else{
Sy stem. out.println("LOANWI LLNOTBEPROVI DED! !!
")
;
}
i
f(accbal >=1000)
{
Sy stem. out.println("Youar eeli
gibl
ef orloan!"
);
}
else{
Sy stem. out.println("LOANWI LLNOTBEPROVI DED! !!
")
;
}
Sy stem. out .
pr i
ntln("Checki ngwhet herthebankappr ov
esf orloan"
);
if(
loanAmExpect ed<=el i
gibl
eloanAmount )
{
if(emi s<=el igibleEmi s)
{
Sy stem. out.println(
"LOANAPPROVED! !
!"
);
Sy stem. out.println(
"Accountnumber :"+accnmbr );
System. out .pr i
ntln("Loant ype: "+loantype);
System. out .pr i
nt l
n("Eligibleloanamount :"+eligibl
eloanAmount );
System. out .pr i
nt l
n("Loanamountr equested:"+l oanAmEx pected);
System. out .pr i
nt l
n("Numberofel i
gibl
eEMI s:"+el i
gibleEmi s)
;
System. out .pr i
nt l
n("Numberofr equestedEMI s:"+emi s);
}
}
else{
Sy st em. out .
println("BANKCANNOTPROVI DELOANTOYOURACCOUNT!!
!"
);
}
}
}
-
--
--
--
--
--
--
--
-
6. cl
assTest
er{
publi
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
i
ntx=2,
y=4,z=21;
i
ntn1=0;
i
ntn5=0;
f
or(
inti
=1; i
<=y;i
++)
{
if
(z>=5)
{
n5=z/5;
z=z-(
n5*5)
;
}
}
f
or(
inti
=1;
i<=x;
i++)
{
i
f(
z>=1)
{
n1=z/1;
z=z-
n1;
}
}
i
f(
n1>x)
{
Syst
em.out.
pr i
ntl
n( "-
1")
;
}
el
se{
Syst
em.out.
pr i
nt("$1not esneeded=")
;
Syst
em.out.
pr i
ntl
n( n1);
i
f(
n5>y)
{
System.out.printl
n("-
1");
}
el
se{
System. out.pr
int(
"$5notesneeded="
);
System.out.printl
n(n5);
}
}
}
}
-
---
--
--
--
--
--
--
---
--
---
----
---
--
--
--
--
--
--
---
--
--
-
7.cl
assTest er{
publ
icstaticv oidmain(Str
ing[
]ar
gs){
intday =1;
intmont h=9;
intyear =15;
if(
mont h==1||month==3||mont
h==5|
|mont
h==7|
|mont
h==8|
|mont
h==10)
{
if(
day ==31)
{
day =1;
mont h=mont h+1;
}
else{
day =day+1;
}
}
elsei f
(mont h==2)
{
i
f(
((year%4==0)&&( year%100!=0)
)| |(y
ear%400==0)
)
{
System.out.pri
ntln(
"Leapyear"
);
i
f(day==29)
{
day=1;
mont h=mont h+1;
}
el
se{
day=day +1;
}
}
el
se{
if
(day==28)
{
day=1;
mont h=mont h+1;
}
else{
day=day +1;
}
}
}
el
sei f
(mont h==4||mont h==6|
|month==9||
month==11)
{
i
f(day==30)
{
day
=1;
month=mont
h+1;
}
el
se{
day =day +1;
}
}
else
{
i
f(day ==31)
{
day=1;
mont h=1;
year=y ear
+1;
}
else
{
day =day+1;
}
}
Sy
stem.
out
.pr
int
(day+"-
");
Sy
stem.
out
.pr
int
(month+"-"
);
Sy
stem.
out
.pr
int
("20"+y
ear);
}
}
-
---
--
--
--
--
--
--
---
--
---
---
--
--
--
--
--
--
--
--
---
--
--
--
---
---
--
---
--
--
--
8.cl
assTest er{
publ
icstaticvoidmain(Str
ing[]args){
intn=3;
if(
n%3==0&&n%5==0)
{
System.out
.pr
intl
n("Zoom" );
}
elsei f
(n%5==0)
{
System.out
.pr
intl
n("Zap");
}
elsei f
(n%3==0)
{
System.out
.pr
intl
n("Zip")
;
}
else
{
System.out
.pr
intl
n("I
nv al
id")
;
}
}
}
-
---
--
--
--
--
--
--
---
*I
TERATIONCONTROLSTRUCTURE*
-
---
--
--
--
--
-
1.cl
assTest er{
publ
icstat i
cv oidmai n(String[
]ar
gs){
inta=46763;
intn=a;
intrem=0;
intquo=0;
whi l
e( a!=0)
{
rem=a%10;
quo=( quo* 10)+rem;
a=a/ 10;
}
Sy stem. out.print
ln(
"Rev er
seofgi
vennumberi
s:"+quo);
if(
quo==n)
{
Sy stem. out.
pri
ntln(n+"isapal
li
ndr
omenumber")
;
}
else{
System. out.
printl
n(n+"i
snotapall
i
ndr
omenumber "
);
}
}
}
-
---
--
--
--
--
--
---
2.cl
assTest er{
publ
icstaticvoi
dmai n(Str
ing[
]args){
i
ntheads=150, l
egs=500;
i
ntr=0,c=0;
i
f((
heads>legs)|
|(heads==0)||(
legs%2!
=0)
)
{
System.out
.pri
ntl
n("Thenumberofchickensandr
abbi
tscannotbef
ound"
);
}
el
se{
r=(i
nt)
((l
egs+(-
2*heads))/
2);
c=(i
nt)(
heads-r)
;
Syst
em.out.pr
intl
n("
Chickens="+c);
Syst
em.out.pr
intl
n("
Rabbits="+r)
;
}
}
}
-
---
--
--
--
--
--
--
----
--
3.cl
assTest er{
publ
icstat i
cv oidmai n(St ri
ng[
]args){
inta=123, n=a, rem=0, temp=0,sum=0;
whi l
e( n!=0)
{
temp=n%10;
sum=sum+t emp;
n=n/ 10;
}
Sy stem. out.printl
n( "sum ofdigi
ts="+sum) ;
rem=a%sum;
if(
rem==0)
{
Sy stem. out.printl
n(a+"i sdiv
isi
blebysum ofit
sdi
git
s")
;
}
else{
Sy stem. out.printl
n(a+"i snotdi
v i
si
blebysum ofi
tsdi
git
s")
;
}
}
}
-
---
--
--
--
--
--
--
----
---
---
4.cl
assTest er{
publ
icstat i
cv oidmai n(St ri
ng[
]args){
intx =45, y=1000, rem=0, temp=0,rev=0,a=x
;
whi l
e( a!=0)
{
rem=a%10;
a=a/ 10;
temp=y *rem;
}
if(
temp==y )
{
Sy stem. out.printl
n(x+"isaseedof"+y );
}
el
se{
Sy stem. out.printl
n(x+"isnotaseedof"+y );
}
}
}
-
---
--
--
--
--
--
--
----
--
5.cl
assTest er{
publ
icstat i
cv oidmai n(St ri
ng[
]args){
inta=1635;
intn=a;
intrem=0;
intrev =0;
doubl et emp=0;
i
ntsum=0;
whi le( a!
=0)
{
r em=a%10;
a=a/ 10;
t emp=Mat h.pow(rem,3)
;
sum=( i
nt)( sum+t emp);
}
Sy stem. out.
print l
n(sum);
i
f(sum==n)
{Sy stem. out.
println(n+"isanArmstr
ongnumber"
);
}
else{
Sy stem.out.pr i
ntl
n(n+"isnotanarmst
rongnumber
");
}
}
}
-
---
--
--
--
--
--
-
6.cl
assTest er{
publ
icstaticvoidmai n(St
ri
ng[]args){
Stri
nga=" 1623"
;
intsum=0;
for(
inti=1; i
<=a.lengt
h();i
=i+2)
{
i
nttemp=a. charAt(i
)-
'0'
;
sum=sum+( temp*t
emp) ;
}
i
f (
sum%9==0)
{
System.out
.printl
n(a+"i
saluckynumber"
);
}
else
{
System.out
.printl
n(a+"i
snotaluckynumber"
);
}
}
}
-
---
--
--
--
--
--
--
----
--
----
7.cl
assTest er{
publ
icstat i
cv oidmai n(
Stri
ng[
]ar
gs){
intnum1=7, num2=9, l
cm=0,max=0;
if(
num1>num2)
max =num1;
else
max =num2;
intst ep=max;
whi l
e( num1! =0)
{
if(max %num1==0&&max %num2==0)
{
l
cm=max;
break;
}
max =max +step;
}
Sy stem. out.pri
ntl
n(l
cm);
}
}
--
--
--
--
--
--
--
--
---
-
8.cl
assTest er{
publ
icstaticv oi
dmai n(Str
ing[
]args){
intn=5;
for(i
nti=1; i
<=n;i++)
{
f
or(intj=n;j
>=i;j
--
)
{
System. out.pri
nt(
"*"+"");
}
Sy stem.out .
pr i
ntl
n();
}
}
}
--
--
--
--
--
--
--
--
---
--
-
--
--
--
--
--
--
--
--
---
--
--
----
---
--
---
--
--
----
---
--
--
--
--
--
--
---
--
--
-
DAY4
--
--
--
--
--
--
--
--
---
--
-
1.CLASSI NTRODUCTI ONTRYOUT
cl
assCust omer{
publ
i
cStri
ngcust
omer I
d;
publ
i
cStri
ngcust
omer Name;
publ
i
clongcont
actNumber;
publ
i
cStri
ngaddress;
publ
i
cv oiddi
spl
ayCustomerDetail
s(){
System.out
.pr
int
ln(
"Di
splayingcustomerdetai
ls\n*
***
***
****
***
***
***
***
***
*")
;
System.out
.pr
int
ln(
"Cust
omerI d: "+customerI
d);
System.out
.pr
int
ln(
"Cust
omerName: "+customerName);
System.out
.pr
int
ln(
"ContactNumber: "+contact
Number);
System.out
.pr
int
ln(
"Address:"+addr ess)
;
System.out
.pr
int
ln(
);
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
//Objectcr
eat
ion
Customercust
omer=newCust
omer
();
//Assi
gni
ngvaluestothei nstancevari
ables
cust
omer.
cust
omer Id=" C101" ;
cust
omer.
cust
omer Name=" StephenAbram";
cust
omer.
contactNumber=7856341287L;
cust
omer.
address=" D089, St.Loui
sStreet,
Spr
ingf
iel
d,62729"
;
//Di
splay
ingthecust
omerdetail
s
cust
omer.di
splay
CustomerDetai
l
s();
//Movetheabovestatementi
mmedi at
elyaf
tert
heobj
ectcr
eat
ion
//st
atementandobservetheoutput
}
}
--
--
--
--
--
--
--
--
--
2.CLASSANDOBJECT-
EXERCI
SE1
cl
assCust omer{
publ
i
cStri
ngcust
omer I
d;
publ
i
cStri
ngcust
omer Name;
publ
i
clongcont
actNumber;
publ
i
cStri
ngaddress;
publ
i
cv oiddi
spl
ayCustomerDetail
s(){
System.out
.pr
int
ln(
"Di
splayingcustomerdetai
ls\n*
***
***
****
***
***
***
***
***
*")
;
System.out
.pr
int
ln(
"Cust
omerI d: "+customerI
d);
System.out
.pr
int
ln(
"Cust
omerName: "+customerName);
System.out
.pr
int
ln(
"ContactNumber: "+contact
Number);
System.out
.pr
int
ln(
"Address:"+addr ess)
;
System.out
.pr
int
ln(
);
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
//Objectcr
eat
ion
Customercust
omer=newCust
omer
();
//Assi
gni
ngvaluestothei nstancevari
ables
cust
omer.
cust
omer Id=" C101" ;
cust
omer.
cust
omer Name=" StephenAbram";
cust
omer.
contactNumber=7856341287L;
cust
omer.
address=" D089, St.Loui
sStreet,
Spr
ingf
iel
d,62729"
;
//Di
splay
ingthecust
omerdetail
s
cust
omer.di
splay
CustomerDetai
l
s();
//Movetheabovestatementi
mmedi at
elyaf
tert
heobj
ectcr
eat
ion
//st
atementandobservetheoutput
}
--
--
--
--
--
--
--
--
--
--
--
--
--
--
3.LOCALVARI ABLE-TRYOUT
cl
assDemo{
publ
ici
ntvar1;/
/Instancev
ari
abl
eoft
hecl
ass
publ
i
cv oidpri
ntVal
ue(){
intvar2=20;//Localvar
iableofthemethod
System.out.
pri
ntl
n(var
1);
System.out.
pri
ntl
n(var
2);//Localvar
iabl
eisaccessi
bleonl
yinsi
de
//themethod
}
}
cl
assTest
er{
publ
i
cstat
icvoidmain(Stri
ngar gs[]
){
Demodemo=newDemo( )
;
demo.
v ar
1=10; //Instancev ar
iabl
ecanbeaccessedf
rom out
sidet
he
//classwi ththehel
pofobj
ect
demo.
pr i
ntVal
ue(
);
/
/Localvariablescannotbeaccessedout
sideamethod
/
/Belowlineswi ll
leadtoacompilat
ioner
rorsay
ingt
hatvar
2isnot
/
/afiel
dorv ar
iable
/
/System.out .
pri
ntln(
demo.var
2);
/
/System.out .
pri
ntln(
var
2);
}
}
--
--
--
--
--
--
--
--
--
---
-
4.*METHODS-EXERCI SE1*
cl
assCalculator{
publ
icdoublefi
ndAv
erage(
intnumber
1,i
ntnumber
2,i
ntnumber
3)
{
doublesum=( number1+number2+number3)
;
doubleaverage=sum/ 3;
doubleroundoff
=Mat h.r
ound(
average*
100.
0)/
100.
0;
ret
urnroundoff;
cl
assTest
er{
publ
i
cst at
icvoi
dmai n(Str
ingar
gs[]
){
Calcul
atorcalculator=newCalculat
or()
;
doublex=calculator.
fi
ndAver
age(12,
8,15);
System.out.
printl
n(x);
/
/Inv
oket
hemet
hodf
indAv
erageoft
heCal
cul
atorcl
assanddi
spl
ayt
heav
erage
}
}
--
--
--
--
--
--
--
--
---
--
--
--
-
DAY5
--
--
1.CONSTRUCTORANDTHI
SKEYWORD:
EXERCI
SE1
cl
assCust omer{
publ
i
cStri
ngcust
omer I
d;
publ
i
cStri
ngcust
omer Name;
publ
i
clongcont
actNumber;
publ
i
cStri
ngaddress;
Cust
omer(St
ri
ngcustomerName,
longcont
act
Number
,St
ri
ngaddr
ess)
{
cust
omerId=customer
Id;
t
his.
cust
omerName=customerName;
t
his.
cont
actNumber=cont
act
Number;
t
his.
addr
ess=address;
}
publ
icv
oiddispl
ayCustomer Det
ail
s(){
Syst
em.out
.pri
ntl
n("
Displ
ayingcustomerdetai
ls")
;
Syst
em.out
.pri
ntl
n("
CustomerI d:"+customerId)
;
Syst
em.out
.pri
ntl
n("
CustomerName: "+customer Name)
;
Syst
em.out
.pri
ntl
n("
ContactNumber: "+contactNumber)
;
Syst
em.out
.pri
ntl
n("
Address: "+address)
;
Syst
em.out
.pri
ntl
n()
;
}
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Cust
omercust
omer=newCust
omer
("Jacob"
,627295480,
"St
.Loui
s,NewYor
k")
;
cust
omer
.di
spl
ayCust
omer
Det
ail
s()
;
}
-
--
--
--
--
--
--
--
--
--
--
--
-
*
METHODS- ASSIGNMENT*
1.cl
assOrder{
publ
icintorder
Id;
publ
icStri
ngorderedFood;
publ
icdoubletotal
Pri
ce;
publ
icStri
ngstatus;
publ
i
cdoubl ecalculat
eTotalPr
ice(i
ntunit
Price)
{
System.out.pr
intl
n("Or
derDetail
s");
tot
alPri
ce=uni tPr
ice*(1+(5/
100.0));
ret
urntot al
Pri
ce;
}
publ
i
cv oiddispl
ayDetail
s()
{
System.out.pr
intl
n("Or
derId:"+orderI
d);
System.out.pr
intl
n("Or
deredFood: "+or deredFood)
;
System.out.pr
intl
n("Or
derStatus:"+stat us);
}
}
cl
assTester{
publ
icstaticvoi
dmai n(Str
ingargs[]
){
Ordero=newOr der (
);
o.
orderId=101;
o.
orderedFood=" Past a"
;
o.
status="ordered";
doublex=o.calculateTotal
Price(
100)
;
o.di
splay
Detail
s()
;
System.out
.pr
int
ln(
"Tot
alPr
ice:
"+x)
;
}
}
-
---
--
--
--
--
--
--
--
--
--
-
2.cl
assRestaurant{
publ
icfl
oatrati
ng;
publ
icStr
ingrestaur
antName;
publ
icl
ongr est
aurantCont
act;
publ
icStr
ingrestaur
antAddress;
publ
i
cv oiddi
spl
ayRestaurantDetai
l
s()
{
System.out
.pr
int
ln("
RestaurantDetai
ls\n**
*********
******
*****
**")
;
System.out
.pr
int
ln("
RestaurantName: "+restaurantName);
System.out
.pr
int
ln("
RestaurantContact:"+restaurantContact
);
System.out
.pr
int
ln("
RestaurantAddress:"+restaurantAddress);
System.out
.pr
int
ln("
Rating:"+rat
ing);
}
}
cl
assTest er {
publi
cst at i
cv oi
dmai n(Str
ingar gs[]
){
Rest aur antr=newRest aurant()
;
r.
r est aurantName=" Domi nos";
r.
r est aurantContact=2534512;
r.
r est aurantAddress="Janipurcolony
";
r.
r ating=4. 3f;
r.
di spl ayRestaur
antDet ai
l
s() ;
}
}
--
--
--
--
---
--
--
-----
------
--
--
-
3.cl
assCal cul ator{
publi
cintnum;
publi
cintsumOf Digi
ts()
{
i
nta=num, r
em=0, sum=0;
whil
e( a!=0)
{
rem=a%10;
a=a/ 10;
sum=sum+r em;
}
ret
urnsum;
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
]){
Calcul
atorcalcul
ator=newCal cul
ator
();
cal
culator.
num=123;
i
ntx =calcul
ator.
sumOf Di
git
s()
;
System.out.
print
ln(
x);
/
/Assi
gnav
aluet
othememberv
ari
abl
enum ofCal
cul
atorcl
ass
/
/Inv
oket
hemet
hodsumOf
Digi
tsofCal
cul
atorcl
assanddi
spl
ayt
heout
put
}
}
-
---
--
--
--
----
---
--
---
-
4.cl
assRect angle{
publicfloatl
engt h;
publicfloatwidth;
publicdoublecal cul
ateArea( )
{
doubl ea=lengt h*width;
doubl earea=Mat h.
round(a*100.0)
/100.
0;
returnarea;
}
publicdoublecal cul
atePerimet er
()
{
doubl ep=2*( length+wi dth)
;
doubl eperi
met er=Mat h.round(p*
100.0)/
100.
0;
returnperimeter;
}
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
]){
Rectanglerectangle=newRect
angl
e();
//Assi
gnv aluest othemembervar
iabl
esofRect
angl
ecl
ass
rect
angle.l
engt h=12f;
rect
angle.width=5f;
//
InvokethemethodsoftheRectangl
eclasst
ocal
cul
atet
hear
eaandper
imet
er
doublex=rect
angl
e.cal
cul
ateArea(
);
doubley=rect
angl
e.cal
cul
atePeri
meter
();
//
Displ
aytheareaandperi
meterusingthelinesgiv
enbel
ow
Syst
em.out.
pri
ntl
n("
Areaoftherect
angleis"+x );
Syst
em.out.
pri
ntl
n("
Peri
meteroftherectanglei
s"+y )
;
}
}
-
---
--
--
--
--
--
--
---
*CONSTRUCTORSANDTHI SKEYWORD*
1.cl
assOr der{
publ
icintorder
Id;
publ
icStri
ngorderedFood;
publ
icdoubletotal
Pri
ce;
publ
icStri
ngstatus;
publ
i
cOr der
()
{
stat
us="order
ed"
;
}
publ
i
cOr der(i
ntorderId,Str
ingorderedFood)
{
this.
orderId=orderI
d;
this.
orderedFood=or deredFood;
status="Order
ed";
}
}
cl
assTester{
publ
icstaticv
oidmain(Str
ingargs[]
){
Ordero1=newOr der(
);
System.out
.pr
int
ln(
"Stat
usofor der1:"+o1.st
atus)
;
Ordero2=newOr der(
1001, "
Pizza"
);
System.out
.pr
int
ln(
"Stat
usofor der2:"+o2.st
atus)
;
System.out
.pr
int
ln(
"OrderIdoforder2:"+o2.order
Id)
;
System.out
.pr
int
ln(
"OrderedFoodf oror
der2:"+o2.order
edFood)
;
}
}
-
--
--
--
--
--
--
--
--
--
---
2. cl
assRest aurant{
publi
cfloatrat i
ng;
publi
cSt r
ingr estaur
antName;
publi
clongr estaurantCont
act;
publi
cSt r
ingr estaur
antAddress;
publ
i
cv oiddi
spl
ayRestaurantDetai
l
s()
{
System.out
.pr
int
ln("
RestaurantDetai
ls\n**
*********
******
*****
**")
;
System.out
.pr
int
ln("
RestaurantName: "+restaurantName);
System.out
.pr
int
ln("
RestaurantContact:"+restaurantContact
);
System.out
.pr
int
ln("
RestaurantAddress:"+restaurantAddress);
System.out
.pr
int
ln("
Rating:"+rat
ing);
}
publicRestaurant (Stri
ngname, l
ongr estaur
antContact
,Str
ingrestaur
antAddress,
floatr
ati
ng)
{
rest
aurantName=name;
thi
s.r
estaur ant Contact=restaurantContact
;
thi
s.r
estaur ant Address=restaurantAddress;
thi
s.r
ating=r at i
ng;
}
}
classTester{
publi
cst ati
cv oidmai n(St
ri
ngar gs[])
{
Restaur antr =newRest aurant("Dominos"
,2534512,"
Janipurcol
ony",
4.3f ) ;
r.
display RestaurantDetail
s();
}
}
--
---
--
--
--
--
--
--
---
---
--
*ENCAPSULATI ON- EXRECI SE1*
cl
assEmpl
oyee{
pr
ivat
eStri
ngemploy
eeI
d;
pr
ivat
eStri
ngemploy
eeName;
pr
ivat
eintsal
ary
;
pr
ivat
eintbonus;
pr
ivat
eintj
obLevel
;
publ
i
cSt ri
nggetEmpl oyeeI
d()
{
ret
urnempl oyeeId;
}
publ
i
cv oidsetEmployeeId(
Stri
ngempl
oyeeI
d){
thi
s.employeeId=empl oy
eeId;
}
publ
i
cSt ri
nggetEmploy eeName(
){
ret
urnempl oyeeName;
}
publ
i
cv oidsetEmployeeName(St
ringempl
oyeeName)
{
thi
s.employeeName=empl oyeeName;
}
publ
i
cintgetSalar
y(){
ret
urnsalary;
}
publ
i
cv oidset
Salary(
intsal
ary
){
thi
s.sal
ary=salar
y ;
}
publ
i
cintgetBonus(){
ret
urnbonus;
}
publ
i
cv oidsetBonus(
intbonus)
{
thi
s.bonus=bonus;
}
publ
i
cintgetJobLevel
()
{
ret
urnjobLevel
;
}
publ
i
cv oi
dsetJobLevel
(i
ntjobLev
el)
{
thi
s.j
obLevel
=jobLevel
;
}
publ
i
cv oidcalcul
ateSalar
y(){
if(
this.
jobLevel>=4){
thi
s.bonus=100;
}else{
thi
s.bonus=50;
}
thi
s.sal
ary+=t his.
bonus;
}
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
]){
Empl
oyeeemployee=newEmpl oyee()
;
empl
oyee.
set
Empl oyeeI
d("
C101")
;
empl
oyee.
set
Empl oyeeName("
Steve"
);
empl
oyee.
set
Salary(
650);
empl
oyee.
set
JobLev el
(4)
;
empl
oyee.
cal
cul
ateSal
ary
();
Sy
stem.
out
.pr
int
ln(
"EmployeeDet
ails"
);
Sy
stem.
out
.pr
int
ln(
"EmployeeI
d:"+empl oy
ee.getEmpl oy
eeId()
);
Sy
stem.
out
.pr
int
ln(
"EmployeeName: "+employee.getEmployeeName(
));
Sy
stem.
out
.pr
int
ln(
"Sal
ary:
"+empl oyee.
getSal
ary())
;
}
}
-
---
--
--
--
--
---
--
--
---
-
*ENCAPSULATI ONASSI GNMENT*
1. classOrder{
pri
vateintorder
Id;
pri
vateStri
ngorderedFood;
pri
vatedoubletotal
Price;
pri
vateStri
ngstatus;
publ
i
cv oidsetOr
deredFood(
Stri
ngorder
edFood)
{
thi
s.order
edFood=orderedFood;
}
publ
i
cSt ri
nggetOrderedFood(
){
ret
urnor der
edFood;
}
publ
i
cv oidsetSt
atus(Str
ingst
atus)
{
thi
s.stat
us=status;
}
publ
i
cSt r
inggetStat
us(){
ret
urnst at
us;
}
publ
i
cv oidsetOrder
Id(
intor
der
Id)
{
thi
s.orderI
d=orderI
d;
}
publ
i
cintget
Order
Id(
){
ret
urnor
derI
d;
}
publ
i
cdoublecalculat
eTotalPr
ice(
intuni
tPr
ice)
{
System.out
.pr
intl
n("Or
derDetail
s")
;
tot
alPri
ce=unitPr
ice*(1+(
5/100.0))
;
ret
urntotal
Pri
ce;
}
}
cl
assTester{
publ
icstati
cv oidmain(Stri
ngargs[
])
{
Ordero=newOr der()
;
o.
setOrderId(
101);
o.
setOrderedFood( "
Pasta"
);
o.
setStat
us( "
ordered")
;
doubl
ex=o.
cal
cul
ateTot
alPr
ice(
100)
;
System.out.
print
ln("
OrderId:
"+o.get
Order I
d())
;
System.out.
pri
nt l
n("
OrderedFood:"+o.getOrderedFood(
));
System.out.
pri
nt l
n("
OrderStat
us:"+o.getStatus()
);
Sy
stem.out.
pri
ntln("
Totalamounttobepaid: "+x );
}
}
-
---
--
---
--
---
--
--
--
-
2.classMov ieTi
cket{
priv
ateintmov i
eId;
priv
ateintnoOfSeats;
priv
atedoublecostPerTi
cket
;
publ
i
cv oi
dsetMovi
eId(
intmov
ieI
d){
thi
s.movi
eId=movi
eId;
}
publ
i
cintgetMov ieI
d()
{
ret
urnmov i
eId;
}
publ
i
cv oidsetNoOfSeats(
intnoOfSeat
s){
thi
s.noOfSeats=noOfSeats;
}
publ
i
cintgetNoOf Seat
s(){
ret
urnnoOf Seats;
}
publ
i
cv oidsetCostPer
Ticket
(doublecost
Per
Ticket
){
thi
s.costPerTi
cket=cost
PerTicket
;
}
publ
i
cdoublegetCostPer
Ticket
(){
ret
urncost
PerTicket
;
}
publ
i
cMov i
eTicket(
intmov i
eId,i
ntnoOfSeat
s)
{
thi
s.mov i
eId=mov i
eId;
thi
s.noOfSeats=noOf Seats;
}
publ
i
cdoubl ecalcul
ateTotalAmount()
{
doublea;
swit
ch( movieI
d)
{
case111: costPerTicket=7;
break;
case112: costPerTicket=8;
break;
case113: costPerTicket=8.
5;
break;
default
:costPerTicket=0;
}
a=costPer Ti
cket*noOfSeat s;
doubleamount =a* (1+(2/100.0)
);
r
etur
namount
;
}
}
classTester{
publi
cst aticvoidmain(Stri
ng[ ]ar gs){
Mov ieTicketmov i
eTicket=newMov ieTicket
(112,3)
;
mov ieTicket.
set
CostPer Ti cket (7)
;
doubl eamount=mov ieTicket .calcul
ateTotal
Amount (
);
i
f(amount ==0)
Sy stem.out
.pri
ntln("Sor ry!Pleaseent erval
idmovieI
dandnumberofseat
s")
;
el
se
Sy stem.out
.pri
ntln("Tot al amount f
or booki
ng : $" +
Mat h.
round(amount *
100)/100.0);
}
}
--
---
--
--
--
--
--
--
*STRINGMETHODS- TRYOUT*
classTester{
publi
cst aticvoidmain(Stri
ngar gs[ ]
){
//l
ength()
Str
ingstr="Welcome";
System.out
.pr
int
ln(
str
.l
engt
h()
);
//concat ()
Strings=" Hell
o";
s.concat("World"
);
Sy stem.out.
pri
ntl
n(s)
;
//si sstil
l"Hel
lo"
//St ri
ngobject
sar ei
mmutabl
ewhichmeanstheycannotbechanged
//Her e,whenweconcatthetwost
ringsanewstr
ingobjectget
screat
ed
St
ri
ngs1=s.concat(
"Worl
d")
;
Sy
stem.
out
.pri
ntl
n(s1);
//+oper at
orcanalsobeusedforst
ri
ngconcat
enat
ion
Str
ingfname=" Jack";
Str
inglname=" Black";
System.out.
pri
ntl
n(fname+""+lname);
//equal
s()
System.
out.
pri
ntl
n(s.
equal
s("
Hel
l
o")
);
//equal
scomparesonlythevaluesoft
hest
ri
ngs
Str
ings2=newSt r
ing(
"Hell
o")
;
System.
out
.pr
int
ln(
s.equal
s(s2)
) ;
//==comparestheobjectr
efer
enceandwi
l
lret
urnf
alsei
nthebel
ow
//case
System.
out
.pri
ntl
n(s==s2);
//equal
sIgnor
eCase()
System.
out.pr
int
ln(
s.equal
sIgnor
eCase(
"hel
l
o")
);
//toLowerCase()andtoUpper
Case()
System.out.
pri
ntl
n(st
r.
toLowerCase(
));
Sy
stem.
out
.pr
int
ln(
str
.t
oUpper
Case(
));
//subst
ri
ng()
Str
ingsubs="Learni
ngisfun"
;
System.
out.
pri
ntl
n(subs.
subst
ri
ng(
4,8))
;
System.
out.
pri
ntl
n(subs.
subst
ri
ng(
4))
;
//charAt(
)
System.out.
pri
ntl
n(subs.
char
At(
10)
);
//contai
ns()
System.out
.pr
int
ln(
subs.
cont
ains(
"i
s")
);
//repl
ace()
System.out
.pr
int
ln(
subs.
repl
ace(
'i
'
,'k'
))
;
}
}
--
---
--
--
--
--
--
--
--
--
--
*STRINGEXERCI SE*
classTester
{
publ
i
cstaticSt
ri
ngremov eWhi
teSpaces(
Stri
ngstr){
Str
ingnoSpaceSt
r=str.
repl
aceAll
("
\\s"
,""
);//usingbui
l
tinmet hod
//I
mplementyourcodehereandchanget heretur
nv al
ueaccor
dingly
ret
urnnoSpaceStr
;
}
publ
i
cst at
icvoi
dmai n(
Stri
ngargs[
]){
Stri
ngstr="Hell
o Howar eyou "
;
str=removeWhiteSpaces(
str)
;
System.out
.pr
int
ln(st
r);
}
}
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
DAY6
--
--
--
--
-
*MULTIDIMENSI ONALARRAY- TRYOUT*
//Pr
ogram toil
lust
ratetheuseofmul ti
dimensi
onalarr
ay
cl
assTest er{
publ
icstati
cvoidmain(Str
ing[]ar
gs){
//Declari
ngandi ni
ti
ali
zing2Dar r
ay
int[
][
]dayWiseTemperature=newi nt[
][
]{{29,21},{24,23},
{26,22},{28,23} ,
{29,24},{23,20},{29,21}};
/
/Di
splaying2Dar ray
f
or(
inti=0; i<7;i++){
for(i
ntj =0; j
<2; j++){
if(j==0)
Syst
em. out.
pri
ntl
n("
MaxTemperaturei
s"
+dayWiseTemper
atur
e[i
][
j]+"onday"+i
);
else
Syst
em. out.
pri
ntl
n("
MinTemperat
ur ei
s"
+dayWiseTemper
atur
e[i
][
j]+"onday"+i
);
}
}
}
}
--
---
--
--
--
--
---
--
--
--
*ARRAY- EXERCISE1*
classTester{
publ
icstati
ci ntcal
culat
eSumOfEvenNumber
s(i
nt[
]number
s){
i
ntsum=0;
f
or(i
nti=0; i
<numbers.l
engt
h;i
++)
{
i
nta=number s[i
]%2;
i
f(a==0)
{
sum=sum+number s[
i]
;
}
}
r
eturnsum;
}
publi
cst ati
cv oi
dmai n(Str
ing[]ar
gs){
i
nt[]number s={ 68,
79,86,99,
23,
2,
41,100}
;
System.out .
pri
ntln(
"Sum of ev
en number
s: "
+calcul
ateSumOf EvenNumber s(numbers)
);
}
}
-
---
--
---
--
--
--
--
---
---
--
*STRINGASSI GNMENT*
-
--
1.cl
assTest er{
publ
i
cstaticSt ri
ngmov eSpecialChar acters(
St r
ingstr
){
Str
ingr egex=" [^
a-zA- Z0-9\ \
s+]";
Str
ingi nputData=" ";
Str
ingspeci alChar acters=" ";
for
(inti=0; i
<str.l
engt h();i
++)
{
charch=st r.
char At(i)
;
if
(String.v
alueOf (ch) .
mat ches(regex))
{
special
Char act ers=speci alCharacter
s+ch;
}
else{
i
nputDat a=i nputDat a+ch;
}
}
Str
ingr esult=input Data+speci al
Char acters;
ret
urnr esult;
}
publ
i
cstaticvoi
dmain(Stri
ngargs[
])
{
Str
ingstr="He@#$llo!
*&";
System.out
.pr
int
ln(
mov eSpeci
alChar
act
ers(
str
));
}
}
-
---
--
--
--
--
--
--
--
--
-
2.cl
assTester{
publ
i
cst at
icbool
eancheckPal
i
ndr
ome(
Str
ingst
r){
intl
ength=st
r.l
engt
h()
;
Stri
ngrev=""
;
f
or(
inti
=lengt
h-1;
i>=0;
i-
-)
{
r
ev=r
ev+st
r.
char
At(
i)
;
}
i
f(st
r.
equal
s(r
ev)
)
{
r
etur
ntrue;
}
r
eturnf
alse;
}
publ
i
cst at
icvoidmain(Str
ingargs[
])
{
Stri
ngstr=" r
adar";
if
(checkPali
ndrome(str)
)
System.out.
pri
ntl
n("Thest
ringi
sapali
ndr
ome!")
;
else
System.out.
pri
ntl
n("Thest
ringi
snotapal
indr
ome!"
);
}
}
-
---
--
--
--
--
--
--
3.cl
assTest er{
publ
icstat i
cSt ri
ngr everseEachWor d(Str
ingst
r){
Stringwor ds[]=st r
.spl i
t("");
Stringr ev ersedSt r
ing=" ";
for(inti=0; i<wor ds.lengt h;
i++)
{
Stringwor d=wor ds[i]
;
Stringr ev erseWor d="";
for(intj=wor d.lengt h()-
1;j
>=0;j-
-)
{
rev erseWor d=reverseWor d+word.char
At(
j)
;
}
reversedSt ring=r eversedString+rever
seWord+"";
}
returnr ev ersedSt r
ing. tri
m( );
}
publ
i
cstaticvoi
dmain(Str
ingar gs[]
){
Str
ingstr="Il
oveprogrammi ng"
;
System.out
.pr
int
ln(
reverseEachWord(
str
));
}
}
-
---
--
--
--
--
--
--
-
4.cl
assTest er{
publi
cstati
cintf i
ndHi ghestOccurr
ence(
Str
ingst
r){
intcount er
=0;
intmax_ count er=0;
charch;
for(
inti=0;i
<st r
.l
ength();
i
++)
{
ch=str.
charAt (
i)
;
counter=0;
f
or(i
ntj=0;
j<st
r.
length();
j++)
{
if
(ch==str.
charAt(j)
)
counter++;
}
i
f(
counter>max_ counter)
{
max_ counter=counter;
}
}
r
etur
nmax_
count
er;
}
publ
i
cstaticvoi
dmai n(
Stri
ngargs[
])
{
Str
ingstr="success";
System.out
.pr
intl
n(f
indHi
ghest
Occur
rence(
str
));
}
}
-
---
--
--
---
--
--
--
--
---
--
5.cl
assTest er{
publi
cstat i
cSt r
ingremov eDupli
catesandSpaces(St
ri
ngst
r){
Str
ingresult=" "
;
for(i
nti=0; i<st r
.l
engt h(
);i
++){
i
f(!
result.
contains(Str
ing.
val
ueOf (
str.
charAt
(i
)))
){
result+=St r
ing.v
alueOf(
str.
charAt(
i)
);
}
}
ret
urnresult.r
eplace("",
"")
;
publ
i
cstaticvoi
dmai n(Str
ingargs[
])
{
Str
ingstr="objectori
entedprogramming";
System.out
.pr
intl
n(removeDupli
catesandSpaces(
str
));
}
}
-
---
--
--
--
--
---
--
--
---
--
--
--
--
--
--
--
--
-
-
---
--
--
--
--
---
--
--
---
--
--
--
*ARRAY-ASSI GNMENTS*
-
---
1.cl
assTeacher{
pri
vateStri
ngteacherName;
pri
vateStri
ngsubject;
pri
vatedoublesalar
y;
publi
cv oi
dsetTeacherName( St
ri
ngt
eacher
Name)
{
t
his.t
eacherName=t eacher
Name;
}
publ
i
cSt ri
ngget TeacherName()
{
ret
urnt eacherName;
}
publ
i
cv oidsetSubject(
Stri
ngsubj
ect
){
thi
s.subject=subject;
}
publ
i
cSt
ri
ngget
Subj
ect
(){
ret
urnsubject;
}
publ
i
cv oidset
Salary(
doubl
esal
ary
){
thi
s.sal
ary
=sal ar
y;
}
publ
i
cdoubl eget Salar
y(){
ret
urnsal ary;
}
publ
i
cTeacher (
Stri
ngt eacherName,
Str
ingsubj
ect
,doubl
esal
ary
)
{
thi
s.teacherName=t eacher
Name;
thi
s.subject=subject;
thi
s.salary=salary
;
}
}
cl
assTester{
publ
icstat
icv
oidmai
n(St
ri
ng[
]ar
gs){
Teacher[]t
eacherobj
ects=newTeacher [4];
teacherobj
ects[
0]=newTeacher ("Alex,"
,"Jav aFundament al,"
,1200.0);
teacherobj
ects[
1]=newTeacher ("John,","RDBMS, ",800.0) ;
teacherobj
ects[
2]=newTeacher ("Sam, ",
"Net wor king,",
900. 0);
teacherobj
ects[
3]=newTeacher ("Mar i
a,","Python, ",
900.0) ;
for(
inti
=0;i<teacher
objects.
length;i++)
{
System.out.
pri
nt(
"Name: "+teacherobj ects[
i].getTeacher Name() +"")
;
System.out.
pri
nt(
"Subject:"
+teacherobj ects[i].
getSubj ect(
)+"")
;
System.out.
pri
ntl
n("Sal
ary:"+teacherobj ects[i]
.getSalary(
)+"")
;
}
}
}
-
---
--
--
--
--
--
--
--
--
--
-
2.cl
assTester{
publ
i
cst ati
cdoubl e[]fi
ndDet ails(
doubl
e[]sal
ary){
//I
mpl ementy ourcodeher eandchanget heret
urnv
alueaccor
dingl
y
double[]arr=newdoubl e[ 3];
doubleav g=0;
for
(inti=0; i<sal ary.l
engt h;
i++)
{
avg+=sal ary[i
];
if(
i==sal ary.l
engt h- 1)avg/=(double)(
i+1)
;
}
arr
[0]=av g;
doublegr eater=0, lesser=0;
for
(doubl edata: salary){
if(
data>av g)gr eater ++;
if(
data<av g)lesser ++;
}
arr
[1]=gr eater;
arr
[2]=l esser;
ret
ur narr;
}
publ
i
cstat
icv oi
dmain(
Stri
ng[
]args){
doubl
e[]sal
ary={23500.
0,25080.0,28760.
0,22340.
0,19890.
0};
doubl
e[]detai
l
s=findDet
ail
s(sal
ary)
;
Sy
stem.
out
.pr
int
ln(
"Aver
agesal
ary
:"+det
ail
s[0]
);
Sy
stem.
out
.pr
int
ln(
"Numberofsal
ari
esgr
eaterthantheaveragesalar
y:"+detai
ls[1]
);
Sy
stem.
out
.pr
int
ln(
"Numberofsal
ari
esl
esserthantheaveragesalary
:"+detail
s[2])
;
}
}
-
--
--
--
--
--
--
--
--
--
--
3. cl
assTester{
publ
i
cst ati
cint[]fi
ndLeapYear
s(intyear
){
//Implementy ourcodehereandchanget her
etur
nval
ueaccor
dingl
y
int[
]ans=newi nt[
15];
intfi
rst=y ear;
whi l
e(t
rue){
if(
fi
rst%4==0) {
if(f
ir
st%100==0) {
i
f(
fi
rst%400==0)br eak;
}elsebreak;
}
fir
st++;
}
inta=0;
for(i
nti=f i
rst;a<ans.l
ength;i+=4)
{
if(
i%100==0&&i %400! =0)conti
nue;
ans[a++]=i ;
}
r
etur
nans;
}
publ
i
cst ati
cv oi
dmai n(
Stri
ng[]args){
intyear=1684;
int[
]leapYears;
leapYears=fi
ndLeapYears(year)
;
for(intindex=0; i
ndex<leapYears.
lengt
h;i
ndex++){
Sy st
em.out.
print
ln(
leapYears[i
ndex]
);
}
}
}
-
---
--
--
--
--
---
--
--
---
--
--
---
--
-
4.cl
assSt udent{
pr
ivateint[
]mar ks;
pr
ivatechar[]grade;
publ
i
cint
[]get
Mar
ks(
){
r
etur
nmarks;
}
publi
cvoi
dset
Marks(
int
[]mar
ks){
thi
s.mar
ks=marks;
}
publi
cchar[
]get
Grade(
){
ret
urngrade;
}
publi
cvoi
dset
Grade(
char
[]gr
ade){
thi
s.gr
ade=gr
ade;
}
publicSt udent(int[
]mar ks)
{
this.mar ks=mar ks;
grade=newchar [marks.
length]
;
}
publicv oidfindGrade()
{
for(
inti=0; i<mar ks.l
ength;i
++)
{
i
f(mar ks[i]
>=92)
{
grade[i]
=' E';
}
elseif(mar ks[i
]>=85&&mar ks[i
]<92)
{
grade[i]
=' A';
}
elseif(mar ks[i
]>=70&&mar ks[i
]<85)
{
grade[i]
=' B';
}
elseif(mar ks[i
]>=65&&mar ks[i
]<70)
{
grade[i]
=' C';
}
else
{
grade[i]
=' D';
}
}
}
}
classTest er{
publ i
cst aticvoidmai n(
Stri
ng[ ]args){
i
nt []marks={79, 87,97,65,78, 99,66};
Studentst udent=newSt udent (
mar ks);
student .
findGr ade()
;
Sy stem.out.pr
intl
n("
Gr adescorrespondingt
othemar
ksar
e:"
);
char []gr
ades=st udent.getGrade( )
;
for( i
ntindex=0; index<gr ades.length;
index++){
Sy stem.out .pri
nt(
grades[i
ndex]+"" );
}
}
}
-
---
--
--
--
--
--
--
--
--
--
5.cl
assTester{
publ
i
cst at
ici
nt[
]fi
ndNumbers(i
ntnum1,
intnum2){
int
[]numbers=newint
[6];
/
/Impl
ementy
ourcodehere
i
f(
num1>num2)ret
urnnumbers;
i
f(
num1>=100||
num2<10)r et
urnnumber
s;
i
nta=0;
f
or(
inti=num1>=10?num1+1:10;
i<=num2&&i<100;
i++)
{
if
(i%5==0&&i %3==0)numbers[
a++]=i
;
}
r
etur
nnumber
s;
}
publ
i
cst at
icv
oidmai
n(St
ri
ng[
]ar
gs){
intnum1=10;
intnum2=30;
i
nt[
]number s=f i
ndNumber s(num1,num2) ;
i
f(numbers[0]==0){
Syst
em. out.print
ln("Therei
snosuchnumber !
")
;
}el
se{
for(
intindex=0; index<=number s.l
ength-1;i
ndex++){
i
f(number s[
index]==0){
break;
}
System. out.
pr i
ntl
n(number
s[index]
);
}
}
}
}
-
---
--
---
--
-----
--
--
---
6.cl
assTest er{
publicstaticintfi
ndTotal
Count
(i
nt[
]number
s){
i
ntlengt h=number s.l
ength;
i
ntcount =0;
for
(inti=0; i
<lengt
h-1;
i++)
{
i
f(
numbers[
i]
==numbers[
i+1]
)
{
count
=count+1;
}
}
r
etur
ncount
;
}
publi
cstati
cvoidmain(St
ri
ng[]args){
i
nt[]numbers={1,1,5,100,-
20, 6,0,
0};
System.out
.pr
int
ln(
"Countofadjacentoccur
rence:
"+f
indTot
alCount
(number
s))
;
}
}
-
---
--
--
--
--
--
--
--
--
---
--
--
-
7.cl
assTester{
publ
icstat
icStr
ing[
]fi
ndPermut
ati
ons(
Str
ingstr
){
//I
mpl ementyourcodeher
eandchangetheret
urnv
alueaccor
dingl
y
Str
ing[]ans=newSt ri
ng[6];
i
nta=0;
for
(inti =0; i<st r.
length();i++){
Stringx=" "
+st r.
charAt(i)
;
for(intj =0; j<st r.
l
engt h();j
++) {
if
(j!=i )
{
Stri
ngy=x+st r.charAt(j
);
for(i
ntk=0; k<st r.
length()
;k++)
{
if(k!=i&&k! =j){
Str
ingz=y+st r.
charAt
(k)
;
ans[a++]=z ;
}
}
}
}
}
i
ntdupl icates=0;
for
(inti =0; i<ans. length; i
++){
Stringt emp=ans[ i]
;
if
(!temp. equal s(""
)){
for(i
ntj =i +1;j<ans. length;j++){
i
f(temp. equals(ans[j])
){
ans[ j
]=" "
;
dupl i
cates++;
i-=1;
br eak;
}
}
}
}
Str
ing[]final_ans=newSt ri
ng[ 6];
i
ntfill=0;
for
(inti =0; i<ans. length; i
++){
if
(!ans[i].
equal s("")
)final_ans[fi
ll
++]=ans[i]
;
}
ret
urnf inal_ans;
}
publ
i
cstati
cv oi
dmai n(
Stri
ngargs[]
){
Str
ingstr=" abc"
;
Str
ingpermut ati
ons[]=fi
ndPermutati
ons(st
r);
for
(Str
ingper mutati
on:permutati
ons)
{
i
f(permut at
ion!
=null
)
Sy stem.out.
pri
ntl
n(permutat
ion)
;
}
}
}
--
--
--
--
--
--
--
--
---
--
-
WEEK2
--
--
--
-
DAY1
--
--
--
--
--
-
1.STATIC-TRYOUT
cl
assTest er{
publ
icstati
cvoi
dmai
n(Str
ing[
]ar
gs){
Carc1=newCar("Red")
;
Carc2=newCar ("
Green")
;
Carc3=newCar ("
Blue"
);
Syst
em.out
.pr
int
ln("
Numberofcar
scr
eat
ed:
"+Car
.get
Number
OfCar
s()
);
}
st
ati
c{
Syst
em.
out
.pr
int
ln(
"Test
ercl
assl
oaded"
);
}
}
cl
assCar{
pr
ivat
est
ati
cStri
ngcolor
;
pr
ivat
est
ati
cintnumberOfCar
s=0;
st
ati
c{
Syst
em.
out
.pr
int
ln(
"Carcl
assl
oaded"
);
}
publ
i
cCar (Str
ingcolor
){
thi
s.color=color;
number OfCars++;
}
publ
i
cstati
cStr
ingget
Col
or(
){
ret
urncol
or;
}
publ
i
cstati
cintget
NumberOf
Car
s(){
ret
urnnumberOfCar
s;
}
}
--
---
---
--
--
-
*EXERCI SE*
classBill
{
privatestat
ici
ntcount
er;
privateStri
ngbi
ll
Id;
privateStri
ngpayment
Mode;
static{
counter
=9000;
}
publ i
cBill
(St
ringpay
mentMode)
{
this.paymentMode=payment
Mode;
bil
lId="B"+++counter;
}
publi
cstat
icintget
Count
er(
){
ret
urncounter;
}
publi
cStri
ngget Bi
ll
Id(
){
ret
urnbill
Id;
}
publi
cvoidsetBil
lI
d(St
ringbi
l
lI
d){
thi
s.bi
ll
Id=bill
Id;
}
publi
cStri
nggetPay
mentMode(){
ret
urnpaymentMode;
}
publi
cvoidset
PaymentMode(
Stri
ngpay
ment
Mode){
thi
s.pay
mentMode=pay ment
Mode;
}
cl
assTester{
publ
icstat
icv
oidmai
n(St
ri
ng[
]ar
gs){
Bi
l
lbi
l
l1=newBi
l
l("
Debit
Card")
;
Bi
l
lbi
l
l2=newBi
l
l("
PayPal"
);
Bi
l
lbi
l
l3=newBi
l
l("
Credi
tCard"
);
Bi
l
lbi
l
l4=newBi
l
l("
PayTm");
Bi
l
lbi
l
l5=newBi
l
l("
GooglePay"
);
/
/Cr
eat
emor
eobj
ect
sandaddt
hem t
othebi
l
lsar
rayf
ort
est
ingy
ourcode
Bi
l
l[
]bi
l
ls={bi
l
l1,
bil
l
2,bi
l
l3,
bil
l
4,bi
l
l5}
;
f
or(
Bil
lbil
l:bi
ll
s){
System.out.
pri
ntl
n("
Bil
lDet
ail
s")
;
System.out.
pri
ntl
n("
Bil
lI
d:"+bil
l.
get
Bil
lI
d()
);
System.out.
pri
ntl
n("
Paymentmethod:"+bil
l
.get
Pay
ment
Mode(
));
System.out.
pri
ntl
n()
;
}
}
}
--
---
--
--
--
--
--
*AGGREGATI ONANDACCESSMODI FIERS-TRYOUT*
classSubject{
pri
vateSt r
ingsubjectName;
publi
cv oidsetSubject
Name(St
ringsubj
ect
Name){
t
his.subject
Name=subjectName;
}
publ
i
cStri
nggetSubject
Name(
){
ret
urnsubj
ectName;
}
Subj
ect
(Stri
ngsubj
ect
Name){
thi
s.subj
ect
Name=subject
Name;
}
}
cl
assStudent{
pri
vateintr
ollNo;
pri
vateStri
ngstudentName;
pri
vateSubjectsubj
ect;
St
udent(
introll
No, St
ringstudent
Name,Subj
ectsubj
ect
){
thi
s.roll
No=r oll
No;
thi
s.studentName=st udent
Name;
thi
s.subject=subj ect
;
}
publ
i
cv oiddi
spl
ayDetai
ls(
){
System.out
.pr
int
ln(
"StudentName:"+st
udentName);
System.out
.pr
int
ln(
"SubjectName:
"+subject
.get
Subj
ect
Name(
));
/
/Wecannotdi
rect
lyaccessthepri
vat
ememberoft hecl
assSubj
ect
/
/Toaccesst
hepri
v at
emember sofaggregat
edcl
ass,wewil
lhav
eto
/
/makeuseofthegetterandset
termethods
/
/Addt
hegetterandsett
ermethodstocl
assSubj
ectandmodi
fyt
he
/
/di
spl
ayDet
ail
smet hodaccor
dingl
y
publ
i
cst at
icvoidmai n(
Stri
ngargs[
]){
Subjectsubject=newSubject(
"Mat hs"
);
Studentstudent=newSt udent(101,"
Nate"
,subj
ect
);
student.
display
Detail
s()
;
}
}
-
---
--
---
--
--
-----
--
*EXERCI SE*
cl
assAddr ess{
pri
vateSt ringdoor No;
pri
vatest aticSt ri
ngst r
eet;
pri
vatest aticSt ri
ngci ty;
pri
vateintz i
pcode;
Address(St ri
ngdoor No,St r
ingst r
eet,
Str
ingci
ty,
intzi
pcode)
{
thi
s.doorNo=door No;
thi
s.str
eet =st r
eet ;
thi
s.ci
ty=ci ty;
thi
s.zi
pcode=zi pcode;
}
publi
cSt ringget Door No(){
ret
urndoor No;
}
publi
cv oidset Door No(Stringdoor No){
thi
s.doorNo=door No;
}
publi
cst aticSt ri
ngget Street(){
ret
urnst reet;
}
publi
cv oidset St reet(Str
ingst reet
){
thi
s.str
eet=st reet ;
}
publi
cst aticSt ri
ngget Cit
y (){
ret
urncity ;
}
publi
cv oidset Ci ty(Stri
ngci ty){
thi
s.ci
ty=ci t
y ;
}
publi
cintget Zipcode( ){
ret
urnzipcode;
}
publi
cv oidset Zi pcode( i
ntzi pcode){
thi
s.zi
pcode=zi pcode;
}
}
classCust omer {
privateSt ringcust omer I
d;
privateSt ringcust omer Name;
privatelongcont act Number ;
privateAddr essaddr ess;
Cust omer ( ){
}
Cust omer ( Stringcust omer I
d, StringcustomerName, l
ongcontactNumber,Addressaddr
ess)
{
this.customer Id=cust omer Id;
this.customer Name=cust omer Name;
this.contact Number =cont actNumber ;
this.address=addr ess;
}
Cust omer ( Stringcust omer Name, l
ongcontactNumber ,Addr
essaddress){
this.customer Name=cust omer Name;
this.contact Number =cont actNumber ;
this.address=addr ess;
}
publ i
cSt ringget Cust omer Id(){
returncust omer Id;
}
publ i
cv oidset Customer I
d( Stringcust omerI
d){
this.customer Id=cust omer I
d;
}
publ i
cSt ringget Cust omer Name( ){
returncust omer Name;
}
publ i
cv oidset Customer Name( Stri
ngcustomer Name){
this.customer Name=cust omer Name;
}
publ i
clongget Cont act Number (){
returncont act Number ;
}
publ i
cv oidset Cont act Number (l
ongcont actNumber ){
this.contact Number=cont actNumber ;
}
publ i
cAddr essget Addr ess( ){
returnaddr ess;
}
publ i
cv oidset Addr ess( Addr essaddr ess){
this.address=addr ess;
}
publ i
cv oiddi splay Cust omer Det ails(
){
Sy stem.out .
pr int
ln("Cust omerI di s:"+getCustomer I
d())
;
Sy stem.out .
pr int
ln("Cust omerNamei s:"+getCustomer Name())
;
Sy stem.out .
pr int
ln("Cont actNumberi s:"+get ContactNumber()
);
Sy stem.out .
pr int
ln("Addr essi s: "+Addr ess.
getCity
()+""+Addr ess.
getSt
reet(
));
}
publ i
cdoubl epay Bill
(doubl etot alPri
ce)
{
totalPri
ce=t otalPrice+5. 0;
returntot alPr ice;
}
}
cl
assTest
er{
publ
icstati
cvoidmain( St
ring[]ar
gs){
Addressadd=newAddr ess( "
H.No.761"
,"Jani
purcolony"
,"Jammu",180007);
Customercust=newCust omer ("
ECE10010","
Sonakshi"
,999463590L,add)
;
cust
.displ
ayCustomerDetails()
;
doublex=cust.payBi
l
l(50);
Syst
em. out
.pr
intl
n(x)
;
}
-
--
--
--
--
--
--
---
--
--
--
--
*
ASSOCI ATIONEXERCI
SE*
cl
assCabSer vicePr ov ider{
pri
vatestat icf inal St r
ingSt r
ing=nul l
;
pri
vateSt ri
ngcabSer v iceName;
pri
vatei ntt ot al
Cabs;
publi
cCabSer vicePr ov ider (Str
ingcabSer v
iceName, inttotal
Cabs){
thi
s. cabSer v i
ceName=cabSer vi
ceName;
thi
s. total Cabs=t ot alCabs;
}
publi
cSt ri
ngget CabSer viceName( ){
returncabSer viceName;
}
publi
cv oi dset CabSer viceName( Stri
ngcabSer viceName){
thi
s. cabSer v i
ceName=cabSer v
iceName;
}
publi
cintget Tot alCabs( ){
returnt otalCabs;
}
publi
cv oi dset Tot alCabs( inttotalCabs){
thi
s. total Cabs=t ot alCabs;
}
publi
cdoubl ecal culat eRewar dPrice(Driv
erdriver)
{
doubl ebonus=0. 0;
doubl er ewar dAmount =0.
0;
i
f(cabSer viceName. equals("Halo")){
if(driver .
get Av erageRat i
ng()>=4. 5&&dr iv
er.getAverageRat
ing(
)<=5){
rewar dAmount =10* driver.
getAverageRat i
ng()
;
}
elsei f
( driver.get Av er
ageRat ing()
>=4&&dr i
ver.get
Av er
ageRati
ng()
<4.5)
{
rewar dAmount =5* dri
v er.
getAverageRating()
;
}
else{
rewar dAmount =0. 0;
}
}
el
sei
f(
cabServi
ceName. equals("
Aber")
){
i
f(dr
iver.
getAverageRati
ng()
>=4. 5&&dri
ver.
getAverageRat
ing(
)<=5){
rewardAmount =8*dri
ver.
getAv
erageRati
ng()
;
}
el
seif(dri
ver.
getAverageRati
ng()
>=4&&dr iver
.get
Av er
ageRati
ng()
<4.5)
{
rewar
dAmount
=3*
dri
ver
.get
Aver
ageRat
ing(
);
}
el
se{
rewar
dAmount
=0.
0;
}
}
else{
rewar
dAmount =0.
0;
}
bonus=Mat h.
round(r
ewar
dAmount
*100.
0)/
100.
0;
retur
nbonus;
}
}
cl
assDr
iver{
pr
ivat
eStr
ingdri
ver
Name;
pr
ivat
efl
oataver
ageRat
ing;
publ
i
cDr i
ver(Str
ingdri
verName,fl
oatav
erageRat
ing)
{
thi
s.dr
iverName=driver
Name;
thi
s.averageRat
ing=aver
ageRati
ng;
}
publ
i
cStri
ngget Dri
verName(
){
ret
urnthi
s.dri
verName;
}
publ
i
cv oidset
Dri
ver
Name(Str
ingdr
iver
Name)
{
thi
s.dri
ver
Name=dr
iver
Name;
}
publ
i
cfloatget
AverageRat
ing(
){
ret
urnthi
s.av
erageRat
ing;
}
publ
i
cv oidset
AverageRat
ing(fl
oataver
ageRat
ing)
{
thi
s.aver
ageRati
ng=averageRati
ng;
}
//DONOTMODI FYTHEMETHOD
//Yourexerci
semi ghtnotbev er
if
iedift
hebel
owmet
hodismodi fi
ed
publicStri
ngtoStr
ing()
{
return "
Driv
er\ndri
verName: "
+thi
s.dr
iver
Name+"
\nav
erageRat
ing:
"
+thi
s.averageRat
ing;
}
}
cl
assTest
er{
publ
i
cstati
cvoidmai
n(Str
ingar
gs[])
{
CabServi
cePr
ovi
dercabSer
viceProv
ider
1=newCabSer
vicePr
ovi
der
("
Hal
o",
50)
;
Dr
iverdr
iver
1=newDr
iver
("
Luke",
4.8f)
;
Dr
iverdr
iver
2=newDr
iver
("
Mark",4.
2f)
;
Dr
iverdr
iver
3=newDr
iver
("
David"
,3.9f
);
Driv
er [
]dr i
versList={driver1, dr
iver2,driv
er 3};
for(Driverdri
v er: dri
ver
sList){
Sy stem.out.print
ln(
"DriverName: "+driver.
getDri
ver
Name( ))
;
doubl ebonus=cabSer viceProv i
der1.calcul
ateRewardPri
ce(dr
iver
);
if(bonus>0)
System. out.
pri
ntln("Bonus: $"+bonus+" \
n");
else
System. out.
pri
ntln("Sorr
y ,
bonusi snotav ai
l
able!
")
;
}
/
/Cr
eat
emor
eobj
ect
sofCabSer
vicePr
ovi
derandDr
ivercl
assesf
ort
est
ingy
our
code
}
}
--
----
---
---
----
*ASSI GNMENTS*
--
---
STATI C
--
----
---
---
---
1.packageaggr egat ion;
classFood{
publ i
cSt ringf oodName;
publ i
cSt ringcui sine;
publ i
cSt ringf oodTy pe;
publ i
ci ntquant i
tyAv ailable;
publ i
cdoubl euni t
Price;
}
classOr der {
privatest atici ntor der Count erI
d;
privatei ntor der Id;
privateFood[ ]or deredFoods;
privatedoubl et otalPrice;
privateSt ringst atus;
static{
order Count er Id=100;
}
publ i
cOr der ( ){
}
publ i
cOr der ( Food[ ]or deredFoods) {
this.order edFoods=or der edFoods;
order I
d=++or derCount erId;
}
publ i
cst at ici ntget Or der CounterId(
){
returnor der Count erId;
}
publ i
cst at icv oidset Or der CounterI
d(i
ntor
der
Count
erI
d){
Or der.order Count erI
d=or derCounterI
d;
}
publ i
ci ntget Order I
d( ){
returnor der Id;
}
publ i
cv oi dset Order I
d( i
ntor derI
d){
this.order Id=or derId;
}
publ i
cFood[ ]get Order edFoods( ){
returnor der edFoods;
}
publ i
cv oidset Or der edFoods( Food[ ]order edFoods){
this.order edFoods=or der edFoods;
}
publ i
cdoubl eget Tot alPr ice(){
returnt ot alPr i
ce;
}
publ i
cv oidset Tot alPr ice( doubl et ot al
Price){
this.totalPr ice=t ot alPr ice;
}
publ i
cSt ringget St atus( ){
returnst atus;
}
publ i
cv oidset Stat us( St ringst atus){
this.status=st at us;
}
publ i
cst atici ntget Tot al NoOf Or der s()
{
return(Or der .
order Count er Id-100) ;
}
publ i
cdoubl ecal cul ateTot alPrice(St ri
ngpay mentMode){
doubl ef oodPr i
ce=0;
doubl ef inal Price=0;
fl
oatser v iceChar ge=0f ;
i
ntuni tPr i
ce=100;
foodPr i
ce+=uni tPrice* 1;
i
f( pay ment Mode. equal s( "CreditCar d"
)| |paymentMode.equal
s("
Debi
tCar
d")
){
ser v
iceChar ge=2. 0f ;
}
elsei f(pay ment Mode. equal s("
Pay Pal ")
){
ser v
iceChar ge=2. 9f ;
}
fi
nal Price=f oodPr ice+f oodPr ice*( serviceChar ge/
100);
this.setTot alPrice( final Price) ;
returnf inal Price;
}
}
classTest er 2{
publ i
cst aticv oidmai n(St ringar gs[]){
Foodf ood1=newFood( );
Foodf ood2=newFood( );
Foodf ood3=newFood( );
Foodf ood4=newFood( );
Foodf ood5=newFood( );
Food[
]tot
alFoods={f
ood1,
food2,
food3,
food4,
food5}
;
Orderorder=newOr der(
);
System.out.
printl
n(order
.calcul
ateTotal
Price(
"Cr
edi
tCar
d")
);
System.out.
printl
n(order
.getOrderedFoods())
;
}
}
--
--
--
---
--
--
--
2.
cl
assPar ti
cipant{
pri
vatestatici
ntcount er;
pri
vateStringregist
rati
onId;
priv
ateSt r
ingname;
priv
ateSt r
ingci ty;
priv
atel ongcont act Number ;
publicPar ti
cipant (Stringname, l
ongcontactNumber
,St
ri
ngci
ty)
{
thi
s. name=name;
thi
s. contact Number =cont actNumber;
thi
s. cit
y=ci ty;
registrati
onI d=" D"+++count er;
}
stati
c{
count er=10000;
}
publicstat i
cintget Count er(){
returncount er;
}
publicstat i
cv oidset Count er(
intcounter
){
Par ti
cipant .
count er=count er;
}
publicSt ri
ngget Regi strati
onId(){
returnr egistrationI d;
}
publicSt ri
ngget Name( ){
returnname;
}
publicvoi dsetName( St ri
ngname){
thi
s. name=name;
}
publicSt ri
ngget City(){
returnci t
y ;
}
publicvoi dsetCi ty(Stringci t
y){
thi
s. cit
y=ci ty;
}
publiclongget Cont act Number (){
returncont actNumber ;
}
publicvoi dsetCont act Number (longcontactNumber
){
thi
s. contact Number=cont actNumber ;
}
}
cl
assAssi
gnment
Stat
ic2{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Par
ti
cipantpar
ti
cipant
1=newPar
ti
cipant
("
Frankl
in"
,7656784323L,
"Tex
as"
);
Par
ti
cipantpar
ti
cipant
2=newPar
ti
cipant
("
Mer i
na",
7890423112L,"
NewYork"
);
/
/Cr
eat
emor
eobj
ect
sandaddt
hem t
othepar
ti
cipant
sar
rayf
ort
est
ingy
ourcode
Par
ti
cipant
[]par
ti
cipant
s={par
ti
cipant
1,par
ti
cipant
2};
f
or(Parti
cipantpartici
pant:
parti
cipant
s){
System. out.
printl
n("
Hi "+par
tici
pant
.get
Name(
)+"
! Your r
egi
str
ati
on i
d i
s
"
+par
ti
ci
pant
.getRegistrat
ionId()
);
}
}
}
--
---
----
---
------
--
3.classBooki ng{
privateSt ringcust omer Emai l;
privatei ntseat sRequi red;
privatebool eani sBooked;
privatest atici ntseat sAv ailable;
static{
seat sAv ailable=400;
}
publ i
cBooki ng( St ri
ngcust omer Emai l,i
ntseat sRequi
red)
{
this.cust omer Emai l
=cust omer Email;
this.seat sRequi r
ed=seat sRequi r
ed;
if
(seat sRequi red<=seat sAv ailable){
isBooked=t rue;
seat sAv ai l
abl e=seat sAv ail
able-seatsRequir
ed;
}
else{
isBooked=f alse;
}
}
publ i
cSt ringget Cust omer Emai l(
){
retur ncust omer Emai l
;
}
publ i
cv oidset Cust omer Emai l
( Stri
ngcust omer Email
){
this.cust omer Emai l=cust omer Emai l;
}
publ i
ci ntget Seat sRequi red(){
retur nseat sRequi red;
}
publ i
cv oidset Seat sRequi r
ed(intseat sRequi red){
this.seat sRequi r
ed=seat sRequi red;
}
publ i
cbool eani sBooked( ){
retur ni sBooked;
}
publ i
cv oidset Booked( bool eani sBooked){
this.isBooked=i sBooked;
}
publ i
cst atici ntget Seat sAv ail
abl e(){
retur nseat sAv ail
abl e;
}
publ i
cst aticv oidset Seat sAvailabl e(i
ntseatsAv ail
abl
e){
Booki ng. seat sAv ai l
able=seat sAv ai
lable;
}
cl
assTester{
publ
icstat
icv
oidmain(
Str
ing[
]ar
gs){
Booki
ngbooki
ng1=newBooking(
"j
ack@email
.com",100)
;
Booki
ngbooki
ng2=newBooking(
"j
il
l
@email
.com",350);
/
/Cr
eat
emor
eobj
ect
sandaddt
hem t
othebooki
ngsar
rayf
ort
est
ingy
ourcode
Booki
ng[
]booki
ngs={booki
ng1,
booki
ng2}
;
classAddr ess{
privateSt ringdoor No;
privatest at i
cSt ri
ngst reet;
privatest at i
cSt ri
ngci ty;
privateintz i
pcode;
Addr ess(St ringdoor No, St r
ingst r
eet,
Str
ingci
ty,
intzi
pcode)
{
this.doorNo=door No;
Addr ess.st reet=st reet;
Addr ess.ci ty=city;
this.zi
pcode=zi pcode;
}
publ i
cSt ringget Door No( ){
returndoor No;
}
publ i
cv oidset Door No( Stringdoor No){
this.doorNo=door No;
}
publ i
cst aticSt ri
ngget Street(){
returnst reet ;
}
publ i
cv oidset St reet (
St r
ingst reet
){
Addr ess.st reet=st reet;
}
publ i
cst aticSt ri
ngget Cit
y (){
returncity ;
}
publ i
cv oidset Ci ty(Stringci ty){
Addr ess.ci ty=ci ty;
}
publ i
cintget Zipcode( ){
returnzipcode;
}
publ i
cv oidset Zi pcode( i
ntzi pcode){
this.zi
pcode=zi pcode;
}
}
classCust omer {
privateSt r
ingcustomer Id;
privateSt r
ingcustomer Name;
privatelongcont actNumber ;
privateAddr essaddr ess;
publ i
cCust omer (
){
}
publ i
c Cust omer(Stri
ng customerId,
Str
ing customerName,l
ong contact
Number
,Addr
ess
addr ess){
this.customer I
d=cust omerId;
this.customer Name=cust omer Name;
this.contactNumber =contactNumber;
this.address=addr ess;
}
publ i
cCust omer (
St r
ingcustomerName,l
ongcontactNumber,
Addressaddr
ess)
{
this.customer Name=cust omer Name;
this.contactNumber =contactNumber;
this.address=addr ess;
}
publ i
cSt ringget Cust omer I
d( ){
returncust omer Id;
}
publ i
cv oidset Cust omer Id(Stri
ngcust omer I
d){
this.customer I
d=cust omer I
d;
}
publ i
cSt ringget Cust omer Name( ){
returncust omer Name;
}
publ i
cv oidset Cust omer Name( St ri
ngcust omer Name){
this.customer Name=cust omer Name;
}
publ i
clongget Cont act Number (){
returncont act Number ;
}
publ i
cv oidset Cont actNumber (l
ongcont actNumber ){
this.contact Number=cont act Number ;
}
publ i
cAddr essget Addr ess( ){
returnaddr ess;
}
publ i
cv oidset Addr ess( Addr essaddr ess){
this.address=addr ess;
}
publ i
cbool eangener ateBi l
l(Orderor der){
Sy stem.out .
pr i
nt ln("Bi l
ldet ails\ n** *
* ***
* ***
");
Sy stem.out .
pr i
nt ln("Itemsor der ed: ");
for(Foodf ood: or der .
get Order edFoods( )){
Sy stem.out .
pr i
nt ln(f ood. getFoodName( ));
}
doubl epay abl eAmount=or der .calculateTot al
Price("
CreditCar
d");
Sy stem.out .
pr i
nt ln("Pay ableAmount: $"+( int
)(payableAmount*100)
/100. 0);
returntrue;
}
publ i
cv oiddi splay Cust omer Det ails(){
Sy stem.out .
pr i
nt ln("Cust omerI di s:"+get Customer I
d())
;
Sy stem.out .
pr i
nt ln("Cust omerNamei s:"+get Customer Name())
;
Sy stem.out .
pr i
nt ln("Cont actNumberi s:"+get ContactNumber()
);
Sy stem.out .
pr i
nt ln("Addr essi s: "+Addr ess.getCity(
)+""+Addr ess.get
Str
eet
())
;
}
}
classOr der {
privatest at i
ci ntor der Count erId;
privateintor der Id;
privateFood[ ]or der edFoods;
privatedoubl et otal Pr i
ce;
privateSt ringst at us;
privateCust omercust omer ;
publ i
cCust omerget Cust omer (){
returncust omer ;
}
publ i
cv oidset Cust omer (Cust omercust omer ){
this.customer=cust omer ;
}
static{
order Count erId=100;
}
publ i
cOr der (){
}
publ i
cOr der (Food[ ]or der edFoods, Customercust omer )
{
this.order edFoods=or der edFoods;
this.cust omer =cust omer ;
order Id=++or der Count er I
d;
}
publ i
cst atici ntget Or der Count er Id(){
returnor der Count er Id;
}
publ i
cst aticv oidset Or der Count er Id(i
ntor derCounter
Id){
Or der.order Count erId=or der Count erI
d;
}
publ i
ci ntget Or der Id( ){
returnor der Id;
}
publ i
cv oidset Or der Id( intor der Id){
this.order Id=or der Id;
}
publ i
cFood[ ]get Or der edFoods( ){
returnor der edFoods;
}
publ i
cv oidset Or der edFoods( Food[ ]order edFoods){
this.order edFoods=or der edFoods;
}
publ i
cdoubl eget Tot alPr ice( ){
returnt ot alPr ice;
}
publ i
cv oidset Tot alPr i
ce( doubl et ot al
Price){
this.totalPr ice=t ot alPr ice;
}
publ i
cSt ringget St atus( ){
returnst atus;
}
publ i
cv oidset St atus( St r
ingst atus){
this.status=st at us;
}
publ i
cst atici ntget Tot alNoOf Or der s()
{
return( Or der .order Count erId-100) ;
}
publ i
cdoubl ecal cul at eTot alPrice( Stri
ngpay mentMode){
doubl ef oodPr ice=0;
doubl ef inal Pr ice=0;
fl
oatser v iceChar ge=0f ;
for(Foodf ood: or der edFoods){
foodPr i
ce+=f ood. get Uni t
Pr ice()* 1;
}
i
f( pay ment Mode. equal s("Cr editCar d"
)| |paymentMode. equal
s("
Debi
tCar
d")
){
ser vi
ceChar ge=2. 0f ;
}
elsei f(pay ment Mode. equal s( "
Pay Pal")
){
ser vi
ceChar ge=2. 9f ;
}
fi
nal Pr i
ce=f oodPr ice+f oodPr i
ce* (serviceChar ge/
100);
this.setTot alPr ice( final Pr i
ce) ;
ret
urnfi
nal
Pri
ce;
}
}
publi
ccl
assTest
er{
}
-
---
--
---
--
--
--
---
----
---
-
2.cl
assAut hor{
pri
vateStringname;
pri
vateStringemai l
Id;
pri
vatechargender ;
publi
cAut hor (
Stringname, Str
ingemailI
d,chargender
){
thi
s.name=name;
thi
s.email
Id=emai l
Id;
thi
s.gender=gender ;
}
publi
cSt r
ingget Name( ){
ret
urnname;
}
publi
cv oi
dset Name( St ri
ngname){
thi
s.name=name;
}
publi
cSt r
ingget Emai l
Id(){
ret
urnemai l
Id;
}
publi
cv oi
dset EmailId(Stri
ngemailId){
thi
s.email
Id=emai l
Id;
}
publi
ccharget Gender (){
ret
urngender ;
}
publi
cv oi
dset Gender (chargender){
thi
s.gender=gender ;
}
}
classBook{
privateSt r
ingname;
privatedoubl epr i
ce;
privateintquant it
y ;
privateAut horaut hor;
publ i
cBook( Stringname, doublepri
ce,i
ntquant i
ty,
Authorauthor
){
this.name=name;
this.pri
ce=price;
this.quantit
y=quant ity
;
this.author=author ;
}
publ i
cv oiddisplayAut horDetai
l
s(){
Sy stem.out.pri
nt l
n("Displ
ayingauthordetail
s");
Sy stem.out.pri
nt l
n("AuthorName: "+author.getName());
Sy stem.out.pri
nt l
n("AuthorEmai l
Id:"
+aut hor.getEmail
Id(
));
Sy stem.out.pri
nt l
n("AuthorGender:"+author.getGender()
);
}
publ i
cSt ri
ngget Name( ){
returnname;
}
publ i
cv oidsetName( Stri
ngname){
this.name=name;
}
publ icdoubl egetPrice( ){
returnpr i
ce;
}
publ icvoidset Pri
ce(doubl epri
ce){
this.pri
ce=pr i
ce;
}
publ ici
ntget Quant i
ty (){
returnquant i
ty;
}
publ icvoidset Quantity (
intquantit
y){
this.quantity=quant i
ty ;
}
publ icAuthorget Aut hor (
){
returnaut hor;
}
publ icvoidset Author (Authorauthor){
this.author=aut hor;
}
}
classTest er 1{
publ icstati
cv oidmai n(Stri
ngargs[])
{
Aut horaut hor1=newAut hor(
"JoshuaBloch",
"joshua@emai
l.
com"
,'
M')
;
Bookbook1=newBook( "Eff
ecti
veJav a1",
45.0,15,aut
hor
1);
book1. displayAuthor Det ail
s()
;
}
}
--
----
---
--
---
--
---
---
3.cl assRoom {
/
/Impl
ementy
ourcodeher
e
//
Uncommentt hebelowmethodaf
teri
mpl
ement
ati
onbef
orev
eri
fyi
ng
//
DONOTMODI FYTHEMETHOD
priv
ateintroomNo;
priv
ateintcapacit
y;
priv
atestati
cintroomCount
er;
stati
c{
roomCount er
=499;
}
publicRoom( )
{
capacit
y=4;
roomNo=++r oomCounter
;
}
publ
i
cintgetCapaci
ty(
){
ret
urncapacit
y;
}
publ
i
cv oidsetCapaci
ty(
intcapaci
ty){
thi
s.capacit
y=capacity;
}
publ
i
cst
ati
cintget
RoomCount
er(
){
r
etur
nroomCount
er;
}
publ
i
cstat
icvoi
dset
RoomCount
er(
introomCount
er){
Room.r
oomCounter=r
oomCounter;
}
publ
i
cintgetRoomNo(
){
ret
urnroomNo;
}
publ
i
cStri
ngtoStr
ing()
{
ret
urn"Room\nroomNo:
"+t
his.
roomNo+"
\ncapaci
ty:
"+t
his.
capaci
ty;
}
cl
assMember{
//
Impl
ementyourcodeher
e
//
Uncommentt hebelowmethodafteri
mpl
ement
ati
onbef
orev
eri
fyi
ng
//
DONOTMODI FYTHEMETHOD
pri
vat
eintmember Id;
pri
vat
eStringname;
pri
vat
eRoom r oom;
publ
i
cMember (intmember
Id,St
ringname)
{
thi
s.member I
d=memberId;
thi
s.name=name;
}
publ
i
cintget
MemberI
d(){
ret
urnmember
Id;
}
publ
i
cv oi
dset
MemberI
d(i
ntmember
Id){
thi
s.member
Id=memberI
d;
}
publ
i
cStri
nggetName(
){
ret
urnname;
}
publ
i
cv oidset
Name(
Str
ingname){
thi
s.name=name;
}
publ
i
cRoom get
Room(
){
ret
urnr
oom;
}
publ
i
cv oi
dset
Room(Room r
oom){
thi
s.r
oom =r
oom;
}
publ
i
cStri
ngtoSt
ri
ng()
{
ret
urn"Member
\nmember
Id:
"+t
his.
member
Id+"
\nname:
"+t
his.
name;
}
cl
assAdmin{
//
Impl
ementyourcodeher e
publ
i
cv oi
dassignRoom( Room[ ]rooms,Membermember ){
for
(Room room: r
ooms) {
i
f(
room.getCapacity
()>0&&r oom.getCapacit
y()
<=4){
member .setRoom( r
oom) ;
room.setCapacity(r
oom.getCapaci
ty()
-1);
break;
}
}
}
}
cl
assTester{
publ
icstat
icvoi
dmai
n(St
ri
ngar
gs[]
){
Room r
oom1=newRoom();
Room r
oom2=newRoom();
Room r
oom3=newRoom();
Room r
oom4=newRoom();
Room r
oom5=newRoom();
Room[
]tot
alRooms={r
oom1,
room2,
room3,
room4,
room5}
;
Admi
nadmi
n=newAdmi
n()
;
Membermember
1=newMember
(101,
"Serena")
;
Membermember
2=newMember
(102,
"Mar t
ha")
;
Membermember
3=newMember
(103,
"Nia")
;
Membermember
4=newMember
(104,
"Mar i
a")
;
Membermember
5=newMember
(105,
"Eva");
Member
[]member
s={member
1,member
2,member
3,member
4,member
5};
--
---
--
--
--
--
--
--
---
---
--
--
---
--
--
--
--
--
--
---
---
---
--
--
--
--
--
--
--
DAY2
--
---
--
-
*CONSTRUCTORCALLI NI NHERI
TANCE-TRYOUT*
--
---
--
--
-
classEmploy ee{
Employee( ){
System. out
.pr
int
ln("
Employ
eeconst
ruct
ori
nvoked"
);
}
}
cl
assManagerextendsEmployee{
Manager(
){
Syst
em. out.
pri
ntl
n("
Managerconst
ruct
ori
nvoked"
);
}
}
cl
assTest er{
publ
icstat
icv
oidmai
n(St
ri
ng[
]ar
gs){
Managermanager=newManager
();
}
}
--
--
--
--
--
--
--
--
--
*NEEDFORSUPERCONSTRUCTORS=TRYOUT*
--
--
--
--
--
cl
assCust omer{
pr
ivat
eSt
ri
ngcust
omer
Id;
pr
ivat
eSt
ri
ngcust
omer
Name;
publ
i
cCust omer
(Str
ingcustomer
Id,
Stri
ngcust
omer
Name){
thi
s.cust
omerI
d=cust omerI
d;
thi
s.cust
omerName=cust omerName;
}
publ
i
cCustomer(){
Syst
em.out
.pri
ntl
n("
Par
entpar
amet
erl
essconst
ruct
or"
);
}
publ
i
cStri
nggetCust
omer
Id(
){
ret
urncust
omerI
d;
}
publ
i
cv oidsetCust
omer
Id(
Str
ingcust
omer
Id){
thi
s.customerI
d=cust
omerId;
}
publ
i
cStri
nggetCust
omerName(
){
ret
urncust
omerName;
}
publ
i
cv oidsetCust
omerName(
Str
ingcust
omer
Name){
thi
s.customerName=cust
omerName;
}
publ
i
cv oiddi
spl
ayCustomerDetai
l
s(){
System.out
.pr
int
ln(
"Di
splay
ingcustomerdetails\n****
***
***
****
***
***
***
***
*")
;
System.out
.pr
int
ln(
"Cust
omerI d:"+thi
s.customer I
d);
System.out
.pr
int
ln(
"Cust
omerName: "+t hi
s.customerName);
System.out
.pr
int
ln(
);
}
cl
assRegul
arCust
omerext
endsCust
omer{
pr
ivat
efl
oatdi
scount
;
publ
i
cRegul
arCust
omer
(St
ri
ngcust
Id,
Str
ingcust
Name){
thi
s.setCustomerId(
custI
d);
thi
s.setCustomerName(custName);
thi
s.discount=5.0f;
System. out
.pr
int
ln("
Chil
dparameter
izedconst
ruct
or"
);
}
publ
i
cfloatgetDi
scount
(){
ret
urndiscount
;
}
publ
i
cv oidsetDiscount
(fl
oatdi
scount
){
thi
s.discount=discount
;
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Regular
Customerregular
Customer=newRegul
arCust
omer
("C1010"
,"JohnsKor
a")
;
regul
arCust
omer.di
splayCust
omerDet
ail
s()
;
}
}
--
--
--
--
--
--
---
--
--
----
*I
NHERI TANCETRYOUT*
--
--
--
--
-
cl
assEmpl oy ee{
i
ntempl oyeeI
d;
St
ri
ngempl oy
eeName;
//Parameteri
zedconstr
uctor
Employee(i
ntemployeeI
d,Str
ingempl oy
eeName){
t
his.
employeeI
d=empl oyeeId;
t
his.
employeeName=empl oyeeName;
}
publ
i
cintget
EmployeeI
d(){
ret
urnempl
oyeeId;
}
publ
i
cv oidsetEmpl
oyeeI
d(i
ntemploy
eeI
d){
thi
s.employeeI
d=employeeI
d;
}
publ
i
cStri
ngget
EmployeeName(
){
ret
urnempl
oyeeName;
}
publ
i
cv oidsetEmploy
eeName(
Str
ingemploy
eeName){
thi
s.employeeName=employeeName;
}
publ
i
cv oiddi
spl
ay(){
System.out
.pr
int
ln(
"Empl
oyeedet
ail
s")
;
System.out
.pr
int
ln(
"Empl
oyeeI
d:"+employ
eeId);
System.out
.pr
int
ln(
"Empl
oyeeName:"+employeeName)
;
}
}
cl
assManagerext
endsEmployee{
pr
ivat
eStri
ngdesi
gnat
ion;
Manager(i
ntemployeeI
d,St
ri
ngemployeeName,
Str
ingdesi
gnat
ion){
super(empl
oyeeId,
employeeName);
thi
s.desi
gnat
ion=designati
on;
}
publ
i
cStri
nggetDesi
gnati
on(
){
ret
urndesi
gnati
on;
}
publ
i
cv oidset
Designat
ion(
Str
ingdesi
gnat
ion){
thi
s.desi
gnat
ion=designati
on;
}
}
cl
assTest er{
publi
cstaticv oi
dmai n(
Str
ing[]ar
gs){
Managerobj =newManager (101,"
John","
Lead")
;
obj.
display(
);
System.out.pr
int
ln(
"Designat
ion:"+obj
.getDesi
gnat
ion(
));
}
}
--
--
--
--
--
--
---
--
---
-
*I
NHERI TANCE- EXERCI SE1*
--
--
--
cl
assCamer a{
pri
vateSt r
ingbrand;
pri
vatedoubl ecost;
publ
i
cCamer a(
){
thi
s.br
and="Ni
kon"
;
}
publ
i
cCamer a(St
ringbr
and,
doubl
ecost
){
thi
s.brand=brand;
thi
s.cost=cost ;
}
publ
i
cSt ri
ngget Brand(
){
ret
urnbr and;
}
publ
i
cv oidsetBrand(Str
ingbrand){
thi
s.brand=br and;
}
publ
i
cdoubl eget Cost(
){
ret
urncost ;
}
publ
i
cv oidsetCost (
doublecost){
thi
s.cost=cost ;
}
}
cl
assDi
git
alCamer
aext
endsCamer
a{
pr
ivat
eintmemor
y;
publ
i
cDigit
alCamera(
Stri
ngbr
and,
doubl
ecost
){
super(
brand,
cost)
;
thi
s.memory=16;
}
publ
i
cintget
Memory(){
ret
urnmemory;
}
publ
i
cv oi
dsetMemory(
intmemor
y){
thi
s.memory=memor y;
}
}
classTester{
publi
cstaticvoi
dmai n(
String[
]args){
Digi
tal
Camer acamer a=newDi git
alCamera(
"Canon"
,100)
;
System.out
.pri
ntl
n(camera.getBrand()+" "
+camer
a.get
Cost
()+"
"+camera.getMemory (
));
}
}
--
---
--
--
--
--
--
--
---
--
--
*METHODOVERLOADI NG-TRYOUT*
--
---
-
classShape{
//Methodt ofi
ndt heareaofcir
cle
publ
i
cf loatcal
culateArea(
fl
oatradi
us){
return3.14f*r adi
us*radi
us;
}
//Methodt ofi
ndt heareaofrectangle
publ
i
cf loatcalcul
ateAr
ea(fl
oatlength,f
loatbr
eadt
h){
returnlength*breadth;
}
/
/Codeanot
herov
erl
oadedmet
hodt
ofi
ndt
hear
eaoft
ri
angl
e
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Shapeshape=newShape(
);
f
loatcir
cleAr
ea=shape.
calculateArea(
1.7f
);
f
loatrect
angleAr
ea=shape.calculat
eArea(
2.5f
,3.
4f)
;
Sy
stem.
out
.pr
int
ln(
"Ar
eaofcir
cle:
"+circl
eAr
ea)
;
Sy
stem.
out
.pr
int
ln(
"Ar
eaofrect
angle:
"+rect
angl
eAr
ea)
;
/
/Invokethemethodt
ofindthear
eaoft
ri
angl
e
/
/Displaythear
eaoft
ri
angle
}
}
--
---
--
--
--
--
--
----
---
-
*METHODOVERLOADI NG-EXERCI SE1*
--
---
-
classPoint{
//I
mpl ementy ourcodeher e
pri
vatedoubl ex Coor dinat e;
pri
vatedoubl ey Coor dinat e;
publi
cPoi nt(doubl exCoor di nat e,doubley Coor di
nate){
this.xCoordi nate=xCoor dinat e;
this.y
Coor di nate=y Coor dinat e;
}
publi
cdoubl ecal culateDi st ance( ){
doubl ex2=t his.xCoor dinat e, x1=0.0,y2=t hi
s.yCoor
dinat
e,y1=0.
0;
doubl ed=( (x2-x1)*(x2- x1) +( y 2-
y1)*(y2-
y1) )
;
doubl edist ance=Mat h. sqr t(
d) ;
returnMat h.round( dist ance* 100.0)/100.0;
}
publi
cdoubl ecal culateDi st ance( Poi ntpoint){
doubl e x 2= t his.xCoor dinate, x 1=poi nt.
xCoordi
nate, y2= thi
s.y
Coor
dinat
e,
y1=point.
yCoor dinat e;
doubl ed=( (x2-x1)*(x2- x1) +( y 2-
y1)*(y2-
y1) )
;
doubl edist ance=Mat h. sqr t(
d) ;
returnMat h.round( dist ance* 100.0)/100.0;
}
publi
cdoubl eget xCoor dinat e( ){
returnxCoor dinate;
}
publi
cv oi
dset xCoor di
nat e( doubl ex Coordinate){
this.xCoordi nate=xCoor dinat e;
}
publi
cdoubl eget yCoordi nat e( ){
returny Coor dinate;
}
publi
cv oi
dset yCoor di
nat e( doubl ey Coordinate){
this.y
Coor di nate=y Coor di nate;
}
}
cl
assTest
er{
publ
i
cstat
icv
oidmain(
Str
ing[
]args){
Poi
ntpoi
nt1=newPoint(
3.5,1.
5);
Poi
ntpoi
nt2=newPoint(
6,4);
Syst
em.out
.pri
ntl
n("
Dist
anceofpoi
nt1fr
om or
iginis"
+poi
nt1.
cal
cul
ateDi
stance(
));
Syst
em.out
.pri
ntl
n("
Dist
anceofpoi
nt2fr
om or
iginis"
+poi
nt2.
cal
cul
ateDi
stance(
));
Syst
em.out
.pri
ntl
n("
Dist
ance of point1 from poi
nt2 i
s
"
+poi
nt1.
cal
cul
ateDi
stance(
point
2))
;
/
/Cr
eat
emor
eobj
ect
sfort
est
ingy
ourcode
}
}
--
--
--
--
--
--
---
--
*I
NHERI TANCE-ASSI
GNEMENT1*
(INCORRECT)
--
--
--
-
cl
assEmpl oyee{
/
/Impl
ementy
ourcodeher
e
//
Uncommentt hebel owmethodaf teri
mpl
ementat
ionbef
orev
eri
fyi
ng
//
DONOTMODI FYTHEMETHOD
pri
vatei
ntempl oyeeId;
pri
vateStr
ingempl oyeeName;
pri
vatedoublesalary
;
publi
cEmployee(intempl oy
eeId,Stri
ngemploy
eeName){
t
his.
employeeId=empl oyeeId;
t
his.
employeeName=empl oyeeName;
}
publi
cintget
Employ
eeI
d(){
ret
urnemployeeI
d;
}
publ
i
cv oidsetEmpl
oyeeI
d(i
ntemploy
eeI
d){
thi
s.employeeI
d=employeeI
d;
}
publ
i
cStri
ngget
EmployeeName(
){
ret
urnempl
oyeeName;
}
publ
i
cv oidsetEmploy
eeName(
Str
ingemploy
eeName){
thi
s.employeeName=employeeName;
}
publ
i
cdoublegetSal
ary
(){
ret
urnsal
ary
;
}
publ
i
cv oidset
Sal
ary
(doubl
esal
ary
){
thi
s.sal
ary=sal
ary
;
}
publi
cStr
ingtoStr
ing(
){
r
etur
n "
Employee\nemployeeId: "
+this.
get
Empl
oyeeI
d()
+"\
nempl
oyeeName:
"
+thi
s.get
EmployeeName()+"
\nsalar
y:"+t
his.get
Sal
ary
();
}
}
cl
assPer
manent
Empl
oyeeext
endsEmpl
oyee{
/
/Impl
ementy
ourcodeher
e
//
Uncommentt hebelowmet hodaf
teri
mplementat
ionbeforev
eri
fyi
ng
//
DONOTMODI FYTHEMETHOD
pri
vatedoubl ebasi cPay;
pri
vatedoubl ehr a;
pri
vatefloatexper i
ence;
publi
cPer manent Employee(intempId,Str
ing name,doubl
ebasicPay,doubl
ehr
a,f
loat
experi
ence){
super(empI d, name);
thi
s.basicPay =basicPay ;
thi
s.hra=hr a;
thi
s.experience=ex perience;
}
publ
i
cvoidcalcul
ateMonthl
ySal
ary
(){
f
loatexper
ienceComponent
Percent
age=0f
;
i
f(t
his.
experi
ence>=3&&t hi
s.experi
ence<5){
experi
enceComponentPer
centage=5;
}el
seif(t
his.
exper
ience>=5&&t his.
exper
ience<10){
experi
enceComponentPer
centage=7;
}el
seif(t
his.
exper
ience>=10){
experi
enceComponentPer
centage=12;
}
doublesalar
y=( f
loat
)((
thi
s.basi
cPay*(
exper
ienceComponent
Per
cent
age/
100)
)+
t
his.
basi
cPay+this.
hra);
thi
s.set
Salar
y(sal
ary);
}
publi
cdoubl
egetBasi
cPay
(){
ret
urnbasi
cPay
;
}
publ
i
cv oidset
Basi
cPay(
doubl
ebasi
cPay
){
thi
s.basi
cPay=basi
cPay;
}
publ
i
cdoubleget
Hra(
){
ret
urnhr
a;
}
publ
i
cv oidset
Hra(doubl
ehr
a){
thi
s.hra=hra;
}
publ
i
cfloatget
Experi
ence(
){
ret
urnexperi
ence;
}
publ
i
cv oidset
Experi
ence(
fl
oatexper
ience){
thi
s.exper
ience=exper
ience;
}
publi
cStri
ngtoStri
ng(){
r
eturn "PermanentEmployee\
nempl oy
eeId:
"
+thi
s.get
EmployeeId(
)+"\
nemploy
eeName: "
+this.
getEmploy
eeName( )+"
\nsalar
y:
"
+thi
s.get
Salar
y()
+"\nbasi
cPay:"+thi
s.get
Basi
cPay
()+"
\nhra: "+t
his.
get
Hr a(
)+"
\nexperi
ence:
"
+thi
s.get
Experi
ence();
}
}
cl
assCont
ract
Empl
oyeeext
endsEmpl
oyee{
/
/Impl
ementy
ourcodeher
e
//
Uncommentt hebel owmet hodafteri
mplement
ati
onbef
orev
erify
ing
//
DONOTMODI FYTHEMETHOD
pri
vatedoubl ewage;
pri
vatefloathour sWor ked;
publi
cCont r
actEmpl oyee(intempId,Str
ingname,
doubl
ewage,f
loathour
sWor
ked)
{
super(empI d,name) ;
thi
s.wage=wage;
thi
s.hoursWor ked=hour sWorked;
}
publi
cv oidcalculateSalary()
{
setSalary(
hour sWor ked*wage);
publi
cdoubleget Wage( ){
returnwage;
}
publ
i
cv oi dsetWage( doublewage){
thi
s. wage=wage;
}
publ
i
cf loatget HoursWorked( ){
returnhour sWor ked;
}
publ
i
cv oi dsetHour sWorked( fl
oathour sWorked){
thi
s. hoursWor ked=hour sWor ked;
}
publi
cStri
ngt oString()
{
return" ContractEmpl oyee\nempl oyeeI
d:"+thi
s.get
Empl
oyeeI
d()+"
\nemployeeName:
"+t
his.
getEmpl oyeeName( )
+"\nsalary: "+t
his.
getSal
ary
()+"\
nwage:
"+t
his.
getWage( )+"\nhoursWor ked: "
+this.
getHoursWorked()
;
}
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
PermanentEmpl oyee permanentEmployee = new Per manent
Employee(
711211,
"Raf
ael
",1850,115,3.5f
);
permanentEmploy ee.
cal
culateMont
hlySal
ary();
System.out.
pri
ntln("
Hi"+permanentEmployee.get
Employ
eeName()+"
,yoursalar
yis
$"+Mat
h.round(per
manent Employee.get
Sal
ary(
)*100)/100.
0);
Cont
ract
Empl
oyeecont
ract
Empl
oyee=new Cont
ract
Empl
oyee(
102,"
Jenni
fer
",16,
90)
;
contract
Employee.cal
culateSalary
( )
;
System.out
.pri
ntl
n("Hi" +contractEmployee.
getEmpl
oyeeName(
)+"
,your sal
aryi
s
$"
+Mat
h.round(cont
ract
Empl oyee.getSalar
y (
)*100)
/100.
0);
/
/Cr
eat
emor
eobj
ect
sfort
est
ingy
ourcode
}
--
--
--
--
--
--
--
--
*METHODOVERLOADI NGASSI GNMENT1*
--
--
--
--
cl
assBi l
l
{
//
Impl ementy ourcodeher e
publ
icdoubl efindPr ice(i
nti t
emI d){
doublepr ice=0;
swi t
ch( it
emI d){
case1001: pri
ce=25;
br eak;
case1002: pri
ce=20;
br eak;
case1003: pri
ce=23;
br eak;
case1004: pri
ce=18;
br eak;
def ault:price=0;
}
returnpr i
ce;
}
publ
icdoubl efindPr ice(Stri
ngbr andName, St
ri
ngi
temTy
pe,
intsi
ze)
{
doublepr ice=0;
i
f(brandName. equal s("
Puma" )){
if(it
emTy pe.equal s("T-
shirt
"))
{
if
(size==34| |size==36){
price=25;
}
}
el seif(
it
emTy pe.equals("Ski
rt"
)){
if(
siz e==38| |size==40){
price=20;
}
}
}
elseif(brandName. equals("Reebok"))
{
if(it
emTy pe.equal s("T-
shirt
"))
{
if
(size==34| |size==36){
price=23;
}
}
el
sei
f(i
temTy pe.
equal
s("
Skir
t"
)){
i
f(
size==38||si
ze==40)
{
pr
ice=18;
}
}
}
r
etur
npr
ice;
}
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Bi
l
lbi
l
l=newBi
l
l()
;
doublepri
ce=bil
l.
fi
ndPri
ce(1001);
i
f(pr
ice>0)
Syst
em.out
.pr
int
ln(
"Pri
ceofthesel
ectedit
em i
s$"
+pr
ice)
;
el
se
Syst
em.out
.pr
int
ln(
"TheItem I
disi
nvali
d")
;
pr
ice=bi l
l
.fi
ndPr
ice(
"Reebok",
"T-shir
t"
,34);
i
f(
price>0)
Sy st
em.out.
pri
ntl
n("
Priceoft heselect
edit
em is$"
+pr
ice)
;
el
se
Sy st
em.out.
pri
ntl
n("
Thev aluesarenotv al
i
d")
;
}
}
--
--
--
--
--
--
--
--
--
--
-
*METHOD- OVERLOADI
NGASSIGNMENT2*
--
--
--
-
cl
assPoint{
//
ReusethecodeofMet
hodOver
loadi
ng-Exer
cise1
/
/Uncommentt
hebelowmet
hodaf
teri
mpl
ement
ati
onbef
orev
eri
fyi
ng
/
/DONOTMODI FYTHEMETHOD
pri
vatedoublex Coor dinat e;
pri
vatedoubley Coor dinat e;
publi
cPoint(doublexCoor di nat
e, doubl
ey Coordinate)
{
thi
s.x
Coor dinate=xCoor di
nate;
thi
s.y
Coor dinate=y Coor di
nate;
}
publi
cdoublecal culateDi st ance(){
doublex2=t his.xCoor dinate,x1=0.0,y2=thi
s.yCoordi
nate,y
1=0.
0;
doubled=( (x2-x1)*(x2- x1)+( y2-
y1)*(y2-
y1))
;
doubledistance=Mat h. sqrt(
d);
ret
urnMat h.round( dist ance*100.0)/100.0;
}
publi
cdoublecal culateDi st ance(Pointpoint)
{
double x 2= t his.xCoor di
nate, x 1=point.xCoor
dinat
e, y2= thi
s.y
Coor
dinat
e,
y
1=point
.yCoordi
nat e;
doubled=( (x2-x1)*(x2- x1)+( y2-
y1)*(y2-
y1))
;
doubledistance=Mat h. sqrt(
d);
ret
urnMath.round(dist
ance* 100.
0)/
100.0;
}
publ
i
cdoubleget xCoordi
nate(){
ret
urnxCoordinate;
}
publ
i
cv oi
dsetxCoor di
nate(
doubl exCoordi
nate){
thi
s.x
Coordinate=xCoor dinate;
}
publ
i
cdoubleget yCoordi
nate(){
ret
urnyCoordinate;
}
publ
i
cv oi
dsetyCoordinate(
doubl eyCoordi
nate){
thi
s.y
Coordinate=y Coordinate;
}
publi
cStr
ingtoStr
ing()
{
r
etur
n "
Point
\nxCoor
dinat
e: "
+thi
s.get
xCoor
dinat
e()
+"\
nyCoor
dinat
e:
"
+thi
s.get
yCoordi
nate(
);
}
}
cl
assTr iangle{
/
/ I
mpl ementy ourcodeher e
pri
vat ePoi ntpoint1;
pri
vat ePoi ntpoint2;
pri
vat ePoi ntpoint3;
publ
icTr iangle(){
point1=newPoi nt(
0,0);
point2=newPoi nt(
1,1);
point3=newPoi nt(
2,5);
}
publ
ic Triangle(double point1XCoor di
nate,
double poi
nt1YCoor
dinat
e,doubl
e
poi
nt2XCoor dinate,
doubl e point2YCoor di
nate,
double poi
nt3XCoor
dinat
e,doubl
e
poi
nt3YCoor dinate){
point1=newPoi nt(
point1XCoor dinate,point1YCoordi
nate)
;
point2=newPoi nt(
point2XCoor dinate,point2YCoordi
nate)
;
point3=newPoi nt(
point3XCoor dinate,point3YCoordi
nate)
;
}
publ
icTr iangle(Pointpoint1,Pointpoint2, Pointpoint3)
{
thi
s.point1=poi nt1;
thi
s.point2=poi nt2;
thi
s.point3=poi nt3;
}
publ
i
cdoublecal culatePeri
met er
(){
doubleperimet er,
a,
b,c;
a=point1.
calcul at
eDist
ance(point
2);
b=point2.
calcul at
eDistance(
point
3);
c=point3.
calcul at
eDist
ance(point
1);
peri
meter=a+b+c;
ret
urnMat h.round(peri
met er
*100)/
100.
0;
}
publ
i
cdoublecalculat
eArea()
{
doubl
earea,s,a,
b,
c;
a=poi
nt1.calcul
ateDi
stance(
poi
nt2)
;
b=poi
nt2.calcul
ateDi
stance(
poi
nt3)
;
c=point3.
cal cul
ateDistance(
point1)
;
s=(a+b+c)
/ 2;
area=Math. sqrt
(s*(s-
a)*(s-
b)*(
s-c))
;
retur
nMat h.round(area*100)/
100.0;
}
publ
i
cPoi ntgetPoint1(){
ret
urnpoint1;
}
publ
i
cv oidsetPoint1(Poi
ntpoi
nt1){
thi
s.point
1=poi nt1;
}
publ
i
cPoi ntgetPoint2(){
ret
urnpoint2;
}
publ
i
cv oidsetPoint2(Poi
ntpoi
nt2){
thi
s.point
2=poi nt2;
}
publ
i
cPoi ntgetPoint3(){
ret
urnpoint3;
}
publ
i
cv oidsetPoint3(Poi
ntpoi
nt3){
thi
s.point
3=poi nt3;
}
}
cl
assTest
er{
publ
i
cstati
cvoi
dmai n(
Str
ing[
]args){
Tri
angl
etri
angl
e1=newTr iangl
e();
Tri
angl
etri
angl
e2=newTr iangl
e(1,2,
6,5,
5,1)
;
Pointpoi
nt1=newPoint
(2,
1);
Pointpoi
nt2=newPoint
(4,
4);
Pointpoi
nt3=newPoint
(9,
1);
Tri
anglet
riangl
e3=newTri
angl
e(poi
nt1,
poi
nt2,
poi
nt3)
;
Sy
stem.
out
.pr
int
ln(
"Peri
meteroftr
iangl
e1is"+t
riangle1.cal
cul
atePeri
met
er(
));
Sy
stem.
out
.pr
int
ln(
"Areaoftr
iangl
e1is"+t
ri
angle1.calculat
eAr
ea())
;
Sy
stem.
out
.pr
int
ln(
"Peri
meteroftr
iangl
e2is"+t
riangle2.cal
cul
atePeri
met
er(
));
Sy
stem.
out
.pr
int
ln(
"Areaoftr
iangl
e2is"+t
ri
angle2.calculat
eAr
ea())
;
Sy
stem.
out
.pr
int
ln(
"Peri
meteroftr
iangl
e3is"+t
riangle3.cal
cul
atePeri
met
er(
));
Sy
stem.
out
.pr
int
ln(
"Areaoftr
iangl
e3is"+t
ri
angle3.calculat
eAr
ea())
;
/
/Cr
eat
emor
eobj
ect
sofTr
iangl
ecl
assf
ort
est
ingy
ourcode
}
}
-
--
--
--
--
--
--
--
--
--
-
--
--
--
--
--
--
--
--
--
--
DAY3
-
---
--
--
--
--
--
--
--
--
--
--
*METHODOVERRI DI
NG-TRYOUT*
-
---
1.cl
assTester{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Cust
omercustomer=newCustomer(
);
Syst
em.out
.pr
int
ln(
"Fi
nal
bil
lamount
:"+cust
omer
.pay
Bil
l
(40.
0))
;
/
/ParentRefer
ence->Par
entObj
ect
Regul
arCust
omerr egul
arCustomer=newRegular
Cust
omer(
);
Syst
em.out.
pri
ntl
n("Fi
nalbi
llamount
:"+r
egul
arCust
omer.
pay
Bil
l(
40.
0))
;
/
/Chi l
dRefer
ence->ChildObject
CustomerregCust=newRegul arCustomer()
;
//ParentRefer
ence->ChildObject
Syst
em. out
.pri
ntl
n("
Final
Bill
:"+r egCust
.payBi
l
l(40.
0))
;
}
}
cl
assCust
omer{
publ
i
cdoublepay Bill
(doublet
otal
Price){
System.out
.print
ln("
Final
bil
lforthecust
omeri
scal
cul
atedher
e")
;
ret
urntotal
Price;
}
}
cl
assRegul
arCust
omerext
endsCust
omer{
@Overr
ide
publ
i
cdoublepay Bil
l(
doubl etot
alPri
ce){
System.out.pri
ntl
n( "
Finalbi
llf
ortheregul
arcust
omeriscal
cul
atedher
e")
;
doublepriceAfterDiscount=total
Pri
ce*(1-(5f/100)
);
ret
urnpriceAf t
erDiscount;
}
-
---
--
--
--
--
-
2.cl
assTester{
publ
icst
aticvoi
dmai n(
Stri
ngar gs[]
){
Bankbank;
bank=newABCBank( );
System.out
.pr
intl
n("
ABCBank-Rat eofInter
est(
%):"
+bank.
getRateOfInter
est
())
;
bank=newDEFBank( )
;
System.out
.pr
intl
n("
DEFBank-Rat eofInt
erest
(%):"
+bank.
getRateOfInter
est
())
;
bank=newGHI Bank()
;
System.out
.pr
intl
n("
GHI Bank-RateofInt
erest
(%):
"
+bank.
getRateOfInter
est
())
;
}
}
cl
assBank{
publi
cfloatgetRat
eOfI
nterest(
){
r
eturn0;
}
//remov etheaccessspecif
ierandobser
vet
heout
put
}
cl
assABCBankext endsBank{
publi
cfloatgetRat
eOfInt
erest
(){
r
eturn8.99f;
}
//Keepingtheparentaccessspeci
fi
eraspubl
i
c,r
emov
ethechi
l
daccess
//speci
fierandobservetheoutput
}
cl
assDEFBankext endsBank{
publi
cfloatgetRat
eOfInt
erest(
){
r
eturn9.4f;
}
//Keepingtheparentaccessspeci
fi
eraspubl
ic,
changethechi
l
daccess
//speci
fiertopri
vate/
protect
edandobser
vetheoutput
}
cl
assGHI Bankext endsBank{
publ
icf l
oatget RateOf
Int er
est(){
ret
ur n8.1f;
}
}
--
--
--
--
--
--
--
----
--
---
--
---
---
-
*GENERICMETHODTRYOUT*
cl
assDy nami cBindingTester{
publ
icst ati
cv oidmain(St r
ing[
]args){
Empl oyeeempl oyee=newEmpl oyee(
);
Managermanager=newManager ();
Empl oyeeeduEmpl oyee=newEducat or()
;
Educatormanager Edu=newManager (
);
di
spl
ayEmpl
oyeeDet
ail
s(employ
ee);
di
spl
ayEmpl
oyeeDet
ail
s(manager);
di
spl
ayEmpl
oyeeDet
ail
s(eduEmployee)
;
di
spl
ayEmpl
oyeeDet
ail
s(managerEdu);
}
//Employeeref erencecanaccepti
tsobjectandanyofthechil
dobj
ect
publ
icstati
cv oiddisplay
EmployeeDetai
ls(
Employeeemployee){
empl oyee.display
Detai
l
s();//displ
ayDetai
l
si nv
oked willbebased on t
heobj
ect
r
ecei
ved
}
}
cl
assEmployee{
Str
ingname="JamesAnt
hony
";
publ
i
cv oiddi
spl
ayDetai
ls(
){
System.out
.pr
int
ln(
name+"i
sanempl
oyee"
);
}
}
cl
assEducat
orext
endsEmpl
oyee{
publ
i
cv oiddi
spl
ayDetai
ls(
){
System.out
.pr
int
ln(
name+"i
saneducat
or"
);
}
}
cl
assManagerext endsEducat or{
publi
cv oiddi splay Det ails(){
Sy stem. out .println(name+"i samanager ")
;
}
}
--
--
--
--
--
--
---
--
*METHODOVERRI DINGEXERCI SE*
--
--
-
cl
assUser {
//
Impl ementy ourcodeher e
pri
vatei ntid;
pri
vateSt r
inguser Name;
pri
vateSt r
ingemai l
Id;
pri
vatedoubl ewal letBalance;
publi
cUser (inti d,St r
inguser Name, Stri
ngemai l
Id,
doublewal
let
Balance)
{
this.i
d=i d;
this.user Name=user Name;
this.emai l
Id=emai lI
d;
this.wal l
et Bal ance=wal l
etBalance;
}
publi
cbool eanmakePay ment (doublebil
lAmount )
{
i
f(this.get Wal l
etBal ance( )>=bi
ll
Amount )
{
doubl ex =( doubl e)(this.
getWall
etBal
ance( )
-bi
l
lAmount)
;
this.set Wal letBalance( x);
retur nt rue;
}
else{
retur nf alse;
}
}
publi
ci ntget Id( ){
returni d;
}
publi
cv oidset I
d( i
nti d){
this.i
d=i d;
}
publi
cSt ri
ngget User Name( ){
returnuser Name;
}
publi
cv oidset User Name( StringuserName){
this.user Name=user Name;
}
publi
cSt ri
ngget Emai l
Id(){
returnemai lId;
}
publi
cv oidset Emai l
Id(St r
ingemai lI
d){
this.emai l
Id=emai l
Id;
}
publi
cdoubl eget Wal letBalance( ){
returnwal l
et Balance;
}
publi
cv oidset Wal letBalance( doublewallet
Balance){
this.wal l
et Bal ance=wal letBal
ance;
}
cl
assPr emi umUserext endsUser {
//Impl ementy ourcodeher e
privatei ntrewar dPoi nts;
publ i
ci ntget Rewar dPoi nts(){
returnr ewar dPoint s;
}
publ i
cv oidset Rewar dPoi nts(i
ntrewar dPoints){
this.rewar dPoints=r ewar dPoints;
}
publ i
cPr emi umUser (intid,Stri
nguser Name, Str
ingemai l
Id,
doubl
ewal
l
etBal
ance)
{
super (i
d, userName, emai l
Id,walletBal
ance) ;
}
@Ov erride
publ
i
cbool eanmakePay ment (doublebill
Amount )
{
if
(this.get Wal l
etBalance( )>=bill
Amount ){
i
nty =(int)(t
his.r
ewar dPoints+((bill
Amount *10)/100)
);
this.setRewar dPoi nts( y
);
doubl ez=( double)( t
his.getWalletBalance()
-bil
lAmount);
this.setWal let
Bal ance( z);
returnt rue;
}
else{
returnf alse;
}
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Useruser=newUser
(101,
"Joe"
,"j
oe@abc.
com"
,100)
;
Pr
emi
umUserpr
emi
umUser=newPr
emi
umUser
(201,
"Ji
l
l",
"ji
l
l@abc.
com"
,300)
;
pr
ocessPay
ment
(user
,70)
;
pr
ocessPay
ment
(pr
emi
umUser
,150)
;
pr
ocessPay
ment
(pr
emi
umUser
,80)
;
pr
ocessPay
ment
(pr
emi
umUser
,120)
;
publ
i
cst at
icvoidprocessPay ment
(Useruser,doubl
ebill
Amount){
if(
user.makePay ment(bi
ll
Amount)){
System.out.
pri
ntl
n( "
Congrat
ulat
ions"+user .
getUser
Name()+"
,paymentof$"+
bi
ll
Amount+"wassuccessf ul
!"
);
}else{
System.out.
pri
ntl
n( "
Sorr
y"+user .
getUser
Name( )+",youdonothaveenough
bal
ancet
opayt hebill
!"
);
}
Syst
em.
out
.pr
int
ln(
"Yourwal
l
etbal
ancei
s$"+user
.get
Wal
l
etBal
ance(
));
i
f(userinstanceofPr emiumUser){
Pr
emi umUserpr emiumUser=(Pr
emiumUser
)user;
Sy
st em. out.
pri
ntln(
"Youhav
e"+premiumUser
.get
Rewar
dPoi
nts(
)+"poi
nts!
")
;
}
Syst
em.out .print
ln(
);
}
}
--
---
--
--
--
--
--
--
--
--
--
--
-
*EQUALOBJECTSTRYOUT*
--
---
--
--
classFood{
pr
ivat
eStri
ngfoodName;
pr
ivat
eStri
ngcuisi
ne;
pr
ivat
eStri
ngfoodType;
pr
ivat
eintquant
ity
Avail
abl
e;
pr
ivat
edoubleunit
Pri
ce;
publ
i
cStri
nggetFoodName(
){
ret
urnfoodName;
}
publ
i
cv oi
dsetFoodName(St
ri
ngf
oodName){
thi
s.f
oodName=f oodName;
}
publ
i
cStri
nggetCuisi
ne(
){
ret
urncui
sine;
}
publ
i
cv oidsetCui
sine(
Str
ingcui
sine){
thi
s.cui
sine=cuisi
ne;
}
publ
i
cStri
nggetFoodTy
pe(
){
ret
urnfoodTy
pe;
}
publ
i
cv oi
dsetFoodTy
pe(St
ringf
oodTy
pe){
thi
s.f
oodType=foodType;
}
publ
i
cintget
Quanti
tyAv
ailabl
e(){
ret
urnquant
it
yAvai
lable;
}
publ
i
cv oidset
Quanti
tyAvai
l
abl
e(i
ntquanti
tyAv
ail
abl
e){
thi
s.quant
it
yAvai
labl
e=quanti
tyAvai
l
able;
}
publ
i
cdoubl
eget
Uni
tPr
ice(
){
r
etur
nuni
tPr
ice;
}
publ
i
cv oidset
Unit
Pri
ce(doubl
euni
tPr
ice){
thi
s.uni
tPr
ice=unit
Price;
}
}
cl
assTest
er{
publ
i
cst at
icvoidmai n(Stri
ng[
]ar gs){
Foodf oodOne=newFood( )
;
foodOne.setFoodName( "
Sandwi ch")
;
foodOne.setCuisine("
Continental"
);
foodOne.setFoodTy pe("
Veg" );
foodOne.setQuant i
tyAvai
lable(100);
foodOne.setUnitPri
ce(10);
FoodfoodTwo=newFood( )
;
foodTwo.
setFoodName( "
Sandwi ch")
;
foodTwo.
setCuisine("
Continental"
);
foodTwo.
setFoodTy pe("
Veg" );
foodTwo.
setQuant i
tyAvai
lable(200);
foodTwo.
setUnitPri
ce(10);
i
f(f
oodOne==foodTwo){
Syst
em.out
.pr
int
ln(
"Thef
oodobj
ect
sar
esame!")
;
}el
se{
Syst
em.out
.pr
int
ln(
"Thef
oodobj
ect
sar
edif
fer
ent!
")
;
}
}
}
--
---
--
--
--
--
--
*EQUALSTRYOUT*
classFood{
pr
ivat
eStri
ngfoodName;
pr
ivat
eStri
ngcuisi
ne;
pr
ivat
eStri
ngfoodType;
pr
ivat
eintquant
ity
Avail
abl
e;
pr
ivat
edoubleunit
Pri
ce;
publ
i
cStri
nggetFoodName(
){
ret
urnfoodName;
}
publ
i
cv oi
dsetFoodName(St
ri
ngf
oodName){
thi
s.f
oodName=f oodName;
}
publ
i
cStri
nggetCuisi
ne(
){
ret
urncui
sine;
}
publ
i
cv oidsetCui
sine(
Str
ingcui
sine){
thi
s.cui
sine=cuisi
ne;
}
publ
i
cStri
nggetFoodTy
pe(
){
ret
urnfoodTy
pe;
}
publ
i
cv oi
dsetFoodTy
pe(St
ringf
oodTy
pe){
thi
s.f
oodType=foodType;
}
publ
i
cintget
Quanti
tyAv
ailabl
e(){
ret
urnquant
it
yAvai
lable;
}
publ
i
cv oidset
Quanti
tyAvai
l
abl
e(i
ntquanti
tyAv
ail
abl
e){
thi
s.quant
it
yAvai
labl
e=quanti
tyAvai
l
able;
}
publ
i
cdoubleget
UnitPr
ice(
){
ret
urnuni
tPr
ice;
}
publ
i
cv oidset
Unit
Pri
ce(doubl
euni
tPr
ice){
thi
s.uni
tPr
ice=unit
Price;
}
cl
assTest
er{
publ
i
cst at
icvoidmai n(Stri
ng[
]ar gs){
Foodf oodOne=newFood( )
;
foodOne.setFoodName( "
Sandwi ch")
;
foodOne.setCuisine("
Continental"
);
foodOne.setFoodTy pe("
Veg" );
foodOne.setQuant i
tyAvai
lable(100);
foodOne.setUnitPri
ce(10);
FoodfoodTwo=newFood( )
;
foodTwo.
setFoodName( "
Sandwi ch")
;
foodTwo.
setCuisine("
Continental"
);
foodTwo.
setFoodTy pe("
Veg" );
foodTwo.
setQuant i
tyAvai
lable(200);
foodTwo.
setUnitPri
ce(10);
i
f(f
oodOne.
equals(
foodTwo)){
Syst
em.out
.pri
ntl
n("
foodOneandf
oodTwoar
esame!
")
;
}el
se{
Sy
stem.
out
.pr
int
ln(
"foodOneandf
oodTwoar
edi
ff
erent
!"
);
}
FoodfoodThree=newFood( )
;
foodThr
ee.set
FoodName( "
Bur ger"
);
foodThr
ee.set
Cuisine("
Continental"
);
foodThr
ee.set
FoodTy pe("
Veg" );
foodThr
ee.set
Quant i
tyAvai
lable(100);
foodThr
ee.set
UnitPri
ce(10);
i
f(f
oodOne.
equals(
foodThree)
){
Syst
em.out
.pri
ntl
n("
foodOneandf
oodThr
eear
esame!")
;
}el
se{
Syst
em.out
.pri
ntl
n("
foodOneandf
oodThr
eear
edif
fer
ent!
")
;
}
}
}
--
--
--
--
--
--
--
--
--
--
--
--
--
*HASHCODETRYOUT*
cl
assFood{
pr
ivat
eStri
ngfoodName;
pr
ivat
eStri
ngcuisi
ne;
pr
ivat
eStri
ngfoodType;
pr
ivat
eintquant
ity
Avail
abl
e;
pr
ivat
edoubleunit
Pri
ce;
publ
i
cStri
nggetFoodName(
){
ret
urnfoodName;
}
publ
i
cv oi
dsetFoodName(St
ri
ngf
oodName){
thi
s.f
oodName=f oodName;
}
publ
i
cStri
nggetCuisi
ne(
){
ret
urncui
sine;
}
publ
i
cv oidsetCui
sine(
Str
ingcui
sine){
thi
s.cui
sine=cuisi
ne;
}
publ
i
cStri
nggetFoodTy
pe(
){
ret
urnfoodTy
pe;
}
publ
i
cv oi
dsetFoodTy
pe(St
ringf
oodTy
pe){
thi
s.f
oodType=foodType;
}
publ
i
cintget
Quanti
tyAv
ailabl
e(){
ret
urnquant
it
yAvai
lable;
}
publ
i
cvoi
dset
Quant
it
yAv
ail
abl
e(i
ntquant
it
yAv
ail
abl
e){
t
his.
quant
it
yAv
ail
abl
e=quant
it
yAv
ail
abl
e;
}
publ
i
cdoubleget
UnitPr
ice(
){
ret
urnuni
tPr
ice;
}
publ
i
cv oidset
Unit
Pri
ce(doubl
euni
tPr
ice){
thi
s.uni
tPr
ice=unit
Price;
}
//hashCodemet hodov er
ri
dden
@Ov erride
publi
ci nthashCode( ){
i
ntr esult=1;
result=r esul
t+( f
oodName.hashCode(
));
returnresult;
}
}
cl
assTest
er{
publ
i
cst at
icvoidmai n(Stri
ng[
]ar gs){
Foodf oodOne=newFood( )
;
foodOne.setFoodName( "
Sandwi ch")
;
foodOne.setCuisine("
Continental"
);
foodOne.setFoodTy pe("
Veg" );
foodOne.setQuant i
tyAvai
lable(100);
foodOne.setUnitPri
ce(10);
FoodfoodTwo=newFood( )
;
foodTwo.
setFoodName( "
Sandwi ch")
;
foodTwo.
setCuisine("
Continental"
);
foodTwo.
setFoodTy pe("
Veg" );
foodTwo.
setQuant i
tyAvai
lable(200);
foodTwo.
setUnitPri
ce(10);
i
f(f
oodOne.
equals(
foodTwo)){
Syst
em.out
.pri
ntl
n("
foodOneandf
oodTwoar
esame!")
;
}el
se{
Syst
em.out
.pri
ntl
n("
foodOneandf
oodTwoar
edif
fer
ent!
")
;
}
Sy
stem.
out
.pr
int
ln(
"Hashcodef
orf
oodOne:
"+foodOne.
hashCode()
);
Sy
stem.
out
.pr
int
ln(
"Hashcodef
orf
oodTwo:"+f
oodTwo.hashCode(
));
FoodfoodThree=newFood( )
;
foodThr
ee.set
FoodName( "
Bur ger"
);
foodThr
ee.set
Cuisine("
Continental"
);
foodThr
ee.set
FoodTy pe("
Veg" );
foodThr
ee.set
Quant i
tyAvai
lable(100);
foodThr
ee.set
UnitPri
ce(10);
i
f(f
oodOne.
equals(
foodThree)
){
Syst
em.out
.pri
ntl
n("
foodOneandf
oodThr
eear
esame!")
;
}el
se{
Syst
em.out
.pri
ntl
n("
foodOneandf
oodThr
eear
edif
fer
ent!
")
;
}
Sy
stem.
out
.pr
int
ln(
"Hashcodef
orf
oodOne:"+f
oodOne.hashCode()
);
Sy
stem.
out
.pr
int
ln(
"Hashcodef
orf
oodThr
ee:"+foodThr
ee.hashCode(
));
}
}
--
--
--
--
--
--
--
--
--
--
--
--
-
*t
o-STRINGTRYOUT*
cl
assFood{
pr
ivat
eStri
ngfoodName;
pr
ivat
eStri
ngcuisi
ne;
pr
ivat
eStri
ngfoodType;
pr
ivat
eintquant
ity
Avail
abl
e;
pr
ivat
edoubleunit
Pri
ce;
publ
i
cStri
nggetFoodName(
){
ret
urnfoodName;
}
publ
i
cv oi
dsetFoodName(St
ri
ngf
oodName){
thi
s.f
oodName=f oodName;
}
publ
i
cStri
nggetCuisi
ne(
){
ret
urncui
sine;
}
publ
i
cv oidsetCui
sine(
Str
ingcui
sine){
thi
s.cui
sine=cuisi
ne;
}
publ
i
cStri
nggetFoodTy
pe(
){
ret
urnfoodTy
pe;
}
publ
i
cv oi
dsetFoodTy
pe(St
ringf
oodTy
pe){
thi
s.f
oodType=foodType;
}
publ
i
cintget
Quanti
tyAv
ailabl
e(){
ret
urnquant
it
yAvai
lable;
}
publ
i
cv oidset
Quanti
tyAvai
l
abl
e(i
ntquanti
tyAv
ail
abl
e){
thi
s.quant
it
yAvai
labl
e=quanti
tyAvai
l
able;
}
publ
i
cdoubleget
UnitPr
ice(
){
ret
urnuni
tPr
ice;
}
publ
i
cv oidset
Unit
Pri
ce(doubl
euni
tPr
ice){
thi
s.uni
tPr
ice=unit
Price;
}
//hashCodemet hodov er
ri
dden
@Ov erride
publi
ci nthashCode( ){
i
ntr esult=1;
result=r esul
t+( f
oodName.hashCode(
));
returnresult;
}
//toStr
ingisoverriddent oprovideacust om t ext
ualrepresentat
ion
@Ov err
ide
publi
cSt r
ingtoStri
ng( ){
ret
urn"Food- >"+" Foodname: "+t his.f
oodName+" ,Cui
sine:"
+t his.cuisine+" ,Foodt y
pe: "+t hi
s.f
oodTy pe
+" ,Quant ityavaialable:"+t his.quanti
tyAvail
able
+" ,unitPrice:"+uni tPrice;
}
}
cl
assTest
er{
publ
i
cst at
icvoidmai n(Stri
ng[
]ar gs){
Foodf oodOne=newFood( )
;
foodOne.setFoodName( "
Sandwi ch")
;
foodOne.setCuisine("
Continental"
);
foodOne.setFoodTy pe("
Veg" );
foodOne.setQuant i
tyAvai
lable(100);
foodOne.setUnitPri
ce(10);
//CommentthetoStr
ing(
)intheFoodcl
assandexecut
ethecode
Syst
em.
out.
print
ln(
foodOne);
}
}
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
--
--
--
--
--
--
*WRAPPERCLASSESTRYOUT*
--
--
-
cl
assTester{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
//Compari
sonofIntegerobjects
I
ntegerx=5;
I
ntegery=newInteger(5)
;//intdat
aconver
tedtoI
ntegerobj
ect
Syst
em.out
.pr
int
ln("
x==y :"+( x==y))
;
Syst
em.out
.pr
int
ln("
x.equal
s(y):"+x.
equal
s(y)
);
//Wrapthepri
miti
vecont enti nt
owr apperclassobject
s
I
ntegernumber=Integer.
v alueOf(x);
Syst
em.out.
pri
ntl
n("
Wr appingxt oit
sInteger:"+number )
;
Syst
em.out.
pri
ntl
n("
Checki fnumberi softypeInteger:
"
+(numberinstanceofI nteger)
);
//Wr apper
scanbeusedt oconvertnumeri
cstr
ingst
onumer
icdat
aty
pes
Str
ingnumSt r="123";
i
ntnumI nt=Integer.
parseI
nt(
numSt r
);
System.out.
pri
ntln(
"Str
ingtoi
nteger:"+numInt
);
Str
ingdoubleStr="123.
45"
;
doublenumDoubl e=Doubl
e.par
seDoubl
e(doubl
eStr
);
System.out
.pri
ntl
n("
Stri
ngt
odouble:"+numDouble)
;
/
/Typecasti
ngcannotbeusedtoconv
ertanywrapperty
pet
oanot
her
,
/
/itwi
ll
givecompil
ati
onerr
or
/
*Uncommentthebelowcodeandobservetheoutput
*/
/
/LongphoneNoLong=(Long)newInt
eger(
44281234);
//Wecanmakeuseofmet hodsli
keint
Value()
,by
teVal
ue(
),
//fl
oatValue(
),et
c.forconversi
on
Int
egerphoneNo=44281234;
//l
ongValue()convert
st heInt
egerval
uetolongdataty
pe
LongphoneNoLong=phoneNo. l
ongVal
ue();
System.out.
pri
ntl
n("
IntegertoLong:"+phoneNoLong);
//Conv
ertstheIntegerobj
ecttobinar
yval
ue
Syst
em.out.
print
ln("
Int
eger5asbinaryst
ri
ng:
"
+Integer.
toBi
narySt
ri
ng(5))
;
}
}
--
---
--
--
--
--
--
---
--
--
--
--
--
-
*BLANKFI NALVARI ABLETRYOUT*
classDemo{
fi
nalintnum;//bl
ankfi
nalv
ari
abl
e
publ
i
cDemo( ){
num =10;
}
publ
i
cv oiddi
spl
ayNumber(){
System.out
.pr
int
ln(
num);
}
}
classTester{
publi
cst aticv oidmai n( Stri
ngar gs[]
){
Demodemo=newDemo( );
demo. di splayNumber (
);
}
}
--
---
--
--
--
--
--
----
-----
-----
--
*FINALEXERCI SE1*
classStudent {
pri
vatef i
nal intSTI PEND=100;
pri
vatei ntst udent I
d;
pri
vatei ntaggr egat eMar ks;
publi
ci ntget Student I
d( ){
retur nst udent Id;
}
publi
cv oidset Student Id(intst udentId){
this.student Id=st udent Id;
}
publi
ci ntget Aggr egat eMar ks( ){
retur naggr egat eMar ks;
}
publi
cv oidset Aggr egat eMar ks( intaggregateMarks){
this.aggr egateMar ks=aggr egateMar ks;
}
publi
ci ntget STI PEND( ){
retur nSTI PEND;
}
publi
cdoubl ecal cul ateTot alStipend(){
i
ntbonus=0;
i
f(thi s.aggr egat eMar ks>=85&&t hi
s.aggregat
eMar ks<90)
{
bonus=10;
}
elsei f
(this.aggr egat eMar ks>=90&&t hi
s.aggregat
eMar ks<95){
bonus=15;
}
elsei f
(this.aggr egat eMar ks>=95&&t hi
s.aggregat
eMar ks<=100){
bonus=20;
}
i
ntgood=( i
nt)(bonus+t his.STIPEND) ;
retur ngood;
}
}
cl
assTest
er{
publ
i
cst at
icvoidmai n(
Stri
ng[]args){
Studentstudent1=newSt udent(
);
student1.
setStudentI
d(1212);
student1.
setAggregateMarks(93);
doubl
etotal
Sti
pend=student
1.calcul
ateTot
alSt
ipend(
);
Syst
em.out.
pri
ntl
n("
The fi
nalstipend of"+ st udent
1.get
Student
Id(
)+"i
s $"+
t
otal
Sti
pend);
St
udentstudent2=newSt udent(
);
st
udent
2.setStudent
Id(1222);
st
udent
2.setAggregateMarks(84);
tot
alSti
pend=student2.
cal
cul
ateTot
alSt
ipend(
);
System.out
.pr
int
ln(
"The fi
nalsti
pend of"+ st udent
2.get
Student
Id(
)+"i
s $"+
t
otal
Sti
pend);
}
}
--
--
--
-
--
--
--
--
---
---
--
--
*DAY4*
--
--
--
--
---
---
--
-
*ABSTRACTCLASSANDMETHODS- TRYOUT*
abstractclassGrandParent{
abstractvoi
ddisplay(
);
}
abstr
actclassPar entextendsGrandParent{/
/Canwer
emov
etheabst
ractkey
wor
dfr
om
here?
f
inal
voi ddisplayI
nParent(){
Sy stem.out.
print
ln("
InParent
");
}
}
cl
assChi
ldextendsPar ent{
voi
ddisplay(){
System. out
.pr
intl
n("
Chil
dcompl
etesPar
entandGr
andPar
ent
")
;
super .
displ
ayI
nParent(
);
}
}
f
inal
classGr andChil
dextendsChil
d{
voiddisplay(){
System. out
.pr
int
ln(
"I
nGrandChi
l
d")
;
super .
displ
ay(
);
}
}
/
/Uncommentthecodegi
venbelowandobser
ve
/
/cl
assGreat
GrandChi
l
dextendsGrandChi
l
d{}
cl
assTest er{
publicstati
cv oi
dmai n(Stri
ng[]ar
gs){
newGr andChil
d().
display
();
}
}
--
--
--
--
---
---
--
---
--
---
*I
NSTANCEOF-TRYOUT*
abstractclassEmpl oyee{
priv
ateSt ri
ngempl oyeeId;
priv
ateSt ri
ngname;
priv
atest ati
cintcounter;
st
ati
c{
count
er=101;
}
publ
i
cEmpl oy ee(Stri
ngname) {
//Checkingt hetypeoft hecurrenti
nstance
if
(thi
sinstanceofPer manentEmployee)
empl oyeeId="P"+counter
++;
elseif
(thisinstanceofContractEmployee)
empl oyeeId="C"+counter++;
setName( name) ;
}
publ
i
cabst
ractv
oidcal
cul
ateSal
ary
();
publ
i
cStri
ngget
EmployeeI
d()
{
ret
urnempl
oyeeI
d;
}
publ
i
cStri
nggetName(
){
ret
urnname;
}
publ
i
cv oidset
Name(St
ri
ngname)
{
thi
s.name=name;
}
}
cl
assPer
manent
Empl
oyeeext
endsEmpl
oyee{
publ
i
cPermanent
Empl
oyee(
Str
ingname)
{
super
(name)
;
}
@Overr
ide
publ
i
cv oidcal
culat
eSalary(
){
System.out
.pri
ntl
n("Cal
culat
ingsal
aryofPer
manent
Empl
oyee"
);
}
publ
i
cv oidcal
culat
eBonus(
){
System.out
.pri
ntl
n("
Cal
cul
ati
ngbonusofPer
manent
Empl
oyee"
);
}
}
cl
assCont
ract
Empl
oyeeext
endsEmpl
oyee{
publ
i
cContract
Employ
ee(
Str
ingname)
{
super
(name);
}
@Overr
ide
publ
i
cv oidcal
culat
eSalary(
){
System.out
.pri
ntl
n("Cal
culat
ingsal
aryofCont
ract
Empl
oyee"
);
}
cl
assSal
ary
Sli
pGener
ator
{
publ
i
cv oi
ddi spl ay Salary
Sli
p( Employeeempl oyee){
employ ee. calculateSal
ar y(
);
//Checkingi fempl oyeei saninstanceofPer manentEmploy
ee
if
(empl oy eei nstanceofPer manent Employee){
//Ty pecast ingpar entclassr ef
erencet ochil
dclassforaccessi
ngchi
ldclass
met
hod
Per manent Empl oyeepermanent Empl oyee=(
PermanentEmployee)
empl
oyee;
per manent Empl oy
ee. cal
culateBonus();
}
}
}
cl
assTester{
publ
icstati
cvoi
dmain(Stri
ng[
]args){
Permanent
EmployeepermanentEmployee=newPermanentEmploy
ee("
Angie"
);
Syst
em.out
.pr
int
ln("
Detai
lsofpermanentemployee")
;
Syst
em.out
.pr
int
ln("
EmployeeId:"
+permanentEmployee.
get
EmployeeI
d()
);
Syst
em.out
.pr
int
ln("
Name: "
+permanentEmployee.
getName()
);
Sy
stem.
out
.pr
int
ln(
);
Contr
act
Employeecont
ractEmployee=newCont ractEmpl
oyee(
"Roger
");
Syst
em.out
.pr
int
ln(
"Det
ailsofcontr
actempl oyee")
;
Syst
em.out
.pr
int
ln(
"EmployeeId:"
+contractEmployee.
get
EmployeeI
d()
);
Syst
em.out
.pr
int
ln(
"Name: "
+contr
actEmpl oyee.
getName()
);
Sy
stem.
out
.pr
int
ln(
);
SalarySl
i
pGeneratorsalary
Sli
pGenerat
or=newSalar
ySlipGenerat
or(
);
System.out
.pr
intl
n("Salar
yofpermanentemployee")
;
sal
ar y
Sli
pGenerat
or.displ
aySal
arySl
i
p(permanentEmployee);
Sy
stem.
out
.pr
int
ln(
);
Syst
em.out
.pr
intl
n("
Salar
yofcontr
actemployee")
;
sal
arySl
i
pGenerat
or.di
spl
aySal
ary
Slip(
cont
ractEmploy
ee)
;
}
}
--
--
--
--
---
---
-
*ABSTRACTEXERCI SE1*
abstractclassSt udent{
//I
mpl ementy ourcodeher e
pri
vateSt ri
ngst udent Name;
priv
atei nt[]testScores;
priv
ateSt r
ingtestResult
;
publicSt udent(Str
ingstudentName){
this.studentName=st udentName;
testScor es=newi nt
[4]
;
}
abstractpubl icvoidgenerateResul
t(
);
publ
i
cSt r
inggetSt
udentName(
){
ret
urnstudentName;
}
publ
i
cv oi
dsetStudent
Name(St
ringst
udent
Name){
thi
s.studentName=st udent
Name;
}
publ
i
cint[]getTestScor es(){
ret
urnt estScores;
}
publ
i
cv oidsetTest Score(inttest
Number,i
nttest
Scor
e){
thi
s.testScores[testNumber ]=test
Score;
}
publ
i
cSt ri
ngget TestResul t(){
ret
urnt estResult;
}
publ
i
cv oidsetTest Result(Stri
ngtest
Resul
t){
thi
s.testResult=t estResult;
}
cl
assUnder
graduateSt
udentext
endsStudent{
publ
icUndergr
aduateSt
udent(
Str
ingstudent
Name){
super(
student
Name) ;
publ
i
cv oidgener ateResult(
){
int[]test Scor es=super .
getTestScores(
);
intt otal =0;
for(inti =0;i<testScores.l
ength;
i++){
total+=t estScores[
i]
;
}
doubl eav er age=total/t
estScores.
length;
if(
av er age>=60){
super .setTestResult(
"Pass");
}elsei f
(av erage<60){
super .setTestResult(
"Fail
");
}
}
}
cl
assGr
aduat
eSt
udentext
endsSt
udent{
publ
i
cGr aduateSt
udent
(St
ringst
udent
Name){
super(st
udentName);
}
publ
i
cv oidgener ateResult(
){
int[]test Scor es=super .
getTest
Scor es(
);
intt otal =0;
for(inti =0;i<testScores.l
ength;
i++){
total+=t estScores[
i]
;
}
doubl eav er age=total/t
estScores.
length;
if(
av er age>=70){
super .setTestResult(
"Pass");
}elsei f
(av erage<70){
super .setTestResult(
"Fail
");
}
}
cl
assTest
er{
publ
i
cstat
icvoidmai
n(St
ri
ng[]args){
Under
graduat
eSt
udentundergraduateSt
udent=newUnder
graduat
eSt
udent
("
Phi
l
ip"
);
under
graduat
eSt
udent.
setTestScor
e(0,70)
;
under
graduat
eSt
udent.
setTestScor
e(1,69)
;
under
graduat
eSt
udent.
setTestScor
e(2,71)
;
under
graduat
eSt
udent.
setTestScor
e(3,55)
;
under
graduat
eSt
udent
.gener
ateResul
t(
);
Sy
stem.
out
.pr
int
ln(
"Studentname:"+under
graduateStudent
.getSt
udentName(
));
Sy
stem.
out
.pr
int
ln(
"Result
:"+under
graduat
eStudent.get
TestResul
t()
);
Sy
stem.
out
.pr
int
ln(
);
Gr
aduat
eSt
udentgraduateSt
udent=newGr
aduat
eSt
udent
("
Jer
ry"
);
gr
aduat
eSt
udent
.setTest
Score(
0,70)
;
gr
aduat
eSt
udent
.setTest
Score(
1,69)
;
gr
aduat
eSt
udent
.setTest
Score(
2,71)
;
gr
aduat
eSt
udent
.setTest
Score(
3,55)
;
gr
aduat
eSt
udent
.gener
ateResul
t(
);
Sy
stem.
out
.pr
int
ln(
"Studentname:"+gr
aduateStudent.
getStudentName(
));
Sy
stem.
out
.pr
int
ln(
"Result:"
+graduat
eStudent
.getTestResult
())
;
/
/Cr
eat
emor
eobj
ect
soft
hecl
assesf
ort
est
ingy
ourcode
}
}
-
--
---
--
--
--
--
--
--
--
--
--
--
*
INTERFACETRYOUT*
i
nterf
aceDemoOne{
i
ntnumber=5;
}
i
nter
faceDemoTwoext
endsDemoOne{
voiddi
spl
ay(
);
}
cl
assDemoClassOneimplement
sDemoTwo{
publ
i
cv oiddi
spl
ay(){
System.out
.pr
int
ln(
number
);
}
}
cl
assTester{
publ
icstat
icvoi
dmai
n(St
ri
ng[
]ar
gs){
DemoTwoobj=newDemoClassOne(
);
obj
.di
spl
ay(
);
}
}
-
--
---
--
---
--
--
--
*
INTERFACEEXERCI SE1*
i
nterfaceTax{
doublecal
cul
ateTax(
doubl
epr
ice)
;
}
cl
assPurchaseDet ail
si mpl ement sTax {
pri
vateSt ri
ngpur chaseI d;
pri
vateSt ri
ngpay ment Ty pe;
pri
vatedoubl etax Percent age;
publi
cSt ri
ngget Pur chaseI d(){
r
eturnpur chaseI d;
}
publi
cv oidsetPur chaseI d( Stri
ngpurchaseI d){
t
his.purchaseI d=pur chaseId;
}
publi
cSt ri
ngget Pay ment Ty pe(){
r
eturnpay ment Ty pe;
}
publi
cv oidsetPay ment Ty pe(Stri
ngpay ment Type){
t
his.payment Ty pe=pay mentType;
}
publi
cdoubl eget TaxPer cent age(){
r
eturnt axPercent age;
}
publi
cv oidsetTaxPer cent age(doublet axPercentage){
t
his.taxPercent age=t axPercentage;
}
publ
i
cPur chaseDet ai
l
s( St r
ingpur chaseId,
St r
ingpayment
Type){
this.
purchaseI d=pur chaseI d;
this.
pay ment Type=pay ment Type;
}
publ
i
cdoubl ecalculateTax (doubl epri
ce){
doublet otal=0;
if
(thi
s.pay ment Type. equal s(
"DebitCard")){
total=price+(price* 0.
02) ;
this.setTaxPercent age(2);
}
elseif(
this.payment Ty pe. equals(
"CreditCard"
)){
total=price+(price* 0.
03) ;
this.setTaxPercent age(3);
}
else{
total=price+(price* 0.
04) ;
this.setTaxPercent age(4);
}
retur
nt otal;
}
}
cl
assSell
erimplement sTax{
pri
vateStri
nglocation;
pri
vatedoubletaxPercent
age;
publi
cStri
ngget Locati
on(
){
r
eturnlocat
ion;
}
publ
i
cv oi
dsetLocation(Stri
nglocati
on){
thi
s.l
ocati
on=l ocation;
}
publ
i
cdoubleget TaxPercentage(){
ret
urntaxPercentage;
}
publ
i
cv oi
dsetTaxPer centage(
doublet axPer
cent
age){
thi
s.t
axPercentage=t axPercentage;
}
publ
i
cSeller
(Str
inglocati
on){
thi
s.l
ocati
on=locati
on;
}
publ
i
cdoubl ecalcul
ateTax(doublepri
ce){
doublet ax=0;
if
(thi
s.l
ocat i
on.equals(
"Middleeast"
)){
tax=pr i
ce*0.
15;
this.set
TaxPercentage(15);
}
elseif(
this.l
ocati
on.equals(
"Europe"
)){
tax=pr i
ce*0.
25;
this.set
TaxPercentage(25);
}
elseif(
this.l
ocati
on.equals(
"Canada")
){
tax=pr i
ce*0.
22;
this.set
TaxPercentage(22);
}
elseif(
this.l
ocati
on.equals(
"Japan")
){
tax=pr i
ce*0.
12;
this.set
TaxPercentage(12);
}
retur
nt ax;
}
}
cl
assI
nter
faceExer
cise{
publi
cst at
icvoidmai n(
St r
ing[]args){
//TODOAut o-
generat edmet hodstub
System.out.
pri
ntln("
Pur chaseDet ai
ls\n* *
*****"
);
PurchaseDetai
lspur chaseDet ai
l
s=newPur chaseDetai
ls("
P1001"
,"
Credi
tCard")
;
System.out.
pri
ntln("
Tot al pur chase amount: " +
Mat
h.round(purchaseDetail
s.calculateTax(100)* 100)/100.0)
;
System.out.
pri
ntln("
Taxper centage: "
+pur chaseDetail
s.getTax
Per
centage(
));
System.out.
pri
ntl
n("
Sell
erDetai
ls\n**
* *
***");
Sell
ersell
er=newSel l
er(
"Middl
eeast ")
;
System.out.
pri
ntl
n("
Tax to be paid by the sel
l
er: " +
Mat
h.r
ound(sel
ler
.cal
culat
eTax(
100)*100)/100.0);
System.out.
pri
ntl
n("
Taxpercentage:"+sel
ler.get
Tax
Percent
age()
);
}
}
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
--
--
--
-
*
ASSIGNMENTS*
-
--
1.METHODOVERRI
DING1
classFacult
y {
//I
mplementy ourcodeher e
pri
vateSt r
ingname;
pri
vatefloatbasi cSalar y;
pri
vatefloatbonusPer cent age;
pri
vatefloatcar All
owancePer cent age;
publi
cFacul ty(
St ri
ngname, f
loatbasi cSalary){
thi
s.name=name;
thi
s.basicSalary =basi cSal ary;
thi
s.bonusPer cent age=4f ;
thi
s.carAll
owancePer cent age=2. 5f;
}
publi
cdoubl ecal culateSal ar y(){
double facul
tySal
ary=
this.
getBasi
cSal ar
y()*(1+( this.get BonusPer centage()
/100)+(this.
getCar
All
owancePer
centage()
/100));
thi
s.setBasicSal ary ((float )facultySalary);
ret
urnf acultySalar y;
}
publi
cSt ri
ngget Name( ){
ret
urnname;
}
publi
cv oidsetName( Stringname){
thi
s.name=name;
}
publi
cfloatget BasicSal ary (){
ret
urnbasi cSal ary;
}
publi
cv oidsetBasi cSal ar y(floatbasi cSalary){
thi
s.basicSalary=basi cSal ar y;
}
publi
cfloatget BonusPer cent age( ){
ret
urnbonusPer cent age;
}
publi
cv oidsetBonusPer cent age( f
loatbonusPer centage){
thi
s.bonusPer cent age=bonusPer centage;
}
publi
cfloatget CarAl l
owancePer cent age(){
ret
urncar AllowancePer cent age;
}
publi
cv oidsetCar AllowancePer centage( fl
oatcarAll
owancePer cent
age){
thi
s.carAll
owancePer cent age=car All
owancePer centage;
}
cl
assOffi
ceSt affex t
endsFaculty{
//
Implementy ourcodehere
pri
vateSt ri
ngdesi gnati
on;
publi
cOf ficeStaff(
Stri
ngname, fl
oatbasi
cSal
ary
,St
ri
ngdesi
gnat
ion)
{
super (name,basicSal
ary)
;
thi
s.desi gnati
on=designati
on;
}
@Ov err
ide
publ
i
cdoubl ecalcul ateSal ary (
){
doubl eaddi t
ional Pay=super .calculat
eSal ar
y()
;
doubl est affSal ary=0;
if
(this.getDesi gnat ion().equals("Account ant
"))
{
staffSalar y=addi t
ionalPay +10000. 0;
}
elsei f(
this.get Desi gnation().
equal s("
Clerk")
){
staffSalar y=addi t
ionalPay +7000.0;
}
elsei f(
this.get Desi gnation().
equal s("
Peon" )
){
staffSalar y=addi t
ionalPay +4500.0;
}
else{
staffSalar y=addi t
ionalPay ;
}
super .setBasi cSal ary((fl
oat)staffSalary);
returnst affSal ary;
}
publ
i
cSt ri
ngget Desi gnat i
on( ){
returndesi gnat i
on;
}
publ
i
cv oidset Desi gnat ion(Stri
ngdesi gnation){
this.designat ion=desi gnati
on;
}
}
cl
assTeacherext endsFacul ty{
//
Impl ementy ourcodeher e
pri
vateSt r
ingqual i
ficat i
on;
publi
cTeacher (
St r
ingname, floatbasi cSalary,Stri
ngquali
fi
cat
ion)
{
super (name, basi cSal ary);
this.qualifi
cat i
on=qual i
ficati
on;
}
@Ov erride
publi
cdoubl ecal culat eSal ary(){
doubl eaddi t i
onal Pay=super .calcul at
eSalary()
;
doubl et eacher Sal ary=0;
i
f(this.getQual ificat i
on( ).
equal s(
" Doct oral
"))
{
teacher Sal ary =addi tionalPay +20000. 0;
}
elsei f(
this.get Qual i
fi
cat i
on( ).
equal s("Masters"))
{
teacher Sal ary =addi tionalPay +18000. 0;
}
elsei f(
this.get Qual i
fi
cat i
on( ).
equal s("Bachelors"
)){
teacher Sal ary =addi tionalPay +15500. 0;
}
elsei f(
this.get Qual i
fi
cat i
on( ).
equal s("Associate"
)){
teacher Sal ary =addi tionalPay +10000. 0;
}
else{
teacher Sal ary =addi tionalPay ;
}
this.setBasi cSal ar y((
float)teacher Sal
ary )
;
returnt eacher Sal ar y;
}
publi
cSt ri
ngget Qual ification(){
returnqual ificat i
on;
}
publ
i
cv oidset
Qualif
icat
ion(
Stri
ngquali
fi
cat
ion){
thi
s.qual
if
icat
ion=qualif
icat
ion;
}
cl
assTester{
publ
icstat
icv
oidmai
n(St
ri
ng[
]ar
gs){
Teacherteacher=newTeacher
("
Caroli
ne"
,30500f
,"Mast
ers"
);
Off
iceStaffof
ficeSt
aff=newOf
fi
ceStaf
f("
James",
24000f,
"Account
ant
")
;
Syst
em.out.
pri
ntl
n("
TeacherDet ai
l
s\n*********
*****
*")
;
Syst
em.out.
pri
ntl
n("
Name: "
+teacher.getName( ))
;
Syst
em.out.
pri
ntl
n("
Quali
fi
cation:"
+teacher.getQuali
fi
cat
ion(
));
Syst
em.out.
pri
ntl
n("
Total salary
: $" +
Mat
h.r
ound(t
eacher
.cal
cul
ateSal
ary(
)*100)/100.0);
Syst
em.out.
pri
ntl
n()
;
System.out
.pr
intl
n("
Offi
ceSt affDetai
ls\n** *
*****
*****
* *
");
System.out
.pr
intl
n("
Name: "+offi
ceStaff.getName( )
);
System.out
.pr
intl
n("
Designation:"+of
ficeStaff.
getDesignati
on(
));
System.out
.pr
intl
n("
Total salary
: $" +
Mat
h.r
ound(off
iceSt
aff
.cal
culat
eSalary()*
100)/100. 0);
/
/Cr
eat
emor
eobj
ect
sfort
est
ingy
ourcode
}
}
-
---
--
--
--
--
--
--
--
--
2.METHODOVERRDI
NG-
2
cl
assEvent {
//
Impl ementy ourcodeher e
pri
vateSt r
ingev entName;
pri
vateSt r
ingpar ti
cipant Name;
pri
vatedoubl er egistrationFee;
publi
cEv ent (
St ri
ngev entName, St r
ingparti
cipant Name){
this.event Name=ev ent
Name;
this.participantName=par ti
cipantName;
}
publi
cv oidregi sterEvent ()
{
i
f(this.get Event Name( )
.equals("Si
ngi
ng")){
this.setRegi strati
onFee(8);
}
elsei f(
this.getEv ent Name( )
.equals("
Danci ng")){
this.setRegi strati
onFee(10) ;
}
elsei f(
this.getEv ent Name( )
.equals("
Digi
t alArt"
)){
this.setRegi strati
onFee(12) ;
}
elsei f(
this.getEv ent Name( )
.equals("
Acting" )
){
this.setRegi strati
onFee(15) ;
}
else{
t
his.
set
Regi
str
ati
onFee(
0);
}
}
publ
i
cSt ri
ngget Event Name( ){
ret
urnev entName;
}
publ
i
cv oidset Event Name( Str
ingev entName){
thi
s.event Name=ev entName;
}
publ
i
cSt ri
ngget ParticipantName( ){
ret
urnpar t
icipant Name;
}
publ
i
cv oidset Par t
icipantName( Stri
ngparti
ci
pantName){
thi
s.partici
pant Name=par ti
cipantName;
}
publ
i
cdoubl eget Regi str
ationFee(){
ret
urnr egistrationFee;
}
publ
i
cv oidset Regist rati
onFee( doubleregi
str
ati
onFee){
thi
s.registrati
onFee=r egistr
ationFee;
}
cl
assSoloEv entext endsEv ent{
//I
mpl ementy ourcodeher e
pri
vatei ntpar tici
pantNo;
publi
cSol oEv ent(Stri
ngev entName, St
ri
ngpar
ti
cipant
Name,
intpar
ti
cipant
No)
{
super (ev entName, parti
cipantName);
this.participantNo=par t
icipantNo;
}
@Ov erride
publ
i
cv oidregi sterEv ent
(){
super .registerEvent();
}
publ
i
cintget Par ti
cipant No(){
ret
urnpar ti
cipant No;
}
publ
i
cv oidset Par ti
cipantNo( i
ntpar ti
cipant
No){
thi
s.participant No=par ti
cipantNo;
}
}
}
publ
i
cintget NoOf Parti
cipants(
){
ret
urnnoOf Part
icipants;
}
publ
i
cv oidset NoOfPar t
ici
pants(i
ntnoOfPar
ti
cipant
s){
thi
s.noOf Parti
cipants=noOf Part
ici
pant
s;
}
publ
i
cintget TeamNo( ){
ret
urnt eamNo;
}
publ
i
cv oidset TeamNo( intteamNo){
thi
s.teamNo=t eamNo;
}
cl
assTest
er{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
}el
se{
Sy
stem.
out
.pr
int
ln(
"Pl
easeent
erav
ali
dev
ent
");
}
System.
out .
pr i
ntl
n();
TeamEv entteamEv ent=newTeamEv ent
("Act
ing"
,"Ser
ena"
,5,
1);
teamEvent.regist
erEvent
();
i
f( t
eamEv ent.get
Registr
ati
onFee(
)!=0){
System.out.
pri
ntl
n("ThankYou"+t eamEvent.
getPar
ti
ci
pantName( )
+ " f or y our partici
pat
ion! Your regi
str
ation fee is $" +
t
eamEvent
.getRegist
rati
onFee()
);
System.out.
pri
ntl
n("Yourteam numberis"+teamEvent.
getTeamNo())
;
}else{
System.out.
pri
ntl
n("Pl
easeenterav al
idevent
");
}
}
}
-
---
--
--
--
--
--
3.FINAL1
cl
assCir
cle{
pri
vatefinal doubl ePI =3.14;
pri
vatedoubl ediamet er;
pri
vatedoubl ecircumf er
ence;
pri
vatedoubl earea;
publi
cdoubl eget Diamet er(){
r
eturndi amet er;
}
publi
cv oidset Diamet er(
doubl ediameter){
t
his.diamet er=di amet er;
}
publi
cdoubl eget Circumf erence(){
r
eturnci rcumf erence;
}
publi
cv oidset Circumf er
ence( doubl
ecircumfer
ence){
t
his.circumf erence=ci rcumf er
ence;
}
publi
cdoubl eget Ar ea(){
r
eturnar ea;
}
publi
cv oidset Area( doubl earea){
t
his.area=ar ea;
}
publi
cdoubl eget PI(){
r
eturnPI ;
}
publ
i
cCi r
cle(doubl edi ameter){
thi
s.diamet er=di amet er
;
}
publ
i
cv oidcal culateCi rcumference(){
cir
cumf er ence=t his.get
Diamet er(
)*(t
his.
PI)
;
thi
s.setCircumf erence(cir
cumf erence);
}
publ
i
cv oidcal culateAr ea(){
doubler =this.get Diameter()
/2;
area=(this.PI)*(
r *r);
thi
s.setAr ea(area) ;
}
}
cl
assFi
nal
Assi
gnment{
publ
i
cstat
icv
oidmain(Str
ing[
]ar
gs){
//TODOAuto-
generatedmethodst
ub
Ci
rcl
eci
rcl
e1=newCi
rcl
e(12.
2);
Ci
rcl
eci
rcl
e2=newCi
rcl
e(33.
2);
//
Createmoreobj
ectsofCircleclassandaddt
othear
raygi
venbel
ow f
ort
est
ing
y
ourcode
Cir
cle[
]ci
rcl
es={
cir
cle1,
cir
cle2};
f
or(
Cir
cleci
rcl
e:ci
rcl
es){
ci
rcl
e.cal
cul
ateCi
rcumfer
ence(
);
ci
rcl
e.cal
cul
ateAr
ea();
System.
out.
pri
ntln("Diameterofthecir
cleis"+ci
rcl
e.getDi
ameter(
));
System.
out.
pri
ntln("Circumfer
ence of t
he ci
rcl
e i
s " +
Mat
h.r
ound(
cir
cle.
get
Cir
cumf erence( )
*100)
/100.0)
;
System.
out.
pri
ntln("Area of the circl
e is " +
Mat
h.r
ound(
cir
cle.
get
Area(
)*100) /100.0)
;
System.
out.
pri
ntln();
}
}
}
-
---
--
--
--
--
--
--
4.ABSTRACT1
abst
ractclassPay ment {
priv
atei ntcust omer I
d;
protectedSt ringpay ment Id;
protecteddoubl eser viceTaxPer cent
age;
publicintget Cust omer I
d(){
returncust omer Id;
}
publicv oidset Cust omer I
d(intcust omerId){
this.customer I
d=cust omer Id;
}
publicSt ri
ngget Pay ment Id(){
returnpay ment I
d;
}
publicv oidset Pay ment Id(St r
ingpay ment I
d){
this.payment Id=pay ment Id;
}
publicdoubl eget ServiceTaxPer centage(){
returnser vi
ceTaxPer cent age;
}
publicv oidset ServiceTax Percent age(
doubleserviceTaxPer
cent
age){
this.serviceTaxPer cent age=ser vi
ceTaxPercentage;
}
publicPay ment (i
ntcust omer Id){
this.customer I
d=cust omer I
d;
}
publicabst ractdoubl epay Bil
l(doubleamount )
;
}
cl
assDebi
tCardPaymentex t
endsPay ment
{
pr
ivat
estati
cintcounter=1000;
pr
ivat
edoublediscountPercent
age;
publ
i
cDebi tCar
dPayment(
intcustomer
Id){
super(cust
omerId)
;
payment I
d="D"
+++counter;
thi
s.setPayment
Id(
paymentId);
}
publ
i
cstati
cintgetCount
er(
){
ret
urncounter
;
}
publ
i
cstat
icvoi
dset
Counter(
intcount
er){
Debi
tCar
dPayment
.count
er=count er
;
}
publ
i
cdoublegetDi
scount
Percent
age(
){
ret
urndi
scount
Percent
age;
}
publ
i
cv oidsetDiscountPercent
age(doubl
ediscount
Per
cent
age){
thi
s.discountPercent
age=di scount
Percent
age;
}
publ
i
cdoubl epay Bill(
doubleamount ){
doublet ax ,
discount ,
bil
l=0.
0;
if
(amount <=500){
doubl eserv i
ceTaxPercentage=2. 5;
this.setSer vi
ceTax Percent
age( servi
ceTax Percentage)
;
discount Per centage=1;
discount =( amount *di
scount Percentage)/
100;
tax=amount +(amount *ser
viceTax Percentage/100);
bil
l=t ax-
discount ;
}
elseif(
amount >500&&amount <=1000){
doubl eserv i
ceTaxPercentage=4;
this.setSer vi
ceTax Percent
age( servi
ceTax Percentage)
;
discount Per centage=2;
discount =( amount *di
scount Percentage)/
100;
tax=amount +(amount *ser
viceTax Percentage/100);
bil
l=t ax-
discount ;
}
elseif(
amount >1000){
doubl eserv i
ceTaxPercentage=5;
this.setSer vi
ceTax Percent
age( servi
ceTax Percentage)
;
discount Per centage=3;
discount =( amount *di
scount Percentage)/
100;
tax=amount +(amount *ser
viceTax Percentage/100);
bil
l=t ax-
discount ;
}
retur
nbi ll;
}
}
cl
assCredit
CardPaymentextendsPayment
{
pr
ivatestat
icintcount
er=1000;
publ
i
cCr edit
CardPay
ment(
intcust
omer
Id){
super(cust
omerId)
;
paymentId="
C"+++count
er;
thi
s.set
PaymentI
d(payment
Id)
;
}
publ
i
cstati
cintgetCount
er(){
ret
urncounter
;
}
publ
i
cstati
cvoidsetCounter(
intcount
er){
Credi
tCardPay
ment .
counter=counter
;
}
publ
i
cdoubl epay Bi
ll
(doubleamount ){
doublet otal=0.
0;
if
(amount <=500){
doubl eserviceTaxPer
centage=3;
thi
s. setServi
ceTaxPer
centage(serv
iceTaxPercent
age);
total=amount +(amount*
ser vi
ceTaxPercent
age/100)
;
}
elseif(
amount >500&&amount <=1000){
doubl eserviceTaxPer
centage=5;
thi
s. setServi
ceTaxPer
centage(serv
iceTaxPercent
age);
total=amount +(amount*
ser vi
ceTaxPercent
age/100)
;
}
elseif(
amount >1000){
doubl eserviceTaxPer
centage=6;
thi
s. setServi
ceTaxPer
centage(serv
iceTaxPercent
age);
total=amount +(amount*
ser vi
ceTaxPercent
age/100)
;
}
retur
nt otal;
}
}
cl
assAbst
ract
Assi
gnment{
publi
cstati
cv oidmai n(Str
ing[]args){
//TODOAut o-generatedmet hodst ub
Debit
CardPay mentdebi t
CardPay ment=newDebi t
CardPay ment(101);
doubl
ebillAmount =Mat h.round(debit
CardPayment.payBil
l(
500)*100)/100.0;
Syst
em.out .
printl
n("CustomerI d:"+debitCardPayment.getCust
omer Id()
);
Syst
em.out .
printl
n("PaymentI d:"+debi t
CardPayment.getPayment I
d())
;
Syst
em.out .
printl
n("Servi
ce t
ax percentage: " +
debi
tCar
dPayment .getServi
ceTaxPer centage())
;
Syst
em.out .
printl
n("Discount percent
age: " +
debi
tCar
dPayment .getDiscountPercentage());
Syst
em.out .
printl
n("Totalbil
lamount :"+bil
lAmount )
;
Credit
CardPaymentcredit
Car dPay ment=newCr editCardPayment(
102);
bil
lAmount=Math.r
ound(creditCardPayment .
payBil
l(1000)*100)/
100.0;
System.out
.pr
int
ln("
CustomerI d: "+credit
CardPay ment .
getCust
omer I
d()
);
System.out
.pr
int
ln("
PaymentI d: "+credit
CardPay ment .
getPaymentI
d())
;
System.out
.pr
int
ln("
Servi
ce tax percentage: " +
cr
edi
tCar
dPay ment.
get
Servi
ceTaxPer centage());
System.out
.pr
int
ln("
Totalbil
lamount :"+bi l
lAmount );
}
}
-
--
--
--
--
--
--
--
5.I
NTERFACE1
i
nter
faceTest
abl
e{
booleant
est
Compat
ibi
l
ity
();
}
cl
assMobile{
pri
vateSt r
ingname;
pri
vateSt r
ingbrand;
pri
vateSt r
ingoperatingSy stemName;
pri
vateSt r
ingoperatingSy stemVer sion;
publi
cSt ri
ngget Name( ){
r
eturnname;
}
publi
cv oidsetName( St r
ingname){
t
his.name=name;
}
publi
cSt ri
ngget Br
and( ){
r
eturnbr and;
}
publi
cv oidsetBrand(Stringbr and){
t
his.brand=br and;
}
publi
cSt ri
ngget OperatingSy stemName( ){
r
eturnoper ati
ngSy stemName;
}
publi
cv oidsetOperatingSy stemName( Str
ingoperatingSystemName){
t
his.operati
ngSyst emName=oper ati
ngSy st
emName;
}
publi
cSt ri
ngget OperatingSy stemVer si
on(){
r
eturnoper ati
ngSy stemVer si
on;
}
publi
cv oidsetOperatingSy stemVer sion(
Stringoperati
ngSystemVer
sion){
t
his.operati
ngSyst emVer si
on=oper atingSystemVersion;
}
publi
c Mobi le(Str
ing name, Str
ing br and,
Str
ing oper
ati
ngSy
stemName,
Str
ing
oper
ati
ngSyst
emVer sion){
t
his.
name=name;
t
his.
brand=brand;
t
his.
operati
ngSystemName=operati
ngSystemName;
t
his.
operati
ngSystemVer
sion=oper
ati
ngSy st
emVersi
on;
}
}
cl
assSmartPhoneextendsMobilei
mplement
sTest
abl
e{
pri
vat
eSt r
ingnet
wor kGener
ati
on;
publ
i
cStri
nggetNet
workGener
ati
on(
){
ret
urnnet
workGener
ati
on;
}
publ
i
cv oidsetNetworkGener
ati
on(St
ringnet
workGener
ati
on){
thi
s.networkGenerat
ion=networkGener
ati
on;
}
publ
i
c Smar
tPhone(
Str
ing name,
Str
ing br
and,
Str
ing oper
ati
ngSy
stemName,
Str
ing
oper
ati
ngSystemVersion,St
ri
ngnetworkGenerati
on){
super(name,
brand,
operat
ingSystemName,oper
ati
ngSy
stemVer
sion)
;
thi
s.networ
kGenerati
on=networkGener
ati
on;
}
publ
i
cbool eantest
Compatibil
it
y()
{
if
(thi
s.get
Operati
ngSy
st emName(
).
equal
s("
Sat
urn"
)){
i
f(
thi
s.net
wor
kGenerati
on.equal
s("
3G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
1.1"
)))
{
ret
urntrue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.equal
s("
3G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
1.2"
)))
{
ret
urntrue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.equal
s("
3G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
1.3"
)))
{
ret
urntrue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.equal
s("
4G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
1.2"
)))
{
ret
urntrue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.equal
s("
4G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
1.3"
)))
{
ret
urntrue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.equal
s("
5G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
1.3"
)))
{
ret
urntrue;
}
else{
ret
urnfalse;
}
}
el
sei
f(
thi
s.get
Oper
ati
ngSy
stemName(
).
equal
s("
Gar
a")
){
i
f(
thi
s.net
wor
kGenerati
on.
equal
s("
3G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
EXRT.
1")
)
)
{
ret
urntr
ue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.
equal
s("
3G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
EXRT.
2")
)
)
{
ret
urntr
ue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.
equal
s("
3G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
EXRU.
1")
)
)
{
ret
urntr
ue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.
equal
s("
4G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
EXRT.
2")
)
)
{
ret
urntr
ue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.equal
s("
4G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
EXRU.
1")
)
)
{
ret
urntrue;
}
else
i
f(
thi
s.net
wor
kGenerati
on.equal
s("
5G"
)&&(
thi
s.get
Oper
ati
ngSy
stemVer
sion(
).
equal
s("
EXRU.
1")
)
)
{
ret
urntrue;
}
else{
ret
urnfalse;
}
}
el
se
ret
urnf
alse;
}
}
cl
assI
nter
faceAssi
gnment{
publi
cstaticvoidmain(Str
ing[]args){
//TODOAut o-
generatedmet hodstub
Smar t
Phonesmar tPhone=newSmar tPhone(
"Qui
ck6Pro","Nebula"
,"Marco","
1.45",
"4G"
);
i
f(smartPhone.t
estCompat ibil
i
ty(
))
System.out
.pr
intl
n("Themobi l
eOSiscompat i
blewit
ht henetworkgenerati
on!"
);
else
System.out
.pr
intl
n("The mobi l
e OS i s not compat i
ble wit
ht he net work
generat
ion!")
;
}
}
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-