Question: Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the users choice of the following functions the program
Here is the assignment:
Fibonacci or Prime number iterator: Design a menu driven program that performs the users choice of the following functions the program exits should also be a users choice.
Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(user input) creates an iterator that iterates Fibonacci numbers less than or equal to user input. Your program then uses this iterator to display all Fibonacci numbers less than or equal to user input in Descending order.
Menu Item 2: Prime number iterator here you are to define an iterator class named PrimeIterator for iterating prime numbers. The constructor takes an argument that specifies the limit of the maximum prime number. For example, prompt the user for size, use the size to call PrimeIterator(user input) creates an iterator that iterates prime numbers less than or equal to user input. Your program then uses this iterator to display all prime numbers less than or equal to user input in Descending order.
Menu Item 3: Exit command, this should present a report of the operations requested since the program was invoked, the total time required for each, the program start and end time, I.E.: 2 Fibonacci commands yielding 713 individual outputs requiring 5.7 seconds. 1 Prime command yielding 27 individual outputs requiring 0.3 seconds. Program started at 21:33:00 and terminated at: 21:34:12.
My issues are the following:
I wrote the code for the Fibonicci part in ascending order, but it needs to be descending and I am not sure how to do that (at least using an iterator)
The second issue is the last part, which is keeping track of the how many times outputs and time active for each case (see Menu 3 of assignment above).
So here is my code:
import java.util.Scanner; import java.util.Iterator;
public class Conversions1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int choice = 0; while(choice != 3) { System.out.println("Enter 1 or 2 to print out Fibonacci or Prime number iterator or enter 3 to exit."); System.out.println("1 - Fibonacci Number Iterator"); System.out.println("2 - Prime Number Iterator"); System.out.println("3 - Exit"); choice = scanner.nextInt();
switch (choice) { case 1: System.out.println("Enter the Max value"); int maxInt2 = scanner.nextInt(); Iterator iterator = new FibonacciIterator(maxInt2); while (iterator.hasNext()) { System.out.println(iterator.next()); } break; case 2: System.out.println("Enter the Max value"); int maxInt = scanner.nextInt(); Iterator iterator2 = new PrimeIterator(maxInt); while (iterator2.hasNext()) { System.out.println(iterator2.next()); } System.out.println(" "); break; case 3: Scanner input = new Scanner(System.in); System.out.println("Program Ended"); System.out.println(" "); break; default: System.out.println("Invalid Input"); } } } static class PrimeIterator implements java.util.Iterator { private int limit = 0; private int current; private int a_number; public PrimeIterator(int current) { this.current = current; } @Override public Integer next() { return current; } static boolean isPrime(int number) { for (int divisor = 2; divisor < number; divisor++) if (number % divisor == 0) return false; return true; } @Override public boolean hasNext() { current--; while (true) { if (isPrime(current)) break; current--; } if (current <= limit) return false; else return true; } } static class FibonacciIterator implements java.util.Iterator { private int limit; private int current = 1;//-1,1,0,1,1,2,3,5 private int prev=-1; public FibonacciIterator(int limit) { this.limit = limit; } @Override public Integer next() { return current; } @Override public boolean hasNext() { int temp=current; current=current+prev;//-1+1=0 prev=temp; if (current >= limit) return false; else return true; } @Override public void remove() { throw new UnsupportedOperationException ("Method not supported"); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
