Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class with an internal iterator that can be used to print out the Fibona

ID: 3640733 • Letter: W

Question

Write a class with an internal iterator that can be used to print out the Fibonacci sequence, starting from Fibonacci number 0, which is 0 and Fibonacci number 1, which is 1, then each next number being the sum of the previous two (#2 is 1, #3 is 2, #4 is 3, #5 is 5, etc.). The constructor for your class should have an int parameter that specifies the maximum Fibonacci number. In your inner class, the remove() method should just throw an UnsupportedOperationException.

Test your class with a driver program that will prompt for and input an integer, instantiate a Fibonacci object with that integer as the maximum Fibonacci number, and print out all of the Fibonacci numbers up to that number, using an enhanced for loop.

The code must be completed how i asked to get full points. The code must include a comment for each method describing the method, a comment for each parameter describing what the parameter represents, and a comment for each variable declaration (including instance variables) describing what the variable represents.

Explanation / Answer

Implemented in java: import java.util.Iterator; import java.util.Scanner; public class Test { public static void main(String[] args) { //Read in user input Scanner sc = new Scanner(System.in); //ask for max and store it System.out.print("Enter max fibonacci number: "); int max = sc.nextInt(); //our fibonacci object Fibonacci fib = new Fibonacci(max); //print all the numbers using an enhanced for loop based on Iterable interface for(int num : fib){ System.out.print(num + " "); } } } //Class implementes Iterable which defines iterator method class Fibonacci implements Iterable{ //maximum value private int max; //previous fib number private int previous = 0; //current fib number private int current = 1; //constructor as described public Fibonacci(int n){ max = n; } //our iterator public Iterator iterator() { return new FibIterator(); } //internal iterator class private class FibIterator implements Iterator{ //ow many numbers we have returned int count = 0; //Method to check whether we can return next number @Override public boolean hasNext() { //iterate up to max return count
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote