Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Fibtester: package fibtester; // Imported to open files import java.io.File; //

ID: 3821820 • Letter: F

Question

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

}

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
{
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);
}
}
}

Loop fib:

package fibtester;

/**
*
* @author Sal
*/
public class LoopFib
{   
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;
}
}
}

I NEED A URLEXCEPTION CLASS AND FASTFIBSEQUENCE CLASS. IDK HOW TO DO IT. JAVA

Explanation / Answer

Program is workin fine, there is no url exception class, But there is a main class (FibTester.java) which has main method in it , And it takes few file arguments , Actually two arguments

In case you are using command prompt ::

If you are using eclipse (This ones Easy)::

And Classes in project are :

Its First and the second you are looking for ..

Hope this helps ..I am copying these classes below :

/////////////////////////////////////////////////////////////////FibTester.java/////////////////////////////////////////////////////////////////////////////


//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
{

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

}

////////////////////////////////////////////////////////////////////////////////////////RecursiveFib.java/////////////////////////////////////////////////////////////////


import java.util.ArrayList;

/**
*
* @author Sal
*/
public class RecursiveFib
{
   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);
      }
   }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////