Question

Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

Here is the assignment:

Fibonacci or Prime number iterator:
Design a menu driven program that performs the user’s choice of the following functions the program
exits should also be a user’s 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("\n");  
        break;
      
        case 3:
        Scanner input = new Scanner(System.in);
  
        System.out.println("Program Ended");
        System.out.println("\n");
        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");
    }
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi Buddy,

Complete Program:

import java.util.Scanner;

import java.util.Iterator;

import java.text.SimpleDateFormat;

import java.util.*;

import java.text.DateFormat;

public class Conversion1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int choice = 0;

int prime = 0, p = 0;

int fib = 0, f = 0;

long pTime = 0;

long fTime = 0;

long pETime = 0;

long fETime = 0;

int ff[];

long start = System.currentTimeMillis();

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:

fTime += System.currentTimeMillis();

System.out.println("Enter the Max value");

int maxInt2 = scanner.nextInt();

ff = new int[maxInt2];

Iterator iterator = new FibonacciIterator(maxInt2);

while (iterator.hasNext()) {

ff[f] = (int) iterator.next();

f++;

}

for (int i = 0; i < f; i++)

System.out.println(ff[i]);

fETime += System.currentTimeMillis();

fib++;

break;

case 2:

pTime += System.currentTimeMillis();

System.out.println("Enter the Max value");

int maxInt = scanner.nextInt();

Iterator iterator2 = new PrimeIterator(maxInt);

while (iterator2.hasNext()) {

System.out.println(iterator2.next());

p++;

}

System.out.println("\n");

pETime += System.currentTimeMillis();

prime++;

break;

case 3:

long pFinal = pETime - pTime;

long fFinal = fETime - fTime;

double fseconds = ((fFinal / 1000) % 60);

double pseconds = ((pFinal / 1000) % 60);

System.out.println("\n");

System.out.println(fib + " Fibonacci commands yielding " + f + " individual outputs requiring "

+ fseconds + " seconds.");

System.out.println("\n");

System.out.println(prime + " Prime commands yielding " + p + " individual outputs requiring " + pseconds

+ " seconds.");

System.out.println("\n");

long end = System.currentTimeMillis();

DateFormat df = new SimpleDateFormat("HH':'mm':'ss");

System.out.println("Program started at " + df.format(new Date(start)) + " and terminated at: "

+ df.format(new Date(end)));

System.out.println("\n");

System.out.println("Program Ended");

System.out.println("\n");

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");

}

}

}

Screenshots:

ranesh@raneshk:/Music/Java_prgs/conversions javac Conversion1.java ramesh@raneshk: /Music/Java_prgs/conversionŞ java Conversi

Enter the Max value 90 89 83 79 73 71 67 61 59 53 47 43 41 37 31 29 23 19 17 13 7 5 2 Enter 1 or 2 to print out Fibonacci or

Enter the Max value 27 1 2 5 8 13 21 Enter 1 or 2 to print out Fibonacci or Prine number iterator or enter 3 to exit. 1-Fibon

Add a comment
Know the answer?
Add Answer to:
Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT