Computer 3

187
Prefa ce Java is a programming language originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation ) and released in 1995 as a core component of Sun Microsystems' Java platform. 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

Transcript of Computer 3

Page 1: Computer 3

PrefaceJava  is a programming language originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. 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.

ZAKWAN NASIR XII – C( 2011- 2012)

Page 2: Computer 3

Acknowledgement

I am helmed in all humbleness and gratefulness to acknowledge our depth to all those who have helped us to put these ideas, well above the level of simplicity and into something concrete. I am very thankful to our guide Mrs. RESHMA KHANNA for his valuable help. She was always there to show us the right track when we needed his help.

I also express my gratitude towards my parents and friends who provided me with innumerable suggestions and bright reflected ideas,. Their valuable support inspired and encouraged me throughout the development of this project.

I give my special thanks to the authors of various books which I consulted for this project.

ZAKWAN NASIR XII – C (ISC)

Index S.No PROGRAM

Page 3: Computer 3

1. Write a program to print the initials of the given name.

2. Write a program to input the names and print it in Alphabetical order of Surname.

3. A program to check whether the date is valid or not and print appropriate message.

4. Write a program to print the Even Series and find the smallest value of ‘n’.

5. Write a program to print the Pythagorian Triplets.

6.Program to input a string and print it in words.

7. Program to calculate the income tax.8. Write a program to input the time using the concept of objects and

print the difference between the two inputted time.9. A program to print all possible combination of a String.10. Write a program to input two Complex number,add them and also

multiply them.11. Write a program to input a String and check whether it is Palindrome

or Not. If not reverse till it become Palindrome.12. Write a program to print numerical value of entered Roman number.13. Write a program to input two Dates using the concept of objects and

compute the Sum of the two inputted Dates.14. Program to calculate the number of people visited the ticket booth

and whether they purchased the ticket or not.15. Write a program to print lucky numbers between two limits using the

concept of Modular Programming.16. Write a program to print the Prime Factors of the inputted Number.17. Program to print the sum of the AP Series and display the required

output.18. Write a program to input the coordinates of the points in Space and

compute the radius and Area of Circle.19. Write a program to input a number and print each digit in words and

that to in reverse Order.20. Write a program to input a Matrix and print its Sum and Difference.21. Write a program to take the total number of days in a month and the

first day of the month and print the required Calender of the Month.

Page 4: Computer 3

22. Write a program to change the case of inputted string and arrange it in alphabetical order.

23. Write a program input the date and print the future days.

24. Write a program to enter an array and check if it is wondrous square or not .

25. A program to.print the Circular Matrix.

26. Write a program to compute the Intersection and Union of two Strings.

27. Write a program to input a Array and perform Binary Search using Recursive Technique.

28. Write a program to input the ISC Scores and print the result of the best four Subjects.

29. Program to calculate the monthly bill of consumers.

30. Write a program to input text and Reverse it and print it without Punctuations except Space.

31. A program to perform various operations on strings in the class String Magic.

32. A program to print the Pascal’s Triangle of n inputted numbers.

33. Write a program to enter a String and check whether the string is ‘SNOWBALL’ or not.

34. Program to state the binomial theorem.

35. Write a program to sum and Difference of the given dates.

36. Write a program to print the unique number between two Limits.

37. Write a program to print the payroll of a employee.

38. Program to enter the coordinates of the point in space and find the distance between the points.

Page 5: Computer 3

Program

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

3.void notsold()-increament 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:-

Page 6: Computer 3

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

Page 7: Computer 3

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); }}Variable Description :-

Type Name Function

double

Amt

To store amount collected.

int no_ppl To store no.of people visiting.

int

k,d

To store no.of people who purchased ticket.

Page 8: Computer 3

Input and Output :-

Page 9: Computer 3

Program

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

Page 10: Computer 3

{ double x,a; public binomial() { x=0; a=0; } public long factorial(long f) { int x3; int ft=1; 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--;

Page 11: Computer 3

} return s; } public void main(int p) { double k; k=sum(p); System.out.print(k); } }Variable Description :-

NAME

TYPE

FUNCTION

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

Page 12: Computer 3
Page 13: Computer 3

Program

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.

Page 14: Computer 3

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

Page 15: Computer 3

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.

Page 16: Computer 3

Input and Output :-

Program

Page 17: Computer 3

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.

Page 18: Computer 3

STEP 5-Specify the methods in the main method.

STEP 6-End.

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

class D3point extends D2point { double z; public D3point() { z=0;

Page 19: Computer 3

} public D3point(double a,double b,double c) { super(a,b); z=c; } public double Distance3d(D3point b) { 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

Page 20: Computer 3

points.

result Double To store the distance between the points.

Input and Output :-

Program

Page 21: Computer 3

Program to input a string 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(less than 99)");int amt=Integer.parseInt(br.readLine());

Page 22: Computer 3

int a,b,c,y,z,g;String x[]={" ","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Ei

ghteen","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]);elseSystem.out.println(x[amt-9]);System.out.println("----------*----------*---------- ");System.out.println("----------*----------*---------- ");}}

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

Page 23: Computer 3

Input and Output :-

Program

Page 24: Computer 3

Define a class 'APSeries' with following specification

Class Name : APSeries

Data Member :

a - To store first term

d - To store common difference

Member Function :

APSeries() - To initialize value of a and d with 0

APSeries(double a,double d) - To initialize value of a and d with

parametric a and d

nTHTerm(int n) - To return nTH 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 haveassign 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. nTHTerm. Parameter is int n.We have return the nth term of A.P series in this function

STEP 6- We have made another return type function i.e. Sum and int ‘n’ is the

Page 25: Computer 3

parameter. In this function we have return the sum of A.P series

STEP 7- We have made a function i.e. Show Series and parameter is int n(Integer Type). In this we have print the nth term of series as well as Sum of AP series.

STEP 8- We have made main function of this class and print the A.P. series of required term.

STEP 9-End

import java.io.*;class APSeries{private double a,d;APSeries(){a = d = 0;}APSeries(double a,double d){this.a = a;this.d = d;}double nTHTerm(int n){return (a+(n-1)*d);}double Sum(int n){return (n*(a+nTHTerm(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++)

Page 26: Computer 3

{System.out.println(nTHTerm(i)+" ");}System.out.println("It's Sum will be : "+Sum(n));System.out.println("------------------------*-------------------------*----------------------");}}class testap{public static void main(String args[]) throws Exception{APSeries a = new APSeries(1,2);a.showSeries(5);}}

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 and Output :-

Page 27: Computer 3
Page 28: Computer 3

Program

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

Page 29: Computer 3

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'){

Page 30: Computer 3

System.out.println(" Sex\t\t\t\t:- Female");}elseSystem.out.println(" Sex\t\t\t\t:- Male");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)(.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);}}class tax{public static void main(String args[])throws Exception{System.out.println(" Program to calculate income tax ");System.out.println(" ------------------------------- ");taxC ob=new taxC();ob.getdata();int s=ob.incometax();ob.display(s);}}

Page 31: Computer 3

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

Page 32: Computer 3
Page 33: Computer 3

Program

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

Page 34: Computer 3

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

Page 35: Computer 3

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

Page 36: Computer 3

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

Program

Page 37: Computer 3

Program to print the payroll of the employee.

Algorithm:-

STEP 1-Start.

STEP 2-Enter the name, post, street, city, pin code, house number in the function get data.

STEP 3-Enter the basic pay , special pay, contribution to gpf, contribution to group scheme, monthly income tax deduction, compensatory allowance.

STEP 4-We have calculated gross pay and net pay according to given slab in the function pay.

STEP 5-We have print all the above calculated things and all the above entrances in the function display.

STEP 6-Use inheritance and made another class of name payroll and create main function and call all the function through object created in the class payrollC.

STEP 7-End.

import java.io.DataInputStream;class payrollC{String name,post,city,hno,str;int BP,SP,GPF,GIS,IT,CCA,DA,HRA,pin,GP,NP;void getdata()throws Exception{DataInputStream d=new DataInputStream(System.in);System.out.println(" Enter the following data for the employee ");System.out.println(" Name ");name=d.readLine();System.out.println(" Designation or post ");post=d.readLine();System.out.println(" House number ");hno=d.readLine();

Page 38: Computer 3

System.out.println(" Street ");str=d.readLine();System.out.println(" City ");city=d.readLine();System.out.println(" PIN code ");pin=Integer.parseInt(d.readLine());DA=0;HRA=0;GP=0;NP=0;System.out.println(" Monthly basic pay ");BP=Integer.parseInt(d.readLine());System.out.println(" Special pay ");SP=Integer.parseInt(d.readLine());System.out.println(" Contribution to GPF ");GPF=Integer.parseInt(d.readLine());System.out.println(" Contribution to Group Scheme ");GIS=Integer.parseInt(d.readLine());System.out.println(" Monthly Income Tax deduction ");IT=Integer.parseInt(d.readLine());System.out.println(" Compensatory Allowence ");CCA=Integer.parseInt(d.readLine());}void pay(){int bp=BP;if(bp<=3500)DA=114*bp/100;else if(bp>3500 && bp<=6000)DA=85*bp/100;elseDA=74*bp/100;if(bp<=1500)HRA=250;else if(bp>1500 && bp<=2800)

Page 39: Computer 3

HRA=450;else if(bp>2800 && bp<=3500)HRA=900;elseHRA=1000;GP=bp+SP+HRA+DA+CCA;NP=GP-(GPF+GIS+(IT*12));}void display(){pay();System.out.println(" Name :- "+name);System.out.println(" Designation or post :- "+post);System.out.println(" House number :- "+hno);System.out.println(" Street :- "+str);System.out.println(" City :- "+city);System.out.println(" PIN code :- "+pin);System.out.println("\n Monthly basic pay :- "+BP);System.out.println(" Special pay :- "+SP);System.out.println(" Contribution to GPF :- "+GPF);System.out.println(" Contribution to Group Scheme :- "+GIS);System.out.println(" Monthly Income Tax deduction :- "+IT);System.out.println(" Compensatory Allowence :- "+CCA);System.out.println(" Total deduction :- "+(GPF+GIS+IT));System.out.println(" Gross pay :- "+GP);System.out.println(" Net pay :- "+NP);}}class payroll{static void main(String args[])throws Exception{System.out.println(" Program to generate the payroll of an employee ");System.out.println(" ----------------------------------------------- ");payrollC ob=new payrollC();

Page 40: Computer 3

ob.getdata();ob.display();}}

Variable Description :-

NAME TYPE FUNCTION

Name , post , city , hno, str

String To enter the name, post, city, house 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 and Output :-

Program

Design a Class StringMagic to perform various operation on strings

Page 41: Computer 3

Class Name : StringMagic

Data Member

str : to store the string

Mamber functios

void input () : to accept a string

int pailin_count() : count the palindrome Words in str

String WordsSort() : Sort the str according to wordsr

length & Alpebatical orde

void display() : Display the orginal & sorted String

Algorithm:-

STEP 1-Start.

STEP 2-We have input the string i.e. String str.

STEP 3-We have made function int pallin count in which we have checked whether the inputted string have pallindrome words and if there are pallindrome words then we have count the pallindrome words.

STEP 4-We have made function Stringwordssort in which we have sorted the inputted string.We have done this process to sort the inputted string- 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();afor(y=0;y<l;y++){if(str.charAt(y)==' '){sp++;

Page 42: Computer 3

}}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;}STEP 5-We have made disp function in order to print the original string and sorted string.STEP 6-End.

import java.io.*;

Page 43: Computer 3

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

Page 44: Computer 3

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)

Page 45: Computer 3

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

NAME TYPE FUNCTION

str , s1 , s2 , t String To store the string.

x , d , f Int Loop variable.

a[] Int To a array

Input and Output :-

Page 46: Computer 3

Program

Page 47: Computer 3

Write a program to print all possible combinations of a string.

Algorithm:-

STEP 1- Start

STEP 2- Accept a string from a user.

STEP 3-Store the string in string variable ‘s’.

STEP 4- Find out the length of the string and store it in ‘l’ i.e. l=s.length().

STEP 5- Make a new character type array of length ‘l’i.e. char ch[]=new char[l].

STEP 6- Set afor loop from 0to l-1 i.e. for(x=0;x<=l-1;x++) and store each character of string in this array.

STEP 7- Set a for loop i.e. for(x=0;x<l;x++) .In this loop swap each & every character ofthe array with the first character of the same array ch[0]=ch[x].

STEP 8- Set another loop from 1 to less than l.inside this loop set another loop i.e. for(z=1;z<l-1;z++)

STEP 9- Inside the previous loop keep on swaping the adjacent characters.

STEP 10-After swaping adjacent characters print the whole array using loop in the previous Loop.

STEP 11-End.

import java.io.*;class permutation{public void main(String s){int pt=1;

Page 48: Computer 3

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

Page 49: Computer 3

s String Inputted String

ch Character

Char array which stores characters of inputted string

l Integer Length of inputted string

x,y,z Integer For loop purpose

temp Character For exchanging characters

Input and Output :-

Program

Page 50: Computer 3

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

Page 51: Computer 3

{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();}}

}

Page 52: Computer 3

Variable Description :-

NAME TYPE FUNCTION

a Integer Array variable

n Integer No of rows and columns of matrix

i,j Integer For variable purpose

t Integer Counter variable

r Integer Variable representing row

c Integer Variable representing column

Input and Output :-

Page 53: Computer 3
Page 54: Computer 3

Program

Program to print Pascal’s Triangle till n numbers.

Example :-

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

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;

Page 55: Computer 3

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

Page 56: Computer 3
Page 57: Computer 3

Program

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)

Page 58: Computer 3

{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

m[] Int An array to store last days of months

K Int To test condition

E Int Counter variable

Page 59: Computer 3

Input and Output :-

Program

Program to print the numerical value of the entered Roman Number.

Page 60: Computer 3

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

Page 61: Computer 3

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

NAME TYPE FUNCTION

X String To enter a string

A String To store roman nos. in the array

b[] Int To store corresponding values of roman nos

C Int Loop variable

Ch Char To extract character of string x

E Int Loop variable

F Int Loop variable

Page 62: Computer 3

N Int To sum up the numbers

I Int To store the element of g[]

G[] Int To evaluate roman nu

D Int Loop variable

Input and Output :-

Program

Program to print the initials of the given name .

Example :-

Input :- Virendra Kumar Singh

Page 63: Computer 3

Output :- 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{

Page 64: Computer 3

b=b+a.charAt(0)+".";a="";}}b=b+a;System.out.println("The initials of string "+s+" is "+b);}}Variable Description :-

NAME TYPE FUNCTION

S String to enter a string

a String to add total string

B String to store first letter of the string

N Int to store length of string

m Int loop variable

C Char to extract characters

Input and Output :-

Page 65: Computer 3

Program

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

Page 66: Computer 3

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;

Page 67: Computer 3

} 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 :-NAME TYPE FUNCTION

N long to store the value of integer n for which the 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.

Page 68: Computer 3

X integer To calculate the factorial.

A integer for looping.

F integer for looping.

Input and Output :-

Page 69: Computer 3

Program

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

Page 70: Computer 3

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 EACH

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

Page 71: Computer 3

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 writeen*/ 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])

Page 72: Computer 3

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

NAME TYPE FUNCTION

dd integer to store the date

mm integer to store the month

m integer to store the date number.

a integer To store array.

Page 73: Computer 3

s integer To return date number.

Input and Output :-

Page 74: Computer 3

Program

