0% found this document useful (0 votes)
22 views

Computer Project

Uploaded by

dedsecajax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Computer Project

Uploaded by

dedsecajax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

INTRODUCTION

Name: Arpit Yadav


Class/Sec: 12th – H
School: CITY MONTESSORI
SCHOOL
Teacher: Mr. Abhishek Gupta
UID: 7848195
Index Number: 2253983/273

1
“Programming without an
overall architecture or
design in mind is like
exploring a cave with only
a flashlight: You don’t
know where you’ve been,
you don’t know where
you’re going, and you don’t
know quite where you are.”
– Danny Thorpe
2
Acknowledgement
I would like to express my gratitude towards the almighty
and thank to our honorable computer sir Mr. Abhishek
Gupta. His vast learning in computers theory and deep
insight into programming language helped me a lot and
nurtured my plant of knowledge.
I also express my gratitude towards my
parents and friends who provided me with innumerable
suggestions and bright reflected ideas,. Their valuable
support and encouraged inspired me throughout the
development of this project.
I give my special thanks to the authors
of various books which I consulted for this project.

Arpit Yadav
XII-ISC

3
Preface
Java is a popular third-generation programming
language. Java serves as a platform for internet applets
and stand alone applications. It has contributed a lot
towards its popularity. It supports the Object Oriented
Programming methodology (OOP), the latest in
software development and the most near to real world.
The following project file contains
Java programs which implement OOP Concepts Data
Abstraction, Data Encapsulation, Modularity,
Inheritance (Hierarchy) and Polymorphism. These
programs are some of our laboratory work done
throughout our ISC classes.

Yours truly,

Arpit Yadav.

4
Index
S.No PROGRAM
1. Program to print Kaprekar number between two
limits.
2. Program to print the future date n days after current
date.
3. Program to print the calendar of the month.

4. Program to calculate distance between two points in


plane and in space.
5. Program to print the circular matrix.

6. Program to print the numerical value of the entered


Roman Number.

7. Program to print n term series.


8. Program to calculate the income tax.

9. Program to find saddle point in a 2D-array.

10. Program to calculate the case of the inputted sentence


and arrange the words in alphabetical order.

11. Program to print Even Series.


12. Program to check for smith number.
13. Program to depict binomial theorem.
14. Program to find transpose of a 2D-array.
15. Program to remove duplicate elements in an array.
16. Program to enter a no. and check whether it is
palindrome or not if not then add its reverse to it until
it becomes a palindrome.

5
17. Program to enter an array having names and arrange
them in alphabetical order according to their
surnames.
18. Program to print magical matrix.
19. Program to add & subtract two 2D-matrices.
20. Program to input a number and print it in words.

6
Program-1
Program to print Kaprekar number between two limits.
e.g. 1,9,45,55,99.
45-> 452=2025=20+25=45. 9-> 92=81=8+1=9.
Algorithm :-
STEP 1-Start
STEP 2-Take two limits.
STEP 3-Run a for loop between two limits & call work(int n).
STEP 4-Store no. of digits of n in f &s=10^f &sq=n*n.
STEP 5-Using s,store first half digits of sq in l &remaining
digits in r.
STEP 6-If r+l==n, then print n ,the passed no.
STEP 7-End.

import java.io.*;
class Kaprekar
{
void work(int n)
{
int c=n,f=0,s,r,l;
do{
f++;c/=10;
}while(c>0);
s=(int)Math.pow(10,f);
r=(n*n)%s;
l=(n*n)/s;
if(r+l==n)
System.out.println(n);
}
void main()throws IOException
{

7
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter two limits”);
int l,u;
l=Integer.parseInt(br.readLine());
u=Integer.parseInt(br.readLine());
for(int i=l;i<=u;i++)
work(i);
}}
Variable Description :-
NAME TYPE FUNCTION
l,u Integer To store lower & upper limit.
i Integer Loop variable.
n Integer To store single number.
f Integer To store no. of digits.
r,l Integer To store parts of n2.

Input and Output :-

8
Program-2
DESIGN A CLASS DATE TO HANDLE DATE RELATED
FUNCTIONS I.E FINDING THE FUTURE DATE N DAYS
AFTER CURRENT DATE.
FOR EXAMPLE -:
DATE AFTER 32 DAYS WILL BE 02/02
FINDING THE NUMBER OF DAYS BETWEEN THE
CURRENT AND THE DATE ON WHICH THE PROJECT
ENDS.
YOU MAY ASSUME THAT ALL THE DATE ARE IN YEAR
2008 AND ARE VALID DATE TO MAKE CALCULATION
EASY.DATE CAN BE CONVERTED TO ITS DATE NUMBER.
DATE DATE NUMBER
01/01 2
20/02 20
02/02 33
03/03 63
31/12 366
Algorithm:-
STEP 1:Start
STEP 2:Take the values of dates through keyboard.
STEP 3:Calculate the date number of the take date.
STEP 4:Return the value of date number.
STEP 5:Take the values of date number through keyboard.
STEP 6:Return the equivalent date.
STEP 7:Take the days n.
STEP 8:Return future date.
STEP 9:End.

class name -date


Data members :-
dd-to store the date

