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

Natural Logarithm of 2 Program Hello, I would really appreciate help on this loo

ID: 3757207 • Letter: N

Question

Natural Logarithm of 2 Program

Hello, I would really appreciate help on this loop program (in Java, please).

Program Concept

The natural logarithm of 2 is approximately: ln(2) = 0.693147180559945309417232121458… Various formulas exist for calculating the natural logarithm of 2. One approach is to calculate the value via a sum of fractions, using the following series. The more terms used in the series, the better the precision of the natural logarithm of 2 that is calculated. ln(2) = 1/1 – 1/2 + 1/3 – 1/4 + 1/5 – 1/6 + … As you might have noticed, calculating the value of this series only requires performing elementary arithmetic. Notice that the terms in the denominator consist of increasing integers and each subsequent term alternates between being added to or subtracted from the previously summed terms. For example, the value of natural logarithm of 2, using the first four terms in this series is: ln(2) = 1/1 – 1/2 + 1/3 – 1/4 = 0.58 (not very accurate with only 4 terms!)

You will write a program based on the series described above that calculates the value of natural logarithm of 2 for a user specified number of terms. This program will display the calculations every X times it does the calculations. X, known as the display count, will be specified by the user. For example, if the user inputs a display count of 10, then the program should display the results after the 10th, 20th, 30th, etc. term has been summed (see sample outputs below).

Within your program code, you must:

Include complete JavaDoc style documentation, including: A program description and author/version tags with your full name (not your username). Comments above each defined method, with a method description and @param and @return tags for parameters and return values. This program will include two additional user-defined methods (besides the main method): Method 1 will read and validate one input from the user. Method 2 will calculate and return the ln(2) approximate values, displaying as it goes.

Method 1 Requirements

Have a descriptive method name. Have two parameters: a String and a Scanner. Within a do-while loop: Use the prompt String passed in to prompt the user for the input. Verify that the user input is positive and non-zero. If it is not, issue an error message and then re-prompt the user to enter the number again, until a positive, non-zero value is entered. Once the user has entered valid input, the valid input value will be returned.

Method 2 Requirements

Have a descriptive method name. Have two parameters: the number of terms to calculate and the display count. Define a local variable to hold the approximate value of ln(2), and initialize it to be 0. Use a for loop to: Calculate each ln(2) approximation value in the series. As the values are calculated, display the calculations at the intervals specified by the display count. All values should be displayed to 9 decimal places, as shown in the sample runs. After the loop exits, return the approximation. HINTS: Use the loop count to decide whether to add or subtract, and whether to display. Modify the denominator each time you loop.

The main method will:

Define necessary variables, including a Scanner variable to read input. Tell the user what the program does before prompting for input. Call method 1 to read the number of terms in the series that will be used to approximate the value (note, the above two formula examples have 6 and 3 terms respectively). Call method 1 a second time to read the display count, representing how often the program will display the calculated value of the series (i.e. the current approximation). Note that calling the same method twice will work, because both user inputs should be positive and non-zero. The only difference between the two calls will be the prompt that is passed in as an argument for one of the method parameters. Display a blank line followed by a “RESULTS” header. Call method 2 to calculate and display the approximations. Display a blank line after method 2 returns. Display the final value of the approximation for the last calculation, also to 9 decimal places.

Input/Output Sample 1:

Program will approximate the natural log of 2

Enter the number of terms to use:

5

Display approximation after every how many steps?

1

RESULTS

At term 1: ln(2) = 1.000000000

At term 2: ln(2) = 0.500000000

At term 3: ln(2) = 0.833333333

At term 4: ln(2) = 0.583333333

At term 5: ln(2) = 0.783333333

Final ln(2) approximation at term 5 = 0.783333333

Input/Output Sample 2:

Program will approximate the natural log of 2

Enter the number of terms to use:

500

Display approximation after every how many steps?

0

Invalid input. Must be positive and non-zero.

Display approximation after every how many steps?

150 RESULTS

At term 150: ln(2) = 0.689824958

At term 300: ln(2) = 0.691483292

At term 450: ln(2) = 0.692037304

Final ln(2) approximation at term 500 = 0.692148181

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. If you are satisfied with the solution, please rate the answer. Thanks

// NaturalLog2.java

import java.util.Scanner;

public class NaturalLog2 {

      /**

      * method to prompt the user and get an integer input, return it after

      * validation

      *

      * @param prompt

      *            - prompt to be displayed

      * @param scanner

      *            - initialized Scanner object

      * @return a positive non zero value

      */

      static int getInput(String prompt, Scanner scanner) {

            int input = -1;

            // looping until a valid input is given

            do {

                  System.out.println(prompt);

                  input = scanner.nextInt();

                  if (input <= 0) {

                        System.out

                                    .println("Invalid input. Must be positive and non-zero.");

                  }

            } while (input <= 0);

            return input;

      }

      /**

      * method to find the natural logarithm of 2 to numTerms terms

      *

      * @param numTerms

      *            - number of terms

      * @param displayCount

      *            how often the approximation should be displayed

      * @return the final value

      */

      static double naturalLog2(int numTerms, int displayCount) {

            double log = 0;

            int count = 0;

            // looping for the specified number of terms

            for (int i = 1; i <= numTerms; i++) {

                  // finding the current term

                  double term = (double) 1 / i;

                  // if i is even, subtracting the term from log

                  // if i is odd, adding the term to the log

                  if (i % 2 == 0) {

                        log -= term;

                  } else {

                        log += term;

                  }

                  count++;

                  if (count == displayCount) {

                        // displaying the approximation

                        System.out.printf("At term %d: ln(2) = %.9f ", i, log);

                        count = 0;

                  }

            }

            return log;

      }

      public static void main(String[] args) {

            // initializing Scanner

            Scanner scanner = new Scanner(System.in);

            System.out.println("Program will approximate the natural log of 2");

            // getting inputs

            int numTerms = getInput("Enter the number of terms to use:", scanner);

            int displayCount = getInput(

                        "Display approximation after every how many steps?", scanner);

            // finding logn(2) and displaying it with a precision of 9 digits after

            // decimal point

            double log = naturalLog2(numTerms, displayCount);

            System.out.printf("Final ln(2) approximation at term %d = %.9f ",

                        numTerms, log);

      }

}

/*OUTPUT (multiple runs)*/

Program will approximate the natural log of 2

Enter the number of terms to use:

5

Display approximation after every how many steps?

1

At term 1: ln(2) = 1.000000000

At term 2: ln(2) = 0.500000000

At term 3: ln(2) = 0.833333333

At term 4: ln(2) = 0.583333333

At term 5: ln(2) = 0.783333333

Final ln(2) approximation at term 5 = 0.783333333

Program will approximate the natural log of 2

Enter the number of terms to use:

500

Display approximation after every how many steps?

0

Invalid input. Must be positive and non-zero.

Display approximation after every how many steps?

150

At term 150: ln(2) = 0.689824958

At term 300: ln(2) = 0.691483292

At term 450: ln(2) = 0.692037304

Final ln(2) approximation at term 500 = 0.692148181

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote