Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Computer Science Project

Session 2022-2023

Name – Shaurya Saxena


Class – XI – A
Acknowledgement

I would like to express my special thanks of gratitude


to my teacher “Swati Chaturvedi Ma’am” who gave me
the golden opportunity to do this wonderful project
which also helped me in doing a lot of research and I
came to know about so many new things. I am really
thankful to them.
Index

Sr. No. Title Page no.

1. Program 1

2. Program 2
3. Program 3

4. Program 4

5. Program 5

6. Program 6

7. Program 7

8. Program 8
9. Program 9

10. Program 10

11. Program 11

12. Program 12

13. Program 13

14. Program 14

15. Program 15

16. Program 16

17. Program 17

18. Program 18

19. Program 19

20. Program 20

21. Program 21

22. Program 22

23. Program 23

24. Program 24

25. Program 25
PROGRAMS

1)A class Mixer has been defined to merge two sorted integer arrays in ascending order.
Some of the members of the class are given below:
Class name : Mixer
Data members/instance variables:
int arr[ ] : to store the elements of an array
int n : to store the size of the array
Member functions:
Mixer(int nn) : constructor to assign n=nn
void accept( ) : to accept the elements of the array in ascending
order without any duplicates
Mixer mix(Mixer A) : to merge the current object array elements with the parameterized
array elements and return the resultant object
void display( ) : to display the elements of the array
Specify the class Mixer, giving details of the constructor(int), void accept( ), Mixer
mix(Mixer) and void display( ) . Define the main( ) function to create an object
and call the function accordingly to enable the task.
CODE:
import java.util.*;
class Mixer
{ int arr[ ];
int n;
static Scanner sc=new Scanner(System.in);
Mixer(int nn)
{ n=nn;
arr=new int[n];
}
void accept( )
{ System.out.println("Enter "+ n+ " elements in ascending order");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
}
Mixer mix( Mixer A)
{ Mixer B=new Mixer(n+A.n);
int x=0,y=0,z=0;
while(x<n && y<A.n)
{ if (arr[x] > A.arr[y])
{ B.arr[z]=A.arr[y];
y++;
}
Else
{ B.arr[y]=arr[x];
x++;
}
z++;
}
while(x<n)
B.arr[z++]=arr[x++];
while(y<A.n)
B.arr[z++]=A.arr[y++];
return B;
}
void display()
{ for(int i=0;i<n;i++)
System.out.println(arr[i]);
}
static void main()
{ Mixer P=new Mixer(4);
Mixer Q=new Mixer(3);
Mixer R=new Mixer(7);
P.accept();
Q.accept();
R=P.mix(Q);
R.display();
}}
ALGORITHM:
(i)Start
(ii)Enter the sorted arrays
(iii)Merge the arrays
(iv)Print the merged array
(v)End
VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
2) A sequence of fibonacci strings is generated as follows:
S0= “a”, S1= “b”, Sn = S(n-1) + S(n -2) where ‘+’ denotes concatenation. Thus the
sequence is:
a, b, ba, bab, babba, babbabab, ......... n terms.
Design a class FiboString to generate fibonacci strings. Some of the members of the
class
are given below:
Class name : FiboString
Data members/instance variables:
x : to store the first string
y : to store the second string
z : to store the concatenation of the previous two strings
n : to store the number of terms
Member functions/methods:
FiboString( ) : constructor to assign x=“a”, y=“b” and z=“ba”
void accept( ) : to accept the number of terms ‘n’
void generate( ) : to generate and print the fibonacci strings. The
sum of (‘+’ ie concatenation) first two strings is
the third string. Eg. “a” is first string, “b” is
second string then the third will be “ba”, and
fourth will be “bab” and so on.

Specify the class FiboString, giving details of the constructor( ), void accept( ) and
void generate( ). Define the main( ) function to create an object and call the functions
accordingly to enable the task.

CODE:
import java.util.*;
class FiboString
{ String x,y,z;
int n;
FiboString( )
{
x="a";
y="b";
z="ba";
}
void accept ()
{
Scanner Sc = new Scanner (System.in);
System.out.println ("Enter number of terms") ;
n = Sc.nextInt( );
}
void generate()
{ System.out.print(x+","+y);
for(int i=0;i<=n-2;i++)
{ System.out.print(","+z);
x=y;
y=z;
z=y+x; OR z= y.concat(x);
}
}
static void main( )
{ FiboString obj=new FiboString( );
obj.accept( );
obj.generate();
}
}
ALGORITHM:
(i) Start
(ii) Enter the no. of terms of Fibonacci series
(iii) Print the series
(iv) Call all the functions in the main function.
(v) End

VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
3.Program to count number of vowels in a sentence.
import java.util.*;
class count{
String str;
count(){
str=””}
void accept(){
Scanner sc=new Scanner(System.in);
System.out.rintln(“Enter sentence”);
str=sc.nextLine();}
int CountVowels(String w){
String v=”AEIOU”;
int c=0;
for(int i=0;i<w.length();i++){
char ch=w.charAt(i);
if(w.indexOf(ch)!==-1)
C++;}
return c;}
void display(){
String Tokenizer st=new String Tokenizer(str);
int m=sc.nextToken();
System.out.println(w+”/t”+countVowels(w));}
public static void main(String args[]){
count ob=new count();
ob.accept();
ob.display();
}
}

ALGORITHIM

Step1-Start
Step2-Accepting sentence by the user from the variable str.
Step3-Counting the number of vowels in the text entered.
Step4-Displaying the number vowels
Step5-End
VALUE DATATYPE CHART
VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
4.Program to accept a message in such a way that the program prints the
characters 13 positions ahead
import java.util.*;
class Encryption{
String mssg;
Encryption(){
mssg=””;
emssg=””;}
void read(){
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a sentence”);
mssg=sc.nextLine();}
String rotate(String s){
String t=””;
for(int i=0;s.length();i++){
char ch=s.charAt(i);
int m=(int)ch+13;
if((m>90&&m<=103)||m>122)
m=m-26;
t=t+(char)m;}
return t;}
void encrypt(){
String Tokenizer st=new String Tokenzier(mssg);
int m=st.countTOkens();
for(int i=1;i<=m;i++){
String w=st.countTokens();
for(int i=1;i<=m;i++){
String w=st.nextTokens();
emssg=emssg+rotate(3(w)+””;
}
}
void disply(){
System.out.pritnln(“original mssg=”+mssg);
System out.pritnln(“Encrypted=”+emssg);}
public static void main(String args[]){
ncryption ob=new Encryption();
ob.read();
ob.display();
}
}
ALGORITHIM

Step 1-Start
Step 2-Accept message from the user in the variable mssg
Step 3-Encrypt characters by shifting 13 positions and store it in the variable
emssg
Step 4-Print Encrypted Message ie the value stored in emssg
Step 5-End

VALUE DATATYPE CHART


VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
5.Program to print A word in the following format

A
CAT
UCATI
DUCAT IO
EDUCAT ION

import jav.util.*;
class pattern{
Sacanner sc=new Scanner(System.in);
System.out.println(“Enter Your Word”);
String w=sc.nextLine();
int a=w.length()/2;a=a+1;
for (int i=0;i<=w.length()/2;i++)
{
for(intj=i;j<=w.length()/2;j++)
b=b+1
System.out.print(“”);
System.out.println(“w.substring(a,b));
a--;
b++;
}

ALGORITHIM
Step1-Start
Step 2-Accept the Word from the user in the variable w
Step3-Print the word according to the conditions in the loop ie in
the form a pyramid.
Step4-End
VALUE DATATYPE CHART
VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
6)Write a program to enter city names and city codes in two separate single dimensional
arrays. According to the user’s choice, either enter city name and find the city code for
that city name or enter the city code and find the name of the city. Display appropriate
message if the name/code not found.

Data Members :
n - size of array
scode[] - array for storing city codes
city[] - array for storing the names of the city

Members Methods :
void city() - for finding the city name using city code
void code() - for finding the city code using the name of the city
CODE:
import java.io.*;
class std
{
static int n;static int scode[];static String city[];
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws IOException
{
System.out.println("Enter number of city names");
n = Integer.parseInt(br.readLine());
scode = new int[n];
city = new String[n];
System.out.println("Enter city name alongwith std code");
for(int i=0;i<n;i++)
{
city[i] = br.readLine();
scode[i] = Integer.parseInt(br.readLine());
}
System.out.println("Enter choice");
System.out.println("1. enter the code and print the city name");
System.out.println("2. enter city name and print the city code");
int choice = Integer.parseInt(br.readLine());
std obj = new std();
switch(choice)
{
case 1:
obj.city();break;
case 2:
obj.code();break;
}
}
void city()throws IOException
{
System.out.println("Enter code of the city");
int code = Integer.parseInt(br.readLine());boolean b=false;
for(int i=0;i<n;i++)
if(code == scode[i])
{ System.out.println("City name = "+city[i]); b=true;}
if(b == false)
System.out.println("City not found");
}
void code()throws IOException
{
System.out.println("Enter name of the city");
String c = br.readLine();boolean b=false;
for(int i=0;i<n;i++)
if(c.equalsIgnoreCase(city[i]))
{ System.out.println("City code = "+scode[i]); b=true;}
if(b == false)
System.out.println("Code not found");
}
ALGORITHM:
(i) Start
(ii) Accept the number of city names to be entered
(iii) Accept the names and codes of the cities and store in the array.
(iv) Ask for user’s choice either to enter city name and find the city code for that city name
or enter the city code and find the name of the city.
(v) If user asks for city name using city code then find whether the city code exists or not. If
exists then print the city name on that index.
(vi) If user asks for city code using city name then find whether the city name exists or not. If
exists then print the city code on that index.
End

VALUE DATATYPE CHART


VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
7)A class dec_bin has been defined to convert a decimal number into its equivalent binary
number. Some of the members of the class are given below:
Class name - dec_bin

Data members:
n - stores the decimal number
s - stores the binary equivalent of the number

Member functions:
dec_bin() - constructor to assign initial value to the data members
void getdata() - to accept the value of n.
void recursive(int) - to calculate the binary equivalent of ‘n’ using the recursive technique.
void putdata() - to display the decimal number ‘n’ and its binary equivalent.

Specify the class dec_bin, giving details of the constructor and the functions,
void getdata(), void recursive(int) and void putdata().
CODE:
import java.io.*;
class dec_bin
{
int n;int s;int i;
dec_bin()
{
n=0;s=0;i=0;
}//default constructor
void getdata()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
n = Integer.parseInt(br.readLine());
recursive(n);
}
void recursive(int x)
{
int z=0;
if(x>0)
{
z = x%2;
recursive(x/2);
s = s*10+z;
}
}//for decimal binary conversion using recursion
void putdata()
{
System.out.println("Original number = "+n);
System.out.println("Binary equivalent = "+s);
}
public static void main(String args[])throws IOException
{
dec_bin obj = new dec_bin();
obj.getdata();
obj.putdata();
ALGORITHM:
(i) Start
(ii) Accept a number from the user.
(iii) Convert the number using recursion into its binary equivalent.
(iv) Print the original number and its binary equivalent.
(v)End
VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
8)A class dec_oct has been defined to convert a decimal number into its equivalent octal
number. Some of the members of the class are given below.
Class name - dec_oct
Data members / instance variables:
n - stores the decimal number
oct - stores the octal equivalent number
Member functions :
dec_oct() - a constructor to initialize the data members n=0, oct=0.
void getnum(intnn) - assig nn to n
void deci_oct ( ) - calculates the octal equivalent of ‘n’ and stores it in oct using the
Recursive Technique.
void show() - displays the decimal number ‘n’, calls the function deci_oct() and displays its
octal equivalent.

Specify the class dec_oct, giving details of the constructor(), void getnum(int ), void
deci_oct(), void show (). Also define a main( ) function to create an object and call the
functions accordingly to enable the task.
CODE:
import java.util.*;
class dec_oct
{
int n;int oct;
dec_oct()
{
n=0;oct=0;
}//default constructor
void getnum(int nn)
{
n=nn;
}
void octal(int x)
{
int z;
if(x>0)
{
z = x%8;
octal(x/8);
oct = oct*10+z;
}
}
void show()
{
System.out.println("Original number = "+n);
octal(n);
System.out.println("Octal equivalent = "+oct);
}
public static void main(String args[])
{
dec_oct obj = new dec_oct();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int nn = sc.nextInt();
obj.getnum(nn);obj.show();
ALGORITHM:
(i) Start
(ii) Accept a number from the user.
(iii) Convert the number using recursion into its octal equivalent.
(iv) Print the original number and its octal equivalent.
(v) End
VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:

9)Write a program to enter a string and encrypt or decrypt the string as per the choice of
the user. The shift code for encryption /decryption is 3.for encryption increase each letter
by 3 and decrease by 3 for decryption.
Class name : cipher

Data Members :
n - for storing the string inputted by the user
a - for storing the encrypted/decrypted string.
x - for storing the choice of the user

Member Methods :
void accept() - for entering the string and choice of the user.
void encrypt() - for encrypting the string
void decrypt() - for decrypting the string
void display() - for printing the message.
CODE:
import java.util.*;
class cipher
{
String n;String a;int x;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter string");
n = sc.nextLine();a="";
System.out.println("Enter 1 to encrypt and 2 to decrypt");
x = sc.nextInt();
switch(x)
{
case 1:
encrypt();break;
case 2:
decrypt();
}
}
void encrypt()
{
for(int i=0;i<n.length();i++)
{
int p = (int)n.charAt(i);
a += (char)(p+3);
}
}
void decrypt()
{
for(int i=0;i<n.length();i++)
{
int p = (int)n.charAt(i);
a += (char)(p-3);
}
}
void display()
{
if(x==1)
System.out.println("Encrypted message = "+a);
else
System.out.println("Decrypted message = "+a);
}
public static void main()
{
cipher obj1 = new cipher();
obj1.accept();
obj1.display();

ALGORITHM:
(i) Start
(ii) Enter a string.
(iii) Accept the choice of the user whether to encrypt or decrypt the string.
(iv) If user chooses encryption then add shift code to the letters of string.
(v) If the user chooses decryption then subtract shift code from the letters of string.
(vi) Print the message.
(vii) End
10) Write a program to find whether a number is a happy number or not using recursion.
Class name - happy_recursion

Data members :
n - stores the inputted number

Member Functions :
happy_recursion() - default constructor
void getnum(int nn) - for initializing n with nn
int sum_sq_digits(int x) - for calculating sum of square of digits.
void ishappy() - for checking whether number is happy number or not.
main() - for calling the functions
CODE:
import java.util.Scanner;
class happy_recursion
{
int n;
happy_recursion()
{
n=0;
}
void getnum(int nn)
{
n=nn;
}
int sum_sq_digits(int x)
{
int sum=0;
while(x!=0)
{
int d=x%10;
sum += Math.pow(d,2);
x /= 10;
}
if(sum > 9)
return sum_sq_digits(sum);
else
return sum;
}
void ishappy()
{
if(sum_sq_digits(n) == 1)
System.out.println("Happy number");
else
System.out.println("Not a happy number");
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int a = sc.nextInt();
happy_recursion obj = new happy_recursion();
obj.getnum(a);
obj.ishappy();
}
}
ALGORITHM:
(i) Start
(ii) Accept a digit number.
(iii) Pass the number through the parameterized constructor for initialization
(iv) Extract last digit.
(v) Multiply the last digit with its position value and then add it up.
(vi) Drop the last digit of the original number.
(vii) Check whether the sum calculated from above three steps is divisible by 11.
(viii) If the sum is divisible by 11 then, it is a valid ISBN number.
(ix) Else it is not a valid ISBN number.
(x) End

VALUE DATATYPE CHART


VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
11) Design a class Change to perform string related operations. The details of the class are
given below :
Class name : Change
Data members/instance variables :
str : stores the word.
newstr : stores the changed word.
len : stores the length of the word.
Member functions/methods :
change() : default constructor.
void inputword() : to accept a word.
char caseconvert(char ch) : converts the case of the character and returns it.
void recchange(int) : extracts character using Recursive technique and changes it case
using caseconvert() and form a new word.
void display : displays both the words.
Specify the class Change giving the details of the functions void inputword(), char
caseconvert(char ch), void recchange(int) and void display(). Define main() function to
create an object and call the functions accordingly to enable the above change in the given
word.
CODE:
import java.util.*;
class Change
{
String str;
String newstr;
int len;
Change()
{
str="";
newstr="";
len=0;
}
void inputword()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter string");
str=sc.next();
}
char caseconvert(cahr ch)
{
if(ch>=65 && ch<=90)
ch=ch+37;
else if(ch>=97 && ch<=122)
ch=ch-37;
else
ch=' ';
}
void recchange(int x)
{
if(x>=0)
{
char ch1=str.charAt(x);
caseconvert(ch1);
newstr= newstr+ch1;
return recchange(x-1);
}
else
return 1;
}
void display()
{
int a = str.length();
recchange(a);
System.out.println("String"+str);
System.out.println("New String"+ newstr);
}
public static void main(String args[])
{
Change ob = new Change();
ob.inputword();
ob.display();
}
}
}

ALGORITHM:
(i) Start
(ii) Enter the string.
(iii) Using recursive technique convert the small letters to capital letters and the capital
letters to small letters.
(iv) Display the new sentence.
(v) End

VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC


INPUT:
OUTPUT:

12)A class Admission contains admission number of 100 students that are already
arranged in ascending order. Some of the data members and member functions are given
below:
Class name : Admission
Data members/instance variables :
Adno[] : integer array to store admission numbers.
Member functions/methods :
Admission( int nn ) : constructor to initialize the array elements.
void fillArray() : to accept the elements/admission numbers into the array in ascending
order.
int binSearch( int l, int u, int v) : to search a particular admission number (v) using
binary search and Recursive technique and return 1 if found and otherwise -1.
Specify a class Admission giving the details of the constructor and void fillArray() and
void binSearch( int, int, int ). Define the main() function to create an object call the
functions accordingy to enable the task.
CODE:
import java.util.*;
class Admission
{
int Adno[];
Admission(int nn)
{
int Adno[] = new int[nn];
}
void fillArray()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the elements in ascending order");
for(int i=0;i<100;i++)
Adno[i] = sc.nextInt();
}
int binSearch(int l,int u,int v)
{
if(u>l)
{
int mid = l+u/2;
if(Adno[mid]<v)
return binSearch(mid+1,u,v);
else if(Adno[mid]>v)
return binSearch(l,mid-1,v);
}
else
return 0;
}
public static void main(String args[])
{
Admission ob = new Admission(100);
ob.fillArray();
ob.binSearch(0,100,0);
}
}
ALGORITHM:
(i) Start
(ii) Enter the admission numbers in an array.
(iii) In binSearch using recursive technique search the element in the array.
(iv) Call all the functions in the main function.
(v) End

VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
13) A disarium number is a number in which the sum of the digits to the power of their
respective position is equal to the number itself.
Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.
Design a class Disarium to check if a given number is a disarium number or not. Some of
the members of the class are given below:
Class name : Disarium
Data members/instance variables:
int num : stores the number
int size : stores the size of the number
Methods/Member functions:
Disarium(int nn) : parameterized constructor to initialize the data
members n = nn and size = 0
void countDigit( ) : counts the total number of digits and assigns it
to size
int sumofDigits(int n, int p) : returns the sum of the digits of the number(n) to the power
of their respective positions(p)
using recursive technique

void check( ) : checks whether the number is a disarium


number and displays the result with an
appropriate message

CODE:
import java.util.Scanner;
public class Disarium
{ int num,size;
static Scanner sc=new Scanner(System.in);
Disarium(int nn)
{ num=nn;
size=0; }
void countDigits()
{ int a=num;
while(a!=0)
{ a=a/10;
size++; }
}
int sumofDigits(int n, int p)
{ return (n==0)? 0: sumofDigits(n/10,p-1) + (int)Math.pow(n%10,p); }
void check()
{ if(num==sumofDigits(num,size))
System.out.print("\n Disarium Number");
else
System.out.print("\n Not a Disarium Number");
}
static void main()
{ System.out.println("Input a Number");
int m=sc.nextInt();
Disarium x= new Disarium(m);
x.countDigits();
x.check();
}
}
Algorithm:
Step 1: Start
Step 2: Enter the number to be checked
Step 3: Check whether the number is disarium or not
Step 4: if the no is disarium then print Disarium No otherwise print Not Disarium
Step 5: End

VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:

14)The following is the process of finding the factorial of a number n.


n! = n x (n-1) x (n-2) x (n-3) x ………. x 1.
A class factorial is declared with the following details:
Class name : factorial
Data members/instance variables :
n : (integer) to store a number.
f : (integer) to store the factorial.
Member functions/methods :
factorial() : a constructor to assign 0 to n.
int fact(int num) : to find and return the factorial of num, if num>0, using the
Recursive Technique, otherwise return 1, if num = 0.
void getFactoial() : to accept n and store the factorial in f by invoking the recursive
function fact(). Print the factorial stored in ‘f’.
Specify the class factorial giving details of the constructor and functions int
fact(int) and void getFactorial(). Write a main() function to create an object and call
the functions to print result.
CODE:
import java.util.*;
class factorial
{
int n;
int f=1;
factorial()
{
n=0;
}
int fact(int num)
{
if(num==0)
return f;
else
{
f=f*num;
return(fact(num-1));
}
}
void getFactorial()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number");
n=sc.nextInt();
fact(n);
System.out.println("Factorial of "+n+"="+f);
}
public static void main(String args[])
{
factorial ob=new factorial();
ob.getFactorial();
}
}
ALGORITHM:
(i) Start
(ii) Enter value of n.
(iii) Using recursive technique find the factorial of n.
(iv) Display the factorial.
(v) End

VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC


INPUT:
OUTPUT:

15) Class Alpha enters word and arranges the alphabets lexicographically.
The details of the class are given below:
Class name : Alpha
Data members/instance variables :
str : to store the entered string.
l : to store to length of the entered string.
Member functions/methods :
void readword() : to enter the string into the program.
void arrange() : to arrange the alphabets of the word in alphabetical order.
void display() : to display the entered string and the arranged string.
Specify the class Alpha giving details pf the constructor and readword(), void
arrange() and void display(). Define a main() function to create an object and
invoking the methods accordingly to enable the task.