Write a program to enter two complex number using the concept of objects. Add these complex numbers and also multiply them. Make the program using the concept of modularity.

Page 75: Computer 3

Algorithm:-

STEP 1:Start

STEP 2:Create a constructor which initiases 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"); String t=br.readLine(); x=Float.parseFloat(t);

Page 76: Computer 3

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

Page 77: Computer 3

Variable Description :- NAME TYPE FUNCTION

x , y int stores the real and imaginary part respectively of complex number

a , b Complex Reference type object

Input and Output :-

Page 78: Computer 3
Page 79: Computer 3

Program

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.

Page 80: Computer 3

STEP 7: End.

import java .io.*;class Time { int hr,min,sec; Time() { hr=0; min=0; 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"); String s=br.readLine(); hr=Integer.parseInt(s); System.out.println("MIN"); s=br.readLine(); min=Integer.parseInt(s); System.out.println("SECOND"); s=br.readLine(); sec=Integer.parseInt(s); } 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) {

Page 81: Computer 3

ob.sec-=60; ob.min++; } if(ob.min>=60) { ob.min-=60; 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

Page 82: Computer 3

{

ob.min=ob.min-min;

}

ob.hr=ob.hr-hr;

return ob;

}

public void main()throws IOException

{

Time obj=new Time();

Time oj=new Time();

obj.accept();

oj=addtime(obj);

System.out.println(oj.hr+" "+oj.min+" "+oj.sec);

} }

Variable Description :-

NAME TYPE FUNCTION

hr Long to store the value of integer n for which the 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.

Page 83: Computer 3

a Integer for looping.

f Integer for looping.

Input and Output :-

Page 84: Computer 3
Page 85: Computer 3

Program

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.

Member function:Matrix(int r,intc) constructor to assign r to m and c to n and

Create the integer matrix.

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 substracted matrix by

Substracting it from current Matrix object.

Write a program to specify the class Matrix giving details of all member function.

main function need 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.

Page 86: Computer 3

STEP-4: To display the inputed 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{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(){

Page 87: Computer 3

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

Page 88: Computer 3

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

NAME TYPE FUNCTION

A Integer To store the array.

M Integer To store size of row dimension.

N Integer To store size of column dimension.

R Integer To store temporary value.

C Integer To store temporary value.

Page 89: Computer 3

Input and Output :-

Page 90: Computer 3

Program

A program to print each digit of a entered number in Words and that to in reverse order. You have to use the Recursion technique in displaying the given output.

Algorithm:-

STEP 1:Start.

STEP 2:Declare int n.

STEP 3:Make default constructor to initialize n=0.

STEP 4:Make a void function inpnum() to enter value of n.

STEP 5:Make a void function extDigit() to input a number a.

STEP 6:If a<=9,call NumToWords(a).

STEP 7:Else call NumToWords(a%10) & call extDigit(a/10)

STEP 8:Make function NumToWords() to input a number.

STEP 9:Using switch case,check the input number for 0 to 9.

STEP 10:Correspondingly print its number name

STEP 11:Make main() function & call inpNum() & extDigit().

STEP 12:End.

import java.util.*;class Convert{int n;Convert(){n=0;

Page 91: Computer 3

}public void inpNum()throws Exception{Scanner ob=new Scanner(System.in);System.out.println("Enter any number:");n=ob.nextInt();}public void extDigit(int a){if(a>9){NumToWords(a%10);extDigit(a/10);}elseNumToWords(a);}public void NumToWords(int x){String s="";switch(x){case 1:s="one";break;case 0:s="zero";break;case 2:s="two";break;case 3:s="three";break;case 4:

Page 92: Computer 3

s="four";break;case 5:s="five";break;case 6:s="six";

break;case 7:s="seven";break;case 8:s="eight";break;case 9:s="nine";break;}System.out.println(s);}public void main()throws Exception{inpNum();extDigit(n);}}

Variable Description :-

NAME Type FUNCTION

a Integer Store Location

n Integer Store Number

Page 93: Computer 3

s String Store Number Name

Input and Output :-

Page 94: Computer 3

Program

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

Page 95: Computer 3

{a/=b;System.out.print(b+"*");}elseb=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

Page 96: Computer 3

Input and Output :-

Page 97: Computer 3

Program

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

Page 98: Computer 3

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

Page 99: Computer 3

}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

n[][] Integer Array to store dates

Y Integer Loop variable

Page 100: Computer 3

Input and Output :-

Page 101: Computer 3

Program

A CLASS ISCSCORES DEFINES THE SCORES OF A CANDIDATE IN 6 SUBJECTS & ANOTHER CLASS BEST4 DEFINES THE BEST FOUR SUBJECTS.THE DETAILS OF

BOTH CLASSES ARE:

CLASS NAME - ISCSCORES

DATA MMEBERS-

INT N[6][2]- TO STORE MARKS OF SIX SUBJECT AND CODE(NUMERIC)

MEMBER METHODS-

ISCSCORES( ) - CONSTRUCTOR TO ACCEPT THE MARKS

INT POINT( ) - TO RETURN THE POINT IN EACH SUBJECT ACCORDING

TO THE FOLLOWING-

MARKS >= 90 - 1

80-89 - 2

70-79 - 3

60-69 - 4

50-59 - 5

40-49 - 6

BELOW 40 – 7

CLASS NAME -BEST4

MEMBER METHODS-

VOID BESTSUBJECT( ) - TO DISPLAY

THE TOTAL POINTS & BEST SUBJECT USING THE CONCEPT OF INHERITANCE.

Page 102: Computer 3

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.*;

Page 103: Computer 3

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

Page 104: Computer 3

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

Page 105: Computer 3

System.out.println("Top 4 points are:");

for(a=0;a<4;a++)

System.out.println(n[a][1]);

}

}

