Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 7 SOLVED

Friday, October 1, 2021

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 7 SOLVED

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 7

Programming for school students ( upper to middle/secondary school)

Design a class to overload a function series() as follows:

(a) void series (int x, int n) - To display the sum of the series given below:

x+x2+x3+........+xn

(b) void series (int p) - To display the following series:

0, 7, 26, 63, ........ upto p terms

(c) void series () - To display the sum of the series given below

&frac12 + 1⁄3 +&frac14 +.......+1⁄10


import java.util.Scanner;
public class ISCJava{
       public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       Series obj = new Series();
       int choice;
       System.out.println("Enter your choice(1/2/3):");
       choice = in.nextInt();
       switch(choice){
              case 1:
              int xn;
              System.out.println("Enter the value of x:");
              x = in.nextInt();
              System.out.println("Enter the value of n:");
              n = in.nextInt();
              obj.series(x,n);
              break;
              case 2:
              int p;
              System.out.println("Enter the value of p:");
              p = in.nextInt();
              obj.series(p);
              break;
              case 3:
              obj.series();
              break;
              default:
              System.out.println("Wrong Choice:");
       }
       in.close();
       }
}
class Series {
       public void series(int xint n) {
              for (int i = 1; i<= n ;i++)
                     sumOfSeries += Math.pow((double)x,(double)i);
              System.out.println("Sum of the series is:"+sumOfSeries);
       }
       public void series(int p) {
              System.out.println("The series is");
              for(int i = 1; i<= p; i++)
                     if (i != p)
                            System.out.print((int)Math.pow(i, 3)-1+",");
                     else
                            System.out.println((int)Math.pow(i, 3)-1);       
       }
       public void series() {
              for (int i = 2; i<= 10 ;i++)
                     sumOfSeries += (1.0/i);
              System.out.println("Sum of the series is:"+sumOfSeries);
       }
       Series() {
              this.sumOfSeries = 0.0;
       }
       private double sumOfSeries;
}
       
PYTHON CODE
Note: Function overloading is not available in Python.

class Series:
    def seriesOne(self,x,n):
        for i in range(n):
            i +=1
            self.__sumOfSeries += x**i
        print("Sum of the series is:",self.__sumOfSeries)
    def seriesTwo(self,p):
        for i in range(p):
            i +=1
            if i<p:
                print(i**3-1end =",")
            else:
                print(i**3-1end =" ")
    def seriesThree(self):
        for i in range(9):
            i +=2
            self.__sumOfSeries += (1.0/(i+1))
        print("Sum of the series is:",self.__sumOfSeries)
    def __init__(self):
       self.__sumOfSeries = 0
    __sumOfSeries = float(0)
# main program    
obj = Series()
try:
    N = int(input("Enter your choice(1/2/3):"))
    if N > 3:
        print("Your choice in invalid")   
    elif N==1:
        x = int(input("Enter the value of x:"))
        n = int(input("Enter the value of n:"))
        obj.seriesOne(x,n)
    elif N==2:
        p = int(input("Enter the value of p:"))
        obj.seriesTwo(p
    else:
        obj.seriesThree()    
except ValueError as e:
    print("Your choice is invalid")

No comments:

Post a Comment