Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Fibonacci (70%) 1. (5%) Create a folder named iterators under the Activity2 folder. 2. (5%) Create a class named IterableFibonacci that represents a

Part 1: Fibonacci (70%) 1. (5%) Create a folder named iterators under the Activity2 folder. 2. (5%) Create a class named IterableFibonacci that represents a Fibonacci sequence. 3. (5%) Add a constructor that declares two parameters of type long for the initial two numbers in the Fibonacci sequence. 4. (20%) Add the following methods: a. add() adds the next Fibonacci number to the sequence. b. toString() returns a nicely formatted string of its contents. For example: IterableFibonnaci fib = new IterableFibonnaci(2, 5); fib.add(); fib.add(); System.out.println(fib); // [2, 5, 7, 12] Its recommended to utilize the toString method of a data structure. c. get(int index) returns the Fibonacci number at the index. Using the above example: get(0) returns 2, get(1) returns 5, and so on. d. length() returns the length of the Fibonacci sequence. 5. (10%) Your Fibonacci class must be iterable to work with a for-each loop. a. IterableFibonacci should implement java.util.Iterable. b. Create a class named FibonacciIterator class that implements java.util.Iterator and use it in your Fibonacci class. 6. (10%) You are expected to declare all the required fields with their respective appropriate types. 7. (15%) Explain the complexity of your operations (add, get, length) knowing that you will use a linked list to keep each Fibonacci value.

Part 2: File Reader (30%) 1. (5%) Create a class named IterableReader.Your reader should have the capability to read text files similar to the manner in which java.io.BufferedReader does, and should support the for-each loop, allowing it to iterate over each line in the file. 2. (5%) Add a constructor that declares a parameter for a file name. In the event that an IOException occurs, the constructor may throw it again. 3. (10%) The IterableReader class should not only be iterable but also an iterator itself. Additionally, the class should be AutoCloseable in order to work with the try-with-resource feature. To be clear, IterableReader implements the following interfaces: a. Iterable b. Iterator c. AutoCloseable Therefore, the following methods must be implemented. - next() reads a line of text and returns it. The method returns null if the end of the stream has been reached or an IOException occurred. - hasNext() returns true if the iterator has more elements, false otherwise. - iterator() returns the iterator for the current instance of IterableReader. - close() closes this stream. 4. (10%) Unit testing for Part 2 of this assignment is optional and can be omitted. Instead, create a main method where you manually test all the methods you wrote. For example, public static void main(String[] args) throws IOException { try (IterableReader reader = new IterableReader("data/simple.txt");) { while (reader.hasNext()) { System.out.println(reader.next()); } } try (IterableReader reader = new IterableReader("data/simple.txt");) { for (String line : reader) { System.out.println(line); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions