Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 3: Apply End result of this part: : pass all tests in these files o ApplyTest.java run the following Queries o TextQuery1a.java o TextQuery1b.java

Part 3: Apply

End result of this part:

: pass all tests in these files

o ApplyTest.java

run the following Queries

o TextQuery1a.java

o TextQuery1b.java

The Apply iterator

Wouldn't it be nice if the IntApply operator worked for data that wasn't just integers? In fact, at the end of this part, you will run a query on some Text data. To get there, you will write a "generic" version of IntApply that can deal with any type of data.

Take a look at Apply.java. You'll see that it looks very similar to IntApply.java except that there are generic types InT and OutT. InT indicates the type of the input data. OutT indicates the type of the output data.

Notice that instead of an IntApplyFunction, Apply uses an ApplyFunction. This interface provides the generic apply() method that can take any input type and return any output type.

public interface ApplyFunction {

public OutT apply(InT x);

}

Finally, when we want to use Apply, we will implement ApplyFunction. In ApplyTest.java you will see a few examples of generic apply functions. One of them is the TimesTwo class rewritten to implement ApplyFunction. Notice that both generic types are Integer to say that our multiply-by-2 operation takes an integer as input and produces an integer as output.

private class TimesTwo implements ApplyFunction {

@Override

public Integer apply(Integer x) {

return x*2; }

}

What you need to do

Fill in the implementation of the Apply class in Apply.java. We've already provided the fields and the constructor to get you started.

Testing your code

Run the tests in ApplyTest.java.

Pair. The "left" element in the Pair is the filename and the "right" element in the Pair is the entire contents of that file. The default query just uses Apply to take the "right" element from a Pair, that is, return the file contents.

The while loop calls next() on the last Iterator and prints out the elements (i.e., the contents of all the text files).

Here is an illustration of the Iterators in TextQuery1a

While loop <---Limit(1)<-----Apply(TakeRight)<------TextFileReader

Now, you need to implement a query yourself, in TextQuery1b.java. This query is very similar, except instead of the Limit, it takes the 12th word from every file. All you need to do to finish the query is fill in the TwelfthWord class. When you run the query, you'll see just the twelfth word from every file.

While loop <---- Apply(TwelthWord)<-----Apply(TakeRight)<------TextFileReader

Code

File: ApplyTest.java

package iterators;

import java.util.Iterator;

public class Apply implements Iterator {

// The function that will be applied to each input element to make an output element

private final ApplyFunction f;

// The Iterator that this Apply object will get its input from

private final Iterator input;

public Apply(ApplyFunction f, Iterator input) {

}

@Override

public boolean hasNext() {

}

@Override

public OutT next() {

}

}

File: TextQuery1a.java

package queries;

import iterators.Apply;

import iterators.ApplyFunction;

import iterators.Limit;

import java.util.Iterator;

import readers.TextFileReader;

// return the contents of the first file

public class TextQuery1a {

public static void main(String[] args) {

Iterator> filenameAndContents = new TextFileReader("../sci.space");

Iterator contents = new Apply(new TakeRight<>(), filenameAndContents);

Iterator firstFileContents = new Limit(1, contents);

while (firstFileContents.hasNext()) {

System.out.println(firstFileContents.next());

}

}

private static class TakeRight implements ApplyFunction, R> {

@Override

public R apply(Pair x) {

return x.right;

}

}

}

File: TextQuery1b.java

package queries;

import iterators.Apply;

import iterators.ApplyFunction;

import java.util.Iterator;

import readers.TextFileReader;

// return the 12th word in every file

public class TextQuery1b {

public static void main(String[] args) {

Iterator> filenameAndContents = new TextFileReader("../sci.space");

Iterator contents = new Apply(new TakeRight<>(), filenameAndContents);

Iterator TwelthWords = new Apply(new TwelfthWord(), contents);

while (TwelthWords.hasNext()) {

System.out.println(TwelthWords.next());

}

}

// The only change you should make to this file:

// define TwelfthWord class here

// Its apply function should take a String

// and return only the 12th word as a String (where words are defined as separated by a " ")

private static class TwelfthWord implements ApplyFunction {

}

private static class TakeRight implements ApplyFunction, R> {

@Override

public R apply(Pair x) {

return x.right;

}

}

}

FIle: Apply.java

package iterators;

import java.util.Iterator;

public class Apply implements Iterator {

// The function that will be applied to each input element to make an output element

private final ApplyFunction f;

// The Iterator that this Apply object will get its input from

private final Iterator input;

public Apply(ApplyFunction f, Iterator input) {

}

@Override

public boolean hasNext() {

}

@Override

public OutT next() {

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

If you dont have a good BATNA, you shouldnt bother to negotiate.

Answered: 1 week ago

Question

2. Identify conflict triggers in yourself and others

Answered: 1 week ago