Question

P9.17 (For Java, and can you please explain as well.) Declare an interface Filter as follows:...

P9.17 (For Java, and can you please explain as well.)

Declare an interface Filter as follows:

public interface Filter
{
boolean accept(Object x);
}











Write a method: public static ArrayList collectAll(ArrayList objects, Filter f) that returns all objects in the objects list that are accepted by the given filter.


Provide a class ShortWordFilter whose filter method accepts all strings of length < 5.

Then write a program that asks the user for input and output textfile names, reads all words from an input file, puts them into an ArrayList, calls collectAll, and prints a list of the short words to the output file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;


interface Filter
{
boolean accept(Object x);
}

class ShortWordFilter implements Filter {
        @Override
        public boolean accept(Object x) {
                return x.toString().length() < 5;
        }
}

class FilterDemo {
        
        public static ArrayList collectAll(ArrayList objects, Filter f)  {
                ArrayList result = new ArrayList();
                for(Object obj: objects) {
                        if(f.accept(obj)) {
                                result.add(obj);
                        }
                }
                return result;
        }
        
        public static void main(String args[]) throws FileNotFoundException {
                String inputFile = "input.txt";
                String outputFile = "output.txt";
                
                Filter f = new ShortWordFilter();
                
                ArrayList words = new ArrayList();
                Scanner reader = new Scanner(new File(inputFile));
                while(reader.hasNext()) {
                        words.add(reader.next());
                }
                
                ArrayList shortWords = collectAll(words, f);
                PrintWriter writer = new PrintWriter(new File(outputFile));
                for(Object s: shortWords) {
                        writer.println(s);
                }
        }
}
Add a comment
Know the answer?
Add Answer to:
P9.17 (For Java, and can you please explain as well.) Declare an interface Filter as follows:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Declare an interface Filter as follows: public interface Filter {boolean accept (Object x); } Modify the...

    Declare an interface Filter as follows: public interface Filter {boolean accept (Object x); } Modify the implementation of the DataSet class in Section 10.4 to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by processing a collection of bank accounts, filtering out all accounts with balances less than $1,000. Please use the code provided and fill in what is needed. BankAccount.java /**    A bank account has a...

  • ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code...

    ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code after the comments in the main method and using the javadoc documentation to implement the other methods. Task 1: Send array data to the screen In ArraysAndFiles.java, create a new method printOnScreen and compile it. In the main method of ArraysAndFiles.java, add the code to declare and initialize the array of Strings and call printOnScreen to print the array on the screen. Task 2:...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • 10.3 Example. When you first declare a new list, it is empty and its length is...

    10.3 Example. When you first declare a new list, it is empty and its length is zero. If you add three objects—a, b, and c—one at a time and in the order given, to the end of the list, the list will appear as a b c The object a is first, at position 1, b is at position 2, and c is last at position 3.1 To save space here, we will sometimes write a list’s contents on one...

  • Java ArrayList Sorting Alphabetically

    1. Write a program to input a list of names (strings) from the user and store them in an ArrayList. The input can be terminated by entering the empty string or byentering the string “quit”.2. Add further functionality to your program so that it searches the ArrayList to find the first string and the last string according to dictionary ordering and then prints out these strings (names). Do this exercise without sorting the names in the ArrayList. For example, if...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare...

    Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare an abstract class FarmAnimalBase that implements the interface FarmAnimal. In the class FarmAnimalBase, define a private final instance variable name (type: String), a public constructor that initializes the value of the instance variable name, and a public method getName() that returns the value of the instance variable name. Note that we do not implement the method talk() declared in the interface FarmAnimal, that’s the...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program...

    Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the input file to be spell checked. The program will read in the words for the dictionary, then will read the input file and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is, add...

  • USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in...

    USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in a number n and then reads n strings. it then reads a string s from the user and gives you the word that was typed in after s. Example input: 4 Joe Steve John Mike Steve Example output: John Example input: 7 Up Down Left Right North South East Right Example output: North 2. Remember to call your class Program Question 5.2 1. Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT