calculate value of PI from the infinite series
π = 4-4/3+4/5-4/7+4/9-4/11 +.....
write a class with two data members aproxivalue(double value ) and percision ( integer value ) where precion is the number of terms of the series used to compute the value of approxivalue :
1- default constructor permits to creats an object with one terms ..
2 the constructor with parameter creats an object with the specific numbers of terms
3- get methods to get the approximate value and the number of terms used ..
4- toString method...


i can not find the general form of this
π = 4-4/3+4/5-4/7+4/9-4/11 +.....

Re: calculate value of PI from the infinite series 80 80

nth term looks like 4/(2n-1) to me.

Re: calculate value of PI from the infinite series 80 80

if i try with two is correct but with positive value /??

i writed like this
if even
4/(1-2n)
else
4/(2n-1)

Re: calculate value of PI from the infinite series 80 80

i will try to code with this form
pow(-1, n+1)/(2*n-1);

thanks ,

Re: calculate value of PI from the infinite series 80 80

You can't omit the multiply sign in Java. 4/(2*n-1) should do it.
(replace 4 by 4.0 to force calc in floating point)

Re: calculate value of PI from the infinite series 80 80

1- default constructor permits to creats an object with one terms ..
2 the constructor with parameter creats an object with the specific numbers of terms

QUOTE]

public class api {
 private double approxipi;
 private int precision ;
 public api (){
 	approxipi=0.0;
 	precision=0;
 }
  //2 the constructor with parameter creats an object with the specific numbers of terms

  public api (api obj){
    	approxipi=obj.approxipi;
    }
}

is true..?? tell me what is error ro correct it
i starting with the constructore because i alwayse make mistake ..

Re: calculate value of PI from the infinite series 80 80

You cannot access obj.approxipi because it is "private" variable to the object. If you want, you need to either change it to public or implement a method that returns the variable value.

Re: calculate value of PI from the infinite series 80 80

do you mean set and get methods ?

Re: calculate value of PI from the infinite series 80 80

Yes.

Re: calculate value of PI from the infinite series 80 80

so both constructors are correct now ?

/**
 * @(#)api.java
 *
 *
 * @author 
 * @version 1.00 2011/10/21
 */


public class api {
 private double approxipi;
 private int precision ;
 public api (){
 	approxipi=0.0;
 	precision=0;
 }
 public void setApp(double a){
 	approxipi=a;
 }
 public double getApp(){
 	return approxipi;}
    public api (api obj){
    	approxipi=obj.getApp();
    }
}
Re: calculate value of PI from the infinite series 80 80

default constructor permits to creats an object with one terms ..

is this mean to make object in parameter of default constructors
sorry i didn't understand question because english is not me speaker language :=)

Re: calculate value of PI from the infinite series 80 80

get methods to get the approximate value and the number of terms used ..
how i can return number of terms
i can compute the value of approximate

Re: calculate value of PI from the infinite series 80 80

Don't worry, English is not my native tongue either.

π = 4-4/3+4/5-4/7+4/9-4/11 +.....
write a class with two data members aproxivalue(double value ) and percision ( integer value ) where precion is the number of terms of the series used to compute the value of approxivalue :
1- default constructor permits to creats an object with one terms ..
2 the constructor with parameter creats an object with the specific numbers of terms
3- get methods to get the approximate value and the number of terms used ..
4- toString method...

I just reread your requirement, you are not going to the right track at the moment...

1)The number of precision is the number of term used.
2)In your default constructor, you must initialize precision to 1 because of the requirement (one term).
3)Another constructor is taking an integer which is going to be the value of precision variable.
4)You need to compute the approximate value right when you are calling a method to get approximate value. In the method, you compute and accumulate the result in the approximate variable, and then return it. The number of term is the precision value.

Before you can go on computation, you need to understand how the series is being compute programmatically. It is going to be a for-loop and the number of loop is going to be equal to the number of precision (term).

Re: calculate value of PI from the infinite series 80 80

I just did not understand how to get the value of precision

i know how to caculate approximate value
but how i can return numbers of term ...

Re: calculate value of PI from the infinite series 80 80

Hmm... Are your constructors as below?

class api {
  private double approxipi;
  private int precision;
  public api() {
    approxipi = 0;
    precision = 1;  // satisfied the requirement #1
  }
  public api(int terms) {
    approxipi = 0;
    precision = terms>0 ? terms : 1;
    // The line above means if the value of incoming terms is greater than 0,
    // assign the value to the precision; otherwise, assign default value of 1.
  }
  ...
}

You can get the precision by creating a method which is similar to your getApp() method, but instead you return precision with "int" type.

PS: You do NOT need "set" methods, so remove the set App() method.

Re: calculate value of PI from the infinite series 80 80

Hmm... Are your constructors as below?

class api {
  private double approxipi;
  private int precision;
  public api() {
    approxipi = 0;
    precision = 1;  // satisfied the requirement #1
  }
  public api(int terms) {
    approxipi = 0;
    precision = terms>0 ? terms : 1;
    // The line above means if the value of incoming terms is greater than 0,
    // assign the value to the precision; otherwise, assign default value of 1.
  }
  ...
}

You can get the precision by creating a method which is similar to your getApp() method, but instead you return precision with "int" type.

PS: You do NOT need "set" methods, so remove the set App() method.

oh .. so what is term referd to ?
you didn't assignd value befor to varible term ..

Re: calculate value of PI from the infinite series 80 80

In the constructor, you are declaring what local variable name you are going to use inside the scope of constructor. In this case, your "requirement" said that "term" is the same value as "precision," so I explicitly use the word to clarify it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.