9
mm-to store the month
Member methods :-
date(int nd,int nn)-constructer to assign nd to dd and nn to
mm.
date_no()-return the date no equivalent to current date object.
date_no_todate(int dx)-return the date equivalent to given
date number dx.
futuredate(int m)return future date in 2008 only.
Main function need not to be written.

class date
{
int dd,mm;
date(int nd,int nn)
{
dd=nd;
mm=nn;
}
int date_no()
{
int s=0;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
for(int b=0;b<(mm-1);b++)
s=s+a[b];
s+=dd;
return s;
}
int date_no_todate(int dx)
{
int b=0;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
int s=0;
while(dx>a[b])
dx=dx-a[b++];
10
return dx;
}
void futuredate(int m)
{
int dx=date_no();
dx+=m;
int b=0;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
int s=0;
while(dx>a[b])
dx=dx-a[b++];
System.out.println("THE FUTURE DATE AFTER "+m+"DAYS
IS"+dx+"/"+(b+1));
}
}
Variable Description:-
NAME TYPE FUNCTION
dd integer to store the date
mm integer to store the month
m integer to store the date
number.
s integer To return date number.

Input and Output:-

11
12
Program-3
A program to take total number of days in a month and the
first day of the month as a input and print the calendar of the
month as a output.
Algorithm:-
STEP 1:Start
STEP 2:Declare an array or 6 rows & 7 columns.
STEP 3:Make a void function input() to input the no of days in
month & first day.
STEP 4:Make funtion void calc()
STEP 5:Make a loop from 0 to 5 in a.
STEP 6:Make a loop from 0 to 6 in b.
STEP 7:If a==0 and b==fd-1,c++.
STEP 8:If c>0 and c<=nd,n[a][b]=c++.
STEP 9:End both loops.
STEP 10:Make the main() function.
STEP 11:Print the names of days in one row.
STEP 12:Print those elements of array that aare not 0.
STEP 13:End

import java.util.*;
class Calender
{
Scanner ob=new Scanner (System.in);
int fd,nd,n[][]=new int[6][7];
void input()throws Exception
{
System.out.println("Enter no.of days in the month:");
nd=ob.nextInt();
System.out.println("Enter I day's no.if 1 is for monday:");
fd=ob.nextInt();
}
void calc()
{
13
int a,b,c=0;
for(a=0;a<6;a++)
for(b=0;b<7;b++)
{
if(a==0&&b==fd-1)
c++;
if(c>0&&c<=nd)
n[a][b]=c++;
}
}
void main()throws Exception
{
String s[]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
input();
calc();
for(int y=0;y<7;y++)
System.out.print(s[y]+"\t");
System.out.println();
for(int h=0;h<6;h++)
for(int t=0;t<7;t++)
{
if(n[h][t]!=0)
System.out.print(n[h][t]);
System.out.print("\t");
}
System.out.println();
}
Variable Description :-

NAME Type FUNCTION

A Integer Loop variable


B Integer Loop variable
C Integer Counter

14
Program-4
Class D2point defines the co-ordinates of point in a plane.
While another class D3point defines co-ordinates in a space:
Details of both classes are given below:
Class name: D2point
DATA MEMBERS:
Double x , y – to store the x and y co-ordinates.
MEMBER METHODS:
1.D2point()-constructor to assign zero to x and y.
2.D2point(double nx , double ny)- constructor to assign nx and
ny to x and y respectively.
3.double Distance2d(D2point b)-to return the distance between
point b.
CLASS NAME: D3point
DATA MEMBERS:double z.
MEMBER METHODS:
1.D3point()-constructor to assign zero to z.
2.D3point(double nz)- constructor to assign nz to z.
3.double Distance3d(D3point b)- to return the distance
between point b in space.
Algorithm:-
STEP 1-Start.
STEP 2-Create member methods of class D2point.Like
D2point(),D2point(double , double ) and double Distance2d() :
to store the distance between point b in a plane.
STEP 3-Create member methods of class D3point. .Like
D3point(),D3point(double , double ) and double Distance3d() :
to store the distance between point b in a space.
STEP 4-Create inheritance between both classes.
STEP 5-Specify the methods in the main method.
STEP 6-End.

class D2point

15
{
double x,y;
public D2point()
{
x=0;
y=0;
}
public D2point(double a,double b)
{
x=a;
y=b;
}
public double Distance2d(D2point b)
{
double s=0.000;
double result=0.000;
s=(Math.pow((b.x-x),2)+Math.pow((b.y-y),2));
result=Math.sqrt(s);
return result;
}
}
class D3point extends D2point
{
double z;
public D3point()
{
z=0;
}
public D3point(double a,double b,double c)
{
super(a,b);
z=c;
}
public double Distance3d(D3point b)
16
{
double s,result;
s=Math.pow((b.x-x),2)+Math.pow((b.y-y),2)+Math.pow((b.z-
z),2);
result=Math.sqrt(s);
return result;
}
public void main(double a,double b,double c)
{
double s1,s2;
D2point b1=new D2point(a,b);
D3point b2=new D3point(a,b,c);
s1=Distance2d(b1);
s2=Distance3d(b2);
System.out.println(" Distance in plane "+s1);
System.out.println(" Distance in space "+s2);
}
}

Variable Description :-
NAME TYPE FUNCTION
x , y ,z double Instance variable to
store the co-
ordinates.
s , s1 , s2 double To store the square
of distance between
the points.
result double To store the distance
between the points.
Input and Output :-

17
18
Program-5
Write a program to print the circular matrix.
Algorithm:-
STEP 1- Start
STEP 2- Accept the size of the matrix I.e. number of rows
and no. of columns i.e. n
STEP 3- Initialize an integer type variable r=0,c=-1,l,f,t=1.
STEP 4- Create a matrix a[][] i.e. int a[][]new =int [n][n]
STEP 5- Set a while loop i.e. while(n>0) if true then goto
Step6 else goto 12.
STEP 6- Inside a while loop set a for loop
i.e.for(i=1;i<=n;i+=) inside this loop store the nos. from 1
to n to the row ‘r’ i.e. a[r][++c] =t++
STEP 7- Set a for loop i.e. for(i=1;i<n;i++) which will store
the numbers from t onwards in the column specified by c
i.e. a[++r][c]=t++
STEP 8- Set a for loop i.e. for(i=1;i<n;i++) .In this loop no.
from t onwards will be stored in a row specified by c i.e.
a[r][--c]=t++
STEP 9- Set a for loop i.e. for(i=1;i<n-1;i++).in this loop no.
from t onwards will be stored in a column specified by
variable r i.e a[--r][c]=t++
STEP 10- Decrease the value of n i.e. the no. of columns or
rows by 2
STEP 11- Goto step5
STEP 12- Print the matrix by setting two for loops
STEP 13- End

import java.io.*;
class Cir_Matrix
{
public static void main(int n)
{

19
int i,r=0,c=-1,t=1,l=n,j;
int a[][]=new int[n][n];
while(n>0)
{
for(i=1;i<=n;i++)
a[r][++c]=t++;
for(i=1;i<n;i++)
a[++r][c]=t++;
for(i=1;i<n;i++)
a[r][--c]=t++;
for(i=1;i<n-1;i++)
a[--r][c]=t++;
n=n-2;
}
System.out.println("Circular Matrix for input "+n+" is:-");
for(i=0;i<l;i++)
{
for(j=0;j<l;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}
Variable Description :-
NAME TYPE FUNCTION

a Integer Array variable

n Integer No of rows and columns


of matrix
i,j Integer For variable purpose
r Integer Variable representing
row
c Integer Variable representing
column

20
Input and Output :-

21
Program-6
Program to print the numerical value of the entered Roman
Number.

Algorithm:-
STEP 1-Start.
STEP 2-Accept a String x.
STEP 3-Create a String a to store roman number.
STEP 4-Create a array b[] to values corresponding to roman
number.
STEP 5-Extract each element of x and match it with the
corresponding values a[] and b[].
STEP 6-Do the sum of the corresponding values.
STEP 7-Stop.

class Roman
{
public static void main(String x)
{
String a="IVXLCDM";
int b[]={1,5,10,50,100,500,1000};
int c,d,e=0,f,n=0,i=0;
char ch;
int g[]=new int[100];
for(c=0;c<x.length();c++)
for(d=0;d<a.length();d++)
if(x.charAt(c)= =a.charAt(d))
{
e=d;
g[c]=b[e];
}
for(f=0;f<g.length-1;f++)
if(g[f]<g[f+1])

22
g[f]=-g[f];
for(int p=0;p<a.length();p++)
n=n+g[p];
System.out.println("The integer value of the roman no. "+x+" is
"+n);
}
}
Variable Description :-
NAME TYPE FUNCTION
x String To enter a string

a String To store roman nos. in the


array
c int Loop variable

e int Loop variable

f int Loop variable

n int To sum up the numbers

Input and Output :-

23
Program-7
Define a class 'Sum' with following specification
Class Name : Sum
Data Member :
a - To store first term
d - To store common difference
Member Function :
Sum() - To initialize value of a and d with 0
Sum(double a,double d) - To initialize value of a and d with
parametric a and d
nTerm(int n) - To return n term of series
sum(int n) - To return sum of series till n terms
showSeries(int n) - To display n terms of series with sum

Algorithm:-
STEP 1- Start.
STEP 2-We have initialize double type variable i.e. a and d
STEP 3-We have made non –parameterized constructor by
which we have assign 0 to ‘a’and‘d’.
STEP 4- We have made a parameterized constructor
.parameters are double a, double d. We have assign a to this. a
i.e. object class and d to this .d
STEP 5-We have made a return type function i.e. nTerm.
Parameter is int n.We have return the nth term of series in
this function
STEP 6- We have made another return type function i.e. Sum
and int ‘n’ is the parameter. In this function we have return
the sum of series
STEP 7- We have made a function i.e. Show Series and
parameter is n(Integer Type). In this we have print the nth
term of series as well as Sum of series.
STEP 8- We have made main function of this class and print
24
the series of required term.
STEP 9-End

import java.io.*;
class Sum
{private double a,d;
Sum()
{a = d = 0;
}
Sum(double a,double d)
{
this.a = a;
this.d = d;
}
double nTerm(int n)
{
return (a+(n-1)*d);
}
double Sum(int n)
{
return (n*(a+nTerm(n))/2);
}
void showSeries(int n)
{
System.out.println("First term of the series is "+a);
System.out.println("Common difference of the series is
"+d);
System.out.println("Hence the series is ");
for(int i=1;i<=n;i++)
System.out.println(nTerm(i)+" ");

25
System.out.println("It's Sum will be : "+Sum(n));
}
}

Variable Description :-

NAME TYPE FUNCTION

a int To store first term of


Sum.
d int To store the common
difference of Sum

Sum double To store sum of Sum

i int Loop variable.

Input and Output :-

26
Program-8
Program to calculate the income tax.

Algorithm:-
STEP 1-Start.
STEP 2-Enter your name, sex, monthly income,
investment in LIC,investment in NSC, investment in UTI
and other monthly investment in function get data.
STEP 3-calculate the yearly investment.
STEP 4-We have calculated Income Tax according to
given slab in the function income tax.
STEP 5-We have check whether the sex given is female
and if this condition found true then we have calculated
Income tax according to given slab in the function
display.
STEP 6-We have made main function and through the
object we have called the function made above and we
able to calculate income tax.
STEP 7-End.

import java.io.*;
class taxC
{
private String name; private char sex;
private int mi,yi,tax; int lic,uti,nsc,oth;
public void getdata()throws Exception
{
DataInputStream d=new DataInputStream(System.in);
System.out.println(" Please enter your name ");
name=d.readLine();
System.out.println(" Please enter your sex");
sex=d.readLine().charAt(0);
System.out.println(" Please enter your monthly income ");

27
mi=Integer.parseInt(d.readLine());
System.out.println("Please enter the following for your
yearly investments”);
System.out.println(" Yearly investment in LIC ");
lic=Integer.parseInt(d.readLine());
System.out.println(" Yearly investment in UTI ");
uti=Integer.parseInt(d.readLine());
System.out.println(" Yearly investment in NSC ");
nsc=Integer.parseInt(d.readLine());
System.out.println(" Other yearly investment ");
oth=Integer.parseInt(d.readLine());
yi=lic+uti+nsc+oth;}
public int incometax()
{int inc=12*mi-25000,s=0;
if(inc>=75000 && inc<100000)
tax=(int)(.1*(inc-75000));
else if(inc>=100000 && inc<125000)
tax=2500+(int)(.15*(inc-100000));
else if(inc>=125000)
tax=6500+(int)(.18*(inc-125000));
if(sex=='f' || sex=='F')
tax-=5000;
tax-=0.1*yi; s=(int)(0.1*tax); tax+=s;
if(tax<=0)
tax=0;
return s;
}
public void display(int s)
{
System.out.println("\n\n Name\t\t\t\t:- "+name);
if(sex=='f')
System.out.println(" Sex\t\t\t\t:- Female");
else
System.out.println(" Sex\t\t\t\t:- Male");
28
System.out.println(" Monthly income\t\t\t:- "+mi);
System.out.println(" Yearly income \t\t\t:- "+mi*12);
System.out.println(" Standard deduction gives is:- 25000");
System.out.println("\t\t\t\t -------");
System.out.println(" Taxable Income\t\t\t:- "+(mi*12-
25000));
System.out.println("\t\t\t\t -------");
System.out.println(" LIC investment\t\t\t:- "+lic);
System.out.println(" UTI investment\t\t\t:- "+uti);
System.out.println(" NSC investment\t\t\t:- "+nsc);
System.out.println(" Other investment\t\t:- "+oth);
System.out.println(" Total investment\t\t:- "+yi);
System.out.println(" Rebate (10% of all investments):-
"+(int)(0.1*yi));
if(sex=='f' || sex=='F')
System.out.println(" Rebate given to woman is:- 5000 ");
System.out.println(" Surcharge 10% of tax :- "+s);
System.out.println(" Total tax to pay :- "+tax);
}
}

Variable Description :-
NAME TYPE FUNCTION
name String To store the name .
sex char To store the sex of a person.
mi int To the monthly income
lic , uti ,nsc ,oth ,yi int To store the LIC , UTI , NSC
, Other and Total
investment respectively.
s , tax int To store surcharge and tax
respectively.

Input and Output :-

29
30
Program-9
WRITE A PROGRAM TO CALCUTATE THE NUMBER OF
PEOPLE VISITED THE TICKET BOOTH AND PURCHASED
THE TICKET OR NOT:
CLASS NAME:-ticbooth
INSTANCE VARIABLES:- no_ppl: no.of people visited.
amt: total amount collected.
MEMBER METHODS:-
1.void initial()-to assign initial values to instance variables.
2.void sold()-increament total number of people visiting,
purchasing and amount collected.
3.void notsold()-increment total number of people visiting and
not purchasing.
4.void disp_totals()-to display no.of people visiting the
booth,purchasing and not purchasing.
5.void dis_ticket()-to display the total no.of sold and amount.

Algorithm:-
STEP 1-Start.
STEP 2-Create a method void initial to assign initial value to
instance variable.
STEP 3-Create a method void notsold() to increament total
number of people visited and purchasing.
STEP 4-Create a method void sold() increament total number
of people visiting, purchasing and amount collected.
STEP 5-Create a method void dis_totals() to display no.of
people visiting the booth,purchasing and not purchasing.
STEP 6-Create a method void disp_ticket() to display the total
no.of sold and amount.
STEP 7-Stop.

class tickbooth
{

31
double amt;
int no_ppl;
public void initial()
{
amt=no_ppl=0;
}
public void notsold()
{
no_ppl++;
}
public void sold()
{
no_ppl++;
amt+=2.5;
}
public void disp_totals()
{
int k=(int)(amt/2.5);
System.out.println("The no.of people who purchased the
ticket "+k);
System.out.println("The no.of people who not purchased the
ticket "+(no_ppl-k));
System.out.println("The no.of people who visited the booth
"+no_ppl);
}
public void disp_ticket()
{
int d=(int)(amt/2.5);
System.out.println("The no.of ticket sold "+d);
System.out.println("Amount "+amt);
}
}

32
Variable Description :-
Type Name Function
double Amt To store amount.
int no_ppl To store no. of
people visiting.
int k,d To store no.of
people who
purchased ticket.

Input and Output :-

33
Program-11
Program to calculate the case of the inputted sentence
and arrange the words in alphabetical order.

Algorithm:-
STEP 1-Start.
STEP 2-We have made parameterized constructor and
the parameter is String str1and we have assigned str to
str1.
STEP 3-Input the String in the function getdata.
STEP 4-We have convert the case of the String and find
function proceed.
out the length of the String in the
STEP 5-We have done type casting and stored
the character in c we have found the character of
the string in ch as well as the next character of the string
in ch1.
STEP 6-We have checked the condition i.e. (ch== ‘’||ch1==
‘c’).If this condition found true then go to Step7 .
STEP 7-We have taken out the character of the string and
check the condition i.e.(ch!= ‘’).If this condition found true
then go to step8.
STEP 8-We have taken the character of the String in
String s.
STEP 9-Apply break.
STEP 10-We have print the arranged string in the
function disp.
STEP 11-Use inheritance and we have made main
function in class alph.
STEP 12-We have called the function made above and we
have arrange the string in alphabetical order.
STEP 13-End.

import java.io.*;

34
class stream
{
String str;
stream(String str1)
{
str=str1;
}
void getdata()throws Exception
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter a String");
str=d.readLine();
}
void proceed()
{
int i,l,j,k;
char ch,c,ch1;
String s= new String(" ");
str=" "+str+" ";
str=str.toUpperCase();
l=str.length();
for(i=65;i<90;i++)
{
c=(char)i;
for(j=0;j<l-1;j++)
{
ch=str.charAt(j);
ch1=str.charAt(j+1);
if(ch==' ' && ch1==c)
for(j++;j<l;j++)
{
ch=str.charAt(j);
if(ch!=' ');
else if(ch==' ')
35
{
s=s+" ";
break;
}
}
}
}
str=" ";
str=s;
}
void disp()
{
System.out.println(str);
}
}

Variable Description :-
NAME TYPE FUNCTION
i ,j Integer For Loop
l Integer Length of the string
ch, ch1 Character For storing the character
of the string
str, str1,s String For storing the String

Input and Output :-

36
Program-12
A CLASS CALLED EVEN SERIES HAS BEEN DEFINED TO
FIND THE SMALLEST VALUE OF INTEGER N SUCH THAT
2+4/2!+8/3!+16/4!+.......+2^N/N! >=S WHERE 2.0<S<7.0 SOME
OF THE MEMBERS OF CLASS EVEN SERIES ARE GIVEN
BELOW.
CLASS NAME - Evenseries
DATA MEMBERS/INSTANCE VARIABLES-
n - Long integer type to store number of number of terms
s - Float variables where 2.0<s<7.0
k - Float variable to store the value of series evaluated
MEMBER FUNCTION:
Evenseries() : constructor to initialize data member to 0.
void accept() : To accept value of data member s.
long fact(long x):To compute and return factorial of x.
void disp():calculates and displays the value of n.
SPECIFY THE CLASS EVENSERIES GIVING THE DETAILS
OF THE CONSTRUCTOR ,Evenseries(),void accept()
long fact(),void disp()

Algorithm :-
STEP 1:Start
STEP 2: Take the values through keyboard.
STEP 3: Calculate factorial value.
STEP 4:Calculate and displays the value of n.
STEP 5:End.

import java.io.*;
class Evenseries
{
long n;
float s,k;

37
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Evenseries()
{
n=0;
}
void accept()throws IOException
{
System.out.println("ENTER THE NUMBER");
s=Integer.parseInt(br.readLine());
}
long fact(long x)
{
long a,f=1;
for(a=1;a<=x;a++)
f*=a;
return f;
}
void disp()
{
while(k<s)
{
n++;
k=(float)Math.pow(2,n)/fact(n)+k;
}
System.out.println("THE SUITABLE VALUE OF n IS "+n);
}
}

Variable Description :-
NAME TYPE FUNCTION
N long to store the value of
integer n for which the

38
sum of series is to be
calculated.
S float to store sum of series.
K float float variable to store the
value of series evaluated.
X integer To calculate the factorial.
A integer for looping.
F integer for looping.

Input and Output :-

39
Program-13
A program to print the prime factors of a given number.

Algorithm:-
STEP 1:Start.
STEP 2:Make a function prime() to accept a value & return
integer.
STEP 3:Return the next prime after the input number.
STEP 4:Make function main() that accepts a value.
STEP 5:Print n+"=".
STEP 6:Generate loop with starting value of a=1,b=2 &
condition that a is greater than 1.
STEP 7:If a%b=0, divide a by b and print b.
STEP 8:Else b=prime(b).
STEP 9:End.

class PrimeFac
{
public static void main()
{
int n,a,b=2;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER");
n=Integer.parseInt(br.readLine());
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT
IS");
System.out.print(n+"=");
for(a=n;a>1;)
if(a%b==0)
{
a/=b;
System.out.print(b+"*");
}
40
else
b=prime(b);
}
public static int prime(int c)
{
int a,b=0,d,e=0;
for(a=c+1;b==0;a++)
{
e=0;
for(d=1;d<=a;d++)
if(a%d==0)
e++;
if(e==2)
b++;
}
return a-1;
}}

Variable Description :-
NAME Type FUNCTION
a Integer Loop variable
b Integer Counter
d Integer Next no. to be searched
e Integer No. of factors

n Integer Store number

Input and Output :-

41
Program-14
WRITE A PROGRAM TO STATE BINOMIAL THEOREM:
CLASS NAME-binomial
INSTANCE VARIABLES-double a,x
MEMBER METHODS-

1.binomial()-non-parameterised constructor.
2.long factorial(long f)-to find factorial of given number.
3.double nCr(int n,int r)-to return nCr=(n!/((n-r)!*r!)).
4.double sum(int n).

Algorithm:-
STEP 1-Start.
STEP 2-Create a method long factorial to calculate factorial of
given number.
STEP 3-Create a method double nCr to calculate the
nCr=(n!/((n-r)!*r!)).
STEP 4-Create a method double sum to calculate the sum of
binomial expression.
STEP 5-Specify these methods in the main method.
STEP 6-End.

import java.io.*;
class binomial
{
double x,a;
public binomial()
{
X=a=0;
}
public long factorial(long f)
{
int x3; int ft=1;

42
for(x3=1;x3<=f;x3++)
ft=ft*x3;
return ft;
}
public double nCr(int n,int r)
{
long p,q,y;
double ncr;
p=factorial(n);
q=factorial(n-r);
y=factorial(r);
ncr=p/(q*y);
return ncr;
}
public double sum(int n)
{
double s=0,t;
int p=n;
for(int xt=0;xt<=n;xt++)
{
t=nCr(n,xt)*(Math.pow(x,p))*(Math.pow(a,xt));
s=s+t;
p--;
}
return s;
}
}

Variable Description :-

NAME TYPE FUNCTION

43
f long Number whose
factorial to be
found.
x , xt int Loop variable.
p,q,y long To store factorial.
s double To store the sum
of binomial
expression.

Input and Output :-

44
Program-15
A CLASS CALLED EVEN SERIES HAS BEEN DEFINED TO
FIND THE SMALLEST VALUE OF INTEGER N SUCH
THAT 2+4/2!+8/3!+16/4!+.......+2^N/N! >=S WHERE 2.0<S<7.0
SOME OF THE MEMBERS OF CLASS EVEN SERIES ARE
GIVEN BELOW.
CLASS NAME - Evenseries
DATA MEMBERS/INSTANCE VARIABLES-
n - Long integer type to store number of number of terms
s - float variables where 2.0<s<7.0
k - float variable to store the value of series evaluated
MEMBER FUNCTION:
Evenseries() : constructor to initialise data member to 0.
void accept() : To accept value of data member s.
long fact(long x):To compute and return factorial of x.
void disp():calculates and displays the value of n.
SPECIFY THE CLASS EVENSERIES GIVING THE DETAILS
OF THE CONSTRUCTOR ,Evenseries(),void accept()
long fact(),void disp()

Algorithm:-
STEP 1:Start
STEP 2: Take the values through keyboard.
STEP 3: Calculate factorial value.
STEP 4:Calculate and displays the value of n.
STEP 5:End.

import java.io.*;
class Evenseries
{
long n;
float s,k;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
45
Evenseries()
{
n=0;
}
void accept()throws IOException
{
System.out.println("ENTER THE NUMBER");
s=Integer.parseInt(br.readLine());
}
long fact(long x)
{
long a,f=1;
for(a=1;a<=x;a++)
f*=a;
return f;
}
void disp()
{
while(k<s)
{
n++;
k=(float)Math.pow(2,n)/fact(n)+k;
}
System.out.println("THE SUITABLE VALUE OF n IS "+n);
}
}
Variable Description :-
NAME TYPE FUNCTION
N long to store the value of integer n .
S float to store sum of series.
K float to store the series evaluated.
X integer To calculate the factorial.
A integer for looping.
F integer for looping.

Input and Output :-

46
47
Program-16

Program to enter a no. and check whether its palindrome or


not if not then add its reverse to it until it becomes a
palindrome.

Algorithm :-

STEP 1-Start
STEP 2-Generate the function to reverse a number.(Step 3-8)
STEP 3-Input a number,n.
STEP 4-Initialize variables a=0,b.
STEP 5-Generate a loop from n to0 in b(Step 6&7)
STEP 6-a=a*10+b%10&b=b/10
STEP 7-Return a
STEP 8-Generate the main function & input a number
STEP 9-Initialize x=0&y=n
STEP 10-Generate loop until x=0
STEP 11-If reverse of y=y,print Y is palindrome&x=1 else print
that reverse of string is not Palindrome &y=y+reverse of y .
STEP 12-End

class Palindrome
{
public static int rev(int n)
{
int a=0,b;
for(b=n;b>0;b/10)
a=a*10+b%10;
return a;}
public static void main(int n)
{
int x=0,y=n;
for(;x==0;)

48
if(rev(y)==y)
{
System.out.printlln(y+is palindrome”);
x=1;
}
else
{
System.out.println(y+”is not palindrome”);
y=y+rev(y);
}
}}

Variable Description :-
Name Type Function
a Integer Store reverse
b Integer Loop variable
n Integer Original number
x Integer Controls loop
y Integer Temporary Variable

Input and Output :-

49
Program-17
Program to enter an array having names and arrange them
in alphabetical order according to their surnames.

Algorithm :-
STEP 1-Start
STEP 2-Input an array.
STEP 3-Declare a 2-D array having no.of rows equal to the
length of string & two columns
STEP 4-Generate loop from 0 to length of array
STEP 5-Split the entered array such that the names go in first
column of first array&space go in second.
STEP 6-Generate loop from 0-length of array.
STEP 7-Generate loop from 0-length of array.
STEP 8-If present surname is more than next
surname,exchange the names& surname
STEP 9-Print the modified array.
STEP 10-End

class Surname
{
public void main(String s[])
{
String t[][]=new String[s.length][2];
String m=””;
int a,b;
for(a=0;a<s.length;a++)
t[a]=s[a].split(“ “);
for(a=0;a<s.length;a++)
for(b=0;b<s.length-a-1;b++)
if(t[b][1].compareTo(t[b+1][1]>0)
{
m=t[b+1][0];

50
t[b+1][0]=t[b][0];
t[b][0]=m;
m=t[b+1][1];
t[b][1]=m;
}
for(a=0;a<s.length;a++)
System.out.println(t[a][0]+” “+t[a][1]);
}
}
Variable Description :-
Name Type Function
a Integer Loop variable
b Integer Loop variable
t String 2D array
s String Entered array
m String Temporary string

Input and Output :-

51
Program-18
DEFINE A CLASS TIME WITH FOLLOWING
SPECIFICATION.
CLASS NAME : TIME
DATA MEMBER/INSTANCE VARIABLES
HR,MIN,SEC : INTEGER
MEMBER METHOD/FUNCTIONS
TIME() : DEFAULT CONSTRUCTOR
VOID ACCEPT() : ACCEPT A TIME IN
HRS.MINUTES.SECONDS(H/M/S)TIME ADDTIME(TIME T1)
: ADD T1 TIME WITH CURRENT TIME AND RETURN
IT.
TIME DIFFTIME(TIME T2) : RETURN THE
DIFFERENCE T1 TIME WITH CURRENT TIME AND
RETURN IT.
WRITE MAIN() & SHOW WORKING OF TIME CLASS BY
DEFINING ALL THE METHODS & USING OBJECT OF
CLASS TIME.

Algorithm:-
STEP 1: Start
STEP 2: Assign class variables with initial values.
STEP 3: Accept the values of hours,minutes and seconds
through keyboard.
STEP 4: Add the dates one which was entered through
keyboard and the one passed through object.Check whether
the sum of minutes and seconds does not exceeds 60 if so then
increase hour and minute respectively.
STEP 5: Similarly calculate the difference between the two
dates and print it.
STEP 6: Create a main describing the functions and printing
difference and addition.
STEP 7: End.

52
import java .io.*;
class Time
{
int hr,min,sec;
Time()
{
hr=min=sec=0;
}
public void accept()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("ENTER THE TIME");
System.out.println("HOUR");
hr=Integer.parseInt(br.readLine());
System.out.println("MIN");
min= Integer.parseInt(br.readLine());
System.out.println("SECOND");
sec= Integer.parseInt(br.readLine());
}
public Time addtime(Time t1)
{
Time ob=new Time();
ob.hr=t1.hr+hr;
ob.min=t1.min+min;
ob.sec=t1.sec+sec;
if(ob.sec>=60)
{
ob.sec-=60;
ob.min++;
}
if(ob.min>=60)
{
ob.min-=60;
53
ob.hr++;
}
return ob;
}
public Time difftime(Time t2)
{
Time ob=new Time();
if(ob.sec<sec)
{
ob.sec+=60;
ob.min--;
ob.sec=ob.sec-sec;
}
else
ob.sec=ob.sec-sec;
if(ob.min<min)
{
ob.min+=60;
ob.hr--;
ob.min=ob.min-min;
}
else
ob.min=ob.min-min;
ob.hr=ob.hr-hr;
return ob;
}
}
Variable Description :-
NAME TYPE FUNCTION
hr Long to store the value of n.
s Float to store sum of series.
k Float to store the value of series.
x Integer To calculate the factorial.

54
Input and Output :-

55
Program-19
A class Matrix has the following details.
Class name -Matrix.
Data members:-
a[][] - integer type array.
m - To store size of column dimension.
n -To store size of row dimension.
Memberfunction:Matrix(intr,intc)
constructor to assign r to m and c to n.
void readMatrix() - input the matrix.
void displayMatrix() -displays the matrix.
void addMatrix(Matrix A,Matrix B)-creates a matrix
which is sum of Matrix A and Matrix B.
Matrix subMatrix(Matrix B) -find and returns the
subtracted matrix by Subtracting it from current Matrix
object.
Write a program to specify the class Matrix giving details
of all member function.
main function need not to be written.

ALGORITHM:-
STEP-1: Start
STEP-2: Initialize class variables with initial values.
STEP-3: To enter the values of matrix through keyboard.
STEP-4: To display the inputted matrix.
STEP-5: To add the two matrices create a function
addMatrix().
STEP-6: To subtract two matrices create a function
subMatrix().
STEP-7: Create a main() method to specify above functions.
STEP-8: Stop.

import java.io.*;
class Matrix
56
{
int a[][];
int m;
int n;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Matrix(int r,int c)
{
m=r;
n=c;
a=new int[m][n];
}
void readMatrix()throws IOException
{
System.out.println("ENTER THE MATRIX");
for(int c=0;c<m;c++)
for(int b=0;b<m;b++)
a[c][b]=Integer.parseInt(br.readLine());
}
void displayMatrix()
{
for(int c=0;c<m;c++)
{
for(int b=0;b<m;b++)
System.out.print(a[c][b]+" ");
System.out.println();
}
}
void addMatrix(Matrix A,Matrix B)
{
int d=m,k=n;
Matrix ob=new Matrix(d,k);
for(int r=0;r<m;r++)
for(int c=0;c<n;c++)
57
ob.a[r][c]=A.a[r][c]+B.a[r][c];
ob.displayMatrix();
}
Matrix subMatrix(Matrix B)
{
int d=m,k=n;
Matrix ob=new Matrix(d,k);
if(n==B.n)
for(int r=0;r<m;r++)
for(int c=0;c<m;c++)
ob.a[r][c]=a[r][c]-B.a[r][c];
else
System.out.println("DATA IS INCORRECT");
return ob;
}
}

Variable Description :-
NAME TYPE FUNCTION
a Integer To store the array.
m Integer To store size of row.
n Integer To store size of column
r Integer To store temporary
value.

Input and Output :-

58
59
Program-20
Program to input a number and print it in words.
Algorithm:-
STEP 1-Start
STEP 2-Input the number in amt (Integer Type).
STEP 3-We have taken the Integer variable i.e.z&g.
STEP 4-We have taken the String array i.e.
String x1[]={“,” “ONE”, “TWO”, “THREE”, “FOUR” ,
“FIVE” ,“SIX” , “SEVEN” , “EIGHT” ,“NINE”}. String x[]={“
,”, “TEN”, “ELEVEN”, “TWELVE”, “THIRTEEN”
,“FOURTEEN”, “FIFTEEN”, “SIXTEEN” , “SEVENTEEN”
,“EIGHTEEN”, “NINTEEN”};String x2[]={“,” “TWENTY”,
“THIRTY”,“FOURTY” , “FIFTY” ,“SIXTY” , “SEVENTY” ,
“EIGHTY” , “NINTY”}.
STEP 5-We have taken the remainder of inputted number
amt in z and the quotient of the Inputted Step number in
g.
STEP 6- We have check the condition i.e. (g!=1).If this
condition found true then Go to Step7 .
STEP 7-Print the Sting array i.e.(x2[g-1]+ “”+x1[z]).
STEP 8-Print the String array i.e.(x[amt-9]).
STEP 9-End.

import java.io.*;
class eng
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String x3;
System.out.println("Enter any Number");

60
int amt=Integer.parseInt(br.readLine());
int a,b,c,y,z,g;
String x[]={"One","Two","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen",
"Fourteen","Fifteen","Sixteen","Seventeen","Eighteen",
"Nineteen"};
String x1[]={"One","Two","Three","Four","Five","Six","Seven",
"Eight","Nine"};
String x2[]={"Twenty","Thirty","Fourty","Fifty","Sixty",
"Seventy","Eighty","Ninety"};
z=amt%10;
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]);
else
System.out.println(x[amt-9]);
}
}
Variable Description :-
NAME TYPE FUNCTION
amt Integer To input the number
X1 String To enter a string array
x2 String To print a string array

Input and Output :-

61
62
Bibliography
I have prepared this project by using the following books and
files:-

1) Computer Science with Java


By:
Sumita Arora.
2) Introduction to Objective Oriented Programming
Through Java
By:
Tata McGraw Hill

63

You might also like