19Use any of the following BagInterface JAVA methods to answer the question belo
ID: 671920 • Letter: 1
Question
19Use any of the following BagInterface JAVA 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 boolean remove()
public boolean remove(T anEntry)
public T getFrequencyOf(T anEntry)
public int getCurrentSize()
public void clear()
Write a complete JAVA method at the client level called countGreaterThan.
#The method header is:
public int counterGreaterThan(BagInterface<Integer> bag, int number)
The method counts how many items in the bag are greater than number. The bag should not be altered when the method completes.
For this question, you are writing code at the client level. This means you do not know how the bag is implemented.
Explanation / Answer
BagInterface:
package bags;
public interface BagInterface<B> {
public int getCurrentSize();
public boolean isFull();
public boolean isEmpty();
public boolean add(B newEntry);
public B remove();
public boolean remove(B anEntry);
public void clear();
public int getFrequencyOf(B anEntry);
public boolean contains(B anEntry);
}
BagCount.java
package bags;
class BagCount implements BagInterface<Object>
{
BagInterface<Object> bagCount=new BagCount();
@Override
public boolean add(Object newEntry)
{
do
{
Object entry = newEntry;
bagCount.add(entry);// TODO Auto-generated method stub
}while(!bagCount.isFull());
return false;
}
@Override
public void clear()
{
while(!bagCount.isEmpty()){
Object entry = bagCount.remove();// TODO Auto-generated method stub
System.out.println(entry);
}
}
@Override
public boolean contains(Object anEntry)
{
// TODO Auto-generated method stub
return false;
}
@Override
public int getCurrentSize()
{
int k=0;
while(!bagCount.isEmpty()){
k++;
}
return k;
}
@Override
public int getFrequencyOf(Object anEntry)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty()
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isFull()
{
// TODO Auto-generated method stub
return false;
}
@Override
public Object remove()
{
// TODO Auto-generated method stub
return null;
}
@Override
public boolean remove(Object anEntry)
{
// TODO Auto-generated method stub
return false;
}
public int counterGreaterThan(BagInterface<Integer> bag,int number){
int n=number;
int size=0;
if(n>getCurrentSize())
{
size=n;
}
return size;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.