I NEED A URLEXCEPTION CLASS AND FASTFIBSEQUENCE CLASS. I DONT KNOW HOW TO DO IT.
ID: 3821824 • Letter: I
Question
I NEED A URLEXCEPTION CLASS AND FASTFIBSEQUENCE CLASS. I DONT KNOW HOW TO DO IT. JAVA
Each FibSequence class will implement its own next() method and any other methods/instance variables required to create the Fibonacci numbers.
Fastfibsequence will just be faster than recursive.
one or more classes to handle user-defined exceptions
Fibtester:
package fibtester;
// Imported to open files
import java.io.File;
// Imported to use the exception when files are not found
import java.io.FileNotFoundException;
// Imported to write to a file
import java.io.PrintWriter;
// Imported to use the functionality of Array List
import java.util.ArrayList;
// Imported to use the exceptions for integer values
import java.util.NoSuchElementException;
// Imported to use when the array is out of bounds
import java.lang.NullPointerException;
// Imported to take input from the user
import java.util.Scanner;
public class FibTester
{
/**
* @param args will contain the name of the input and output file
*/
public static void main(String[] args)
{
int fibNumber = 0;
double initialTime = 0.0;
double finalTime = 0.0;
double iterativeTime = 0.0;
double recursiveTime = 0.0;
/*
Our table will have a fixed sized of 5 columns but the number of rows
will vary.
*/
final int COLUMNS = 5;
/*
The time will be stored in nanoseconds but the output will be displayed
in milliseconds. To convert, we need the following constant.
*/
final int MILLISECOND = 1000000;
ArrayList<Long> saveFibNumber = new ArrayList<Long>();
try
{
File inputFile = new File (args[0]);
/*
The delimeter will be used to ignore everything that is not a number
*/
Scanner inputStream = new Scanner(inputFile).useDelimiter("[^0-9]+" + "^\.*");
PrintWriter outputFile = new PrintWriter(args[1]);
try
{
fibNumber = inputStream.nextInt();
if (fibNumber >= 0)
{
LoopFib iterativeTest = new LoopFib();
RecursiveFib recursiveTest = new RecursiveFib();
//Start the Timer for Iterative Fib
initialTime = System.nanoTime();
for (int start = 0; start < fibNumber; start++)
{
saveFibNumber.add(iterativeTest.fib(start));
}
// Stop Timer
finalTime = System.nanoTime();
iterativeTime = (finalTime - initialTime) / MILLISECOND;
// Print to outputFile
outputFile.println("Expected Values. Time of execution: " + iterativeTime
+ " milliseconds ");
displayTable(COLUMNS, saveFibNumber, outputFile);
outputFile.println(" ");
// Start the Timer for Recursive Fib
initialTime = System.nanoTime();
for (int start = 0; start < fibNumber; start++)
{
saveFibNumber.set(start, recursiveTest.fib(start));
}
// Stop the Timer
finalTime = System.nanoTime();
recursiveTime = (finalTime - initialTime) / MILLISECOND;
outputFile.println("‘Fast’ recursively-computed Fibonacci numbers. "
+ " Time of execution: " + recursiveTime + " milliseconds ");
displayTable(COLUMNS, saveFibNumber, outputFile);
}
else
{
System.out.println("The input file contains a negative number. "
+ "Change format to a positive integer.");
}
}
catch (NoSuchElementException exceptionInteger)
{
System.out.println("Make sure the file ONLY contains an Integer");
}
finally
{
outputFile.close();
inputStream.close();
}
}
catch (FileNotFoundException exceptionFile)
{
System.out.println("File not found. Please try again.");
}
catch (NullPointerException exceptionCommandLine)
{
System.out.println("Provide both input and output file names. Please "
+ "try again");
}
}
/**
* This method will display the Fibonacci number to the output file in a
* formated table
* @param COLUMNS How many columns the table will have
* @param saveFibNumber The array containing the Fibonacci number that will
* be printed
* @param out The output file that will be used to print the Fibonacci numbers
*/
public static void displayTable(int COLUMNS, ArrayList<Long> saveFibNumber, PrintWriter out)
{
for (int currentElement = 0; currentElement < saveFibNumber.size(); currentElement++)
{
out.printf("%9d" + " |", saveFibNumber.get(currentElement));
if ((currentElement + 1) % COLUMNS == 0)
{
out.printf(" ");
}
}
}
}
Sequence:
package fibtester;
/**
*
* @author Sal
*/
public interface Sequence {
int next();
}
Recursivefib:
package fibtester;
import java.util.ArrayList;
/**
*
* @author Sal
*/
public class RecursiveFib implements Sequence
{
private ArrayList<Long> storingFibNumber = new ArrayList<Long>();
/*
To calculate the fibonacci number, you need the previous 2 fibonacci numbers
*/
private final int SECONDFIB = 2;
/**
* Method that will calculate the Fibonacci number
* @param fibNumber Which Fibonacci number we want to calculate
* @return the current Fibonacci number
*/
public long fib(int fibNumber)
{
if(storingFibNumber.size() < SECONDFIB)
{
storingFibNumber.add((long)1);
return 1;
}
if (storingFibNumber.size() < fibNumber)
{
return (fib(fibNumber - 1) + fib(fibNumber - SECONDFIB));
}
if (storingFibNumber.size() == fibNumber)
{
storingFibNumber.add(storingFibNumber.get(fibNumber - 1) + storingFibNumber.get(fibNumber - SECONDFIB));
return storingFibNumber.get(fibNumber - 1) + storingFibNumber.get(fibNumber - SECONDFIB);
}
else
{
return storingFibNumber.get(fibNumber - 1) + storingFibNumber.get(fibNumber - SECONDFIB);
}
}
}
Loopfib:
package fibtester;
/**
*
* @author Sal
*/
public class LoopFib implements Sequence
{
public long fib (int fibNumber)
{
if (fibNumber < 2)
{
return 1;
}
else
{
long olderValue = 1;
long oldValue = 1;
long newValue = 1;
for (int fibIteration = 2; fibIteration <= fibNumber; fibIteration++)
{
newValue = oldValue + olderValue;
olderValue = oldValue;
oldValue = newValue;
}
return newValue;
}
}
}
FastFibSequence
package fibtester;
/**
*
* @author Sal
*/
public class FastFibSequence implements Sequence {
}
URLException Class:
-Empty-
Explanation / Answer
class fibonacciSequence
{
static int fib(int n)
{
int F[][] = new int[][]{{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n-1);
return F[0][0];
}
static void multiply(int F[][], int M[][])
{
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
static void power(int F[][], int n)
{
if( n == 0 || n == 1)
return;
int Matrix[][] = new int[][]{{1,1},{1,0}};
power(F, n/2);
multiply(F, F);
if (n%2 != 0)
multiply(F, Matrix);
}
public static void main (String args[])
{
// print nth fibonacci number
System.out.println(fib(n));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.