Question

In: Computer Science

java programing project Implement a program that computes the Fibonacci of a specified number, the factorial...

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

  • Valid input ranges
    • Fibonacci input range is: 0 to 40
    • Factorial input range is: 0 to largest Java Integer
    • Iterations range for the 'e' [n] parameter is: 1 to largest Java Integer
  • For computing the Fibonacci, use the sequence beginning with 1; 1, 1, 2, 3, 5, 8, ...
  • Place all of your code in a single .java file named Assign1.java.
  • You should use BigInteger for the factorial computation
  • You should use BigDecimal for the e computation: documentation

Solutions

Expert Solution

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


Related Solutions

( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
Design a program that asks the user for a number and the program computes the factorial...
Design a program that asks the user for a number and the program computes the factorial of that number and displays the result . Implement with two different modules - one that uses a for loop and one that uses a while loop Grading Rubrick Program Compiles Cleanly  syntax errors25 pts-5 per errorProgram runs without runtime errors ( validation)run-time errors 25 pts-5 per errorProgram give correct answersLogic errors30 pts-5 per errorProgram is repeatableNot repeatable5 pts-5 ptsProgram is well modularizedBarely Modularized10 pts-...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
Here is a program that computes Fibonacci series. Can you add comments to variables and functions...
Here is a program that computes Fibonacci series. Can you add comments to variables and functions of every functions? For example, what are these variables meaning? What "for" function do on this program? Describe important section of code to text file. For example, which section of this code important? Why? Thank you all of help. #include<iostream> using namespace std; int main() { int arr[100]; int n1, n2; cin>>n1; arr[0] = 0; arr[1] = 1; for(int i = 2;i<n1;i++){ arr[i] =...
programing language JAVA: Design and implement an application that reads a sentence from the user, then...
programing language JAVA: Design and implement an application that reads a sentence from the user, then counts all the vowels(a, e, i, o, u) in the entire sentence, and prints the number of vowels in the sentence. vowels may be upercase
In this project, you are required to write the java program “IO.java” to implement integer operations...
In this project, you are required to write the java program “IO.java” to implement integer operations “+”, “−”, “*”. Specifically, your program reads operands from a file named “input.txt” (which will be manually placed under the directory of the program) as strings. Then your program converts strings to integers, and computes the addition, subtraction, and multiplication of the operands. Finally, your program creates a file named “output.txt” (under the directory of the program) and writes the results to the file....
a program that takes an integer and outputs the resulting factorial number.
assembly x86 language program a program that takes an integer and outputs the resulting factorial number. 
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that:...
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that: -Loads chemical-element data by scanning through the file (accessed via file path or URL) exactly once. FILENAME: Elements.txt -Expects one or more command-line arguments that constitute a chemical formula, as described below. *A chemical formula, for the sake of this program, shall be defined as: One or more whitespace-delimited tokens Each token consists of either: An element symbol, implying one atom of that element...
C++ PROGRAMING Implement a program to evaluate simple mathematical expressions. Assume that the original expression is...
C++ PROGRAMING Implement a program to evaluate simple mathematical expressions. Assume that the original expression is provided to the program as a text string. Allowed expression tokens: brackets “(” and “)”, integers like “4”, “15”, addition “+”, subtraction “-”, multiplication “*”, division “/”. Output can be float. Trim spaces from an expression. Brackets should contain at least one operation. Make sure that an expression is validated before it is calculated; reject the invalid expressions with appropriate message. The program must...
There is a Java program that is missing one recursive function: public class Factorial { /*...
There is a Java program that is missing one recursive function: public class Factorial { /* / 1 when n is 0; * fact(n) = | * \ n*fact(n-1) otherwise */ public static int fact(int n) { return 0; } /* Factorial Test Framework * * Notice the odd expected value for fact(20). It is negative because * fact(20) should be 2432902008176640000, but the maximum int is only * 2147483647. What does Java do when integers run out of range?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT