A class SeriesOne is desgined to calculate the sum of the following series:
Sum = x2/1! + x4/3! + x6/5! + xn/(n-1)!
Some of the members of the class are given below:
class name : SeriesOne
Data members/ instance variables :
x : to store an integer number
n : to store number of terms
sum : double variable to store the sum of the series
Member functions:
SeriesOne(int xx, int nn) : constructor to assign x=xx and n=nn
double findfact(int m) : to return the factorial of m .
double findpower(int x, int y) : to return x raised to the power of y
void calculate() : to calculate the sum of the series
void display() : to display the sum of the series.
Specify the class SeriesOne, giving the details of the above member data and methods only.
Define the main() function to create an object and call the member function accordingly to enable the task.
import java.util.*;
class SeriesOne
{
int x,n,sum;
SeriesOne(int xx, int nn)
{
x=xx;
n=nn;
}
double findfact(int m)
{
int fact=1;
for(int i=1;i <=m; i++)
fact=fact*i;
return fact;
}
double findpower(int x, int y)
{
double res;
res= Math.pow(x,y);
return res;
}
void cal()
{
double term=0;
for(int i=2; i <=n; i=i+2)
{
term= findpower(x,i) / findfact( i-1);
sum+=term;
}
}
void display()
{
System.out.println("Sum of the series:"+sum);
}
public static void main(String[] args){
SeriesOne so=new SeriesOne();
so.cal();
so.display();
}
}
Enter the value of x:
3
Enter the value of n:
4
Sum of the series:22