I can\'t implement this code..pls implement in eclipse ide and upload all files
ID: 3752398 • Letter: I
Question
I can't implement this code..pls implement in eclipse ide and upload all files in zip file by google drive and sent me link
--------------------------------------------------------------
import java.util.Scanner;
// Class Fibonacci definition
public class Fibonacci
{
// Static method to return number of fibonacci number less than or equals to n
public static int nbFibLeq(long n)
{
// Initializes the variables
int first = 1, second = 0, third = 0;
// Sets the counter to -1
int counter = -1;
// Loops till n
while(third <= n)
{
// Increase the loop counter by one
counter++;
// Adds the first and second and stores it in third
third = first + second;
// Assigns second value to first
first = second;
// Assigns third value to second
second = third;
}// End of while loop
// Returns the counter
return counter;
}// End of method
// main method definition
public static void main(String[] args)
{
// To store number
int no;
// Scanner class object created
Scanner sc = new Scanner(System.in);
// Accepts a number
System.out.print(" Enter a number to count number of fibonacci numbers: ");
no = sc.nextInt();
// Call the method and displays the count
System.out.println(" Number of fibonacci numbers less than or equals to "
+ no + " is: " +nbFibLeq(no));
}// End of main method
}// End of class
Sample output 1:
Enter a number to count number of fibonacci numbers: 1
Number of fibonacci numbers less than or equals to 1 is: 2
Sample output 2:
Enter a number to count number of fibonacci numbers: 10
Number of fibonacci numbers less than or equals to 10 is: 6
------------------------------------------------------------------------------------------------------------
package ClassProg;
// Defines a class MyArrayFactory
public class MyArrayFactory
{
// Creates and return an array of size n
public static <T extends Comparable<T>> MyArray<T> getMyArray(final int n)throws Exception
{
// Creates an anonymous class and overrides the interface methods
MyArray <T>obj = new MyArray<T>()
{
// Creates an template array of size n
T array[] = (T[]) new Integer[n];
// Return the element at position i
public T get(int i)
{
return array[i];
}// End of method
// Set the element at position i
public void set(int i, T e)
{
array[i] = e;
}// End of method
// Return the index of smallest element in the array (index of first occurrence returned)
public int min()
{
// Stores the zero index position value as minimum value
T min = array[0];
// Stores the zero index position as minimum index position
int index = 0;
// Loops till length of the array
for(int x = 1; x < array.length; x++)
{
// Checks if the current index position value is less than the earlier minimum
if(array[x].compareTo(min) < 0)
{
// Update the minimum to current index position value
min = array[x];
// Sets the current loop value as minimum index position
index = x;
}// End of if condition
}// End of for loop
// Returns the index
return index;
}// End of method
// Return the index of largest element in the array (index of first occurrence returned)
public int max()
{
// Stores the zero index position value as maximum value
T max = array[0];
// Stores the zero index position as maximum index position
int index = 0;
// Loops till length of the array
for(int x = 1; x < array.length; x++)
{
// Checks if the current index position value is greater than the earlier minimum
if(array[x].compareTo(max) > 0)
{
// Update the maximum to current index position value
max = array[x];
// Sets the current loop value as maximum index position
index = x;
}// End of if condition
}// End of for loop
// Returns the index
return index;
}// End of method
// Return the number of elements largest or equal e1 and smallest or equal e2
public int nbBetween(T e1, T e2)
{
// Initializes the counter to 0
int counter = 0;
// Loops till length of the array
for(int x = 0; x < array.length; x++)
// Checks the current index position of the array value with the range
if(array[x].compareTo(e1)>= 0 && array[x].compareTo(e2)<= 0)
// Increase the counter by one
counter++;
// Returns the counter
return counter;
}// End of method
};// End of anonymous class
// Returns the object
return obj;
}// End of method
// main method definition
public static void main(String[] args) throws Exception
{
// Creates an integer array
int arr[] = {12, 6, 88, 7, 1, 23, 54, 99};
// Calls the method to return MyArray object
MyArray m = getMyArray(arr.length);
// Loops till length of the array
for(int x = 0; x < arr.length; x++)
// Calls the method to assign data
m.set(x, arr[x]);
System.out.println("My array contains: ");
// Loops till length of the array
for(int x = 0; x < arr.length; x++)
// Calls the method to retrieve the data
System.out.print(m.get(x) + ", ");
// Calls the method to display minimum
System.out.println(" My array minimum value index positon: " + m.min());
// Calls the method to display maximum
System.out.println(" My array maximum value index positon: " + m.max());
// Calls the method to display count of numbers between range
System.out.println(" My array number of elements between 1 - 10: " + m.nbBetween(1, 10));
}// End of main method
}// End of class
Sample Output:
My array contains:
12, 6, 88, 7, 1, 23, 54, 99,
My array minimum value index positon: 4
My array maximum value index positon: 7
My array number of elements between 1 - 10: 3
Explanation / Answer
Create a class in eclipse Fibonacci.java for the first Fibonacci class of first program given int the top. The given program is correct run the program using the top icon menu in the eclipse play/Run the program will give the correct output:
Fibonacci.java:
package chegg;
import java.util.Scanner;
//Class Fibonacci definition
public class Fibonacci
{
//Static method to return number of fibonacci number less than or equals to n
public static int nbFibLeq(long n)
{
//Initializes the variables
int first = 1, second = 0, third = 0;
//Sets the counter to -1
int counter = -1;
//Loops till n
while (third <= n)
{
//Increase the loop counter by one
counter++;
//Adds the first and second and stores it in third
third = first + second;
//Assigns second value to first
first = second;
//Assigns third value to second
second = third;
} // End of while loop
//Returns the counter
return counter;
}// End of method
//main method definition
public static void main(String[] args)
{
//To store number
int no;
//Scanner class object created
Scanner sc = new Scanner(System.in);
//Accepts a number
System.out.print(" Enter a number to count number of fibonacci numbers: ");
no = sc.nextInt();
//Call the method and displays the count
System.out.println(" Number of fibonacci numbers less than or equals to "+ no + " is: " + nbFibLeq(no));
}// End of main method
}// End of class
Sample output 1:
Enter a number to count number of fibonacci numbers: 1
Number of fibonacci numbers less than or equals to 1 is: 2
Sample output 2:
Enter a number to count number of fibonacci numbers: 10
Number of fibonacci numbers less than or equals to 10 is: 6
The second program needs to create the interface to execute the program coerrectly. Below is the interface created. craete a interface MyArray save it MyArray.java.
MyArray.java:
public interface MyArray<T extends Comparable<T>> {
T get(int i);
void set(int i, T e);
int min();
int max();
int nbBetween(T e1, T e2);
}
craetae a class MyArrayFactory save it MyArrayFactory.java below is the class given by you is correct.
MyArrayFactory.java:
//Defines a class MyArrayFactory
public class MyArrayFactory
{
//Creates and return an array of size n
public static <T extends Comparable<T>> MyArray<T> getMyArray(final int n) throws Exception
{
//Creates an anonymous class and overrides the interface methods
MyArray<T> obj = new MyArray<T>()
{
//Creates an template array of size n
T array[] = (T[]) new Integer[n];
//Return the element at position i
public T get(int i)
{
return array[i];
}// End of method
//Set the element at position i
public void set(int i, T e)
{
array[i] = e;
}// End of method
//Return the index of smallest element in the array (index of first occurrence returned)
public int min()
{
//Stores the zero index position value as minimum value
T min = array[0];
//Stores the zero index position as minimum index position
int index = 0;
//Loops till length of the array
for (int x = 1; x < array.length; x++)
{
//Checks if the current index position value is less than the earlier minimum
if (array[x].compareTo(min) < 0)
{
//Update the minimum to current index position value
min = array[x];
//Sets the current loop value as minimum index position
index = x;
} // End of if condition
} // End of for loop
//Returns the index
return index;
}// End of method
//Return the index of largest element in the array (index of first occurrence returned)
public int max()
{
//Stores the zero index position value as maximum value
T max = array[0];
//Stores the zero index position as maximum index position
int index = 0;
//Loops till length of the array
for (int x = 1; x < array.length; x++)
{
//Checks if the current index position value is greater than the earlier minimum
if (array[x].compareTo(max) > 0)
{
//Update the maximum to current index position value
max = array[x];
//Sets the current loop value as maximum index position
index = x;
} // End of if condition
} // End of for loop
//Returns the index
return index;
}// End of method
//Return the number of elements largest or equal e1 and smallest or equal e2
public int nbBetween(T e1, T e2)
{
//Initializes the counter to 0
int counter = 0;
//Loops till length of the array
for (int x = 0; x < array.length; x++)
//Checks the current index position of the array value with the range
if (array[x].compareTo(e1) >= 0 && array[x].compareTo(e2) <= 0)
//Increase the counter by one
counter++;
//Returns the counter
return counter;
}// End of method
};// End of anonymous class
//Returns the object
return obj;
}// End of method
//main method definition
public static void main(String[] args) throws Exception
{
//Creates an integer array
int arr[] = { 12, 6, 88, 7, 1, 23, 54, 99 };
//Calls the method to return MyArray object
MyArray m = getMyArray(arr.length);
//Loops till length of the array
for (int x = 0; x < arr.length; x++)
//Calls the method to assign data
m.set(x, arr[x]);
System.out.println("My array contains: ");
//Loops till length of the array
for (int x = 0; x < arr.length; x++)
//Calls the method to retrieve the data
System.out.print(m.get(x) + ", ");
//Calls the method to display minimum
System.out.print(" My array minimum value index positon: " + m.min());
//Calls the method to display maximum
System.out.print(" My array maximum value index positon: " + m.max());
//Calls the method to display count of numbers between range
System.out.print(" My array number of elements between 1 - 10: " + m.nbBetween(1, 10));
}// End of main method
}// End of class
sample output:
My array contains:
12, 6, 88, 7, 1, 23, 54, 99,
My array minimum value index positon: 4
My array maximum value index positon: 7
My array number of elements between 1 - 10: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.