-1

I have assignment in java ,I need help please. I tried to solve it but I have some problem that can't understand them.

Assignment is: In this exercise, use the Template method pattern to define an abstract class Filter with a public method filter (the template method) that calls the method accept (the hook method) that can be implemented in different ways in the different concrete classes. Write a test program by extending the class Filter and defining accept so that only strings of at most three characters are accepted.

public abstract class Filter<T> {
    public abstract T[] filter(T[] list);

    public abstract boolean accept(T val);
}

public class FilterTest<T> extends Filter<T> {
    private int capacity = 0;

    public FilterTest(int cap) {
        this.capacity = cap;
    }

    @Override
    public T[] filter(T[] list1) {
        @SuppressWarnings("unchecked")
        T[] finalList = (T[]) Array.newInstance(list1.getClass().getComponentType(), capacity);

        int counter = 0;

        for (T t : list1) {
            if (accept(t)) {
                finalList[counter] = t;
                counter++;
            }
        }

        return finalList;
    }

    public void printArray(T[] list2) {
        for (int i = 0; i < list2.length; i++) {
            if (list2[i] != null) {
                System.out.print(list2[i] + " ");
            }
        }

        System.out.println();
    }

    @Override
    public boolean accept(T val) {
        return String.valueOf(val).length() > 0 && 
            String.valueOf(val).length() <= 3;
    }

    public static void main(String[] args) {
        FilterTest<String> filterTest = new FilterTest<>(8);

        String[] lists = { 
            "Hi", "here", "is", "the", "AOOP", "course", "at", "University" 
        };

        System.out.print("My original list is: ");
        filterTest.printArray(lists);

        System.out.print(" The filtered list is: ");
        String[] filteredList = filterTest.filter(lists);
        filterTest.printArray(filteredList);
    }
}

Here is comment from my teacher: "not correct, only the accept method should be abstract in the Filter class, the filter method should be already implemented in the Filter class and not be abstract all implementation will be the same, only the accept method changes for different filters)".

I don't understand what should I do now, how the code will be correct. help please, Thanks

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
Osama
  • 71
  • 5

1 Answers1

0

I assume that Filter should look something like this

public abstract class Filter<T> {

    public T[] filter(T[] list1) {
        @SuppressWarnings("unchecked")
        T[] finalList = (T[]) Array.newInstance(list1.getClass().getComponentType(), capacity);

        int counter = 0;

        for (T t : list1) {
            if (accept(t)) {
                finalList[counter] = t;
                counter++;
            }
        }

        return finalList;
    }

    public abstract boolean accept(T val);
}

You can even declare Filter<T> as an interface and have a default implementation for filter. Have a look here

samvel1024
  • 1,123
  • 4
  • 15
  • 39
  • You don't change any thing, can you explain what you have done please – Osama Oct 31 '19 at 17:09
  • Please have a look more accurately, the method definition of filter is moved to class Filter instead of being in the implementation. That's the point of template method design pattern. – samvel1024 Oct 31 '19 at 17:19
  • Oh, Thanks, What about capacity variable? – Osama Oct 31 '19 at 17:30