Home / Expert Answers / Computer Science / import-java-util-iterator-import-java-util-nosuchelementexception-problem-2-the-constructor-pa620

(Solved): import java.util.Iterator; import java.util.NoSuchElementException; // Problem 2 // The constructor ...



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

// Problem 2
// The constructor of Iterator EveryOther2 takes as input any
// iterator over elements of type E. The sequence it returns
// consists of every other element of iter, starting with
// the first element of iter. See EveryOther2Test for examples.

public class EveryOther2<E> implements Iterator<E> {
        private Iterator it;
        // you'll need to add instance variables

        public EveryOther2(Iterator<E> iter){
        }

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

        @Override
        public E next() {
                if (!hasNext()) throw new NoSuchElementException();
                
                return null;
        }
}

import org.junit.Test;

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

public class EveryOther2Test {
    @Test
    public void test1() {
        Integer[] input = {1,2,3,4,5,6};
        // get an iterator over the elements of the array
        Iterator<Integer> i = Arrays.asList(input).iterator();
        Integer[] expected = {1, 3, 5};
        TestUtilities.check(expected, new EveryOther2(i));
        // if you need to print the sequence, use this line instead, with true as a 3rd argument to check
        // TestUtilities.check(expected, new EveryOther2(i), true);
    }

    @Test
    public void test2() {
        Integer[] input = {5,7,9,11,13,15};
        Iterator<Integer> i = Arrays.asList(input).iterator();
        Integer[] expected = {5,9,13};
        TestUtilities.check(expected, new EveryOther2(i));
    }



    @Test
    public void test4() {
        Integer[] input = {2,3,4,5,6};
        Iterator<Integer> i = Arrays.asList(input).iterator();
        Integer[] expected = {2,4,6};
        TestUtilities.check(expected, new EveryOther2(i));
    }

    @Test
    public void test5() {
        String[] input = {"def", "ghi", "jkl", "mno", "pqr"};
        Iterator<String> i = Arrays.asList(input).iterator();
        String[] expected = {"def", "jkl", "pqr"};
        TestUtilities.check(expected, new EveryOther2(i));
    }

    @Test
    public void test6() {
        String[] input = {"a", "b", "c", "d", "e", "f"};
        Iterator<String> i = Arrays.asList(input).iterator();
        String[] expected = {"a", "c", "e"};
        TestUtilities.check(expected, new EveryOther2(i));
    }

    @Test(expected = NoSuchElementException.class)
    public void test7() {
        // this test intentionally asks the EveryOther Iterator for more
        // elements even after hasNext returns false
        String[] input = {"a", "b", "c", "d"};
        Iterator<String> i = Arrays.asList(input).iterator();
        String[] expected = {"a", "c"};
        TestUtilities.checkBeyond(expected, new EveryOther2(i));
    }
}


We have an Answer from Expert

View Expert Answer

Expert Answer


We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe