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

Java Write a complete getSomeEntry() method to add to the ArrayBag class. ArrayB

ID: 3599524 • Letter: J

Question

Java

Write a complete getSomeEntry() method to add to the ArrayBag class.

ArrayBag implements BagInterface using an array.

The method returns an unspecified entry of the ArrayBag. You can decide which entry to return.

Be sure to account for all possible times when the method could be invoked.

The method header is:

public T getSomeEntry()

For this question, you are writing code at the implementation level. This means you have access to the private instance data variables:

private T[] bag

private int numberOfEntries

You can assume the following methods are already implemented:

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 of required method.

public T getSomeEntry() {

       // bag is empty

       if(numberOfEntries == 0)

           return null;

       Random rand = new Random(); // random Object

       // getting a random number in range 0 - (numberOfEntries-1)

       int index = rand.nextInt(numberOfEntries);

       return bag[index];

   }

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