Students
Teachers & SchoolsStudents
Teachers & SchoolsAPCS: Standard Data Structures Drill 1, Problem 2. Which of the following methods are not appropriate for reuse in other programs?
AP | AP Computer Science |
AP Computer Science | Standard Data Structures |
Computer Science | AP Computer Science |
Language | English Language |
Program Analysis | Program correctness |
Test Prep | AP Computer Science |
carring in here is the code you skimming skimming skimming
that's giving Come on All right And here the potential
answers Causeway We thought yourself when your mother's competing you
and all right let's go Alright so looks like we're
being asked to figure out which method or methods would
not be suitable to reuse in another project somewhere else
The many coders frequently reuse when possible and what's the
point in reinventing the wheel if you've already written a
perfectly suitable piece of code for another project Well let's
start with phil array This one looks like it counts
the number of values in a given array whether it's
five imagers or five thousand and fills an empty array
with the values there's Nothing in this method that is
specific to this exact project So we could certainly reuse
this code elsewhere if necessary We can cross a d
off the list by how about swap Well it's not
just a clever name Looking at the code here it
appears swap takes two values in an array and well
stops them well More specifically it takes the first of
two array variables Moves it out of the way into
a variable called temp Ferreri then moves the second variable
this one into the first variables position in the array
We'll temp is then moved into the second variables place
into the switches Complete there's nothing and swap that tied
specifically to this project either So it's safe to say
that swap can work elsewhere and we can scratch Be
off the list All right What about print array for
the need of this is just one line How could
it not work somewhere else Well let's examine this four
loop more closely There are three expressions when you start
a four statement contained right there in the princess The
first is the initialization statement which runs once at the
beginning of the luke Here we set a variable up
to be our iterated er or counter it's eye with
a value of zero to start The second expression is
the terminator Which when It becomes false will terminate the
luke piri its eyes less than twenty meaning it'll continually
run until a value i becomes twenty or greater Meaning
it will run twenty times Cried it's in your base
number after the loop terminates The rest of the program
will continue running The third expression is the increment which
will run each time the loop runs It increases I
buy one each time so all this put together means
the variable i will started zero print a member of
the array then add one toe i over and over
and over until i reaches twenty Bottom line is this
method prince twenty values oven array then stops that might
work just fine for this program but can we really
use that code anywhere else What if we need to
print an array with only five values Or what about
honore With three hundred or an array that changes sizes
This print array method is just too inflexible to guarantee
that we could use it elsewhere That's not to say
it's bad code so we can't really rely on it
for any other purposes without modifications So the answer c
is the only one That fits the bill Three is
not suitable for dropping into another program and it really
is time to change that shirt Come on frankie Yeah