Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So this is part of what I need help and the instructions. At the bottom is the c

ID: 3738906 • Letter: S

Question

So this is part of what I need help and the instructions. At the bottom is the code that I need help with. If I could get a response as soon as possible I would appreciate that. Thank you

Part 2: IntApply

End result of this part:

• pass all tests in these files

o IntApplyTest.java

Here is a motivating query. Let's suppose we have a list of numbers [10,50,1,400]. This list will

be our input data. The query is "multiply each element of the input by 2". The chain of Iterators

to answer this question would be

The TimesTwoIterator reads one value at a time from List.iterator by calling the List.Iterator's

next() method. Whoever wants to read the output of the query will call the next() method on

the last Iterator in the chain, which is TimesTwoIterator.

Here is the sequence of next() calls that would occur to process this query (we are omitting the

hasNext() calls).

TimesTwoIterator List.iterator

TimesTwoIterator List.iterator

next()...

next() returns 10

returns 20

next()...

next() returns 50

returns 100

next()...

next() returns 1

returns 2

next()...

next() returns 400

returns 800

The IntApply iterator

A common operation in query processing is to simply call some function, ??, on the input

element to get an output element. This operation is known as "Apply". In fact, the

TimesTwoIterator above is a special case of Apply, where ??just multiplies the input by two.

Here is our example query written in Java. We've used IntApply instead of TimesTwoIterator.

Integer[] inputValues = {10,50,1,400};

List<Integer> input = Arrays.asList(inputValues);

IntApply op = new IntApply(new TimesTwo(), input.iterator());

The IntApply constructor takes two arguments: the first is an object of type IntApplyFunction

and the second is the input iterator.

IntApplyFunction is an interface with one method, apply(). Any class that "implements"

IntApplyFunction must provide an implementation of apply().

public interface IntApplyFunction {

public int apply(int x);

}

In IntApplyTest.java you can see an example of how IntApply is used. We define a class

TimesTwo that defines an apply() that multiplies its input by 2.

private class TimesTwo implements IntApplyFunction {

@Override

public int apply(int x) {

return x*2;

}

}

What you need to do

Fill in the implementation of the IntApply class in IntApply.java. You need to complete the

constructor, hasNext, and next.

This is the code that I have right now.

package iterators;

import java.util.Iterator;

public class IntApply implements Iterator {

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

private final IntApplyFunction f;

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

private final Iterator<Integer> input;

public IntApply(IntApplyFunction f, Iterator<Integer> input) {

(this is where I need help with implementing a different function from the test result)   

}

@Override

public boolean hasNext() {

  

}

@Override

public Integer next() {

  

}

  

}

Explanation / Answer

import java.util.Iterator;
import java.util.NoSuchElementException;

public class IntApply implements Iterator {
        // The function that will be applied to each input element to make an output element
       private final IntApplyFunction f;

        // The Iterator that this Apply object will get its input from
       private final Iterator<Integer> input;      

       public IntApply(IntApplyFunction f, Iterator<Integer> input) {
                    this.input = input;
                    this.f = f;
       }

       @Override
       public boolean hasNext() {
                    return input.hasNext();
       }

       @Override
       public Integer next() {
                    if (input.hasNext()){
                        int T = f.apply((input.next()));
                        return T;
       }
                    else{
                        throw new NoSuchElementException();
                    }
                }
}
              

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote