CS365 Midterm

advertisement
CS365 Midterm
1)
2)
3)
4)
1.
You must answer all of the questions.
Place your name on the exam.
You must use Java to implement the coding questions.
Good luck!
(22points)Choosetheappropriatetermfromthefollowinglisttofillineach
blankbelow:
abstract
type
executable
abstract
class
extends
class
friend
inheritance interface
jar
module
nonvirtual
protected
namespace
parametric private
composition concrete
type
generic
hash
tables
java
jump
tables
Object
objectoriented
public
pure
virtual
static
subclass
Throwable
throws
…
Error
Exception
implementation
implements
library
longjmp
overloading
package
replicated
Runtime
Exception
superclass
virtual
setjmp
shared
source
subtype
supertype tar
template
types
virtual
void
vtables
machine
a. ____________________________polymorphismusestemplatesinstantiatedwith
typeswhile____________________________polymorphismmakesuseofclass
hierarchiestosupportpolymorphism.
b. ______________________________inheritancereferstomultipleinheritanceinwhich
asubclassreceivesmultiplecopiesofanysharedsuperclasses.
c. C++attemptstoprovidesomeofthebenefitsofpackages/modulesthrough
its______________________________and___________________________mechanisms.
d. A__________________________isaninterpreterthatsimulatestheexecutionofa
machinebyexecutingthemachine'sinstructionsetusingsoftwarepieceof
softwarethatsimulatesmachineinstructions
e. ________________________________Ifyoucreateauser-definedexceptionclassin
Java,whichJavaclassshoulditsubclass?
f. InJava,aninterfaceisusedtodeclarea(n)_______________________________________.
g. _________________________________areusedtoimplementvirtualmethodsatruntime
h. __________________________________TheCfunctionthattakesstoredstate
informationandanerrorcodeasarguments,andthatthrowscontrolbackto
thecallingfunctionbypoppingthestackandrestoringthestateinformation
ofthecallingfunction.
i. A_____________________________fileinJavaallowsacollectionofclassestobe
bundledtogetherandtreatedlikeasinglefileforexecution,muchlikea
binaryfileinC/C++.
2. (3points)SupposethatclassGoowantstorestrictaccesstoitsinstance
variablesizeto1)anyclassinitspackageand2)anysubclass,whetherthat
subclassisinGoo’spackageoranotherpackage.Whataccessprotectionshould
theprogrammerassigntosize?
a. private
b. protected
c. package
d. public
3. (4points)SupposeyouhavethefollowingC++classes:
ClassFruit{…}
ClassMelon:publicFruit{…}
ClassCantelope:publicFruit{…}
FurtherassumethatFruitisanabstractclassbecauseithasoneormore
purevirtualmethods.Circleallallofthefollowingdeclarationsthatarelegal:
a. Fruitf;
b. Fruitf=Fruit();
c. Fruitf=Melon();
d. Fruit*f=newFruit();
e. Fruit*f=newMelon();
f. Fruit*f=newCantalope();
4. (20points--Generics)WriteaJavatemplateclassnamedStackthattakesa
singletypeparameternamedEandsupportsthreemethods:
a. aconstructorthatinitializesthestack.
b. pop:takesnoparameters,removesthefirstvaluefromthetopofthestack
andreturnsit.Ifthestackisemptyitshouldthrowaninstanceofaclass
namedStackEmptybacktothecallingfunction.Youdonotneedtoknowthe
particularsofStackEmptyandyoushouldassumethatthatclassalready
exists.
c. push:avoidmethodthattakesasingleparameteroftypeEandassignsitto
thetopofthestack.
Problemrequirements:
• Thestackmustbeimplementedasalinkedlistandyoumaynotuse
anyoftheJavacollectionclassestoimplementit.Thisproblemtests
whetheryouunderstandhowtomanipulate“pointers”inJava.
• ThestackshouldhaveanestedclasscalledStackNodethatcontainsa
valuefieldthatstoresavalueandanextfieldthatpointstothenextitem
intheStack.
• TheStackshouldmaintainaninstancevariablenamedtopthatpointsto
thetopnodeintheStack,whichshouldbethefirstnodeinthelist.The
popandpushmethodscanusethisvariabletoremoveoraddavalueto
thestack.
Startyouranswerhereandcontinueonthenextpageifnecessary
5. (21points--Inheritance)Supposethatyouaredesigningtheinputportionofa
Javaapplication.Theapplicationshouldbeabletoreadfromthe1)console,2)a
file,or3)awidgetsuchasatype-intextbox.Youhavebeengiventhefollowing
specifications:
• Regardlessofwhichinputobjectyouareusing,theapplicationneedsto
beabletoopen/closetheobject(openandclosemethodsthattakeno
parametersandreturnvoid).
• Theapplicationneedstobeabletoreadatextstringfromaninputobject
(readmethodthatreturnsastringandtakesnoparameters).
• TheclassesforthereaderobjectsshouldbeConsoleReader,FileReader,
andTextReader.
• Theopenandclosemethodsusethesameimplementationforeachofthe
threeclasses(yesthat’sunrealisticbutjustassumeit’strue)andtheread
method’simplementationisdifferentforeachclass.
a. DeclareaninterfacenamedReadersothatobjectsofanyofthethree
classescanbeassignedtoaReadervariable.Forexample:
ReadermyReader=newConsoleReader();
b. Designanddrawaclasshierarchyfortheabovethreeobjectsusing
Java.
• Youshoulddrawatreerepresentingyourclasshierarchy.You
mayhavetocreateoneormoreadditionalclasses
• Nexttoeachclasslistthemethodsthatyouwoulddeclarewith
thatclass.Youshouldlistamethodwithaclassonlyifyouwould
provideanimplementationforthatmethodinthatclass.
• Putanasterisknexttoanymethodthatshouldbedeclared
abstract
c. ProvideJavaclassdeclarationsforanysuperclassesthatyoucreatedin(b)
andforConsoleReader.
a. TheclassesmustimplementtheReaderinterface
b. Youshouldnotshowanyimplementation.Forexample,todeclarea
voidmethodnamedfoo,write:
publicvoidfoo(){…}
6. (25points--JavaProgramming)WriteacompleteJavaprogram,including
importstatementsinaclasscalledGraderthatmeetsthefollowing
specifications:
a. TheclassshouldbepartofthepackageGrade.
b. Usetheconstructortoimplementwhateveryouwouldnormallyimplement
inmaininaC++program.Theconstructorshouldtakeasinglestring
argumentwhichisthenameofthegradefile.
c. Yourprogramshouldreadscoresandstudentnamesfromagradefilewhose
namehasbeenprovidedasacommandlineargumentandforeachstudentit
shouldprintthestudent’snameandaveragescoretostdout.Theaverage
shouldbecomputedasanintegeraverage.Forexample,ifthestudent’s
scoresare7and10,thentheprintedaverageshouldbe8.
d. YourconstructorshouldnothandletheIOExceptionthatmaybegenerated
byopeningthefilebutyourmainmethodshouldcatchtheexceptionand
printtheexception’smessagebyinvokingitsgetMessage()method.
Eachlineofthegradefilelistsastudent'snameandthenthestudent’sscores.The
gradefilemayhavemultiplestudents.Forexample:
Baby Daisy 59 75 93 53
Smiley The Amazing Hound 86 45 100 63 78 91
Chipmunk 45
Notethatanamemayconsistofanarbitrarynumberofwordsandthatastudent
mayhaveanarbitrarynumberofscores.
Youroutputshouldbeformattedasfollows:
1. Thenameshouldbeleft-justifiedinafield30characterswide.
2. Theaverageshouldberight-justifiedinafield3characterswide.
3. Thereshouldbeaspacebetweenthetwofields.
Fortheaboveinput,yourprogramwouldproducetheoutput:
Baby Daisy
Smiley The Amazing Hound
Chipmunk
70
77
45
Hereissomeadditionalinformation:
1. Youareguaranteedthat:
• thereisatleastonescoreforeachstudent,
• thatallscoresarenon-negativeintegers,and
• thateverylinestartswithatleastoneword.
2. TheAPIfortheScannerclasshasbeenprovidedattheendofthisexam.
3. YoushouldusetheFileReaderclassforafile.YoudonotneedtheAPIforthe
FileReaderclassinordertocompletethisproblem.
4. TheScannerclassisinJava’sutillibraryandtheFileReaderclassisinJave’s
iolibrary
Writeyouranswertoquestion6onthispage.Thereisonemorequestion,
question7,onthenextpage.
7. (5points:ExecutingaJavaProgram)Answerthefollowingquestionsabout
thepreviousproblem.
a) WhatisthenameofthefileinwhichGradershouldbeplaced?
b) WhatisthenameofthedirectoryinwhichGrader’sfileshouldbeplaced?
c) SupposethatIaminanarbitrarydirectoryandthatthedirectorycontaining
Grader’sfileisstoredinadirectorynamed/home/bvz/labs.Furtherassume
thatthegradefileisnamedinput.txt.Writethefulljavacommandrequired
toexecutetheGraderprogramthatyouwroteinthepreviousquestion.
Download