Variable Description :-

NAME Type FUNCTION

n[] Integer Array to store marks

y Integer Loop variable

x Integer Counter

A Integer Loop Variable

B Integer Loop Variable

C Integer Temporary Variable

S Integer Store sum

Page 106: Computer 3

Input and Output :-

Page 107: Computer 3

Program

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 WITHINA GIVEN CIRCLE THE DISTANCE OF THEPOINT 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

Page 108: Computer 3

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.

Page 109: Computer 3

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

Page 110: Computer 3

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;elsereturn 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 :-

Page 111: Computer 3

NAME Type FUNCTION

X Integer x coordinate

Y Integer y coordinate

R Integer Store radius

D Integer Store distance

N Integer Temporary

Input and Output :-

Page 112: Computer 3

Program

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)

Page 113: Computer 3

{int x=0,y=n;for(;x==0;){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 :-

Page 114: Computer 3

Program

Page 115: Computer 3

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 tolength 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++)

Page 116: Computer 3

{if(t[b][1].compareTo(t[b+1][1]>0){m=t[b+1][0];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 :-

Page 117: Computer 3
Page 118: Computer 3

Program

Program to print difference between two dates.

Algorithm :-

STEP 1-Start

STEP 2-Take instance variables dd,mm,yy of integer type

STEP 3-Define a constructor Date() to initialize dd,mm,yy to 0

STEP 4-Define a function Input() to take day no.,month & year as input.

STEP 5-Define a function difference (Date ob) of Date type to calculate difference between two dates.

STEP 6-Create an object dob of Date class

STEP 7-Calculate the difference of two dates and store it in dob object and return it.

STEP 8-Create main()method

STEP 9-Create objects ob1& ob2 of Date class.

STEP 10-Call Input()function

STEP 11-Call difference(ob1) &store the result in ob2.

STEP 12-Display the difference

STEP 13-End

import java.io.*;Class Date {int dd,mm,yy;public Date(){dd=0;

Page 119: Computer 3

mm=0;yy=0;}public void Input()throws IOException{BufferedReader br=new BufferedReader (new InputStreamReader(System.in));System.out.println(“Enter day no.”);dd=Integer.parseInt(br.readLine());System.out.println(“Enter month No.”);mm=Integer.parseInt(br.readLine());System.out.println(“Enter year”);yy=Integer.parseInt(br.readLine()); }public Date difference(Date ob){Date dob=new Date();If(ob.dd<dd){ob.dd+=30;ob.mm=ob.mm-1;}dob.dd=ob.dd-dd;if(ob.mm<mm){ob.mm=ob.mm+12;ob.yy=ob.yy-1;}dob.mm=ob.mm-mm;dob.yy=ob.yy-yy;return dob;}public void main()throws IOException {Date ob1=new Date();Date ob2=new Date();

Page 120: Computer 3

System.out.println(“Enter previous date”); Input();System.out.println(“Enter current date”); ob1.Input();ob2=difference(ob1);System.out.println(“Difference between two dates is”+ob2.dd+”Days”+ob.mm+”Months”+ob.yy+”Years”);}}Variable Description :-

Name Type Function

dd Integer Stores date

mm Integer Stores month

yy Integer Stores year

Input and Output :-

Page 121: Computer 3

Program

Page 122: Computer 3

Program to print the words of a text in reverse order without any punctuation marks other than blanks.

Algorithm :-

STEP 1-Input a string s.

STEP 2-Count the no. of words in s &store it in ‘a’.

STEP 3-String r[]=new String[a].

STEP 4-Generate loop to extract Word one by one.

STEP 5-Call rlt()to remove any Punctuation mark.

STEP 6-Reverse the string.

STEP 7-Define rlt(String m) with return type String.

STEP 8-Remove all punctuation marks.

STEP 9-End.

import java.util*;class Words{public void main(String s){StringTokenizer st=new StringTokenizer(s);int a=st.countTokens();String r[]=new String[a];int b;for(b=0;b<a;b++)r[b]=st.nextToken();for(b=0;b<a:b++){r[b]=rlt(r[b]);

Page 123: Computer 3

}for(b=a-1;b>0;b--)System.out.println(r[b]+” “);}public String rlt(String m){String n=””;int a=m.length();int f;char ch;for(int b=0;b<a;b++){ch=m.charAt(b);if(ch!=’;’&&ch!=’.’&&ch!=’:’ &&ch!=’.”)n=n+ch;}return n;}}Variable Description :-

Name Type Functions

a Integer Stores no. of words

in String s

s String Stores the string

b Integer Loop variable

r[] String Array containing

words of Strings

n String Temporary String

m String String to be passed

Page 124: Computer 3

in main()

Input and Output :-

Program

Page 125: Computer 3

Program to use recursive technique to search an element in an array using binary search.

Algorithm :-

STEP 1-Start.

STEP 2-Declare an array.

STEP 3-Make a function Binary that inputs three integer variables.

STEP 4-Check if lower limit is greater than higher one,return 0.

STEP 5-Initialize the mean of lower&upper limit to m.

STEP 6-Check if element is present at m,then return m.

STEP 7-If element is less than mth element,call

same function with upper limit m-1.

STEP 8-If element is more than mth element ,call

same function with lower limit m+1..

STEP 9-In main function,enter an array&element to be searched.

STEP 10-Initialize global array with new one

STEP 11-Call Binary function&print element not found if return is 0.

STEP 12-Else print the appropriate position.

STEP 13-End.

class Recbin{ int arr[];public int Binary(int a,int b,int c){int m=(b+c)/2;

Page 126: Computer 3

if(b>c)return 0;else if(a==arr[m])return m;else if(a<arr[m])return Binary(a,b,c-1);else return Binary(a,b+1,c);}public void main(int y[],int n){

int b=y.length,c;arr=new int[b];for(c=0;c<b;c++)arr[c]=y[c];if(Binary(n,0,b)==0)System.out.println(“Element not found”);elseSystem.out.println(n+”found at"+Binary(n,0,b)+1);}}

Variable Description :-Name Type Functions

arr Integer Global array

a Integer Stores element to

be searched

b Integer Loop variable

c Integer Loopvariable

Page 127: Computer 3

n Integer Stores element

to be

searched

y Integer Entered array

Stores middle

Input and Output :-

Program

Page 128: Computer 3

Program to print Pythagorean triplets between two limits.

Algorithm :-

STEP 1-Start

STEP 2-Enter the limits a&b

STEP 3-Declare numbers c,d,e

STEP 4-Generate loop from a-b in c(STEP 5-7)

STEP 5-Generate loop from c-b in d(STEP 6&7)

STEP 6-Generate loop from d-b in e

STEP 7-If c*c=d*d+e*e or d*d=c*c+e*e or e*e=d*d+c*c, print this triplet .

STEP 8-End

Class Pythagoras{public static void main(int a,int b){int c,d,e;for(c=a;c<=b;c++){for(e=d;e<=b;e++){if(c*c==d*d+e*e)System.out.println(c+”,”+d+”,”+”are Pythagorian triplets”);if(d*d==c*c+e*e)System.out.println(d+”’”+c+”’”+”are Pythagorian triplets”);if(e*e==c*c+d*d)System.out.println(e+”,”+c+”,”+”are Pythagorian triplets”); }

Page 129: Computer 3

}}}}Variable Description :-Name Type Functions

a Integer Lower limit

b Integer Upper limit

c Integer Loop variable

d Integer Loop variable

e Integer Loop variable

Input and Output :-

Program

Page 130: Computer 3

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.

import java .io.*;class Time

Page 131: Computer 3

{ int hr,min,sec; Time() { hr=0; min=0; 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"); String s=br.readLine(); hr=Integer.parseInt(s); System.out.println("MIN"); s=br.readLine(); min=Integer.parseInt(s); System.out.println("SECOND"); s=br.readLine(); sec=Integer.parseInt(s); } 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) {

Page 132: Computer 3

ob.min-=60; 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;

}

public void main()throws IOException

{

Time obj=new Time();

Page 133: Computer 3

Time oj=new Time();

obj.accept();

oj=addtime(obj);

System.out.println(oj.hr+" "+oj.min+" "+oj.sec);

}

Variable Description :-

NAME TYPE FUNCTION

hr Long to store the value of integer n for which the 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 :-

Page 134: Computer 3
Page 135: Computer 3

Program

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

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;

Page 136: Computer 3

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

Page 137: Computer 3

Input and Output :-

Page 138: Computer 3

Program

WRITE A PROGRAM TO PRINT THE UNION AND INTERSECTION OF TWO STRINGS.

Algorithm :-

STEP 1-Start

STEP 2-int f=0,u=””,t=””,w=””,i=””.

STEP 3-Accept the string in the variable a & b.

STEP 4-If there are some tokens left,goto step 5 else goto step 7.

STEP 5-w=ast.NextToken().

STEP 6-if(t.compareTo(w)==0)then f=f+1.

STEP 7-if f=0 then goto step 8 otherwise goto step 9.

STEP 8-u+” “+t.

STEP 9-i=i+” “+t.

STEP 10-Display union is ‘u’.

STEP 11-Display intersection is i.

STEP 12-End.

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

Page 139: Computer 3

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;elsei=i+" "+t;}System.out.println("UNION-"+u);System.out.println("INTERSECTION-"+i);System.out.println();System.out.println();}}

Variable Description :- NAME TYPE FUNCTION

a String First string

b String Second string

Page 140: Computer 3

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 and Output :-

Page 141: Computer 3
Page 142: Computer 3

Program

WAP TO CHECK WHETHER THE SENTENCE IS SNOWBALL OR NOT.

CLASS NAME – SNOWBALL

DATA MEMBER – String SENTENCE

MEMBER METHODS –

(i)SNOWBALL()- CONSTRUCTOR TO ASSIGN NULL TO STR

(ii)READ()- TO TAKE STR FROM USER.

(iii)DISPLAY() – TO DISPLAT IT ON THE SCREEN.

(iv)CHECK() – RETURNS 1 IF THE SENTENCE IS SNOWBALL

OTHERWISE 0.

(v)SPECIFY THE CLASS ‘SNOWBALL’ ALONG WITH MAIN METHOD .

Algorithm-

STEP 1- START

STEP 2- TAKE THE SENTENCE INTO THE STRING VARIABLE “SENTENCE”.

STEP 3- MAKE A FUNCTION ‘READ’ TO TAKE THE DATA IN STRING “SENTENCE”.

STEP 4- MAKE ANOTHER FUNCTION ‘DISPLAY’ TO PRINT THE SENTENCE ON THE SCREEN.

STEP 5- FOLLOWING MAKE ANOTHER FUNCTION “CHECK” WHERE WE ARE CHECKING THAT WHETHER THE NUMBER OF WORDS ARE EQUAL TO THEIR POSITION.

STEP 6- IN THIS FUNCTION FIRST SPLIT THE STR & THEN COMPARE WHETHER THE LENGTH OF THE EACH WORD IS EQUAL TO THEIR SUBSCRIPT NO.+1. IF SO THEN RETURN 1 OTHERWISE 0.

STEP 7-THEN MAKE THE MAIN FUNCTION IN WHICH WE ARE CHECKING WHETHER SENTENCE IS SNOWBALL OR NOT.

Page 143: Computer 3

STEP 8- STORE THE RETURNING VALUE IN A VARIABLE. IF ITS VALUE IS EQUAL TO 1 THEN PRINT THAT THE SENTENCE IS SNOWBALL OTHERWISE NOT

STEP 9-END.

import java.io.*;class snowBall{String sentence;public snowBall(){sentence="";}void read()throws Exception{DataInputStream c=new DataInputStream(System.in);System.out.println("Enter the string.");sentence=c.readLine();}void display(){System.out.println("the inputted String-"+sentence);}int check(){String t[];t=sentence.split(" ");int i,j,f=0;for(i=0;i<t.length;i++){j=t[i].length();if(j!=i+1)f++;}if(f==0)

Page 144: Computer 3

return(1);else return(0);}void main()throws Exception{read();int ii=check();if(ii==1)System.out.println("SnowBall");else System.out.println("Snow is not ball.");}}

Variable Description :-

Name Type Function

c string to take str from user

t[] string Stores splitted str

i int initialization

j int initialization

f int Increment variable

ii int Stores returning var.

Program

Page 145: Computer 3

WAP TO MAKE A FILE ON THE HARD DISK & STORE IN IT THE ‘N’ NO. OF TEN NAMES.

Algorithm:-

STEP 1- START.

STEP 2- FIRST OF ALL MAKE THE FILE ON THE HARD DISK IN WHICH THE DATA WILL GET STORED.

STEP 3-TAKE THE VALUE OF THE ‘N’ FROM THE USER IN WHICH WE WILL COME TO KNOW THAT HOW MANY NAMES WILL BE WRITTEN IN THE FILE.

STEP 4- AFTER THAT TAKE THE NAMES FROM THE USER & AT THE SAME TIME WRITE IT INTO THE FILE

WITH THE PATTERN-FILEPRINTER OBJECT.println(var)

STEP 5 – AT LAST CLOSE THE FILE.

import java.io.*;class kill{public static void main()throws Exception{DataInputStream c=new DataInputStream(System.in);FileWriter fw=new FileWriter("rachna.txt");BufferedWriter bw=new BufferedWriter(fw);PrintWriter pw=new PrintWriter(bw);System.out.println("how many names do you want to enter?");int n=Integer.parseInt(c.readLine());System.out.println("enter "+n+ "number of names.");String s[]=new String[n];int i;for(i=0;i<n;i++){s[i]=c.readLine();

Page 146: Computer 3

}for(i=0;i<n;i++){pw.println(s[i]);}pw.close();bw.close();fw.close();}}

Variable Description :-

-

Name Type Function

n int to store no. of names

s[] String array to store names.

i int initialization

Program

WAP TO READ THE FILE FROM THE HARD DISK & PRINT THOSE NAME WHICH ARE PALINDROME.

Page 147: Computer 3

Algorithm:-

STEP 1- START

STEP 2- READ THE FILE FROM THE HARD DISK & STORE IT INTO THE STRING ARRAY A[].

STEP 3- TAKE TWO NESTED LOOPS WHICH WILL START IN SUCH A WAY THAT 1st ONE STARTS FROM 0 & THE 2nd ONE STARTS FROM LAST INDEX

STEP 4- STORE THE CHARACTERS COMING OUT FROM 1st LOOP IN CH1 & THE CHARACTER COMING OUT FROM 2nd LOOP IN CH2.

STEP 5- IN THIS RUN IF CH1 IS NOT EQUAL TO CH2

THEN INCREMENT THE ‘F’ VARIABLE OTHERWISE NOT.

STEP 6- IF ‘F’ RECORDS NO INCREMENT THEN PRINT THAT THE READ NAME IS OF PALINROME TYPE OTHERWISE NOT.

STEP 7-END.

import java.io.*;class teen{public static void main()throws Exception{FileReader fr=new FileReader("rachna.txt");BufferedReader br=new BufferedReader(fr);System.out.println("the name which are palindrome-");String a[]=new String[10];int i,f=0,j,k;char ch1,ch2;for(i=0;i<10;i++){a[i]=br.readLine();}try{

Page 148: Computer 3

for(i=0;i<10;i++){for(j=0;j<a[i].length();j++){for(k=a[i].length()-1;k>=0;k--){ch1=a[i].charAt(j);ch2=a[i].charAt(k);if(ch1!=ch2)f++;}}if(f==0)System.out.println(a[i]);}}catch(NullPointerException rr){System.out.println("end of file.");}br.close();fr.close();}}Variable Description :- Name Type Function

a[] string array to store names from file.

i int initialization

j int initialization

f int Checking inc. variable

k int initialization

ch1 char to store characters

Page 149: Computer 3

ch2 char to store characters

Program

PROGRAM TO INPUT A LIMIT AND PRINT THE

LUCKY NUMBERS

Algorithm :-

STEP 1-Start

Page 150: Computer 3

STEP 2-Accept the limit in variable ‘n’

STEP 3-Initialise b=2,t=n,g=1,m=2,p=0,hr=0,gt=0.

STEP 4-Declare a double dimension array i.e. int a[][]=new int[n/2][n]

STEP 5-Run a for loop from 1 to n i.e.for(x=1;x<=n;x++) and store the Values of x in a[0][x-1]

STEP 6-Run a while loop i.e while(b<=t) and initialize f=b-1,h=0.

STEP 7-Run a for loop from 0 to (t-1) i.e for(int y=0;y<t;y++) within the Above loop

STEP 8-Check if (y!=f).If the condition is true then store the value of A[g-1][y] in a[g][h] and increase h by 1 and initialize p=h Otherwise goto step9

STEP 9-Add the values of ‘m’ in ‘f’ and close the for loop

STEP 10-Increase the value of ‘b’, ‘g’ and ‘m’ by 1 and initialize t=p and Hr=g-1 and close the while loop.

STEP 11-Run a for loop from 0 to (t-1) i.e. for(gt=0;gt<t;gt++) and print The values of a[hr][gt].

STEP 12-End.

import java.io.*;class lucky{public void main(int n){int b=2,t=n,g=1,m=2,p=0,hr=0,gt=0;int a[][]=new int[n/2][n];for(int x=1;x<=n;x++)a[0][x-1]=x;while(b<=t){int f=b-1,h=0;

Page 151: Computer 3

for(int y=0;y<t;y++){if(y!=f){a[g][h]=a[g-1][y];h++;p=h;}elsef=f+m;}b++;g++;t=p;m++;hr=g-1;}for(gt=0;gt<t;gt++)System.out.print(a[hr][gt]+ “ “);}}

Variable Description :- NAME TYPE FUNCTION

b,t,g,m,f,h Integer Counter variable

gt,x,y, Integer Loop variable

a[][] Integer Storing the lucky nos.

p Integer Storing the no. of rows

hr Integer Position of row where lucky nos. are stored

Page 152: Computer 3

Input and Output :-

Program

Write a program to enter an Array of that size and check if it is WONDROUS SQUARE or not.

Algorithm :-

STEP: 1 Start.

STEP: 2 Enter a number.

STEP: 3 Enter array

STEP: 4 Store all the numbers in another array and sort it.

Page 153: Computer 3

STEP: 5 Check if it contains numbers from 1 – n*n.

STEP: 6 Check if sum of each row and column is equal.

STEP: 7 After executing step 5 and step 6 display suitable messages.

STEP: 8 Stop.

import java.io.*;class wondorousq { public static void main(int n)throws Exception { DataInputStream c=new DataInputStream(System.in); int i,j,sum=0,sums=0,p=0,t=0,r=0,rs=0,k=0; int ar[][]=new int[n][n]; int a[]=new int[n*n]; int u[]=new int[2*n]; System.out.println("Enter array"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { ar[i][j]=Integer.parseInt(c.readLine()); } } System.out.println("Array is"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { a[p++]=ar[i][j]; System.out.print(ar[i][j]+" "); } System.out.println();

Page 154: Computer 3

} for(i=0;i<p;i++) { for(j=0;j<p-i-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(i=0;i<p;i++) System.out.println(a[i]); for(i=0;i<p;i++) { if(a[i]==(i+1)) continue; else { r=1; break; } } for(i=0;i<n;i++) { sum=0; sums=0; for(j=0;j<n;j++) { sum=sum+ar[i][j]; sums=sums+ar[j][i]; } u[k]=sum;

Page 155: Computer 3

k++; u[k]=sums; k++; }

for(i=0;i<2*n;i++) { for(j=0;j<2*n-1;j++) { if(u[i]==u[j+1]) continue; else { rs=1; break; } } } if(r==1||rs==1) System.out.println("It is not a wondorous square"); else System.out.println("It is a wondorous square"); }} Variable Description :-

Name Type

n Integer

i Integer

j Integer

sum Integer

sums Integer

Page 156: Computer 3

p Integer

t Integer

r Integer

k Integer

rs Integer

n Integer

ar Integer

a Integer

u Integer

Input and Output :-

Page 157: Computer 3
Page 158: Computer 3

ZAKWAN NASIR XII – C

COMPUTER

APPLICATION

PROJECT

(2011-2012)