-3

I dont have any clue how to approach the problem i am facing. We have to print out an array using function.Function as a parameter and using lambas to print out 1. the even and 2. the odd numbers.

final class Main
{
    private Main() {}

    public static void main(String[] argv)
    {
        // Fill the array with integer values and print out only
        // values of even indices
        DataStructure ds = new DataStructure();
        ds.printEven();


        // Even
        ds.print(..);
     }
     // Odd
        ds.print(...);

}

// end of main class

import java.util.Arrays;
import java.util.function.Function;

class DataStructure
{
    // Create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    int getSize()
    {
        return SIZE;
    }

    int[] getInts()
    {
        return arrayOfInts;
    }

    DataStructure()
    {
        // fill the array with ascending integer values
        for (int i = 0; i < SIZE; i++)
        {
            arrayOfInts[i] = i;
        }
    }

    private DataStructureIterator getIterator(IteratorStrategy strategy)
    {
        return strategy.getIterator(this);
    }

    void print(IteratorStrategy strategy)
    {
        DataStructureIterator iterator = getIterator(strategy);
        while (iterator.hasNext())
        {
            System.out.print(iterator.next() + " ");
        }
    }


    void printEven()
    {
        // Print out values of even indices of the array
        DataStructureIterator iterator = 
                                       getIterator(newEvenIteratorStrategy());
        while (iterator.hasNext())
        {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }


    //this method is still missing - we have to use the given parameters

    void print(java.util.function.Function <Integer,Boolean> iterator)
    {

    }

}

The expected output is for isEven 0 2 4 6 8 10 12 14 and for isOdd 1 3 5 7 9 11 13

The rest of the given class shouldn't be important atleast i think so. I am searching for solutions or just a hint for hours but I dont find anything useful. Your help is much appreciated and sorry for my english If you need anything let me know. Btw I am new to stackoverflow so I hope I did everything right

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
noob
  • 1
  • 1
  • You're missing some code (iterator stretegies), which means that this can't be compiled as shown. Also, a bit more clarity about what the missing "print" function is actually supposed to achieve. Is the above what has been provided to you or did you come this far through own effort. Finally, unfortunately this looks a bit (lot) like coursework, which isn't really appropriate for posting here. – Thomas Timbul May 13 '19 at 12:18

1 Answers1

0

Not sure that I understood what you really wanted but maybe you should have a look at this link: https://www.boraji.com/java-8-javautilfunctionfunction-example.

It explains how to use the Function interface and gives an example with even and odds number...

In your case, Function<Integer, boolean> means that you are going to receive an Integer as input and that you must return a boolean (I suppose true if even and false if odd).

Hope it helps! ;)

Wallarou
  • 63
  • 1
  • 7