CODE:
import java.io.*;
class Alpha
{
String str;
int l;
char c[] = new char[100];
public Alpha()
{
str = "";
l =0;
}
public void readword() throws IOException
{
System.out.println("enter word - ");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
l = str.length();
}
public void arrange()
{
int i,j;
char temp;
for(i=0;i c[j+1])
{
temp = c[j];
c[j] = c[j+1];
c[j+1] = temp;
}
}
}
}
public void display()
{
System.out.println();
for(int i=0;i<l;i++)
{System.out.print(c[i]);
}
public static void main(String args[]) throws IOException
{
Alpha obj = new Alpha();
obj.readword();
obj.arrange();
obj.display();
}
}
ALGORITHM:
(i) Start
(ii) Accept a sentence.
(iii) Calculate the number of words in the sentence.
(iv)Store the words of the sentence separately in a
string array.
(v)Sort the array lexicographically.
(vi)Print the sorted array.
(vii)End

16)The following are the ways of finding n raise to the power m.


Example 1 : Let n=2 and m=0, nm = 20 = 1
Example 1 : Let n=2 and m=3, nm = 23 = 8
Example 1 : Let n=2 and m=-3, nm = 2-3 = 0.125
A class poweris delared with the following details:
Class name : poweris
Data members/instance variables :
base : (integer) to store an integer number as base.
expn : (integer) to store an integer number to exponent/power.
p : (double type) to store ‘base’ raise to the power ‘expn’.
Member functions/methods :
poweris() : a constructor to initialize data members with default values.
int power(int n, int m) : to find and return ‘n’ raise to the power ‘m' using the
Recursive technique.
void findresults() : to accept ‘base’ and ‘expn’, and store base raise to the power
expn in p, by invoking the recursive function.
void printresults() : using suitable documentation print value stored in ‘p’.
Specify the class poweris giving details of the constructor and int power(int, int)
and void findresults() and void printresults(). Write a main function to cretate an
object and call the functions to print result.
CODE:
import java.util.*;
class poweris
{
int base,expn;
double p;
poweris()
{
base=0;
expn=0;
p=0.0;
}
int power(int n, int m)
{
if(n!=0)
{
if(m==0)
return 1;
else if(m<0)
1/return(n*power(n,m+1));
else
return (n*power(n,m-1));
}
}
void findresults()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter base");
base=sc.nextInt();
System.out.println("Enter exponent");
expn=sc.nextInt();
p=power(base,expn);
}
void printresults()
{
System.out.println("Result="+p);
}
public static void main(String args[])
{
poweris ob= new poweris(System.in);
ob.findresults();
ob.printresults();
}
}
ALGORITHM:
(i) Start
(ii) Accept the base and exponent.
(iii) Using recursive technique find the base raised
to then
power exponent.
(iv) Display the output.
(v) End

17. Program to Accept Password from the user and tell the user its strength.

import jav.util.*;
class Password{
String p;
String strength;
Password(){
p=””;
strength=””;
}
void accept(){
Scanner sc=new Scanner(System.in);
System.out.pritnln(“Enter a Password”);
p=sc.nextInt();
}
void check(){
int a=0,b=0,c=0;
for(int i=0;i<p.length();i++){
char ch=p.charAt(i);
if(Character.isLetter(ch))
a=1;
else f(character.isDigit(ch)
b=1;
else
c=1;
}
int d=a+b+c;
if(d==1)
strength=”weak”;
else if(d==2)
strength=”Strong”;
else
strength=”very strong”;
}
ALGORITHIM 4-
Step1-Start
Step2-Input the Password from the user and store it in variable p
Step3-Check for the strength by using the conditions of weak,strong and very
strong
Step4-Print The strength of the Password
Step5-End
18) Design a class ArmNum to check if a given number is an Armstrong number or not.
[A number is said to be Armstrong if sum of its digits raised to the power of length of the
number is equal to the number]
Example : 371 = 33 + 73 + 13
1634 = 14 + 64 + 34 + 44
54748 = 55 + 45 + 75 + 45 + 85
Thus 371, 1634 and 54748 are all examples of Armstrong numbers.
Some of the members of the class are given below:
Class name : ArmNum
Data members/instance
variables:
n : to store the number
l : to store the length of the number
Methods/Member functions:
ArmNum (int nn) : parameterized constructor to initialize the data member n=nn
int sum_pow(int i) : returns the sum of each digit raised to the power of the length of the
number using recursive technique
eg. 34 will return 32 + 42 (as the length of the number is 2)
void isArmstrong( ) : checks whether the given number is an
Armstrong number by invoking the function
sum_pow( ) and displays the result with an
appropriate message

Specify the class ArmNum giving details of the constructor( ), int sum_pow(int) and void
isArmstrong( ). Define a main( ) function to create an object and call the functions
accordingly to enable the task.
CODE:
import java.util.*;
public class ArmNum
{ int n,l;
static Scanner x=new Scanner(System.in);
ArmNum(int nn)
{ n=nn;
l=Integer.toString(n).length();
}
int sum_pow(int i)
{ if(i==0)
return 0;
else
return (int)Math.pow(i%10,l) + sum_pow(i/10);
}
void isArmstrong()
{ if(sum_pow(n)==n)
System.out.println(n + "is an Armstrong number");
else
System.out.println(n + "is not an Armstrong number");
}
static void main()
{ ArmNum obj=new ArmNum(546748);
obj.isArmstrong();
}
}
ALGORITHM:
Step 1: Start
Step 2: Enter the number to be checked as Armstrong or not.
Step 3:Enter n and l to store the number and length of the number respectively.
Step 4: Check whether the no entered has sum of its digits raised to the power of length of
the number is equal to the number.
Step 5: print the result
Step 6: end
VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
19.Program to accept and print an address

import java.util.*;
class Address{
String add;
Address(){
add=””;}
void accept(){
Scanner sc=new Scanner(system.in);
System.out.println(“Enter address”);
add=sc.nextline();}
boolean hasPin(){
int m=add.lastIndexOF(‘,’);
String p=add.substring(m+1,add.length()-1);
if(p.length()==6){
for (int i=0;i<6;i++){
char ch=p.charAt(i);
if(character.isDigit(ch)==false)
return false;
}
return true;
else
return false;}
void printAdd(){
String Tokenizer st=new String Tokenizer(add,”,”);
int m=st.countToken();
for (int i=1;i<=m;i++)
System.out.println (st.countToken());
if(hasPin()==false)
System.out.println(“****”);
}
public static void main(String args[])
{
Address ob=new Address();
ob.accept();
ob.printAdd();
}
}
ALGORITHIM :

Step1-Start
Step2-store the value of address in the variable add
Step3-check for Pincode
Step4-Print **** if no pin
Step5-Print Address with Pin otherwise
Step6-End

VALUE DATATYPE CHART


VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
20.Program to convert Decimal to Binary.

import java.util.*;
class DecToBin{
int n;
DecToBin(){
n=0;
}
void accept(){
Scanner sc=new Scanner(System.in);
System.out.println(“Enter value”;
n=sc.nextInt();}
void display();{
int b=toBinary();
System.out.println(‘Decimal number=”+n);
System.out.println(“Binary Equivalent=”+b);}
public static void main(String args[]){
DecToBin ob=new DecToBin();
ob.accept();
ob.display();
}
int to Binary(){
int b=0,m=n,p=0;
while(m!=0){
int r=m%2;
b=b+r*(int)Math.pow(10,p++);
m=m/2;
}
return b;
}
}
ALGORITHIM

Step1-Start
Step2-store the value of decimal number in n
Step3-covert the Decimal number into binary
Step4-Display the binary number
Step5-ENd

VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
21.Program to Transpose a matrix.

import java.util.*;
class Transpose{
int a[][[],r,c;
static Scanner sc=new Scanner(System.in);
TRansopse(int nr,int nc){
r=nr;
c=nc;
a-new int[r][c];}
void accept(){
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
System.out.println(“Eneter Value”);
a[i][j]=sc.nextInt();}
void disp Transpose()}
for(int i0;i<c;i++)}
for(int j=0;j<r;j++)
System.out.println(a[j][i]+”\t”);
System.out.println();}}
void display(){
for(int i=0;i<r;i++){
for(int j=0;j<c;;j++)
System.out.println(a[i][j]+”\t”);
System.out.println()
}}
public static void main(String args[]){
System.out.println(“Eneter rows and columns”);
int nr=sc.nextInt();
int nc=sc.nextInt();
Transpose ob=new Transpose(nr,nc);
ob.accept();
System.out.println(“My Matrix”);
ob.display();
System.out.println(“Transpose of matrix”);
ob.dispTranspose();
}
}
ALGORITHIM
Step1-Start
Step2-Accept the matrix from the user in the variable a on the bass of number of
rows and columns stored in nr and nc
Step3-Transpose the matrix
Step4-Display the transposed matrix
Step5-End

VALUE DATATYPE CHART


VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
22.Program to check whether a number is Palindrome or not

import java.util.*;
class Palindrome{
int n;
Palindrome(){
n=0;
}
void accept()
{
Scanner sc=new Scanner (System.in);
Sstem.out.println(“Enter a number”);
}
int reverse(){
int m=n,r=0,d;
for(int i=m;i>0;i++)
{
d=i%10;
r=(r*)+10;
}
return r;
void display()
{
int p=reverse();
if(n==p)
System.out.println(“The number is Plaindrome”);
else
System.out.println(“The number is not Plaindrme”);
}
public static void main(String args[])
{
Palindrome ob=new Palindrome();
ob.accept();
ob.display();
}
}
ALGORITHIM

Step1-Start
Step2-Accept the number from the user
Step3-check for Palindrome number
Step4-Print whether it is or not Palindrome number
Step5-End
VALUE DATATYPE CHART
VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
23.Program to print HCF and LCM of two numbers
import java.util*;
class HcfLcm{
int a,b;
HcfLcm(int na,int nb){
a=na;
b=nb;
void display()
{
System.out.println(“HCF”+hcf());
System.out.println(“lcm=”+lc());
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Eneter two number “);
int x=sc.nextInt();
int y=sc.nextInt();
hcflcm ob=new hcflcm(x,y);
ob.display()
}
}int hcf(){
int x=a’ y=b;
while(x!=y)
{
if(x>y)
x=x-y;
return x;
}
int lcm()
{
int l=(a*b)/hcf();
return l;
}
}
ALGORITHIM

Step1-Start
Step2-Enter the number
Step 3: calculate the hcf and lcm of the number entered.
Step 4: print the result
Step 5: end

VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

INPUT:
OUTPUT:
24) A class Merger concatenates two positive integers that are greater than 0 and
produces a new merged integer. Example: If the first number is 23 and the second is 764,
then the concatenated number will be 23764. Some of the members of the class are given
below:
Class name : Merger
Data members/instance variables:
n1 : long integer to store first number
n2 : long integer to store second number
mergNum : long integer to store the merged number
Member functions:
Merger() : constructor to initialize the data members
void readNum( ) : to accept the values of the data members n1 and n2
voidJoinNum( ) : to concatenate the numbers n1 and n2 and store it in mergNum
void show( ) : to display the original numbers and the merged number with appropriate
messages
Specify the class Merger, giving the details of the constructor, void readNum( ),void
JoinNum( )and void show( ). Define the main( ) function to create an object and call the
functions accordingly to enable the task.

CODE:
import java.util.*;
public class Merger
{
long n1,n2,mergNum;
Merger( ) { } OR Merger(){ n1= n2 = mergNum = 0;}
void readNum()
{ Scanner x=new Scanner(System.in);
System.out.println("Enter two numbers");
n1=x.nextLong();
n2=x.nextLong();
}void JoinNum()
{ String s=Long.toString(n1);
String s1=Long.toString(n2);
String s2=s+s1;
mergNum=Long.valueOf(s2);
}
void show()
{ System.out.println("First Number= " + n1);
System.out.println("Second Number= " + n2);
System.out.println("Merged Number= " + mergNum);
}
static void main()
{ Merger obj=new Merger();
obj.readNum();
obj.JoinNum();
obj.show();}}
25) A class TheString accepts a string of a maximum of 100 characters with only one blank
space between the words.
Some of the members of the class are as follows:
Class name : TheString
Data member/instance variable:
str : to store a string
len : integer to store the length of the string
wordcount : integer to store the number of words
cons : integer to store the number of consonants
Member functions/methods:
TheString( ) : default constructor to initialize the data members
TheString( String ds) : parameterized constructor to assign str=ds
void countFreq() : to count the number of words and the number of
consonants and store them in wordcount and cons respectively
void Display( ) : to display the original string, along with the
number of words and the number of consonants
Specify the class TheString giving the details of the constructors, void countFreq() and void
Display( ). Define the main( ) function to create an object and call the functions
accordingly to enable the task.

CODE:
public class TheString
{
String str;
int len,wordcount,cons;
TheString(){} OR TheString( ) { str=””; wordcount=0; cons=0;}
TheString(String ds)
{ str=ds; len=str.length();
}
void countFreq()
{ char c;
str=str.toLowerCase ();
for(int i=0;i<len;i++)
{ c=str.charAt(i);
if(c==32)
wordcount++;
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u')
cons++;
}
cons = cons – wordcount;
++wordcount;
}
void Display()
{ System.out.println(" Number of words=" + wordcount ); System.out.println(" Number of
consonants=" + cons);
static void main()
{ TheString x=new TheString("india is my country");
x.countFreq();
x.Display();
}
}
Algorithm:
VALUE DATATYPE CHART

VARIABLE DATATYPE CHARACTERISTIC

Input:

Output:

You might also like