Question
Iterating Over a Collection Iterating a method over a collection of objects is a common task that you may complete unknowingly each day. Within a
Iterating Over a Collection
Iterating a method over a collection of objects is a common task that you may complete unknowingly each day. Within a collection of similar things, you identify the characteristics that make the individual elements unique and treat them differently according to their classifications. For example, assume that you are at a store and want to pay for something in cash. The bills in your wallet or purse represent a collection of similar objects, differentiated by denomination. It would not be very helpful to treat all of the bills the same, even though they are made from the same paper and have the same dimensions—eight bills does not equate to eight dollars unless each is a one-dollar bill.
How would you calculate how much money you have to spend? Each denomination needs a different value assigned. You compute a running total until you have accounted for all of the bills. For the moment, assume you are uncomfortable carrying large bills, so your wallet or purse contains $1, $5, $10, or $20 bills. The following program represents a "pseudo code" solution to this exercise.
totalCash is a whole number that starts at zero
while (I still have bills left to account for)
if (current bill is $1)
add 1 to totalCash
if (current bill is $5)
add 5 to totalCash
if (current bill is $10)
add 10 to totalCash
if (current bill is $20)
add 20 to totalCash
if (totalCash > itemPrice)
I have enough money to buy the item.
else
I don't.
This is an example of an iterative method because it is repeated over and over for the entire collection.
Think of a way in which you apply an iterative method to a collection of similar objects in your day-to-day life. Be creative—in other words, identify a situation that does not involve money (either paper or coins). Think about the ways in which the items of your collection are alike, as well as the key ways in which they differ.
Post a response that:
- Briefly describes your collection and the elements that comprise it.
- Identifies the characteristic(s) that differentiate(s) the elements of the collection.
- Briefly describes the method you apply to that collection.
- Defines the method using pseudocode that contains at least two if methods.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Collection Closet organization Elements Shirts Differentiating characteristics Type of ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started