A FilteredData class and Measurable interface are included below. Create an inte
ID: 3663375 • Letter: A
Question
A FilteredData class and Measurable interface are included below. Create an interface Filter
public interface Filter {
boolean accept(Object o);
}
Modify FilteredData to optionally take a Filter on its constructor(s). A FilteredData object that has a Filter ignores Measurable objects that its filter does not accept.
Write a method to test your FilteredData object. Your tests should include
multiple FilteredData objects, at least one with no filter, others using a filter
at least two different Filter implementations
Measurable objects that both pass and don't pass the Filters.
FilteredData class and Measurable interface are below:
import java.util.ArrayList;
public class FilteredData {
private ArrayList<Measurable> ds;
private Measurable min;
private Measurable max;
public FilteredData() {
ds = new ArrayList<>();
min = null;
max = null;
}
public void add(Measurable ele) {
if ((min == null) || (min.getMeasure() > ele.getMeasure())) {
min = ele;
}
if ((max == null) || (max.getMeasure() < ele.getMeasure())) {
max = ele;
}
ds.add(ele);
}
public Measurable getMin() {
return min;
}
public Measurable getMax() {
return max;
}
}
public interface Measurable {
int getMeasure();
}
Explanation / Answer
public interface Filter
{
boolean accept(Object o);
}
public class ShortWordFilter implements Filter
{
public boolean accept(Object o)
{
return x.toString().length() < 5;
}
public static ArrayList<Object> collectAll(ArrayList<Object> objects, Filter f)
{
ArrayList<Object> collected = new ArrayList<Object>();
for (Object object : collected)
{
if (f.accept(object))
{
collected.add(object);
}
}
return collected;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.