“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
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 and Polymorphism. These
programs are some of our laboratory
work done throughout our ISC classes.
Yours truly,
RUCHIR SHARMA
Index
S.N PROGRAM
o
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 monthly bill of a consumer.
5. Program to add and multiply two complex numbers.
6. Program to print the circular matrix.
7. Program to print Pascal’s Triangle till n numbers.
8. Program to check whether the date is valid or not.
9. Program to print the prime factors of a given number.
10. Program to print unique nos. between two limits.
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
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 :-
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
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++];
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:-
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()
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
H Integer Loop Variable
T Integer Loop Variable
Program-4
A CLASS TELECALL CALCULATE THE MONTHLY BILL OF A
CONSUMER.
DATA MEMBERS-
phno:phone number.
n:no.of calls made.
name:name of consumer.
amt:bill amount.
MEMBER METHODS-
1.telecall():parameterized constructor.
2.void compute():to calculate the details
3.void dispdata():to display the details
1-100 Rs 500/- rental only
101-200 Rs 1.00/call+R.C
201-300 Rs 1.20/call+R.C
above 300 Rs 1.50/call+R.C
Algorithm:-
STEP 1-Start.
STEP 2-Create a non-parameterized constructor.
STEP 3-Create a method void compute() to calculate the bill.
STEP 4-Create a method void dispdata() to display the details.
STEP 5-Stop.
class telecall
int phno,n;
double amt;
String name;
public telecall(int a,int b,String s)
phno=a;
n=b;
name=s;
public void compute()
double m;
if(n<=100)
m=500;
else if(n>100&&n<=200)
m=500+(n-100)*1;
else if(n>200&&n<=300)
m=1000+100+(n-200)*1.2;
else
m=1120+(n-300)*1.5;
amt=m;
void dipdata()
System.out.println("Phone number "+phno);
System.out.println(" Name "+name);
System.out.println(" Total Calls "+n);
System.out.println(" Amount "+amt);
Variable Description :-
NAME TYPE FUNCTION
phno , n int To store phone
number and no.of
calls.
name String To store the name of
consumer.
amt , m double To store the bill
amount.
Input and Output :-
Program-5
Write a program to enter two complex number using the
concept of objects. Add these complex numbers and also
multiply them.
Algorithm:-
STEP 1:Start
STEP 2:Create a constructor which initialises data members
to zero
STEP 3:Create another method which inputs the values of
data members
STEP 4:Create a method which displays the complex number
STEP 5:Create another method in which complex
numbers(inputed by using the call by reference concept) are
added
STEP 6:Create another method in which complex numbers
are multiplied and a object is returned
STEP 7:In main function,complex numbers are inputed by
using appropriate messages and results are displayed using
appropriate messages
STEP 8:End
import java.io.*;
class Complex
float x,y;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Complex()
float x=0;
float y=0;
void readComplex()throws IOException
System.out.println("ENTER THE VALUE OF X");
x=Float.parseFloat(br.readLine());
System.out.println("ENTER THE VALUE OF Y");
y= Float.parseFloat(br.readLine());
void showComplex()
System.out.println("THE COMPLEX NUMBER" +x+"+i"+y);
void addComplex(Complex a,Complex b)
Complex ob=new Complex();
ob.x=a.x+b.x;
ob.y=a.y+b.y;
ob.showComplex();
Complex mulComplex(Complex b)
Complex ob=new Complex();
ob.x=(float)(x*b.x-y*b.y);
ob.y=(float)(x*b.y+y*b.x);
return ob;
}
Variable Description :-
NAME TYPE FUNCTION
x,y int Stores the real and
imaginary part of
complex number.
a,b Complex Reference type
object.
Input and Output :-
Program-6
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)
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
Input and Output :-
Program-7
Program to print Pascal’s Triangle till n numbers.
Example :-5
1
11
121
1331
14641
Algorithm:-
STEP 1- Start
STEP 2- Take an array of n+1 locations
STEP 3- Run a loop till n
STEP 4- Run another loop till previous loop and print the element
of array with one space
STEP 5- Change the line
STEP 6- Run a loop to add the current and previous element until
0.
STEP 7- End
class Pascal
{
public static void main(int n)
int pas[]=new int[n+1];
pas[0]=1;
for(int i=0;i<n;i++)
for(int j=0;j<=i;j++)
System.out.print(pas[j]+" ");
System.out.println();
for(int j=i+1;j>0;j--)
pas[j]=pas[j]+pas[j-1];
Variable Description :-
NAME TYPE FUNCTION
N int to enter no. of lines
pas[] int array to calculate nos.
I int loop variable
J int loop variable
Input and Output :-
Program-8
Program to check whether the date is valid or not. If the
date is valid or invalid , print the appropriate message.
Algorithm:-
STEP 1- Start.
STEP 2- Enter day,month and year.
STEP 3- Take an array to store last days of the 12 months.
STEP 4- Use ternary operator to check whether the year is
divisible by 400 and 100 or 4 to check leap year and add 1 int
month of February.
STEP 5- Check whether the month is between 1-12
otherwise print month is invalid.
STEP 6- Check whether the number of days according to
corresponding element of the array.
STEP 7- Check whether the year>0 and if all the conditions are
true then print date is valid.
STEP 8-End.
class Valid_Date
public static void main(int dd,int mm,int yy)
int k=0,e=0;
int m[]={30,28,31,30,31,30,31,31,30,31,30,31};
k=(yy%400==0)?yy%100==0?1:0:yy%4==0?1:0;
m[k]=m[k]+k;
if(mm<1 || mm>12)
{
e=1;
System.out.println(mm+"is invalid month");
if(dd<0 || dd>m[mm-1])
e=1;
System.out.println(dd+" is invalid date");
if(yy<1)
e=1;
System.out.println(yy+" is invalid year");
if(e==0)
System.out.println(dd+"-"+mm+"-"+yy+" is valid date");
Variable Description :-
NAME TYPE FUNCTION
dd int To store day
mm int To store month
yy int To store year
K int To test condition
Input and Output:-
Program-9
WAP 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");
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT IS");
n=Integer.parseInt(br.readLine());
System.out.print(n+"=");
for(a=n;a>1;)
if(a%b==0)
a/=b;
System.out.print(b+"*");
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 :-
Program-10
Program to print unique nos. between two limits . A
unique no. is one in which none of the digits gets
repeated.
Algorithm :-
STEP 1-Start
STEP 2-Create a function to accept a number
STEP 3-Create an array of 10 elements
STEP 4-Generate a loop which takes out each digit
of the number.
STEP 5-Update index no. of array Of which digit is Obtained
STEP 6-Check each element of array &check if
any>1
STEP 7-Return true if none>1&false if it is
STEP 8-Generate main() accepting limits a&b(STEP 9&10)
STEP 9-Check uniqueness of each Number
STEP 10-Print the unique no.
STEP 11-End
Class Unique
public static boolean uni(int n)
{
int f[]=new int[10],b;
boolean x=true;
for(b=n;b>0;b/=10)
f[b%10]++;
for(b=0;b<10;b++)
if(f[b]>1)
x=false;
return x;
public static void main(int a,int b)
for(int c=a;c<=b;c++)
if(uni(c)==true)
System.out.println(c+”is unique”);
Variable Description :-
Name Type Function
a Integer Lower limit
b Integer Upper limit
n Integer Number
f Integer Array
x Integer Stores result
c Integer Loop variable
Input and Output :-