1

I have a class BoundedSkipper that implements Iterable<Integer>, which looks like public Iterator<Integer> iterator() {}

BoundedSkipper is supposed to take in two integers (int k, int n) where int k is supposed to be the number of integers greater than 0 that should be printed if the integer is NOT divisible or contains 3 in it.

For example:

for (int v: new BoundedSkipper(3, 11)) {
  System.out.println(v); 
}

the output would be 1, 2, 4, 5, 7, 8, 10, 11, 14, 16, 17 where k = 3 and n = 11,

since the total number of integers is n which is 11, and all the integers excluded, which are 3, 6, 9, 12, 13, 15 are all either divisible or contains k which is 3.

Here is my code so far:

public class BoundedSkipper implements Iterable<Integer> {
    int k;
    int n;

    public BoundedSkipper(int k, int n) {
        this.k = k;
        this.n = n;

    }
    int count = 0;

    @Override
    public Iterator<Integer> iterator() {
        while (count < k) {
            

        }
    }
}

The reason why I left this blank is I have absolutely no clue on how to return k numbers that satisfy the condition since there is no specific parameter in this. Any help WITH CODE EXPLANATION would be extremely useful. Thank you.

Nutnicha
  • 335
  • 1
  • 10

1 Answers1

0

when implementing the Iterable Interface you @Override the method itertor() that returns Iterator<Type> Type is Integer in your case, since Iterator<E> is Interface also to use its methods you should @Override them to use them as you pleased.

https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html

https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html

Can we write our own iterator in Java?

String.valueOf(int k) -> return the value of k as a String since you state that the numbers that being checked cannot contains k it was easier for me to do it with a String - you could take digit by digit and check that as well - it should work both ways.

since I needed k as String for the whole process while iterating I converted at first and use the String from that point.

The logic in the code are very straight forward - I wanted to show how to implements Iterable Interface - one of many ways.

    public static class BoundedSkipper implements Iterable<Integer> {
    int k;
    int n;

    public BoundedSkipper(int k, int n) {
        this.k = k;
        this.n = n;

    }

    @Override
    public Iterator<Integer> iterator() {
        Iterator<Integer> iterator = new Iterator<Integer>() {

            private int currentIndex = 1;
            private final String K_AS_A_STRING = String.valueOf(k);
            private int NUMBER_DISCOVERED = 0;

            @Override
            public boolean hasNext() {
                return NUMBER_DISCOVERED < n - 1;
            }

            @Override
            public Integer next() {
                if (currentIndex == 1) return currentIndex++;
                else {
                    if(!String.valueOf(currentIndex).contains(K_AS_A_STRING) &&
                            !(currentIndex % k == 0) ){
                        NUMBER_DISCOVERED++;
                        return currentIndex++;
                    }else {
                        currentIndex++;
                        return next();
                    }
                }
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
        return iterator;
    }
}
Eyal Hadad
  • 35
  • 5
  • can you please provide a little bit of explanation on what your code under public iterator does?? I want to understand the logic behind it. – Nutnicha Oct 19 '22 at 01:07
  • The code is not that hard to understand, anyway I took the conditions that you mentioned in your question and put it in the code, maybe the recursion got you a bit confused - since while iterating not all numbers are qualified - we will call the `next()` method once again but before will advanced the index that indicates which number is being checked 1 step ahead. I will edit the answer so it will be more clear – Eyal Hadad Oct 19 '22 at 05:20