Computer Project
Computer Project
NAME: Shakher
ROLL NUMBER:016
CLASS : XII
SECTION: B
SUBJECT
COMPUTER :
SCIENCE
ASSIGNMENT
SUBJECT
TEACHER:MR.
MANISH TRIPATHI
SCHOOL:CITY
MONTESSORI
SCHOOL
PREFACE
I have tried my level best to prepare this project by
sheer dedication of time to this creation.In this
project ,I have
depicted various programs based on the basic
concepts of programming such as
Inheritance,Operations on arrays,strings,operations
on numbers,functions and methods,object
passing,and programs based on patterns.
INDEX
Serial Category of Programs Number Of Page Number
Numbe Programs
r
1. ARRAYS 10 6-34
VARIABLE DESCRIPTION:-
class Spiral
{
public static void main()
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER");
n=Integer.parseInt(br.readLine());
int a[][]=new int[n][n],c=0,r=0,d=1,v=n;
a[r][c]=1;
a[n/2][n/2]=0;
for(c=1;a[n/2][n/2]==0;c++,r++)
{
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT IS");
while(c<v)
{
a[r][c]=++d;
c++;
}
c--;r++;
while(r<v)
{
a[r][c]=++d;
r++;
}
c--;r--;
while(c>=n-v)
{
a[r][c]=++d;
c--;
}
v--;c++;r--;
while(r>=n-v)
{
a[r][c]=++d;
r--;
}
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
System.out.print(a[r][c]+"\t");
System.out.println();
}
}
}
VARIABLE DESCRIPTION:-
VARIABLE DESCRIPTION:-
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 methords :-
date(int nd,int nn)-constructer to assing nd to ddand nnto 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];
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];
b++;
}
System.out.println("THE FUTURE DATE AFTER "+m+"DAYS IS"+dx+"/"+(b+1));
}
}
VARIABLE DESCRIPTION:-
INPUT/OUTPUT:-
import java.io.*;
class Matrix
{
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++)
{
System.out.println("ENTER THE NUMBER");
String y=br.readLine();
a[c][b]=Integer.parseInt(y);
}
}
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++)
{
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;
}
public void main(int q,int i)throws IOException
{
Matrix obm=new Matrix(q,i);
obm.readMatrix();
Matrix obj=new Matrix(q,i);
obj.readMatrix();
System.out.println("ADDED MATRIX IS");
Matrix obk=new Matrix(q,i);
addMatrix(obm,obj);
System.out.println("SUBSTRACTED MATRIX IS");
Matrix obn=new Matrix(q,i);
obk=subMatrix(obj);
obn.displayMatrix();
}
}
VARIABLE DESCRIPTION:-
INPUT&OUTPUT:-
class magic
{
public static void main(int n)
{
int a[][]=new int [n+1][n+1];
for(int x=0;x<(n+1);x++)
{
for(int y=0;y<(n+1);y++)
{
a[x][y]=0;
}
a[n-1][((n+2)/2)-1]=1;
int f=2;
int m=n-1,p=((n+2)/2)-1;
for(;f<=n*n;)
{
m=++m;
p=++p;
if((m==n)&&(p!=n)){
a[0][p]=f;f++;
m=0;
}
else if((m!=n)&&(p==n))
{
a[m][0]=f;
f++;
p=0;
}
else if((m==n)&&(p==n))
{
a[m-2][p-1]=f;f++;
m=m-2;
p=p-1;
}
else if(a[m][p]!=0)
{
a[m-2][p-1]=f;
f++;
m=m-2;
p=p-1;
}
else
{
a[m][p]=f;
f++;
}}
for( x=0;x<(n+1);x++)
{
for(int y=0;y<(n+1);y++)
{
if(a[x][y]!=0)
System.out.print(a[x][y]+" ");
}
System.out.println();
}
}
}
}
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
n Int To store the size of matrix.
a[] Int To store the matrix.
x,y Int Loop variable.
INPUT/OUTPUT:-
Algorithm:-
STEP 1- Start.
STEP 2-Initialise the array i.e. int a[ ]=new int[10].
STEP 3-Now declare the constructor arr( ) and within it
initialize 0 to all the values of array.
STEP 4-Now make another function input( ).
STEP 5-Initialise a loop from 0 to 9 i.e.
for(x=0;x<10;x++).
STEP 6-Within this loop accept the number by the user
and
store number in a[ ].
STEP 7-Now make another function arrange( ).
STEP 8-Within the function initialize a loop from 0 to 9
i.e.
for(x=0;x<10;x++) and initialise temp=0.
STEP 9-Run another loop from 0 to 8 i.e.
for(y=0;y<9;y++) and check if (a[y]%2==0 &&
a[y+1%2!=0).If true then
goto step 10.
STEP 10-Initialise temp=a[y], a[y]=a[y+1] and
a[y+1]=temp
and then close all the loops.
STEP 11-Initialise another loop for(x=0;x<10;x++) and
print
the values of a[x].
STEP 12-End.
import java.io.*;
class arr
{
int x;
int a[]=new int[10];
arr()
{
for(x=0;x<10;x++)
{
a[x]=0;
}
}
void input()throws IOException
{
int x;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
for(x=0;x<10;x++)
{
System.out.println("Enter any number");
a[x]=Integer.parseInt(br.readLine());
}
}
void arrange()
{
int x,y;
for(x=0;x<01;x++)
{
int temp=0;
for(y=0;y<9;y++)
{
if(a[y]%2==0&&a[y+1]%2!=0)
{
temp=a[y];
a[y]=a[y+1];
a[y+1]=temp;
}
}
}
for(x=0;x<10;x++)
{ System.out.println(a[x]);
}
}
}
VARIABLE DESCRIPTION:-
import java.io.*;
class diagonalsum
{
public static void main()throws IOException
{
int c=0,sum=0,sum1=0;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("ENTER THE INDEX FOR A SQUARE MATRIX");
int a=Integer.parseInt(br.readLine());
int [][]sq=new int [a][a];
for(int b=0;b<a;b++)
{
for(c=0;c<a;c++)
{
System.out.println("ENTER AN ELEMENT");
sq[b][c]=Integer.parseInt(br.readLine());
}
}
System.out.println();
for(int f=0;f<a;f++)
{
for(c=0;c<a;c++)
System.out.print(sq[f][c]+" ");
System.out.println();
}
System.out.println();
(for(c=0;c<a;c++)
sum=sum+sq[c][c];
int e=0;
for(c=a-1;c>=0;c--)
{
sum1=sum1+sq[e][c];
e++;
}
int x=0,y=0;
for(x=0;x<a;x++)
{
for(y=0;y<a;y++)
{
if(x==y || (x+y)==a-1)
System.out.print(sq[x][y]+" ");
else
System.out.print(" ");
}
System.out.println(" ");
}
System.out.println();
System.out.println("SUM OF FIRST DIAGONAL "+sum);
System.out.println("SUM OF SECOND DIAGONAL "+sum1);
System.out.println();
int sum2=0;
for(x=0;x<a;x++)
{
System.out.print("SUM OF ELEMENTS OF ROW "+(x+1)+":");
for(y=0;y<a;y++)
sum2=sum2+sq[x][y];
System.out.println(sum2);
sum2=0;
}
}
}
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
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:-
INPUT&OUTPUT:-
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:-
INPUT/OUTPUT:-
PROGRAMS
BASED
ON
FUNCTIONS ,
METHODS
AND
OBJECT
PASSING
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 ");
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-=.1*yi; s=(int)(.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");
System.out.println(" Monthly income\t\t\t:- "+mi);
VARIABLE DESCRIPTION:-
Name , post , city , String To enter the name, post, city, house
hno, str number, street
BP,SP,GIS,GPF,IT,CCA Integer To enter all the pays and
contributions
DA, HRA, GP, NP Integer To calculate the gross pay and net
pay
INPUT &
OUTPUT:-
VARIABLE DESCRIPTION:-
INPUT&OUTPUT:-
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:-
INPUT&OUTPUT:-
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));
Evenseries()
{
n=0;
}
void accept()throws IOException
{
System.out.println("ENTER THE NUMBER");
String y=br.readLine();
s=Integer.parseInt(y);
}
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:-
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");
String t=br.readLine();
x=Float.parseFloat(t);
System.out.println("ENTER THE VALUE OF Y");
t=br.readLine();
y=Float.parseFloat(t);
}
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;
}
public void main()throws IOException
{
Complex ob=new Complex();
Complex obj=new Complex();
ob.readComplex();
obj.readComplex();
Complex obk=new Complex();
obk.addComplex(ob,obj);
Complex obo=new Complex();
obo=mulComplex(obj);
obo.showComplex();
}
}
VARIABLE DESCRIPTION:-
INPUT&OUTPUT:-
VARIABLE DESCRIPTION:-
PROGRA
MS
BASED
ON
NUMBER
S
WRITE A PROGRAM TO PRINT TWIN PRIME NUMBERS
BETWEEN TO A LIMIT.
Algorithm:-
STEP 1- Start
STEP 2- Store the limit in variable a.
STEP 3- Set for loop i.e. for(k=1;k<=a;k+)
STEP 4- Inside a loop find out the total number of
factors of the limit i.e. a by using the condition if (a
%k==0) if true then goto Step-5.
STEP 5- Increase the counter by 1i.e. c++.
STEP 6- Check whether (c==2) if true then goto Step7
if false then got to Step8
STEP 7- Return true .
STEP 8- Return false.
STEP 9- Make another main method
STEP 10-Accept a lower limit lo and an upper limitu.
upto which the twin numbers are to be printed .
STEP 11- Set a loop i.e. for(b=lo ;b<u;b++)
STEP 12- Inside this loop call the pime functioni.e.
if(prime(b)&&prime(b+2))
STEP 13- If the upper condition is true then print b&
b+2.
STEP 14- End.
import java.io.*;
class Twin_Prime
{
public static boolean prime(int a)
{
int k,c=0;
for(k=1;k<=a;k++)
{
if(a%k==0)
c++;
}
if(c==2)
return true;
else
return false;
public static void main()throws IOException
{
int lo,up;
InputStreamReader i=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i);
System.out.println("Enter lower limit");
lo=Integer.parseInt(br.readLine());
System.out.println("Enter upper limit");
up=Integer.parseInt(br.readLine());
System.out.println("Twin Prime between "+lo+" to "+up+" are:-");
int b=0;
for(b=lo;b<=(up-2);b++)
{
if(prime(b)&&prime(b+2))
{
System.out.println(b+" "+(b+2));
}
}
System.out.println();
System.out.println();
}
}
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
k,b Integer For Loop Purpose
C Integer Counter variable
A Integer Reference variable for limits
Lo Integer Lower limit
Up Integer Upper limit
VARIABLE DESCRIPTION:-
INPUT/OUTPUT:-
A PROGRAM TO PRINT FIRST N PRIME NUMBERS.
Algorithm:-
STEP 1- Start.
STEP 2- Enter a number n.
STEP 3- Run a loop to check whether the counter=n or not.
STEP 4- Run a loop to check whether the no. is prime or not.
STEP 5- If the no. divisible only twice then print no. is prime.
STEP 6- Increment counter by 1 and go to Step3.
STEP 7- End.
class Prime
{
public static void main(int n)
{
int a,c=1,l=0,d=0;
System.out.println("The first "+n+" prime numbers are:");
while(l!=n)
{
for(a=1;a<=c;a++)
{
if(c%a= =0)
d++;
}
if(d= =2)
{
System.out.println(c);
l++;
}
d=0;
c++;
}
}
}
VARIABLE DESCRIPTION:-
INPUT/OUTPUT:-
A PROGRAM TO PRINT NUMERICAL VALUE OF A
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++)
{
ch=x.charAt(c);
for(d=0;d<a.length();d++)
{
if(ch= =a.charAt(d))
{
e=d;
g[c]=b[e];
}
}
}
for(f=0;f<g.length-1;f++)
{
i=g[f];
if(i<g[f+1])
{
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:-
INPUT/OUTPUT:-
INPUT&OUTPUT:-
PROGRAMS
BASED
ON
OPERATIONS
ON
STRINGS
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
Num int To enter a number
oc[] int An array to store frequency of digits
B int Loop variable
N int It stores the value of num
D int To store last digit of the number
Y int It stores the value of num
K int To initialize the elements with 0
S int To store sum of digits
INPUT/OUTPUT:-
PROGRAM TO CONVERT THE CASE OF 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
out the length of the String in the function proceed.
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.*;
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!=' ')
{
s=s+ch;
}
else if(ch==' ')
{
s=s+" ";
break;
}
}
}
}
}
str=" ";
str=s;
}
void disp()
{
System.out.println(str);
}
}
class alph
{
public static void main(String args[])throws Exception
{
stream ob=new stream(" ");
ob.getdata();
ob.proceed();
ob.disp();
}
}
VARIABLE DESCRIPTION:-
import java.io.*;
class StringMagic
{
String str;
public void input()throws IOException
{
InputStreamReader i=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i);
System.out.println("Enter String");
str=br.readLine();
}
public int pailin_count( )
{
int l=0,p=0,x=0,y=0,q=0;
String s1="",s2="";
str=str+" ";
l=str.length();
for(x=0;x<l;x++)
{
if(str.charAt(x)!=' ' && str.charAt(x+1)==' ')
{
s1=str.substring(p,x+1);
s1=s1.trim();
p=x+2;
for(y=(s1.length()-1);y>=0;y--)
{
s2=s2+s1.charAt(y);
}
if(s2.equals(s1))
{
q++;
}
s2="";
}
}
System.out.println("No. of palindrome words are"+" "+q);
return q;
}
public String wordssort( )
{
int l=0,p=0,x=0,y=0,q=0,sp=0,z=0,c=0,d=0,f=0,u=0;
String s1="",t=" ";
str=str+" ";
l=str.length( );
for(y=0;y<l;y++)
{
if(str.charAt(y)==' ')
{
sp++;
}
}
String s2[]=new String[sp];
int a[]=new int[sp];
for(x=0;x<l;x++)
{
if(str.charAt(x)!=' ' && str.charAt(x+1)==' ')
{
s1=str.substring(p,x+1);
s1=s1.trim( );
p=x+2;
s2[c]=s1;
a[c]=s1.length( );
c++;
}
}
for(d=1;d<str.length();d++)
{
for(f=0;f<sp;f++)
{
if(a[f]==d)
{
t=t+s2[f]+" ";
}
}
}
return t;
}
void disp( )
{
System.out.println("the Original String is\n "+str);
System.out.println("the Sorted String is\n "+wordssort());
}
}
VARIABLE DESCRIPTION:-
import java.io.*;
class permutation
{
public void main(String s)
{
int pt=1;
System.out.println("INPUT--");
System.out.println(s);
System.out.println("OUTPUT--");
int l=s.length();
char ch[]=new char[l];
char tmp1,tmp2;
for(int z=0;z<l;z++)
ch[z]=s.charAt(z);
for(int x=0;x<l;x++)
{
tmp2=ch[0];
ch[0]=ch[x];
ch[x]=tmp2;
for(int y=1;y<l;y++)
{
for(int z=1;z<l-1;z++)
{
tmp1=ch[z];
ch[z]=ch[z+1];
ch[z+1]=tmp1;
System.out.print(pt+".");
pt++;
for(int p=0;p<l;p++)
{
System.out.print(ch[p]);
}
System.out.println();
}
}
}
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
S String Inputted String
Ch Character Char array which stores character
string
L Integer Length of inputted string
x,y,z Integer For loop purpose
Temp Character For exchanging characters
import java.util.*;
import java.io.*;
class Union_Inter
{
public static void main() throws IOException
{
String a,b,u="",i="",t="",w="";
int f=0;
InputStreamReader ip=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ip);
System.out.println("Enter first string");
a=br.readLine();
System.out.println("Enter second string");
b=br.readLine();
u=a;
StringTokenizer bst=new StringTokenizer(b);
while(bst.hasMoreTokens())
{
t=bst.nextToken();
f=0;
StringTokenizer ast=new StringTokenizer(a);
while(ast.hasMoreTokens())
{
w=ast.nextToken();
if(t.compareTo(w)==0)
f=f+1;
}
if(f==0)
u=u+" "+t;
else
i=i+" "+t;
}
System.out.println("UNION-"+u);
System.out.println("INTERSECTION-"+i);
System.out.println();
System.out.println();
}
}
VARIABLE DESCRIPTION:-
NAME FUNCTION
TYPE
A String First string
B String Second string
T String Stores extracted words of second string
W String Stores extracted words of first string
U String Stores union of strings
I String Stores intersection of strings
INPUT/OUTPUT:-
A PROGRAM TO PRINT INITIALS OF A GIVEN OF A
GIVEN NAME.
e.g- Virendra Kumar Singh
V.K. Singh
Algorithm:-
STEP 1- Start.
STEP 2- Enter a String.
STEP 3- Remove spaces from front and back of the string.
STEP 4- Run a loop to extract a character.
STEP 5- Check whether the extracted character is a space
then store first character of the string.
STEP 6- Add the string.
STEP 7- End.
class Initials
{
public static void main(String s)
{
String a="",b="";
s=s.trim();
int n=s.length(),m;
for(m=0;m<n;m++)
{
char c=s.charAt(m);
if(c!=' ')
{
a=a+c;
}
else
{
b=b+a.charAt(0)+".";
a="";
}
}
b=b+a;
System.out.println("The initials of string "+s+" is "+b);
}
}
VARIABLE DESCRIPTION:-
INPUT/OUTPUT:-
Write a program to input a sentence and arrange its words according to increasing length . In
case length of two or more words is same , arrange them in alphabetical order. In case a word is
present more than one in the sentence , remove it from the resultant sentence .
ALGORITHM
1.Input the string and use the split() function to bifurcate each word as a separate array element.
3.Use Bubble Sort to arrange the sentence in order of increasing length of the words,length of
the words being different.
4.Use compareTo() function to arrange all the words in alphabetical order,if length of two or
more words is same.
6.Extract each array element and calculate its frequencies ,before and after the word ,
respectively.
If these frequencies come out to be zero,then add the words to form a new string.
CODE
class string
for(i=0;i<a.length;i++)
for(j=0;j<a.length-i-1;j++)
if(a[j].length()>a[j+1].length())
s1=a[j];
a[j]=a[j+1];
a[j+1]=s1;
else if((a[j].length())==(a[j+1].length()))
if(a[j].compareTo(a[j+1])>0)
s1=a[j];
a[j]=a[j+1];
a[j+1]=s1;
for(i=0;i<a.length;i++)
{
c=0;c1=0;
for(j=i-1;j>=0;j--)
if(a[i].equals(a[j]))
c++;
for(j=i+1;j<a.length;j++)
if(a[i].equals(a[j]))
c1++;
System.out.print(a[i]+" ");
VARIABLE DESCRIPTION
OUTPUTS
INPUT 1
THIS IS WHAT HE IS DOING
Accept a sentence in upper case which is terminated by either .,?or !.Each word of sentence
is separated by single blank space.Arrange all the letters of the words in alphabetical order ,and
replace all the recurring characters by a *and print it.
ALGORITHM
START
1.Input the string and extract each word from the string.
2.Compare each letter of the word with the alphabet one by one,and store it as a separate word.
STOP
CODE:
class String1
String s1="",s2="",s3="",s4="";
s=s+" ";
for(int i=0;i<s.length();i++)
char ch=s.charAt(i);
s1=s1+ch;
else
for(char a='A';a<='Z';a++)
{
for(int j=0;j<s1.length();j++)
char ch1=s1.charAt(j);
if(a==ch1)
s2=s2+ch1;
s3=s3+s2+" ";
s2="";
s1="";
System.out.println(s3);
for(int i=0;i<s3.length()-1;i++)
char a=s3.charAt(i);
System.out.print(a);
while(s3.charAt(i+1)==a)
System.out.print("*");
i++;
}
}
VARIABLE DESCRIPTION
OUTPUT:
PROGRAMS
BASED
ON
THE
CONCEPT
OF
INHERITANCE
THE DISTANCE BETWEEN TWO POINTS(X1,Y1)
AND (X2,Y2) IS GIVEN BY:
((X1-X2)^2+(Y1-Y2)^2)^1/2
TO DETERMINE IF A POINT LIES WITHIN A
GIVEN CIRCLE THE DISTANCE OF THE POINT
FROM THE CENTRE OF THE CIRCLE IS
COMPUTED FIRST AND THEN THIS DISTANCE IS
COMPARED WITH THE RADIUS OF THE CIRCLE.
A CLASS POINT DEFINES THE COORDINATES OF
A POINT WHILE ANOTHER CLASS CIRCLE
REPRESENTS THE RADIUS AND CENTRE OF A
CIRCLE. THE DETAILS -*OF BOTH THE CLASSES
ARE GIVEN BELOW:
CLASS NAME-POINT
DATA MEMBERS-
DOUBLE X, Y-TO STORE THE X AND Y COORDINATES
MEMBER METHODS-
POINT( )-CONSTRUCTOR TO ASSIGN 0 TO X AND Y
VOID READPOINT( )-READS THE COORDINATES OF A
POINT
VOID DISPLAYPOINT( )-DISPLAYS THE COORDINATES
OF A POINT
CLASS NAME-CIRCLE
DATA MEMBERS-
DOUBLE R-TO STORE THE RADIUS
POINT O-TO STORE THE COORDINATES OF THE
CENTRE
MEMBER METHODS-
CIRCLE( )-CONSTRUCTOR
VOID READCIRCLE( )-READS THE RADIUS AND CENTRE
OF A CIRCLE
VOID DISPLAYCIRCLE( )-DISPLAYS THE RADIUS AND
CENTRE OF A CIRCLE
INT CONTAINS(POINT P)-RETURNS 1 IF THE POINT P
LIES WITHIN THE CURRENT CIRCLE OBJECT,
OTHERWISE RETURNS 0
WAP TO SPECIFY THE CLASS POINT AND CIRCLE
GIVING THE DETAILS OF ALL THE ABOVE METHODS.
Algorithm:-
STEP 1:Start
STEP 2:In class Point1,declare double x & y.
STEP 3:In constructor Point1,assign 0 to x & y.
STEP 4:In readPoint1() function,enter the value of x & y.
STEP 5:In displayPoint1(),display x & y.
STEP 6:Make a class Circle that extends Point1.
STEP 7:Declare a Point1 object o and double r.
STEP 8:Make an unparameterized constructor to give r=0 &
call Point1().
STEP 9:Also o=new Point1().
STEP 10:Make a function readCircle() to accept radius of
circle & coordinates.
STEP 11:Make a function displayCircle(),display radius &
coordinates.
STEP 12:Make an int returning function contains() accepting
a Point1 object p.
STEP 13:Calculate the distance between points o and p.
STEP 14:If d<=r,return 1 else 0.
STEP 15:End.
import java.util.*;
class Point1
{
double x,y;
public Point1()
{
x=y=0.0;
}
public void readPoint1()
{
Scanner sc=new Scanner(System.in);
x=sc.nextDouble();
y=sc.nextDouble();
}
public void displayPoint1()
{
System.out.println("The coordinates are :"+x+","+y);
}
}
import java.util.*;
class Circle extends Point1
{
double r;
Point1 o;
Circle()
{
super();
r=0.0;
o=new Point1();
}
public void readCircle()throws Exception
{
Scanner sc1=new Scanner(System.in);
System.out.println("Enter the radius & coordinates of circle");
r=sc1.nextDouble();
o.readPoint1();
}
public void displayCircle()
{
System.out.println("radius= "+r+"& centre of circle = ");
o.displayPoint1();
}
public int Contains(Point1 p)
{
double d=Math.sqrt(Math.pow((o.x-p.x),2)+Math.pow((o.y-p.y),2));
if(d<=r)
return 1;
else
return 0;
}
public void main()throws Exception
{
readCircle();
displayCircle();
Point1 ob1=new Point1();
System.out.println("Enter coordinates of circle");
ob1.readPoint1();
int n=Contains(ob1);
if(n==1)
System.out.println("Point lies");
else
System.out.println("Point not lies");
}
}
VARIABLE DESCRIPTION:-
Algorithm:-
STEP 1:Start
STEP 2:In ISCScores class, declare an array of 6 rows & 2
columns.
STEP 3:In the constructor ISCScores(),accept marks in
subjects & store in I column.
STEP 4:Make an integer returning function Point to accept a
no. x.
STEP 5:Check n[x][0] for the range in which it lies & return
proper grade.
STEP 6:Make a class Best4 extending ISCScores.
STEP 7:In constructor Best4(),call ISCScores().
STEP 8:Make a function BestSubjects().
STEP 9:Using bubble sort,arrange the array in descending
order.
STEP 10:Make a loop from 0 to 5.
STEP 11:For each element,call the function Point() & store
points in II column.
STEP 12:Print the total of I column as total marks.
STEP 13:Print the first 4 rows of II column.
STEP 14:These will be the best 4 points.
STEP 15:End.Step 10:Make the main() function.
STEP 11:Print the names of days in one row.
STEP 12:Print those elements of array that are not 0.
STEP 13:End
import java.util.*;
class ISCScores
{
Scanner ob=new Scanner(System.in);
int n[][]=new int[6][2];
ISCScores()throws Exception
{
for(int y=0;y<6;y++)
{
System.out.println("Enter marks:");
n[y][0]=ob.nextInt();
}
}
int Point(int x)
{
if(n[x][0]>=90)
return 1;
else if(n[x][0]>=80)
return 2;
else if(n[x][0]>=70)
return 3;
else if(n[x][0]>=60)
return 4;
else if(n[x][0]>=50)
return 5;
else if(n[x][0]>=40)
return 6;
else
return 7;
}
}
import java.util.*;
class Best4 extends ISCScores
{
Scanner ob=new Scanner(System.in);
public Best4()throws Exception
{
super();
}
void BestSubjects()
{
int a,b,c,s=0;
for(a=0;a<6;a++)
{
for(b=0;b<5-a;b++)
{
if(n[b][0]<n[b+1][0])
{
c=n[b+1][0];
n[b+1][0]=n[b][0];
n[b][0]=c;
}
}
}
for(a=0;a<6;a++)
{
s+=n[a][0];
n[a][1]=Point(a);
}
System.out.println("Total marks="+s);
System.out.println("Top 4 points are:");
for(a=0;a<4;a++)
System.out.println(n[a][1]);
}
}
VARIABLE DESCRIPTION:-
class D2point
{
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;
}
}
VARIABLE DESCRIPTION:
PROGRAM
S
BASED
ON
PATTERNS
A PROGRAM TO PRINT FOLLOWING PATTERN:-
MADAM
A A
D D
A A
MADAM
Algorithm:-
STEP 1- Start
STEP 2- Enter the String
STEP 3- If counter=0 or it is equal to length-1 then print the
string
STEP 4- Print the character of the string and print spaces
until
length-2
STEP 5- Repeat Step4 until the length-1 of the string
STEP 6- If counter =length then go to Step3
STEP 7- End
class Pattern
{
public static void main(String s)
{
int l=s.length(),e=0,a,x;
for(a=0;a<l;a++)
{
if(e= =0||e= =l-1)
System.out.print(s);
else
{
System.out.print(s.charAt(a));
for(x=0;x<l-2;x++)
{
System.out.print(" ");
}
System.out.print(s.charAt(a));
}
e++;
System.out.println();
}
}
}
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
S String To enter String
L Int To store length of string
E Int Counter variable
X Int To run loop to print spaces
A Int To run loop to print string
INPUT/OUTPUT:-
VARIABLE DESCRIPTION:-
INPUT/OUTPUT:-
INPUT/OUTPUT:-
A PROGRAM TO PRINT FOLLOWING PATTERN:-
a
aa
aaa
aaaa
aaa
aa
a
Algorithm:-
STEP 1- Start.
STEP 2- Enter a number and a character.
STEP 3- Run a loop till n.
STEP 4- Run another loop until n-1 and print spaces.
STEP 5- Run another loop until Step3 and print character
with space.
STEP 6- Change the line.
STEP 7- Run reverse loop from n-1 to 1.
STEP 8- Run another loop till n-1 and print spaces.
STEP 9- Run a loop till Step8 and print character with
spaces.
STEP 10- Change the line and repeat Step3 till n.
STEP 11- End.
class Pattern2
{
public static void main(char e,int n)
{
int c=n-1,b,d,a;
for(a=1;a<=n;a++)
{
for(b=1;b<=c;b++)
System.out.print(" ");
for(d=1;d<=a;d++)
System.out.print(e+" ");
c--;
System.out.println();
}
c=c+2;
for(a=n-1;a>=1;a--)
{
for(b=1;b<=c;b++)
System.out.print(" ");
for(d=1;d<=a;d++)
System.out.print(e+" ");
c++;
System.out.println();
}
}
}
VARIABLE DESCRIPTION:-
INPUT/OUTPUT:-
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
A int To store first term of AP Series.
D int To store the common difference
of AP Series.
Sum double To store sum of AP Series.
I int Loop variable.
INPUT & OUTPUT:-