Please consider offering answers and suggestions to help other students!
And if you fix a problem by following a suggestion here,
it would be great if other interested students could see a short
"Great, fixed it!" followup message.
hello there, i am having some trouble with the reverseNext method in the StudentListIterator, in light of moving on to something else while i think about this I was wodnering if I could get some help understanding the logic of the method:
this is my error messages
StudentListIteratorTest
testIteration: ERROR: unpexpected java.util.NoSuchElementException
at studentstats.StudentListIterator.reverseNext(StudentListIterator.java:117)
at studentstats.StudentListIterator.reverseNext(StudentListIterator.java:15)
at test.studentstats.StudentListIteratorTest.testIteration(StudentListIteratorTest.java:66)
at test.studentstats.StudentListIteratorTest.run(StudentListIteratorTest.java:21)
at test.studentstats.Test.run(Test.java:11)
at test.Test.run(Test.java:14)
at test.Test.main(Test.java:5)
testUnitNewestStudents: ERROR: unpexpected java.util.NoSuchElementException
at studentstats.StudentListIterator.reverseNext(StudentListIterator.java:117)
at studentstats.StudentListIterator.reverseNext(StudentListIterator.java:15)
at itertools.Itertools$2.next(Itertools.java:68)
at itertools.Itertools$3.advance(Itertools.java:100)
at itertools.Itertools$3.<init>(Itertools.java:94)
at itertools.Itertools.filter(Itertools.java:90)
at studentstats.StudentStats.unitNewestStudents(StudentStats.java:76)
at test.studentstats.StudentStatsTest.testUnitNewestStudents(StudentStatsTest.java:58)
at test.studentstats.StudentStatsTest.run(StudentStatsTest.java:19)
at test.studentstats.Test.run(Test.java:12)
at test.Test.run(Test.java:14)
at test.Test.main(Test.java:5)
I take this to mean that there is a problem in the way that reverseNext() is referenced in other methods as well, but in general this is my interpretation of what the method should do:
[PROJECT DESCRIPTION OF TASK 8]
TASK(8): Implement StudentListIterator
See src/studentstats/StudentListIterator.java
Implement a `DoubleEndedIterator` over the list of student records pulled from the student API.
Since calls to `getPage()` may fail with a `QueryTimedOutException`, your implementation should retry the connection in case it was just a momentary failure. A retry quota is given when constructing the iterator. If the API is still not reachable after exceeding the retry quota, you should raise an `ApiUnreachableException`.
The iterator should not simply load the entire list and then iterate over it, as if we need to access only a prefix or suffix of the list, this would be extremely inefficient.
-----------------------------------------------------------------------------------------
MY LOGIC FOR THIS>>
1.We first check if we have reached the end of the entire student list. If currentPage is equal to the last page index (list.getNumPages() - 1) and currentIndex is equal to the last index of the current page data (currentPageData.length - 1), it means there are no more elements to iterate in the reverse direction.
2.In this case, we throw a NoSuchElementException {Q1: tests get super upset when I try to throw an exeption here? why is that?}
3.If currentIndex is 0, it means we have reached the beginning of the current page. In this case, we need to move to the previous page.
4.We decrement currentPage to move to the previous page.
5.We fetch the page data for the previous page using fetchPage(currentPage) and assign it to currentPageData.
6.We set currentIndex to currentPageData.
7.length - 1 to point to the last element of the previous page.
8.If currentIndex is not 0, it means we are still within the current page.
9.In this case, we simply decrement currentIndex to move to the previous element in the current page.
10.Finally, we return the student record at the current position (currentPageData[currentIndex]).
Just genuinely confused as to why it wouldn't want the exception... or maybe my chain of execution isn't correct? where would the exceptions need to go within the method?
Actually sorry to make my question more brief, I just would like to ask instead of returning exception or null pointer [both of these give error] what should we do if it's out of bound from the index?
Actually sorry to make my question more brief, I just would like to ask instead of returning exception or null pointer [both of these give error] what should we do if it's out of bound from the index?
Firstly, for future reference please avoid sharing what amounts to pseudocode with other students. The project is not just assessing your ability to write Java, but your ability to design the system using the features Java provides. In this case I am not concerned, as the pseudocode you have provided is not likely to give any other students an unfair advantage.
The task you have been asked to solve has no concept of an "index", so there is no specification as to what to do with an index out of bounds. If you are using an index as part of your implementation, how to handle it being out of bounds is part of your own design.
From the information you have provided, I think your design does not solve the task as stated. To be frank, it looks logically almost identical to the (very, very wrong) solution that ChatGPT generated when I tested it. I encourage you to read the task and documentation again carefully and make sure you understand it. Make sure you understand what a double ended iterator is: It is an iterator that can consume a sequence from both ends, not one that can move a cursor back and forth. Then I suggest you construct some test cases and attempt to do them by hand yourself, as though you are the StudentListIterator. The logic required is not difficult, but I have seen a lot of students struggling because they launch straight into the code without planning out their strategy, and then find that they have not considered all the possibilities. We have a small test for it, but consider how your StudentListIterator will behave if you call next() and reverseNext() alternatingly.