You are on page 1of 30

Classes & Objects in Java

Q6.
A class pattern is designed to print given pattern:
1357531
13531
131
1

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 pattern2
{

void display()throws IOException


{
BufferedReader br=new BufferedReader( new
InputStreamReader(System.in));

int n, i, j, k, a;

System.out.println("Enter total number of rows: ");


n=Integer.parseInt(br.readLine());
for(i=n;i>=1;i--)// for the rows
{
System.out.println();//to move to new line

for(j=i;j<n;j++)// for the blank spaces in the beginning


System.out.print(" ");

for(k=1;k<=i;k++)//for left hand side elements


System.out.print(k*2-1);

for(k=i-1;k>=1;k--)//for right hand side elements


System.out.print(k*2-1);

}
}//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

//loop for blank spaces present in the beginning of each row


for(j=1; j<=n-i; j++)
System.out.print(" ");

System.out.print("*");// to be printed at left hand side

//loop for blank spaces in the middle of each row


for(k=1; k<i*2-2;k++)
System.out.print(" ");

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

//loop for blank spaces present in the beginning of each row


for(j=1; j<=n-i; j++)
System.out.print(" ");

System.out.print("*");// to be printed at left hand side

//loop for blank spaces in the middle of each row


for(k=1; k<i*2-2;k++) System.out.print(" ");

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

//constructor to assign x=xx and n=nn


SeriesOne(int xx, int nn)
{
-----------------------------------
}
//series: Sum=x2/1! + x4/3! + x6/5! + ...... n terms

import java.io.*;
class SeriesOne
{
//Data members
int x, n;
double sum;

//constructor to assign x=xx and n=nn


SeriesOne(int xx, int nn)
{ x=xx; n=nn; sum=0.0;
}
//to return the factorial of m
double findfact(int m)
{
--------------------------------

//to return x raised to the power of y


double findpower(int x, int y)
{
-------------------------------------

}
//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;
}

//to return x raised to the power of y


double findpower(int x, int y)
{
return Math.pow(x,y);//sum=x+(i+2)/(i-1)
}
//to calculate the sum of the series x^2/1! + x^4/3! + x^6/5! + ... n
terms By calling findfact() and findpower()
void calculate()
{
--------------------------------------------------------

//to display the sum of the series


void display()
{
-------------------------------------------------------------------
}
//to calculate the sum of the series x^2/1! + x^4/3! + x^6/5! + ...... n
terms
void calculate()
{ int a=1;
for(int i=1;i<=n; i++)
{ sum=sum+ ( findpower(x,i*2)/findfact(a) );
a=a+2;
}
}
//or
void calculate()
{ for(int i=1;i<=n; i++)
sum=sum+ ( findpower(x,i*2)/findfact(i*2-1) );
}

void display() //to display the sum of the series


{ System.out.println("Sum of the series: "+sum); }
public static void main()
{

BufferedReader --------------------------

-----------------------------------------
}
}
public static void main()throws IOException
{

BufferedReader br=new BufferedReader( new


InputStreamReader(System.in));
int x1, n1;
System.out.println("Enter a number: ");
x1=Integer.parseInt(br.readLine());
System.out.println("Enter the total terms in the series: ");
n1=Integer.parseInt(br.readLine());

SeriesOne ob=new SeriesOne(x1, n1);


ob.calculate();
ob.display();
}
}
Q9.
A class Series is designed to calculate the sum of the following series:
Sum= 1/1! + (1+2)/2! + (1+2+3)/3! + …. N terms
Some of the members of the class are given below:
class name : Series
Data members/ instance variables :
N : to store number of terms
s : double variable to store the sum of the series
Member functions:
Series() : constructor to initialize the data member
void input() : to accept N from the user
double sum(int m) : to return sum of the series 1+2+3+….+ m terms
double fact(int x) : to calculate and return the factorial of x
void display() : to display the sum of the series by calling the
methods of the class..
 Specify the class Series, 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.
class name : Series
Data members/ instance variables :
N : to store number of terms
s : double variable to store the sum of the series
Series() : constructor to initialize the data member
import java.io.*;
class Series
{
//Data members
------------------------------------

// constructor to initialize the data member


Series ( )
{
-----------------------------------
}
class name : Series
Data members/ instance variables :
N : to store number of terms
s : double variable to store the sum of the series
Series() : constructor to initialize the data member

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

void input() throws IOException


{

BufferedReader br=new BufferedReader( new


InputStreamReader(System.in));

System.out.println(“Enter the value of N: “);


N=Integer.parseInt(br.readLine());
}
}
double sum(int m) : to return sum of the series 1+2+3+….+ m terms

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

Sum= 1/1! + (1+2)/2! + (1+2+3)/3! + …. N terms

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.

public static void main()


{

------------------------
}
main() function to create an object and call the member
function accordingly to enable the task.

public static void main() throws IOException


{
Series ob=new Series();
ob.input();
ob.display();
}//end of main()
}//end of class

You might also like