QuestionQuestion

Introduction to Programming Loops
If you were to measure the circumference around any circle and the diameter across the same circle, and then divide the circumference by the diameter, you would find that your answer is the same for every circle you measure, about 3.14159265358979323. This is such an amazing result, that mathematicians have given this result a name, the Greek character π (Pi).

Assuming you’ve accurately measured a circle, you might also be amazed to find out that your division never ends since you can always achieve a better precision to the right of the decimal for this calculation. Furthermore, it has been proved that there is no repeating pattern for the digits to the right of the decimal. This type of number is referred to as an irrational number by mathematicians.
Various formulas exist for calculating the value of Pi. One approach is to calculate the value of the following series, the more terms used in the series, the better the precision of Pi that is calculated.
π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + …
Things to notice about this series:
- the numerator of each term is always the same (4)
- the value of the denominator in the terms consist of increasing odd numbers
- each subsequent term alternates between being added to or subtracted from the previously summed terms
For example, the value of Pi using the first three terms in this series is:
π = 4 – 4/3 + 4/5 = 3.46666
You will write a program based on the series described above that calculates the value of Pi 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).
Implementation Requirements
The program will be written in NetBeans.
- Create a new project named: Topic9project
- With a main class named: PiApproximator
The more terms that are used, the closer the approximation will become to Pi's actual value.

Within your program code, you must:
- Include complete program description and author comments with your full name (not yourusername) in the top of file comments
- Include comments above each defined method, including a method description and @param and@return tags for parameters and return values.
- Follow all other CS210 Coding Standards

This program will include two additional user-defined methods (besides the main method):
Method 1 will read input from the user. This method will:
- Have a descriptive method name
- Have two parameters: a String and a Scanner
o The String parameter will be the prompt for user input that this method will display (For example, a call might supply the String "Enter the number of terms to use").
o The Scanner parameter will be the Scanner object created in the main method.
- Within either a while or a do-while loop (you choose):
o Use the prompt String passed in to prompt the user for the input.
o Verify that the user input is positive and non-zero.
o 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 value that the user entered will be returned to the main method.
Method 2 will calculate and display the Pi approximate values. This method will:
- 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 Pi, and initialize it to be 0.
- Use a for loop to:
o Calculate each Pi approximation value in the series.
o 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.
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 of Pi (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 approximation of Pi).

o 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 results.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

/*
* PiApproximator class
* Description: Calculates pi using a specified number of terms, displaying results
*/
public class PiApproximator {
   
    /*
    * Method to present prompt string and collect user input
    * @param prompt String
    * @param scanner Scanner
    * @returns input int
    */
    public static int readInput(String prompt, Scanner scanner) {
       while (true) {
            System.out.println(prompt);
            int input = scanner.nextInt();
            if (input > 0)
                return input;
            else
                System.out.println("Error: Input must be positive and non-zero.");
       }
    }
   
    /*
    * Method to calculate pi and display results
    * @param numberOfTerms int
    * @param displayCount int
    */
    public static void calculatePi(int numberOfTerms, int...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

50% discount

Hours
Minutes
Seconds
$35.00 $17.50
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats