Professional Documents
Culture Documents
Q6.
A class pattern is designed to print given pattern:
1357531
13531
131
1
class pattern2
{
int n, i, j, k, a;
}
}//end of main()
}//end of class
Q7.
A class pattern is designed to print given pattern:
*
* *
* *
* *
* *
* *
*
Some of the members of the class are given below:
class name : pattern
Member functions:
void display() : to display the given pattern.
Specify the class pattern, giving the details of the above member
method. No need to make main().
import java.io.*;
class pattern
{
public static void display()throws IOException
{
int n;
BufferedReader br=new BufferedReader(new
InputStreamReader (System.in));
System.out.println("Enter the value of n lines in the upper half: ");
n=Integer.parseInt(br.readLine());
int i, j, k;
//upper part of pattern
for(i=1;i<=n;i++)// rows of upper part
{
System.out.println();// to move to the new line
if(i!=1)
System.out.print('*'); // second * should be printed if it is not line no-1
}
for(i=n-1 ;i>=1; i--)// rows of lower part
{
System.out.println();// to move to the new line
if(i!=1)
System.out.print('*'); // second * should be printed if it is not line no-1
}
} //end of main()
} //end of class
Q8.
A class SeriesOne is designed to calculate the sum of the following
series: Sum=x2/1! + x4/3! + x6/5! + …….n terms. 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.
//series: Sum=x2/1! + x4/3! + x6/5! + ...... n terms
x : to store an integer number
n : to store number of terms
import java.io.*; sum : double variable to store the sum of the series
Member functions:
class SeriesOne SeriesOne(int xx, int nn) : constructor to assign x=xx and n=nn
{
//Data members
------------------------------------
import java.io.*;
class SeriesOne
{
//Data members
int x, n;
double sum;
}
//to return the factorial of m
double findfact(int m)
{
int f=1;
for(int i=1;i<=m;i++)
{ f=f*i;
}
return (double)f;
}
BufferedReader --------------------------
-----------------------------------------
}
}
public static void main()throws IOException
{
import java.io.*;
class Series
{
//Data members
int N; double s;
// constructor to initialize the data member
Series ( )
{
N=0; s=0.0;
}
void input() : to accept N from the user
void input()
{
BufferedReader --------------------------
-----------------------------------------
}
}
void input(): to accept N from the user
double sum(int m)
{
-------------------------------
-------------------------------
}
double sum(int m) : to return sum of the series 1+2+3+….+ m terms
double sum(int m)
{
int s1=0;
for(int i=1;i<=m;i++)
{ s1=s1+i;
}
return (double) s1;
}
double fact(int x) : to calculate and return the factorial of x
double fact(int x)
{
-------------------------------
-------------------------------
}
double fact(int x) : to calculate and return the factorial of x
double fact(int x)
{
int s1=1;
for(int i=1 ; i<=x ; i++)
{ s1=s1*i;
}
return (double)s1;
}
void display() : to display the sum of the series by calling the
methods of the class.
By calling int sum(int), double fact(int);
void display()
{
------------------------
}
void display() : to display the sum of the series by calling the
methods of the class.
Sum= 1/1! + (1+2)/2! + (1+2+3)/3! + …. N terms
void display()
{
for(int i=1;i<=N;i++)
{ s= s+ sum(i)/fact(i);
}
System.out.println(“Sum of the series: “+s);
}
main() function to create an object and call the member
function accordingly to enable the task.
------------------------
}
main() function to create an object and call the member
function accordingly to enable the task.