1'()'2(1* Ma x . T i me : 6 0mi n
BITS, Pilani – KK Birla Goa Campus Department of CS & IS CS/IS F213 !"e#t riente$ Pro%rammin% IS C3 C313 ! !"e#t r riente$ Pr Pro%rammin% an an$ De Desi%n T e s t –I
Ma x . Ma r k s : 4 5
Instructions: 1' +nser +nser all all parts parts of of a -uesti -uestion on at at one pla#e' pla#e' 2' .rite .rite #lear #lear anser ansers s for for #ompl #omplete ete #re$it' #re$it' 3' a0e an an in$e in$e on te !a#0 !a#0 si$e si$e of te front front pa%e of of anser anser !oo0let' !oo0let'
Question-1 ICC neDa4 Cri#0et .orl$ Cup 2(15 is to !e pla4e$ in +ustralia an$ 6e 7ealan$' 8ou a9e !een #osen as softare $e9eloper for #reatin% softare to mana%e te #ri#0etin% aspe#ts of te tournament: oter parts of te tournament su# as ti#0et sales, %roun$/9enue mana%ement, mana%ement, li9e tele#ast, ospitalit4, ospitalit4, tourism an$ promotion are not part of 4our $e9elopment man$ate' ;sin% 4our un$erstan$in% of o!"e#t oriente$ #on#epts, anser te folloin% -uestions' +' I$entif4 if4 ierar#4, 0e4 a!stra#tions for te softare s4stem' <5M= 5M= B' Dr D ra #lass an$ o!"e#t ierar#4' <5M= 5M= C' It is sai$ tat primiti9es primiti9es at ea# le9el le9el of ierar#4 ierar#4 are relati9e relati9e to o!ser9er o!ser9er' I$entif4 relati9e primiti9es as seen !4 ;mpire, Team Team Captain' Point out te $ifferen#es' <5M <5M== D' Dra C>C #ar$ for a #ri#0eter' <4M= 4M= Ans: A.
?ierar#4 – Tournament, at#, Team, Cri#0eter Te 0e4 a!stra#tions in ea# ierar#4 are son !elo' Tournament – at#es, Points Ta!le, Teams, ;mpire Panel, >eferee Panel, ICC >ules an$ >e%ulations at# – Teams, S#ore#ar$
eferee, ICC at# >ules Team – Cri#0eter, Coa#, Support Staff, Captain, ICC >ules for Team Cri#0eter – Spe#ialit4 ules for Cri#0eter
B'
C' >elati9e primiti9es primiti9es as seen !4 ;mpires are@ !oler !oler,, !atsman, #aptain, #aptain, fiel$er, fiel$er, tir$umpire, mat# referee, ICC rules an$ re%ulations' >elati9e primiti9es as seen !4 team #aptain are@ strate%ies – !attin%, !olin%, fiel$in%, umpires, mat# referee' ;mpires are suppose$ to !e impartial an$ %o !4 te rule !oo0' +s far as umpires are #on#erne$, te4 treat all pla4ers impartiall4' impartiall4' ;mpires $eal it pla4ers, te net le9el of #onta#t is it te #aptain of te team: ;mpires are primar4 #on$uit for intera#tion it tir$ empire' Captain is more #on#erne$ it is on team strate%4' Captain as to pi#0 te ri%t team to ee#ute te strate%4' strate%4' Tis strate%4 an$ te 0e4 team mem!ers to ee#ute te strate%4 are not 0non to umpires' D' ne possi! possi!le le C>C #ar$ #ar$ for Cri#0ete Cri#0eterr #lass is as as follos' follos'
Question-2 .rite a Aa9a pro%ram to iterate trou% a set of senten#es an$ i$entif4 te senten#es #ontainin% te or$s smalltal0 or SmallTal0' Print te senten#es tat #ontain te %i9en or$s' Te pro%ram must a9e te folloin% elements' +' .rite a re%ular epression to i$entif4 te %i9en or$s' <2M= B' .rite te #omplete Aa9a pro%ram' <6M= C' .rite proper Aa9a $o#umentation for 4our #o$e' <3M= Ans: += !sSEmalltTEal0! Both B and C parts are included in the followin code! / Tis is te pro%ramm for uestion 2 B of Test 1 eam Hautor stu$ent H9ersion 1'(, sep 2(1* / import "a9a'util're%e'at#er: import "a9a'util're%e'Pattern: import "a9a'io':
pu!li# #lass Strin%at#er / tet arra4 ol$s input strin%s / pri9ate stati# final Strin%E tetJ "a9a is o!"e#t oriente$ pro%rammin% lan%ua%e, SmallTal0 is pure o!"e#t oriente$ pro%rammin%, smalltal0 uses an optimiLin% #ompiler, +noter o!"e#t oriente$ lan%ua%e is SmallTal0, +noter o!"e#t oriente$ lan%ua%e is 1SmallTal0, Aa9a lan%ua%e is simpleM: / p1 ol$s te re%ular epression to !e mat#e$ see Ntet / pri9ate stati# final Pattern p1 J Pattern'#ompile<!sSEmallTtEal0!=: / ain Fun#tion Print te senten#es i# #ontains or$ SmallTal0 or smalltal0 / pu!li# stati# 9oi$ main
Question-3 >ea$ te sample Aa9a #o$e %i9en !elo' pa#0a%e or%'"netp#ap'eamples: import "a9a'io'File: import "a9a'nio'B4teBuffer: import "a9a'util'+rra4Qist: import "a9a'util'Qist: import or%'"netp#ap'P#ap: import or%'"netp#ap'P#apDumper: import or%'"netp#ap'P#ap?an$ler: import or%'"netp#ap'P#apIf:
pu!li# #lass P#apCapture implements SerialiLa!le, QistOP#apPa#0etR pu!li# stati# 9oi$ mainISC;;S, 1( 1(((, err!uf=: if
+' Dra se-uen#e $ia%ram for te #o$e %i9en' <"M= B' Dra te #lass an$ o!"e#t $ia%rams for te #o$e %i9en' Clearl4 so te roles, 9isi!ilit4, 9aria!les an$ meto$s outline$ in te #o$e %i9en a!o9e' <#M= C' Dra pa#0a%e $ia%ram for te %i9en #o$e' <5M=
Ans: A$ %e&uence 'iara(
B$ Class 'iara(
)*+ect 'iara(
C$ ,acae 'iara(
221'2'1( Ma x . T i me : 6 0mi n
BITS, Pilani – KK Birla Goa Campus Department of CS & IS CS/IS F213 !"e#t riente$ Pro%rammin% IS C313 !"e#t riente$ Pro%rammin% an$ Desi%n T e s t –I I
Ma x . Ma r k s :
4 5
Instructions: 1 )ns*er all parts of a +uestion at one pla#e 2 rite #lear ans*ers for #omplete #re$it 3 -a.e an in$e on t0e !a#. si$e of t0e front pa%e of ans*er !oo.let
Question-1 rite a aa pro%rame to implement t0e follo*in% input pro#essin% state $ia%ram for a .e!oar$ $rier 415M5
Ans:
enum States { GatheringInput, ProcessingInput, FINAL } public class KeyboardDriver { States state !!so"e arbitrary constant private static int REQUIRED_LENGTH # $% int input !!input &ength boolean sub"itInput !!output !!'rite constructors public KeyboardDriver (States s){ this*state # s state+achine() }
public void keystroke(){ switch (state) { case GatheringInput !!-or every .eystro.e, increase input &ength by $ input// if(input 0 REQUIRED_LENGTH) { this *state # States*GatheringInput state+achine() } else { this *state # States*ProcessingInput state+achine()
this *state # States*FINAL state+achine() } break
default break } } !!"ethod to create the state "achine private void stateMachine(){ switch (state) { case GatheringInput gatheringInput() break
case ProcessingInput processingInput() sub"itInput # true break case FINAL sub"itInput # false default break } } !!"ethods -or each state private void gatheringInput(){ }
private void processingInput(){ } } N123 I- Phases 3N4+ is 'ritten, its5 variab&e phase "ust a&'ays be in A62I73 phase*
Question-2 A. rite a aa pro%ramme to implement t0e 6-7 #lass $ia%ram %ien !elo*
B.
8efa#tor t0e #lass $ia%ram to improe t0e namin% #onentions an$ in0eritan#e semanti#s ustif our #0an%es
Ans: )
public interface 8DShape { void set9idth(float 'idth) void set:eight(float height) float getArea() 8DShape(float 'idth) 8DShape(float 'idth, float height) } public interface ;DShape extends 8DShape { float get7o&u"e() } public class 6ube implements ;DShape { float depth # <*< public Cube(float side){ !!can a&so be &e-t b&an. depth # side 'idth # side height # side } } public class =ectang&e implements 8DShape {
(15M)
(7M)
float 'idth # <*< float height # <*< }
public class S>uare implements 8DShape { float edge # <*< }
B.
Question-3 ) aa pro%ram to rea$ #ontents of a file an$ sum up t0e alues is %ien -o$if t0e #o$e to anti#ipate FileNotFoundException, IOException an$ BadDataException from rea$File45 met0o$ of DataSetReader #lass T0e net45 met0o$ of Scanner #lass #an also t0ro* NoSuchElementException an$ IllegalStateException e#eptions )ll t0e state$ e#eptions 0ae a #onstru#tor t0at ta.es String message as a sin%le ar%uement In t0e try...catch...finally !lo#., print t0e e#eption messa%e usin% %et-essa%e45 met0o$ of t0ese e#eption #lasses (13M) import "aaioFile9otFoun$:#eption; import "aaioI:#eption; import "aautilS#anner; pu!li# #lass Data)nal? ar%s5 =
S#anner in @ ne* S#anner4Sstemin5; DataSet8ea$er rea$er @ ne* DataSet8ea$er45; !oolean $one @ false; *0ile 4A$one5 = Sstemoutprintln4Please enter t0e file name 5; Strin% filename @ innet45; $ou!le>? $ata @ rea$errea$File4filename5; $ou!le sum @ '; for 4$ou!le $ $ata5 sum @ sum $; Sstemoutprintln4T0e sum is sum5; $one @ true; E E E
Ans: import "aaioFile9otFoun$:#eption; import "aaioI:#eption; import "aautilS#anner; public class Data)nal? ar%s5 = S#anner in @ ne! S#anner4Sstemin5; DataSet8ea$er rea$er @ ne! DataSet8ea$er45; boolean $one @ "alse; !#ile 4A$one5 = tr$ = Sstemoutprintln4Please enter t0e file name 5; Strin% filename @ innet45; //file name rea$ su##essfull, no* tr to rea$ from file tr$ = double>? $ata @ rea$errea$File4filename5; double sum @ '; "or 4double $ $ata5 sum @ sum $; Sstemoutprintln4T0e sum is sum5; E catc# 4File9otFoun$:#eption e#eption5 = Sstemoutprintln4File not foun$5; E catc# 4Ba$Data:#eption e#eption5 = Sstemoutprintln4Ba$ $ata e#eption%et-essa%e455; E
catc# 4I:#eption e#eption5 = e#eptionprintSta#.Tra#e45; E "inall$ = $one @ true; E E catc# 49oSu#0:lement:#eption e#eption5 = Sstemoutprintln49o input e#eption%et-essa%e455; E catc# 4Ille%alState:#eption e#eption5 = Sstemoutprintln4Ba$ state e#eption%et-essa%e455; E E in#lose45; E E
11122'1( Max.Ti me:3Hour s
BITS, Pilani – KK Birla Goa Campus Department of CS & IS CS/IS F213 !"e#t riente$ Pro%rammin% IS C313 !"e#t riente$ Pro%rammin% an$ Desi%n Compr ehensi veExam
Max.Mar ks :105
Question-1 )rite t*e readStudents, displayStudents, lookUpStudents met*o$s for ParticipantList #lass
+15M
import java.util.*; public class ParticipantList { private HashMap studentsList = new HashMap(; public static void main(String!" args { ParticipantList list = new ParticipantList(; Scanner sc = new Scanner(S#ste$.in; list.readStudents(sc; list.displa#Students(; list.l%%&'pStudents(sc; sc.cl%se(; Announcement: readStudents met*o$ a$$s items to -as*.ap displayStudents met*o$ lists #ontents of -as*.ap an$ lookUpStudents met*o$ loo0s up a stu$ent in t*e -as*.ap !ase$ on stu$entID T*e *as*map *as !een parameterie$ !ase$ on stu$ent ID an$ stu$ent name Ans:
import java.util.*; public class ParticipantList { private HashMap studentsList = new HashMap(; public static void main(String!" args { ParticipantList list = new ParticipantList(; Scanner sc = new Scanner(S#ste$.in; S#ste$.%ut.println()eading in student details); list.readStudents(sc; S#ste$.%ut.println()+ispla#ing student details); list.displa#Students(; S#ste$.%ut.println()L%%&ing up student details); list.l%%&'pStudents(sc; sc.cl%se(; -s& the user %r student details t% st%re in studentsList. private void readStudents(Scanner sc { while (true { String /+ = sc.ne0tLine(; if (/+.e1uals()) break; String na$e = sc.ne0tLine(; studentsList.put(/+, na$e; Print %ut all the student details in the participant list. private void displayStudents( { /terat%r it = studentsList.&e#Set(.iterat%r(; while (it.has2e0t( { String /+ = it.ne0t(; String na$e = studentsList.get(/+; S#ste$.%ut.println(/+ 3 )4 ) 3 na$e; -ll%5 the user t% students in the participantList 6# l%%&ing up the student /+ ass%ciated 5ith a na$e. private void lookUpStudents(Scanner sc { while (true { S#ste$.%ut.println()7nter /+ t% l%%&up4 ); String /+ = sc.ne0tLine(; if (/+.e1uals()) break;
String na$e = studentsList.get(/+; if (na$e == null { S#ste$.%ut.println(/+ 3 ) n%t in students list); else { S#ste$.%ut.println(na$e; Question-2 tentatie se4uen#e $ia%ram for semester re%istration of first 5ear stu$ents is s*o6n .o$if5 t*e $ia%ram to in#orporate t*e follo6in% #*an%es +10M 1 #las* in time ta!le 6ill !e in$i#ate$ !5 Volunteer 6it* a clash return messa%e as a response to messa%e ( If t*e time ta!le is #las*7free, Volunteer returns 0 2 PackageRegistrar ma5 eit*er return a registered or full messa%e If stu$ent %ets a full messa%e, stu$ent nee$s to sit $o6n 6it* olunteer to sele#t anot*er pa#0a%e ppropriate met*o$s t*at 6ill !e useful are8 ownTT.alterTT, volunteer.seekHelp, an$ pr.register T*ese steps are to !e repeate$ until t*e re%istration is su##essful 3 )*en a pa#0a%e is full, sen$s an as5n#*ronous Whatsapp messa%e to all frien$s informin% t*em of t*e pa#0a%e status T*e up$ate is $one !5 ino0in% sendMessage(friends, “Package no is not availa!le any"ore#$ on wapp:WAlient o!"e#t
Ans:
Question-! Diersit5 is a $an#e %roup from 9K t*e5 seem popular no6 :ets sa5 5ou are one of t*e or%aniers of )aes72'1; an$ #*ose to inite Diersit5 for a musi# ni%*t Diersit5 %roup mem!ers *ae personal information in t*e form of name, $ate of !irt* an$ t6itter *an$le For t*e musi# ni%*t, $iersit5 s*all perform four $an#es > ?s*le5Ban"o
@or$an Ban"o Born8 31 De#em!er 1==2 ?@or$anABan"o
Sam Cras0e Born8 Septem!er 1==' ?SamACras0e
.it#*ell Cras0e Born8 2 pril 1== ?.it#*ACras0e
I0e <e06u%o Born8 #to!er 1=> ?I0eT*eKi$$
Perri Kiel5 Born8 1; De#em!er 1==; ?realperri0iel5
)arren ussell Born8 3 .a5 1==' ?)arrenADE
Terr5 Smit* Born8 12 Fe!ruar5 1=>; ?TelADE
T*e first $an#e of ni%*t 6ill !e performe$ !5 Ban"o !rot*ers as lea$ pair an$ rest of t*e #re6 as !a#0up $an#ers T*e se#on$ $an#e s*all !e performe$ !5 I0e <e06u%o an$ Sam Cras0e in t*e lea$ 6it* t*e rest of $an#ers ta0in% on t*e role of !a#0up $an#ers 9sin% t*e %ien information, ans6er t*e follo6in% 4uestions 1 Dra6 t*e CC #ar$ of a $an#er 2 Create a #lass $ia%ram t*at 6ill #apture t*e personal $etails of ea#* $an#er, %roup stru#ture of ea#* $an#e an$ t*at of t*e musi# ni%*t 3 Dra6 o!"e#t $ia%ram for t*e first an$ se#on$ $an#es +20M Ans: R ar# o$ %ancer
lass #iagram $or #ance night
&'(ect #iagram $or $irst two #ances
Question-) Create a #lass #alle$ %og t*at *as t*ree fiel$s8 Strin% name, Person o6ner, an$ int a%e Create a #onstru#tor for %og t*at ta0es a Strin% name an$ a Person o6ner for t*e Do% an$ uses t*em for initialiation -ae t*e a%e for a Do% start at ' Implement %etter an$ setter met*o$s for !ot* name an$ o6ner .a0e t*e mutator for name su#* t*at 6*eneer a name is applie$ to a Do%, t*e a#tual name of t*e Do% is Gien Hame t*e Do% +e% %ien Ton5, t*e Do%Js name !e#omes Ton5 t*e Do% Implement onl5 an %etter for a%e Implement a met*o$ #alle$ *aeBirt*$a5 t*at $oes not return an5t*in% an$ simpl5 in#reases a Do%Js a%e !5 one Finall5, 6rite a met*o$ #alle$ toStrin% t*at returns a strin% of t*e form8 Do% name is a%e an$ !elon%s to 6ner name e% Ton5 t*e Do% is an$ !elon%s to a*ul T*e $efinition for Person is foun$ !elo6 +10M
pu6lic class Pers%n { private inal String na$e; pu6lic Pers%n(String ne52a$e { na$e = ne52a$e; pu6lic String get2a$e( { return na$e; Ans:
public class +%g { private String na$e; private Pers%n %5ner; private int age; public Dog(String na$e, Pers%n %5ner { set2a$e( na$e ; set85ner( %5ner ; age = 9; public void setName(String ne52a$e { na$e = ne52a$e 3 ) the +%g); public String getName( { return na$e; public void setOwner(Pers%n ne585ner { %5ner = ne585ner; public Pers%n getOwner( { return %5ner; public void haveBirthday( { 33age; public int getAge( { return age; public String toString( { return na$e3) is )3age3) and 6el%ngs t% )3%5ner.get2a$e(;
Question-5 Implement a *ic+cle #lass 6*i#* *as t*e follo6in% t*ree met*o$s8 pu!li# oi$ in#reaseSpee$+ pu!li# oi$ $e#reaseSpee$+ pu!li# !oolean is.oin%+ )it*in t*e *ic+cle #lass, 5ou must 0eep tra#0 of t*e Bi#5#leJs state8 moin% or not moin% ou must also 0eep tra#0 of t*e Bi#5#leJs #urrent spee$ )*eneer t*e Bi#5#le *as a positie #urrent spee$, t*e state s*oul$ !e moin% )*eneer t*e Bi#5#le *as a #urrent spee$ of ' t*e state must !e not moin% ou must use a !oolean aria!le to maintain t*e Bi#5#leJs state T*e met*o$s increaseSpeed an$ decreaseSpeed al6a5s in#rement an$ $e#rement Lrespe#tiel5M t*e #urrent spee$ !5 1 If increaseSpeed is #alle$ on a Bi#5#le 6*i#* is not #urrentl5 moin%, t*e Bi#5#le s*oul$ !e set to moin%, an$ t*e #urrent spee$ s*oul$ !e in#rease$ !5 1 If decreaseSpeed is #alle$ on a Bi#5#le 6*i#* is moin%, t*e #urrent spee$ s*oul$ !e $e#remente$ !5 one If t*e #urrent spee$ is eer $e#rease$ to ', t*e Bi#5#leJs state s*oul$ #*an%e from moin% to not moin% T*e met*o$ isMoving s*oul$ return t*e status of t*e Bi#5#le our Bi#5#le #lass s*oul$ also proi$e t6o #onstru#tors ne #onstru#tor ta0es no ar%uments an$ t*e ot*er ta0es an inte%er representin% t*e initial spee$ of t*e Bi#5#le T*e $efault #onstru#tor s*oul$ #reate a Bi#5#le 6*i#* is not #urrentl5 moin% an$ *as a #urrent spee$ of ' T*e se#on$ #onstru#tor s*oul$ set t*e #urrent spee$ to t*e passe$ initial spee$ H: if t*e spee$ is positie It s*oul$ also set t*e Bi#5#leJs state to moin% If t*e initial spee$ %ien is ne%atie or ', t*e #urrent spee$ s*oul$ !e set to ' an$ t*e Bi#5#leJs state s*oul$ !e not moin% +10M Ans:
public class :ic#cle { private boolean isM%ving; private int currentSpeed; public Bicycle ( { isM%ving = false; currentSpeed = 9; public Bicycle ( int speed { if( speed > 9 { isM%ving = true ; currentSpeed = speed; else { isM%ving = false; currentSpeed = 9; public void increaseSpeed( { if( isM%ving { currentSpeed33; else { isM%ving = true ; currentSpeed33; public void decreaseSpeed( { if( isM%ving { currentSpeed; if( currentSpeed == 9 { isM%ving = false; public boolean isMoving( { return isM%ving; public int getCurrentSpeed( { return currentSpeed;
Question-, )rite a "9nit test suite 6*i#* 6oul$ test in#reaseSpee$+ an$ is.oin%+ met*o$s of Bi#5#le #lass spe#ifie$ in Question-5 T*e !e*aior of !ot* t*e met*o$s *as !een $es#ri!e$ in Question-5 +15M Ans:
import junit.ra$e5%r&.*; public class est:ic#cle etends estase{ private :ic#cle 6ic#cle9, 6ic#cle2, 6ic#cleP; public !estBicycle" (String $eth%d { super($eth%d; protected void setUp( { 6ic#cle9 = new :ic#cle(; 6ic#cle2 = new :ic#cle(?9; 6ic#cleP = new :ic#cle(?9; protected void tearDown( { 6ic#cle9 = null; 6ic#cle2 = null; 6ic#cleP = null; public void test#ncreaseSpeed( { 6ic#cle9.increaseSpeed(; assertTrue()2% arg c%nstruct%r), 6ic#cle9.isM%ving(; assertEquals()2% arg c%nstruct%r), ?, 6ic#cle9.geturrentSpeed(; 6ic#cle9.increaseSpeed(; assertTrue()2% arg c%nstruct%r), 6ic#cle9.isM%ving(; assertEquals()2% arg c%nstruct%r), , 6ic#cle9.geturrentSpeed(; 6ic#cle2.increaseSpeed(; assertTrue()2egative arg c%nstruct%r), 6ic#cle2.isM%ving(; assertEquals()2egative arg c%nstruct%r), ?, 6ic#cle2.geturrentSpeed(; 6ic#cle2.increaseSpeed(; assertTrue()2egative arg c%nstruct%r), 6ic#cle2.isM%ving(; assertEquals()2egative arg c%nstruct%r), , 6ic#cle2.geturrentSpeed(; 6ic#cleP.increaseSpeed(; assertTrue()P%sitive arg c%nstruct%r), 6ic#cleP.isM%ving(; assertEquals()P%sitive arg c%nstruct%r), ??, 6ic#cleP.geturrentSpeed(; 6ic#cleP.increaseSpeed(; assertTrue()P%sitive arg c%nstruct%r), 6ic#cleP.isM%ving(; assertEquals()P%sitive arg c%nstruct%r), ?, 6ic#cleP.geturrentSpeed(;
public void test#sMoving( { assertFalse()2% arg c%nstruct%r), 6ic#cle9.isM%ving(; assertFalse()2egative arg c%nstruct%r), 6ic#cle2.isM%ving(; assertTrue()P%sitive arg c%nstruct%r), 6ic#cleP.isM%ving(;
public static est suite( { estSuite suite; suite = new estSuite()est Main); suite.addest(new est:ic#cle()test/ncreaseSpeed); suite.addest(new est:ic#cle()test/sM%ving); return suite;
Question- +ns6er on t*e !a#0 si$e of t*is s*eet Seeral $esi%n properties for a pro!lem are presente$ !elo6 9se t*ese properties in or$er to 6rite all t*e ne#essar5 #lasses an$/or interfa#es for a solution to t*e pro!lem Fo#us on #lass stru#ture an$ intera#tion ou ma5 implement 5our solution *o6eer 5ou 6is*, !ut 5ou 6ill !e ealuate$ on t*e appropriateness of 5our solution to t*e re4uirements Hote t*e use of #apitaliation an$ parent*eses for #larifi#ation ou ma5 use 6*ateer #onstru#tors or a$$itional met*o$s 5ou 6is* ou must $efine a stru#ture t*at #an represent nimals nimals *ae t6o !e*aiors t*e5 #an spea0+ an$ t*e5 #an moe+ B5 $efault, 6*en an animal moes, t*e teNt T*is animal moes for6ar$ is $ispla5e$ B5 $efault, 6*en an animal spea0s, t*e teNt T*is animal spea0s is $ispla5e$ %eneral nimal s*oul$ not !e a!le to !e instantiate$ Define also t6o #lasses, Goose an$ :5nN, t*at are nimals Bot* Goose an$ :5nN !e*ae su#* t*at 6*ere animal is $ispla5e$ in spea0+ or moe+, %oose or l5nN is $ispla5e$ !5 t*e appropriate #lasses Finall5, an5 instan#e of Goose #an fl5+, "ust as an5 Fl5in% o!"e#t #an n irplane is also a Fl5in% o!"e#t Define t*e irplane #lass su#* t*at it is Fl5in% an$ ma0e sure t*at an5 instan#e of Goose is also Fl5in% T*e spe#ifi# !e*aiors 6*en instan#es of eit*er #lass fl5+ are up to 5ou Instan#es of eit*er Goose or irplane s*oul$ !e a!le to !e store$ in a aria!le of t5pe Fl5in% +20M Ans:
abstract class -ni$al { private final String na$e; public Animal ( String na$e { this.na$e = na$e; public Animal ( { this( )ani$al) ; public void speak( { S#ste$.%ut.println( )his ) 3 na$e 3 ) spea&s) ; public void move( { S#ste$.%ut.println( )his ) 3 na$e 3 ) $%ves %r5ard) ;
class L#n0 etends -ni$al { public $yn( { super( )l#n0) ; interface @l#ing { public void fly(; class A%%se etends -ni$al implements @l#ing { public %oose ( { super( )g%%se) ; public void fly( { S#ste$.%ut.println( )his ) 3 getlass(.get2a$e( 3 ) s%ars, 5ings lapping.);
class -irplane implements @l#ing { public void fly( { S#ste$.%ut.println( )his ) 3 getlass(.get2a$e( 3 ) s%ars, engines running.);
Question-. +ns6er on t*e !a#0 si$e of t*is s*eet s part of a 6il$ life tra#0in% soft6are $eelopment, 5ou nee$ to store an$ retriee lo#ations of animals
import java.s1l.*; class -ni$al { public String ani$al/+; public String ani$al#pe; public float lattitude; public float l%ngitude; public Animal ( { ani$al/+ = )B?9CD); ani$al#pe = )Be6ra); lattitude = (float D.9EEFE; l%ngitude = (float DG.DC9EG;
public class -ni$al+:/nterace { public AnimalDB#nterface( { %nnecti%n c%n; State$ent stat; try { lass.orNa!e()%rg.h.+river); c%n = +riverManager."etConne#tion()jd6c4h4ile4IildLierac&ing), )%%p), )pass5d); stat = c%n.createState$ent(; stat.e0ecute()7-7 -:L7 /@ 28 7J/SS L-SSP87+) 3 )(-2/M-L/+ K-H-(F9, -2/M-LP7 K-H-(F9, ) 3 )L-/'+7 @L8-, L82A/'+7 @L8-, P/M- 7 N 3 N(-2/M-L/+); c%n.cl%se(; catch(70cepti%n e { e.printStac&race(; public void store$ocation(-ni$al ani$al { %nnecti%n c%n; State$ent stat; try { lass.orNa!e()%rg.h.+river); c%n = +riverManager."etConne#tion()jd6c4h4ile4IildLierac&ing), )%%p), )pass5d); stat = c%n.createState$ent(; stat.e0ecute()/2S7 /28 L-SSP87+ K-L'7S (O) 3 ani$al.ani$al/+ 3 )O,O) 3 ani$al.ani$al#pe 3 )O,O) 3 ani$al.lattitude 3 )O,O) 3 ani$al.l%ngitude 3 )O); c%n.cl%se(; catch(70cepti%n e { e.printStac&race(;
public void fetch$ocation(String ani$al/+ { %nnecti%n c%n; State$ent stat; esultSet rs; float lattitude, l%ngitude; String ani$al#pe; try { lass.orNa!e()%rg.h.+river); c%n = +riverManager."etConne#tion()jd6c4h4ile4IildLierac&ing), )%%p), )pass5d); stat = c%n.createState$ent(; rs = stat.e0ecuteuer#()S7L7 * @8M L-SSP87+ IH77 ) 3 )-2/M-L/+ = O) 3 ani$al/+ 3 )O); while (rs.ne0t( { ani$al#pe = rs.getString()ani$al#pe); lattitude = rs.get@l%at()lattitude); l%ngitude = rs.get@l%at()l%ngitude); S#ste$.%ut.println(ani$al/+ 3 ),) 3 ani$al#pe 3 ),) 3 lattitude 3 ),) 3 l%ngitude; c%n.cl%se(; catch(70cepti%n e { e.printStac&race(;