I was given the following code and asked to write the Solution class extending from TestList. I wrote a constructor for it (which just called super) and the printSecond2() method invoked in the last line of the code below. All other methods are inherited. Here's the code:
public class Test3A {
public static void main(String[] args) {
TestList tl = new Solution();
tl.loadList();
((Solution) (tl)).printSecond2();//prints every second element
}
}
However, the damn thing was never printing anything out, so I went into the TestList class (which was provided) and put println statements after every single line of the loadList () method:
public void loadList ()
{
if (input.hasNextInt ())//input is a Scanner object
{
int number = input.nextInt ();
loadList ();
add (number);
}
}
I discovered that I can continue to add whitespace, newlines and integers indefinitely, and that the add(number) method is only finally called when I input a character. So if I don't do that, it just sort of hangs around waiting for more input instead of moving on.
I'm confused by this as the provided sample input/output is:
sample input
1 2 3 4 5
sample output
2 4
So there's no character being inputted by the automatic marker.
I have tried overriding the method in Solution (we can't touch the other classes) and:
- ) changing if to while
- ) adding an else block
- ) adding an else if (!input.hasNextInt ())
None of these changed anything. I have no idea how the program is supposed to move on and get as far as calling printSecond2()
.
Any thoughts? I'd really like to pass my next prac test :D