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 +.....

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

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

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

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

thanks ,

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)

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

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.

do you mean set and get methods ?

Yes.

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();
    }
}

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 :=)

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

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).

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

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.

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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.