Question

In: Computer Science

Iterating a method over a collection of objects is a common task that you may complete...

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:

  1. Briefly describes your collection and the elements that comprise it
  2. Identifies the characteristic(s) that differentiate(s) the elements of the collection
  3. Briefly describes the method you apply to that collection
  4. Defines the method using pseudo code that contains at least two if methods

Solutions

Expert Solution

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();
}
}


Related Solutions

Scroll down to complete all parts of this task. Steppenwolf Co. uses the direct method to...
Scroll down to complete all parts of this task. Steppenwolf Co. uses the direct method to prepare its statement of cash flows. Steppenwolf's financial statements are as follows: Comparative Balance Sheet 12/31/Year 2 12/31/Year 1 Cash $10,000   $20,500   Accounts receivable 17,000   13,000   Inventory 25,000   20,000   Supplies 900   500   Prepaid insurance 800   2,000   Equipment, net of accumulated depreciation   36,000     40,000        Total assets $89,700   $96,000   ̿ ̿ ̿ ̿ ̿ ̿ ̿ ̿ ̿ ̿ ̿ ̿    ̿ ̿ ̿ ̿ ̿...
3. For the following collection of statements, describe the sets of actions, objects, and subjects; and...
3. For the following collection of statements, describe the sets of actions, objects, and subjects; and draw an access control matrix to represent the scenario. 1 Mark Alice can climb trees and eat apples. Bob can climb fences, eat apples, and wave flags. Trees can hurt apples. Carol can jump waves and wave flags.
Task 2 requires you to accurately complete the activities described in each question. Complete the answers...
Task 2 requires you to accurately complete the activities described in each question. Complete the answers in your own Excel document, putting your name at the top along with the Task and Task letter appropriate to each answer and submit below for your Assessor. a) Make the Journal entry to record the sale of merchandise that cost $6,000, for $10,000 cash. b) Record journal entries for the following transactions. Purchased office furniture for $3,200, agreed to pay the entire amount...
This task is about classes and objects, and is solved in Python 3. We will look...
This task is about classes and objects, and is solved in Python 3. We will look at two different ways to represent a succession of monarchs, e.g. the series of Norwegian kings from Haakon VII to Harald V, and print it out like this: King Haakon VII of Norway, accession: 1905 King Olav V of Norway, accession: 1957 King Harald V of Norway, accession: 1991 >>> Make a class Monarch with three attributes: the name of the monarch, the nation...
Task 4 The class Polynomials is a collection of polynomials of either implementation stored in an...
Task 4 The class Polynomials is a collection of polynomials of either implementation stored in an instance of the generic library class List. class Polynomials { private List<Polynomial> L; // Creates an empty list L of polynomials public Polynomials ( ) { … } (1 mark) // Retrieves the polynomial stored at position i in L public Polynomial Retrieve (int i) (1 mark) { … } // Inserts polynomial p into L public void Insert (Polynomial p) (1 mark) {...
Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that...
Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that contains an array as a private instance variable. Construct methods with arrays as parameters and return values. Use partially filled arrays to implement a class where objects can be dynamically added. Implement searching and sorting algorithms. Instructions For this assignment you will be implementing an application that manages a music collection. The application will allow the user to add albums to the collection and...
To complete this task you are required to design an information system for Fashion clothing store...
To complete this task you are required to design an information system for Fashion clothing store to assist with their business. You have discussed Porter’s Value Chain in class and you should understand the Primary and support activities within businesses. For this task you need to concentrate on Marketing and Sales only. The development of your professional skills includes researching information systems to assist with organisational issues that are encountered in contemporary business. You will be learning important ‘agile’ skills...
Titration of Na2CO3 with HCl. You may want to record your answers in the Data Collection...
Titration of Na2CO3 with HCl. You may want to record your answers in the Data Collection section of your lab manual for use during the lab. (pKa1(H2CO3) = 6.352, pKa2(H2CO3) = 10.329). (a) What are the first and second equivalence volumes (Veq1 and Veq2) when 20.00 mL of 0.10 M Na2CO3 is titrated with 0.20 M HCl? Veq1 = ? mL Veq2 = ? mL (b) What do you expect the pH to be at the following points The first...
Another task you need to complete, before you prepare the annual financial statements for the year...
Another task you need to complete, before you prepare the annual financial statements for the year ending 30 June 2019, is to determine the June insurance expense, and the wage expenses for GreenEarth shop employees. How do you do the journal entry? You’ve collected the following information. Insurance: Insurance is now $1,200 per calendar month and is paid in advance on the 21st day of each month. So, for example, GreenEarth pays $1,200 insurance on 21 March 2019 for the...
1. You can use any materials from the course to complete this task. 2. You are...
1. You can use any materials from the course to complete this task. 2. You are applying for a job and the posting is below: Position: Executive Administrative Assistant at Sinclair Community College. Requires high school diploma or equivalent and some college. Primary duties are customer service, professional writing, research, and basic computer skills. Apply by submitting a cover letter and resume to Natalie Rindler, Human Resource Generalist, 444 West Third Street, Dayton, OH 45402, no later than the posted...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT