In: Computer Science
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:
Solution:
import java.util.*;
public class Main
{
// to take input from the user and calling the calculate function
to get the result
void input(){
Scanner sc= new Scanner(System.in);
ArrayList<String> l1= new ArrayList<>();
System.out.println("Enter the total number of bills you
have");
int n=sc.nextInt();
//input should be in the format: $5, $10 etc
System.out.println("Enter the bill values");
for(int i=0; i<n; i++){
l1.add(sc.next());
}
System.out.println("Enter the item price");
int p=sc.nextInt();
int totalcash=calculate(l1);
if(totalcash > p)
System.out.println("I have enough money to buy them");
else
System.out.println("I dont have enough money to buy them");
}
//to calculate the total cash from all the bills which are
there
static int calculate(ArrayList<String> l1){
int totalcash=0;
//t=iterating throught the collection
for(String s: l1){
String str="";
//getting the numeric part of the string without $ sign
for(int i=1; i<s.length(); i++){
str=str+s.charAt(i);
}
//converting the string to integer
int amt= Integer.parseInt(str);
totalcash=totalcash+amt;
}
return totalcash;
}
public static void main(String[] args) {
Main obj= new Main();
obj.input();
}
}