Please help Write a complete method that takes a bag and a number as parameters
ID: 3841660 • Letter: P
Question
Please help
Write a complete method that takes a bag and a number as parameters and the minimum number in the bag.
The method header is: public Integer findMin(BagInterface<Integer> bag)
The bag should not be altered when the method completes.
Use only the methods listed below from BagInterface.
Note that toArray is not listed and should not be used in your solution.
You can also refer to BagInterface and ListInterface Files for the full details about the bag.
You are writing code at the client level, which means you do not know how the list is implemented.
BagInterface methods:
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()
/**
An interface that describes the operations of a bag of objects.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public interface BagInterface<T>
{
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
public int getCurrentSize();
/** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
public boolean isEmpty();
/** Adds a new entry to this bag.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful, or false if not. */
public boolean add(T newEntry);
/** Removes one unspecified entry from this bag, if possible.
@return Either the removed entry, if the removal.
was successful, or null. */
public T remove();
/** Removes one occurrence of a given entry from this bag.
@param anEntry The entry to be removed.
@return True if the removal was successful, or false if not. */
public boolean remove(T anEntry);
/** Removes all entries from this bag. */
public void clear();
/** Counts the number of times a given entry appears in this bag.
@param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
public int getFrequencyOf(T anEntry);
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if the bag contains anEntry, or false if not. */
public boolean contains(T anEntry);
/** Retrieves all entries that are in this bag.
@return A newly allocated array of all the entries in the bag.
Note: If the bag is empty, the returned array is empty. */
public T[] toArray();
} // end BagInterface
Explanation / Answer
Since we do not know how the interfaces are implemented, I will use the general interface functions(without using toArray() ).
Code:
------------------------------------------------------------------------------------------------------
import java.util.*;
import java.lang.*;
import java.io.*;
public class Bag implements Bag {
{
public Integer findMin(BagInterface<Integer> bag) {
int temp;
int min=null;
while((temp = bag.remove())!=null) { //Get one item from bag till its empty
if(min==null || temp < min) { //Update the minimum
min=temp;
}
bag.add(temp); //Put the item back in the bag
}
return min;
}
}
----------------------------------------------------------------------------------------------------------------
HOPE IT HELPS!