Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Write a complete method at the client level called countMultipleOf. The met

ID: 3599534 • Letter: J

Question

Java

Write a complete method at the client level called countMultipleOf.

The method counts how many items in a bag are multiples of some number.

For example, if a bag contains (8, 9, 6, 3, 8, 6, 1) and you invoke the method with number = 3, the method will return 4 because there are 4 numbers that are multiples of 3 in the bag (the 9, 6, 3, and 6).

The bag should not be altered when the method completes.

The method header is:

public int countMultipleOf(BagInterface<Integer> bag, int number)

For this question, you are writing code at the client level. This means you do not know how the bag is implemented.

Use any of the following BagInterface methods to answer the question below.

Note that toArray is not listed and should not be used in your solution.

public boolean add(T newEntry)
public boolean isEmpty()
public boolean contains(T anObject)
public T remove()
public boolean remove(T anEntry)
public int getFrequencyOf(T anEntry)
public int getCurrentSize()
public void clear()

Explanation / Answer

Please find my implementation.

Please let me know in case of any issue.

public int countMultipleOf(BagInterface<Integer> bag, int number) {

       int count = 0;

       while(!bag.isEmpty()) {

           int x = bag.remove();

           if(x%number == 0)

               count++;

       }

       return count;

   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote