ShmoopTube

Where Monty Python meets your 10th grade teacher.

Search Thousands of Shmoop Videos


AP Computer Science A Videos 108 videos

AP Computer Science 1.2 Program Development
293 Views

AP Computer Science 1.2 Program Development. Which of the following is correct?

AP Computer Science 2.3 Classes and Objects
191 Views

AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?

AP Computer Science 2.3 Program Development
195 Views

AP Computer Science 2.3 Program Development. What is correct implementation of the Emperor constructor method?

See All

AP Computer Science 1.2 Standard Data Structures 194 Views


Share It!


Description:

APCS: Standard Data Structures Drill 1, Problem 2. Which of the following methods are not appropriate for reuse in other programs?

Language:
English Language

Transcript

00:00

Thank you We sneak And here's your smoke du jour

00:05

brought to you by reusable methods A bit like wearing

00:08

the same shirt day after day Except fewer people will

00:11

notice All right which of the following methods are not

00:14

appropriate for reuse in other programs Hillary swap print array

00:20

carring in here is the code you skimming skimming skimming

00:23

that's giving Come on All right And here the potential

00:27

answers Causeway We thought yourself when your mother's competing you

00:31

and all right let's go Alright so looks like we're

00:34

being asked to figure out which method or methods would

00:37

not be suitable to reuse in another project somewhere else

00:41

The many coders frequently reuse when possible and what's the

00:44

point in reinventing the wheel if you've already written a

00:46

perfectly suitable piece of code for another project Well let's

00:49

start with phil array This one looks like it counts

00:53

the number of values in a given array whether it's

00:55

five imagers or five thousand and fills an empty array

00:58

with the values there's Nothing in this method that is

01:00

specific to this exact project So we could certainly reuse

01:03

this code elsewhere if necessary We can cross a d

01:06

off the list by how about swap Well it's not

01:10

just a clever name Looking at the code here it

01:13

appears swap takes two values in an array and well

01:16

stops them well More specifically it takes the first of

01:19

two array variables Moves it out of the way into

01:22

a variable called temp Ferreri then moves the second variable

01:27

this one into the first variables position in the array

01:30

We'll temp is then moved into the second variables place

01:33

into the switches Complete there's nothing and swap that tied

01:36

specifically to this project either So it's safe to say

01:39

that swap can work elsewhere and we can scratch Be

01:41

off the list All right What about print array for

01:44

the need of this is just one line How could

01:47

it not work somewhere else Well let's examine this four

01:51

loop more closely There are three expressions when you start

01:55

a four statement contained right there in the princess The

01:58

first is the initialization statement which runs once at the

02:01

beginning of the luke Here we set a variable up

02:04

to be our iterated er or counter it's eye with

02:07

a value of zero to start The second expression is

02:10

the terminator Which when It becomes false will terminate the

02:14

luke piri its eyes less than twenty meaning it'll continually

02:18

run until a value i becomes twenty or greater Meaning

02:21

it will run twenty times Cried it's in your base

02:24

number after the loop terminates The rest of the program

02:28

will continue running The third expression is the increment which

02:31

will run each time the loop runs It increases I

02:34

buy one each time so all this put together means

02:37

the variable i will started zero print a member of

02:40

the array then add one toe i over and over

02:42

and over until i reaches twenty Bottom line is this

02:46

method prince twenty values oven array then stops that might

02:49

work just fine for this program but can we really

02:51

use that code anywhere else What if we need to

02:54

print an array with only five values Or what about

02:57

honore With three hundred or an array that changes sizes

03:00

This print array method is just too inflexible to guarantee

03:02

that we could use it elsewhere That's not to say

03:04

it's bad code so we can't really rely on it

03:07

for any other purposes without modifications So the answer c

03:11

is the only one That fits the bill Three is

03:13

not suitable for dropping into another program and it really

03:17

is time to change that shirt Come on frankie Yeah

Related Videos

AP Computer Science 1.2 GridWorld Case Study and APIs
493 Views

AP Computer Science 1.2 GridWorld Case Study and APIs. What is the direction of the actor?

AP Computer Science 1.4 Standard Algorithms
200 Views

AP Computer Science 1.4 Standard Algorithms. How many times will mystery be called for mystery(n) for n > 1?

AP Computer Science 2.3 Classes and Objects
191 Views

AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?

AP Computer Science 3.4 Inheritance, Abstraction, and Polymorphism
204 Views

AP Computer Science 3.4 Inheritance, Abstraction, and Polymorphism. Which of the following will satisfy the conditional if statement for boo, str,...

AP Computer Science 4.2 Standard Algorithms
191 Views

AP Computer Science 4.2 Standard Algorithms. What kind of algorithm is the following?