In: Computer Science
java programing project
Implement a program that computes the Fibonacci of a specified number, the factorial of a specified number, or estimates the value of 'e' using the specified number of iterations (of a Taylor series). Please feel free to use the Internet to find resources that explain how to estimate the value of 'e' using a Taylor series.
In the case of no or invalid number of parameters, the program should show help instructions that looks like:
--- Assign 1 Help --- -fib [N] : Compute the Fibonacci of N; valid range [0, 40] -fac [N] : Compute the factorial of N; valid range, [0, 2147483647] -e [N] : Compute the value of 'e' using N iterations; valid range [1, 2147483647]
Example command-line usages of the program look like:
java Assign1 -fib 6 java Assign1 -fac 6 java Assign1 -e 24 java Assign1 -fib 6 -fac 6 -e 24 java Assign1 -fib 6 -fib 10
Example output from correct and incorrect command-line arguments:
java Assign1 -fib 6 -fib 8 -fac 10 -fac 0 -e 10 -e 100 Fibonacci of 6 is 13 Fibonacci of 8 is 34 Factorial of 10 is 3628800 Factorial of 0 is 1 Value of e using 10 iterations is 2.7182815255731922 Value of e using 100 iterations is 2.7182818284590450
java Assign1 -fib 6 -fib 50 -fac 0 -e 10 -e 0 Fibonacci of 6 is 13 Fibonacci valid range is [0, 40] Factorial of 0 is 1 Value of e using 10 iterations is 2.7182815255731922 Valid e iterations range is [1, 2147483647]
java Assign1 -abc 123 Unknown command line argument: -abc
Your program should be able to handle any number of requested computations. Additionally, it should be able to handle the same computation request more than one time on a single command line; refer to the example command-line usages above.
Additional Notes
//Assign1.java
import java.math.BigInteger;
import java.math.RoundingMode;
import java.math.BigDecimal;
public class Assign1 {
public static void main(String[] args) {
if(args.length > 0) // check the number of command line arguments
{
// loop over the array of command line arguments
for(int i=0;i<args.length;i=i+2)
{
if(args[i].equalsIgnoreCase("-fib")) // fibonacci
{
int n = Integer.parseInt(args[i+1]);
int f1 = 1, f2 =1;
if(n < 0 || n > 40)
System.out.println("Fibonacci valid range is [0, 40]");
else if(n < 2)
System.out.println("Fibonacci of "+n+" is 1");
else
{
int f3=0;
for(int c=2;c<=n;c++)
{
f3 = f1+f2;
f1 = f2;
f2 = f3;
}
System.out.println("Fibonacci of "+n+" is "+f3);
}
}else if(args[i].equalsIgnoreCase("-fac")) // factorial
{
int n = Integer.parseInt(args[i+1]);
if( n < 0)
{
System.out.println("Factorial valid range is [0,"+Integer.MAX_VALUE+"]");
}else
{
BigInteger fact = new BigInteger("1");
for(int c=1;c<=n;c++)
{
fact = fact.multiply(BigInteger.valueOf(c));
}
System.out.println("Factorial of "+n+" is "+fact);
}
}else if(args[i].equals("-e")) // exp(0)
{
int n = Integer.parseInt(args[i+1]);
if(n <= 0)
{
System.out.println("Valid e iterations range is [1,"+Integer.MAX_VALUE+"]");
}else
{
BigDecimal e = new BigDecimal("1");
for(int c=1;c<=n;c++)
{
BigInteger fact = new BigInteger("1");
for(int k=1;k<=c;k++)
fact = fact.multiply(BigInteger.valueOf(k));
BigDecimal d = new BigDecimal(fact);
e = e.add(BigDecimal.valueOf(1).divide(d,16,RoundingMode.HALF_EVEN));
}
System.out.println("Value of e using "+n+" iterations is "+e);
}
}else
System.out.println("Unknown command line argument: "+args[i]);
}
}else
{
System.out.println("--- Assign 1 Help ---\r\n" +
" -fib [N] : Compute the Fibonacci of N; valid range [0, 40]\r\n" +
" -fac [N] : Compute the factorial of N; valid range, [0, 2147483647]\r\n" +
" -e [N] : Compute the value of 'e' using N iterations; valid range [1, 2147483647]");
}
}
}
//end of Assign1.java
Output:
java Assign1 -fib 6 -fib 8 -fac 10 -fac 0 -e 10 -e 100 -abc 123 -e 0