Question

Use java and make sure to output only last 20 values of the series. Thanks

2) 18 points) Calculate the value of π from the infinite series: 4 44, 44 3 5 7911 Print a table that shows the value of π approximated by computing the first 200,000 terms of this series. How many terms do you have to use before you first get a value that begins with 3.14159? Note: As the output include only last 20 values. Do not paste all 200K results.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

pr.java

/*
Calculating PI:

Calculate the value of π from the infinite series:  
π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + …
Use a command-line argument to decide how far into the series to go.
Make sure to use a separate class to perform the calculation.
Print a table that shows the value of π approximated by computing the first
    200,000 terms of this series.
*/
import java.util.Scanner;
import java.math.*;
public class pr {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
      
        int howFar;
      
        System.out.println("How far into the infinite series would you like to go?");
        Scanner sc = new Scanner(System.in);
        howFar = sc.nextInt();
        double PICalced;
        PICalced = Calculation.calculate(howFar);
        System.out.println("PI is " + PICalced + ".");
      
        PICalced = Calculation.calculate(howFar);
        System.out.println("\n\nWould you like to print a table of the first 200,000 terms? (Type 1 for yes, 2 for no)");
        Scanner sc2 = new Scanner(System.in);
        if(sc2.nextInt() == 1);
            makeTable();
    }
    /**
     *
     */
    public static void makeTable()
    {
        double PICalcedBig = Calculation.calculate(200000);
      
        for(int i = 0; i < 200000; i++)
        {
            System.out.println(Calculation.getAllNums().get(i));
        }
    }
  
}


Calculation.java


//π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + …

import java.math.*;
import java.util.*;

public class Calculation
{
    private static ArrayList<String> numByNum = new ArrayList<String>();;
    /**
     *
     * @param numPlaces
     * @return
     */
    public static double calculate(int numPlaces)
    {
        double PInum = 4.0;
        int denominator = 3;
        boolean operatorSwitch=true; //true is minus; false is plus
      
      
        for (int i = 0; i < numPlaces; i++)
        {
            //PT -- notice that both the if and else code are the same,
            //   other than the + or -. Could you get rid of the if?
            //   hint: (-1)^n is -1 if n is odd, and 1 if n is even
            if(operatorSwitch == true) // then subtract
            {
                PInum = PInum - (4.0/denominator);
                //System.out.println(PInum + " with " + i+1 + " additions.");
                numByNum.add("Number " + i + ": " + PInum);
                denominator += 2;
                operatorSwitch = false;
            }
            else // then add
            {
                PInum = PInum + (4.0/denominator);
                //System.out.println(PInum + " with " + i+1 + " additions. (+)");
                numByNum.add("Number " + i + ": " + PInum);
                denominator += 2;
                operatorSwitch = true;
            }
        }
        return PInum;
    }
    /**
     *
     * @return
     */
    public static ArrayList<String> getAllNums()
    {
        return numByNum;
    }
      
}


Add a comment
Know the answer?
Add Answer to:
Use java and make sure to output only last 20 values of the series. Thanks 2)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Calculating the Value of TT) Calculate the value of TT from the infinite series 3 57 91 Display a table that shows the...

    (Calculating the Value of TT) Calculate the value of TT from the infinite series 3 57 91 Display a table that shows the value of TT approximated by computing one term of this series, by two terms, by three terms, and so on. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159? (Calculating the Value of TT) Calculate the value of TT from the infinite series 3 57 91 Display...

  • 5. Use Newton's Binomial Theorem to write y- V1- 2 as an infinite series (write out at least four non-zero terms) a...

    5. Use Newton's Binomial Theorem to write y- V1- 2 as an infinite series (write out at least four non-zero terms) and apply Rule I of De analysi term-by-term to find the area under the curve y-V1-x2 in the first quadrant (again using at least four non-zero terms). Using only the first four non-zero terms in the series you found, estimate the value of T. (Remark: This is a very inefficient way to estimate π. Using 1000 terms of the...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • The program has to be written in C The expected output: Please make sure to use...

    The program has to be written in C The expected output: Please make sure to use two-dimensional arrays. OPTIONAL TAKE-HOME QUIZ WORTH 10 POINTS OUT OF A GRAND TOTAL OF 110 FOR THE COURSE A DC electrical circuit consists of an ideal battery with user-suppled terminal voltage VB which drives a resistive load comprised of a user-supplied number m of branches connected in parallel across each other, with each branch consisting of a variable user-supplied number n of resistors and...

  • -------------------------------------------------------------------- 1. How does Java support the concept of encapsulation? -------------------------------------------------------------------- 2. Describe the difference between...

    -------------------------------------------------------------------- 1. How does Java support the concept of encapsulation? -------------------------------------------------------------------- 2. Describe the difference between an object and a class. -------------------------------------------------------------------- 3. What is the difference between the contents of a Java variable of a primitive type and a Java variable of a class type? -------------------------------------------------------------------- 4. (a) How is a 'static' class method different from a regular (non-static) class method? (b) How is a 'static' variable in a class different from a regular (instance) variable in a class?...

  • ***C++ ONLY*** Menu driven program First load an array(lists) with the values 3, 4, 84, 5,...

    ***C++ ONLY*** Menu driven program First load an array(lists) with the values 3, 4, 84, 5, 2, 47, and 7 in this order Then sort the array(list) Create a program with a menu, with choices 1 for factorial (single value, which everyone will use 20!), 2 for median value , 3 for average, 4 for maximum, 5 for minimum, and 6 to end the program. Use a do loop (not while) Python does not have a Do loop, so instead...

  • ***C++ Program ONLY*** Menu driven program First load an array(lists) with the values 3, 4, 84,...

    ***C++ Program ONLY*** Menu driven program First load an array(lists) with the values 3, 4, 84, 5, 2, 47, and 7 in this order Then sort the array(list) Create a program with a menu, with choices 1 for factorial (single value, which everyone will use 20!), 2 for median value , 3 for average, 4 for maximum, 5 for minimum, and 6 to end the program. Use a do loop (not while) Python does not have a Do loop, so...

  • MATLAB ONLY!! PLEASE WRITE IN COMPUTER SO I CAN COPY PASTE!!! ANSWER COMPLETELY, USE FOR LOOPS....

    MATLAB ONLY!! PLEASE WRITE IN COMPUTER SO I CAN COPY PASTE!!! ANSWER COMPLETELY, USE FOR LOOPS. THE PROGRAM TO BE MODIFIED IS THE NEXT ONE: clc clear % Approximate the value of e ^ x using the Taylor Series x = input( 'Enter the value of x for e^x. x = ' ); t = input( 'Enter the amount of desired terms. t = ' ); i = 1; e_taylor = 0; % initializing the variable for the accumulator while...

  • Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script...

    Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script that evaluates this expression, ignoring all terms that are strictly smaller than .000001. Your script should display the number of terms summed and the sum. Use a while-loop. Think about the initialization of your variables and the order of computation carefully! In the loop body you need to calculate the value of a term only once. We use the above series for approximating (π^2)/8...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT