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");
}
}
}
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:
Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...
Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator<Integer> f //complete this You are required to create an inner class in the FibSequence class. The inner class should be called FibIterator. It implements the Iterator<Integer> interface and implements the hasNext() and the next() methods. The iterator iterates through the Fibonacci sequence. The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers....
java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> { LinkedList<T> list; public Stack() { list = new LinkedList<T>(); } public boolean isEmpty() { return list.isEmpty(); ...
PYTHON 3 PROGRAM PLEASE 18.19* (Prime number iterator) Write an iterator class for prime numbers. Invoking the __next__() method returns the next prime number. Write a test program that displays all prime numbers less than 10000.
How could I take this code and make a JavaFX program with the same results? import java.util.Scanner; public class CellPhoneApp { private static int[] data = {8,16,20}; private static double[] dataPrice = {45.00,65,.00,99.00}; private static int[] model ={100,110,200}; private static double[] modelPrice= {299.95,399.95,499.95}; private static String[] options = {"Phone Replacement Insurance","WiFi Hotspot Capability"}; private static double[] optionsPrice ={5.00,10.00}; private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int planIndex = getDataPlan(); int modelIndex = getModel(); int...
The part in bold is giving me an error. Ther error is that I cant convert an int to a boolean. The program asks the user to input two integer values and determines whether the first is divisible (without a remainder) by the second import java.util.Scanner; public class CheckDivisible { int isDivisible(int dividend, int divisor) { if(dividend % divisor == 0) return 1; else return 0; } public static void main(String[] args) { int dividend = 0; int divisor =...
Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) { char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in); System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...
Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...
In this assignment, you will write a subclass of the MemoryCalc class from Sixth Assignment – Memory Calculator. This new calculator, called ScientificMemCalc, should be able to do everything that the MemoryCalc could do, plus raise the current value to a power and compute the natural logarithm of the current value. This is a fairly realistic assignment – often when you are working for a company, you will be asked to make minor extensions to existing code. You will need...
FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...
The prime factorization of a number is the unique list of prime numbers that, when multiplied, gives the number. For example, the prime factorization of 60 is 2 ∗ 2 ∗ 3 ∗ 5. In this problem you must write code to recursively find and return the prime factorization of the given number. You must print these in ascending sorted order with spaces in between. For example, if your input is: 120 then you should print the following output: 2...