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

1. Write a complete method at the client level called countMultipleOf. The metho

ID: 3733463 • Letter: 1

Question

1. 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:

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.

2. Write a complete method at the client level called countGreaterThan.

The method determines how many numbers on a list are greater than some number.

The list should not be altered by the method.

The method header is:

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

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

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

Explanation / Answer

Please find my answer for Q1.

Please repost others in separate post.

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

       // creating a temp bag

       BagInterface<Integer> tempBag = new ArrayBag<>();

       int count = 0;

       while(!bag.isEmpty()) {

           int x = bag.remove();

           tempBag.add(x);

           if(x%number == 0)

               count++;

       }

      

       // now restoring original bad

      

       while(!tempBag.isEmpty()) {

           int x = tempBag.remove();

           bag.add(x);

       }

      

       return count;

   }