A class CalcSeries has been defined to calculate the sum of the series:
sum = 1 + x + x2 + x3 + … + xn
Some of the members of the class are given below:
Class name: CalcSeries
Data members/instance variables:
x: integer to store the value of x
n: integer to store the value of n
sum: integer to store the sum of the series
Member functions/methods:
CalcSeries(): default constructor
void input(): to accept the values of x and n
int power(int p, int q): return the power of p raised to q (pq) using recursive technique
void cal(): calculates the sum of the series by invoking the method power() and displays the result with an appropriate message
Specify the class CalcSeries, giving details of the constructor, void input(), int power(int, int) and void cal(). Define the main() function to create an object and call the member functions accordingly to enable the task.
import java.util.Scanner;
class CalcSeries{
int x;
int n;
int sum;
public CalcSeries(){
x = 0;
n = 0;
sum = 0;
}
public void input(){
Scanner in = new Scanner(System.in);
System.out.print("x = ");
x = Integer.parseInt(in.nextLine());
System.out.print("n = ");
n = Integer.parseInt(in.nextLine());
}
public int power(int p, int q){
if(q == 0)
return 1;
if(q == 1)
return p;
return p * power(p, q - 1);
}
public void cal(){
for(int i = 0; i <= n; i++)
sum += power(x, i);
System.out.println("Sum = " + sum);
}
public static void main(String[] args){
CalcSeries obj = new CalcSeries();
obj.input();
obj.cal();
}
}
PLEASE UPLOAD THE ALGORITHMS ALSO FOR ISC PROGRAMS
OK