CS1083/Submissions/As7/IsaacShoebottom_As7_Report/TestSeq.java

59 lines
1.6 KiB
Java
Raw Normal View History

2022-10-07 00:44:12 -03:00
import java.text.NumberFormat;
import java.util.Scanner;
/**
A simple driver that uses the Seq class to compute the
nth element of the sequence.
@author Isaac Shoebottom (3429069)
*/
public class TestSeq{
public static void main(String[] args){
int n, seqRec, seqMem, seqIter;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
n = scan.nextInt();
seqRec = Seq.seqR(n);
seqMem = Seq.seqM(n);
seqIter = Seq.seqI(n);
System.out.println("seqR(" + n + ") is: " + seqRec);
System.out.println("seqM(" + n + ") is: " + seqMem);
System.out.println("seqI(" + n + ") is: " + seqIter);
NumberFormat form = NumberFormat.getInstance();
form.setMaximumFractionDigits(7);
form.setMinimumFractionDigits(7);
System.out.println();
System.out.println("Execution Times in Milliseconds (ms)");
System.out.println("Seq(n) \tRecursive \tMemoization \tItertive");
long start, end;
int seqA;
double time;
for(int i = 20; i <= 40; i+=10){
start = System.nanoTime();
seqA = Seq.seqR(i);
end = System.nanoTime();
time = (double)(end-start)/1000000;
System.out.print(i + "\t" + form.format(time));
start = System.nanoTime();
seqA = Seq.seqM(i);
end = System.nanoTime();
time = (double)(end-start)/1000000;
System.out.print(i + "\t" + form.format(time));
start = System.nanoTime();
seqA = Seq.seqI(i);
end = System.nanoTime();
time = (double)(end-start)/1000000;
System.out.print(i + "\t" + form.format(time));
System.out.println();
}
}
}