0% found this document useful (0 votes)
29 views91 pages

Sairindhri Bhattacharya Computer Practical Final

Uploaded by

emogirlslovewine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views91 pages

Sairindhri Bhattacharya Computer Practical Final

Uploaded by

emogirlslovewine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

NAME: SAIRINDHRI BHATTACHARYA

CLASS: XII

ROLL No.: 13

Page | 1
CONTENT

Sl. No. Program Page


No.
1. Program to find Fascinating numbers in the range of 100 to 1000. 3
2. Program to find composite magic numbers with range given by user. 5
3. Menu driven program to check if (i)a palindrome number or (ii) a palindrome 8
word.
4. Program to count the frequency of consonants and words. 11
5. Menu driven program to input two matrices from the user and find the (i) sum 13
(ii) difference (iii) product
6. Program to sort the elements of row and column of a matrix using bubble sort. 18
7. Program to show overloading of function. 21
8. Program in java to accept a string to arrange all the letters of the string in 24
alphabetical order and find the frequency of each character
9. Program in java to accept a number and check whether it a smith number or 27
not.
10. Program in java to accept a matrix and find the saddle point of it. 30
11. Program in java to add two angles using object passing as arguments. 33
12. Program to separate two sentences and count the frequency of common 36
words in each sentence.
13. Program to check whether the given number is a circular prime number or not. 39
14. Program to check whether the given matrices are mirror matrix or not. 42
15. Menu driven program to convert a binary number to decimal number or vice 45
versa using recursion.
16. Program to check whether a number is disarium number or not using 47
recursion.
17. Program to accept up to 1000 boxes from the user and insert them with 49
minimum no. of cartons used.
18. Program in java to check the equality of two matrices using object passing as 52
arguments.
19. Program 1 to show inheritance 55
20. Program 2 to show inheritance 58
21. Program 3 to show inheritance 61
22. Stack program 1 64
23. Stack program 2 68
24. Queue program 1 71
25. Queue program 2 75
26. Dequeue program 79
27. Circular Queue program 83
28. Date Calendar program 87

Page | 2
PROGRAM 1:

Write a program to find Fascinating numbers in the range of 100 to 1000.

ALGORITHM:

Step 1: Start.
Step 2: Method isFascinating(int num) checks whether a number num is a fascinating number or not.
Step 3: Multiply 2 with the number num and store it to A1, and multiply 3 with the number num and
store it to A2.
Step 4: Concatenate the number num with A1 and A2 and store in a string concat.
Step 5: Initialize a flag variable ‘f’ to true.
Step 6: A for-loop between range of character ‘1’ to’ 9’ is run.
Step 7: Initialize a counter variable with the value of 0.
Step 8: An inner for-loop between the range of 0 to the length of the string concat is run.
Step 9: Each character of concat is extracted using charAt() method and compared with characters
‘1’ to ‘9’, if equal increment counter by 1.
Step 10: Inner for-loop closed.
Step 11: If counter variable is equal to 0 or more than 1, then assign ‘f’ to false and break from the
loop.
Step 12: Outer for-loop closed.
Step 13: Check if ‘f’ is equal to true, only then num is fascinating number and print the number num.
Step 14: Create the main method, in a for-loop between the range of 100 to 1000 call the
“isFascinating” method with the value of the loop variable.
Step 15: Stop.

SOURCE CODE:

public class Fascinating _Class


{
static void isFascinating(int num)
{
int A1 = num * 2;
int A2 = num * 3;
char i;
int j = 0;
boolean f = true;
char ch ;
String concat = num +" "+ A1 +" "+ A2 ;
for(i='1';i<='9';i++)
{
int count = 0 ;
for(j= 0 ; j<(concat.length()) ; j++)
{
ch = concat.charAt(j) ;
if(ch == i)
count++ ;
}
if(count == 0 || count>1)

Page | 3
{
f = false ;
break ;
}
}
if(f == true)
System.out.println(num) ;
}
public static void main(String args[])
{
System.out.println("The Fascinating numbers between the range 100 to 1000 are:") ;
for(int k = 100; k<=1000;k++)
isFascinating(k) ;
}
}

VARIABLE DESCRIPTION:

VARIABLE NAME DATA TYPE DESCRIPTION


Num int Stores the number to be checked, if fascinating.
A1 int Stores the product of 2 and the number.
A2 int Stores the product of 3 and the number.
I char Used as for-loop variable, in range of character 1 to 9.
J int Used as for-loop variable, in range of 0 to the length of the
string concat
F boolean The flag variable.
Ch char Stores each character of the concat
Concat String Stores the concatenated string of num,A1 and A2
Count int The counter variable.
K int Used as for-loop variable to invoke the isFascinating method
between the range of 100 to 1000.

OUTPUT:

Page | 4
PROGRAM 2:

Write a program to find composite magic numbers with range given by user.

ALGORITHM:

Step 1: Start.
Step 2: Method boolean isComposite(int x) checks if a number is composite or not.
Step 3: Variable int count is taken to count the number of factors.
Step 4: A for-loop of range 1 to x is run.
Step 5: The value of count increases with 1 if the number is the factor of x.
Step 6: The for-loop closes.
Step 7: If the value of count is more than 2 then return true else return false.
Step 8: Method int sum_of_digits (int x) to perform the summation of all the digits.
Step 9: Variable int sum is taken to store the sum of digits.
Step 10: Variable int d is taken to store the extracted digit.
Step 11: A while-loop is run till x is greater than 0.
Step 12: Digit extraction is performed and the summation is done.
Step 13: The while-loop is closed and then return sum.
Step 14: Method boolean isMagic(int sum) checks if a number is magic or not.
Step 15: Variable int a stores the sum of digit by calling the method sum_of_digits (sum).
Step 16: A while-loop is run till a is greater than 9.
Step 17: Perform summation of digits.
Step 18: The while-loop closes.
Step 19: If a is equal to 1 then return true else return false.
Step 20: Create the main method.
Step 21: The lower limit (lp) and the upper limit (up) is to be inputted by the user using Scanner class.
Step 22: A for-loop between the range of lp to up is run.
Step 23: If the lp is smaller than up then the isMagic() is called with the value of the for-loop.
Step 24: Stop.

SOURCE CODE:

import java.util.*;
public class Composite_Magic_Number
{
public boolean isComposite(int x)
{
int count=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
count++;
}
if(count>2)
return true;
else
return false;
}

Page | 5
public int sum_of_digits(int x)
{
int sum = 0;
while(x>0)
{
sum = sum + x%10;
x = x/10;
}
return sum;
}
public boolean isMagic(int sum)
{
int a = sum_of_digits(sum);
while(a>9)
{
a = sum_of_digits(a);
}
if(a == 1)
return true;
else
return false;
}
public void main()
{
Composite_Magic_Number ob = new Composite_Magic_Number();
Scanner sc=new Scanner(System.in);
System.out.print("Enter the lower limit: ");
int lp=sc.nextInt();
System.out.print("Enter the upper limit: ");
int up=sc.nextInt();
if (lp<up)
{
System.out.println("The Composite Magic Integers are: ");
for(int i=lp; i<=up; i++)
{
if(ob.isComposite(i)==true && ob.isMagic(i)==true)
System.out.println(i);
}
}
else
System.out.println("Invalid range");
}
}

Page | 6
VARIABLE DESCRIPTION:

VARIABLE DATA DESCRIPTION


NAME TYPE
x int Stores the number to be checked.
count int Stores the number of factors.
i int Used in for-loop.
sum int Stores the sum of digits.
a int Stores the initial sum of the digits.
lp int Stores lower limit of range.
up int Stores upper limit of range.

OUTPUT:

Page | 7
PROGRAM 3:

Write a menu driven program to check if (i) a palindrome number, (ii) a palindrome
word

ALGORITHM:

Step 1: Start
Step 2: A main method is created and a menu is printed showing the options available.
Step 3: The option chosen by the user is stored in variable choice.
Step 4: Switch-case function is used to perform the desired option.
Step 5: In case 1, the number entered by the user is stored in variable num.
Step 6: The value of num is copied and stored in variable t.
Step 7: A while-loop is run.
Step 8: The number is reversed inside the loop and stored in variable d.
Step 9: If the reversed number is equal to the original number, then the message “The number is
palindrome” is printed else “The number is not palindrome” is printed.
Step 10: Break function is used to discontinue the loop.
Step 11: In case 2, the word entered by the user is stored in variable wrd.
Step 12: The word is converted to uppercase and stored in variable wrd.
Step 13: The length of the word is stored in variable len.
Step 14: An empty variable rever_wrd is taken.
Step 15: A for-loop is run.
Step 16: The word is reversed and stored inside the variable rever_wrd.
Step 17: If the word is equal to the reversed word then the message ”The word is palindrome” is
printed else “The word is not palindrome” is printed.
Step 18: If the option given by user in step 3 is not available then show an error message.
Step 19: Stop.

SOURCE CODE:

import java.util.*;
public class switch_menu
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("MENU:");
System.out.println("1. Palindrome Number");
System.out.println("2. Palindrome Word");
System.out.print("Enter your choice : ");
int choice = sc.nextInt();
switch(choice)
{
case 1:
int rever_num = 0;
System.out.print("Enter the number to be checked : ");
int num = sc.nextInt();
int t = num;

Page | 8
while(t>0)
{
d = (d*10)+(t%10);
t = t/10;
}
if(d==num)
System.out.println(num+" is a palindrome number");
else
System.out.println(num+" is not a palindrome number");
break;
case 2:
System.out.print("Enter the word to be checked: ");
String wrd = sc.next();
wrd = wrd.toUpperCase();
int len = wrd.length();
String rever_wrd = "";
for(int i = len-1; i >= 0; i--)
{
rever_wrd = rever_wrd + wrd.charAt(i);
}
if(wrd.equals(rever_wrd))
System.out.println(wrd+" is a palindrome word");
else
System.out.println(wrd+" is not a palindrome word");
break;
default:
System.out.println("Invalid Choice");
break;
}
}
}

VARIABLE DESCRIPTION:

VARIABLE DATA DESCRIPTION


NAME TYPE
choice int Stores the choice given by the user.
Num int Stores the number given by the user.
T int Stores the copied number.
rever_num int Stores the reversed number.
Wrd String Stores the word given by the user.
Len int Stores the length of the word.
rever_wrd String Stores the reversed word.

Page | 9
OUTPUT:

Page | 10
PROGRAM 4:

Write a program to input a String from the user and count the frequency of words and
consonants in the String.

ALGORITHM:

Step 1: Start.
Step 2: A class of name TheString is made.
Step 3: The variables are globally declared.
Step 4: The variables are initialised in a default constructor.
Step 5: A parameterised constructor is used to store the given value in variable str.
Step 6: A function countfreq is made.
Step 7: The string is converted to lowercase.
Step 8: The length of the string is stored in variable len.
Step 9: A for loop is made from 0 till len.
Step 10: Each character of the string is stored in the variable ch.
Step 11: If the character is neither a vowel nor a space there will be an increment in the variable cons
else there will be an increment in the variable word_count.
Step 12: A function display is made.
Step 13: The original statement, the number of consonents and the number of words are printed.
Step 14: The main method
Step 15: A string is given by the user and passed into the parameterised constructer.
Step 16: All the methods are called in it.
Step 17: Stop.

SOURCE CODE:

import java.util.*;
public class TheString
{
String str;
int len;
int word_count;
int cons;
TheString()
{
String str="";
int len=0;
int word_count=0;
int cons=0;
}
TheString(String ds)
{
str=ds;
str=" "+str;
}
void countfreq()
{

Page | 11
String s=str;
s=s.toLowerCase();
len=s.length();
for(int i=0;i<len;i++)
{
char ch=s.charAt(i);
if(!(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch==' '))
cons++;
if(ch==' ')
word_count++;
}
}
void display()
{
System.out.print(" Statement: "+str+"\n Number of consonents: "+cons+"\n Number of words:
"+word_count);
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a statement: ");
String st=sc.nextLine();
TheString ob=new TheString(st);
ob.countfreq();
ob.display();
}
}

VARIABLE DESCRIPTION:

VARIABLE NAME DATA TYPE DESCRIPTION


word_count, cons int Stores the number of words and consonants.
Str String Stores the word passed through the method.
Ds String Stores the parameterised value.
S String Stores the copied string.
len int Stores the length of the word.
ch char Stores each character of string.
st String Stores the word given by the user.

OUTPUT:

Page | 12
PROGRAM 5:

Write a menu driven program to input 2 Matrixes from the user and find (i) the sum, (ii)
the difference, (iii) the product

ALGORITHM:

Step 1: Start.
Step 2: A class ‘Menu_Matrix’ is created.
Step 3: Matrix ‘A’,’B’,’C’ and variables ‘ra’,’rb’,’ca’,’cb’ are globally declared .
Step 4: A constructer is created to initialize the value of ‘ra’,’rb’,’ca’,’cb’.
Step 5: Matrix A and Matrix B are initialized in the method named ‘size()’.
Step 6: The values of the matrix are given by the user in the ‘input()’ method.
Step 7: In the ‘add()’ method, addition of matrix A and matrix B is done and stored matrix C.
Step 8: In the ‘subtract()’ method, subtraction of matrix A and matrix B is done and stored matrix C.
Step 9: In the ‘multiply()’ method, multiplication of matrix A and matrix B is done and stored matrix C.
Step 10: A ‘menu()’ method is created to perform switch case.
Step 11: The choice will be given by the user and then the corresponding output will be displayed.
Step 12: All the functions are called in the ‘main()’ method.
Step 13: Stop.

SOURCE CODE:

import java.util.*;
class Menu_Matrix
{
int A[][],B[][],C[][],ra,rb,ca,cb;
Menu_Matrix()
{
ra=0;
rb=0;
ca=0;
cb=0;
}
void size()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows in matrix A: ");
ra=sc.nextInt();
System.out.print("Enter the number of columns in matrix A: ");
ca=sc.nextInt();
A=new int[ra][ca];
System.out.print("Enter the number of rows in matrix B: ");
rb=sc.nextInt();
System.out.print("Enter the number of columns in matrix B: ");
cb=sc.nextInt();
B=new int[rb][cb];
}
void input()

Page | 13
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values of matrix A: ");
for(int i=0;i<ra;i++)
{
for(int j=0;j<ca;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("Enter the values of matrix B: ");
for(int i=0;i<rb;i++)
{
for(int j=0;j<cb;j++)
{
B[i][j]=sc.nextInt();
}
}
}
void add()
{
if(ra==rb && ca==cb)
{
C=new int[ra][ca];
for(int i=0;i<ra;i++)
{
for(int j=0;j<ca;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
System.out.println("Result:");
for(int i=0;i<ra;i++)
{
for(int j=0;j<ca;j++)
{
System.out.print(C[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("Matrix not of same size");
}
void subtract()
{
if(ra==rb && ca==cb)
{
C=new int[ra][ca];

Page | 14
for(int i=0;i<ra;i++)
{
for(int j=0;j<ca;j++)
{
C[i][j]=A[i][j]-B[i][j];
}
}
System.out.println("Result:");
for(int i=0;i<ra;i++)
{
for(int j=0;j<ca;j++)
{
System.out.print(C[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("Matrix not of same size");
}
void multiply()
{
if(ca==rb)
{
C=new int[rb][ca];
for(int i=0;i<rb;i++)
{
for(int j=0;j<ca;j++)
{
int s=0;
for(int k=0;k<ca;k++)
{
s=s+A[i][k]*B[k][j];
}
C[i][j]=s;
s=0;
}
}
System.out.println("Result:");
for(int i=0;i<rb;i++)
{
for(int j=0;j<ca;j++)
{
System.out.print(C[i][j]+" ");
}
System.out.println();
}
}
else

Page | 15
System.out.println("Matrix not of valid size");
}
void menu()
{
Scanner sc=new Scanner(System.in);
System.out.println("MENU: \n1.Addition \n2.Substraction \n3.Multiplication");
System.out.print("Enter your choice: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
add();
break;
case 2:
subtract();
break;
case 3:
multiply();
break;
default:
System.out.println("Error in choice");
}
}
void main()
{
Menu_Matrix ob=new Menu_Matrix();
ob.size();
ob.input();
ob.menu();
}
}

DESCRIPTION TABLE:

VARIABLE DATA DESCRIPTION


NAME TYPE
A[][],B[][] int Stores the values of 2 matrixes given by the user.
C[][] int Stores the result.
ra,rb int Stores the number of rows in Matrix A and Matrix B.
ca,cb int Stores the number of columns in Matrix A and Matrix B.

Page | 16
OUTPUT:

Page | 17
PROGRAM 6:

Write a program to do Bubble sorting in the Matrix, row wise and column wise.

ALGORITHM:

Step 1: Start.
Step 2: A class ‘BubbleSortMatrix’ is created.
Step 3: Variables ‘rows’,’columns’ and ‘matrix’ are globally declared .
Step 4: A parameterized constructer is created to initialize the value of ‘rows’ and ‘columns’ as rows=r
and columns=c. The size of the matrix is initialised with respect to rows and columns.
Step 5: The values of the matrix are given by the user in the ‘input()’ method.
Step 6: A ‘sortMatrixRowWise()’ method is created.
Step 7: The numbers in each row are compared and arranged in ascending order.
Step 8: A ‘sortMatrixColumnWise()’ method is created.
Step 9: The numbers in each colums are compared and arranged in ascending order.
Step 10: A display() method is created to print the matrix.
Step 11: The values of ‘r’ and ‘c’ are passed through the constructor in the ‘main()’ method.
Step 12: All the functions are called in the ‘main()’ method.
Step 13: Stop.

SOURCE CODE:

import java.util.*;
public class BubbleSortMatrix
{
static int rows,columns,matrix[][];
static Scanner sc=new Scanner(System.in);
BubbleSortMatrix(int r,int c)
{
rows=r;
columns=c;
matrix=new int[rows][columns];
}
void input()
{
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
matrix[i][j] =sc.nextInt();
}
}
}
void sortMatrixRowWise()
{
for (int i = 0; i < rows; i++)
{

Page | 18
for (int j = 0; j < columns - 1; j++)
{
for (int k = 0; k < columns - j - 1; k++)
{
if (matrix[i][k] > matrix[i][k + 1])
{
int temp = matrix[i][k];
matrix[i][k] = matrix[i][k + 1];
matrix[i][k + 1] = temp;
}
}
}
}
}
void sortMatrixColumnWise()
{
for (int i = 0; i < columns; i++)
{
for (int j = 0; j < rows - 1; j++)
{
for (int k = 0; k < rows - j - 1; k++)
{
if (matrix[k][i] > matrix[k + 1][i])
{
int temp = matrix[k][i];
matrix[k][i] = matrix[k + 1][i];
matrix[k + 1][i] = temp;
}
}
}
}
}
void printMatrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
static void main()
{
System.out.print("Enter the number of rows: ");
int r =sc.nextInt();
System.out.print("Enter the number of columns: ");
int c =sc.nextInt();

Page | 19
BubbleSortMatrix ob=new BubbleSortMatrix(r,c);
ob.input();
System.out.println("Original Matrix:");
ob.printMatrix();
ob.sortMatrixRowWise();
System.out.println("Matrix after row-wise sorting:");
ob.printMatrix();
ob.sortMatrixColumnWise();
System.out.println("Matrix after column-wise sorting:");
ob.printMatrix();
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPES DESCRIPTION


rows int To store the number of rows.
columns int To store the number of columns.
Matrix[][] int To store the values given by user.
temp int To store the value while swapping using bubbleshot.
r,c int To store the values and pass it through the constructor.

OUTPUT:

Page | 20
PROGRAM 7:

Design a class to overload a function series( ) as follows:


(a) void series(int a, int n) — To display the sum of the series given below:
a - (a/2!) + (a/3!) - (a/4!) + …… n terms
(b) void series(int n) — To display the sum of the series given below:
1/2 - 2/3 + 3/4 - 4/5 + …… n terms
Write a main method to create an object and invoke the above methods.

ALGORITHM:

Step 1: Start.
Step 2: A class of name series is made.
Step 3: A method factorial(double x) is created.
Step 4: To find the factorial of the number passed and the result is returned.
Step 5: A parameterized constructor is used in which an integer variable a and n is passed.
Step 6: The sum of the required series is found and printed.
Step 7: A parameterized constructor is overloaded in which an integer variable n is passed.
Step 8: The sum of the required series is found and printed.
Step 9: A main method is made.
Step 10: The input is taken from the user.
Step 11: The values are then passed to the respective methods for execution of the program.
Step 12: Stop.

SOURCE CODE:

import java.util.*;
class series
{
int factorial(double x)
{
int f=1;
for(int i=1;i<=x;i++)
{
f=f*i;
}
return f;
}
void series(int a,int n)
{
double sum=0.0;
for(double i=1.0;i<=n;i++)
{
if(i%2==0)
sum=sum-a/factorial(i);
else
sum=sum+a/factorial(i);
}
System.out.println("1st series:"+sum);

Page | 21
}
void series(int n)
{
double s=0.0;
for(double i=1.0;i<=n;i++)
{
if(i%2==0)
s=s-i/(i+1);
else
s=s+i/(i+1);
}
System.out.println("2nd series:"+s);
}
void main()
{
Scanner sc=new Scanner(System.in);
int A=0,N=0,N1=0;
System.out.print("Enter value of a:");
A=sc.nextInt();
System.out.print("Enter the limit of 1st series:");
N=sc.nextInt();
System.out.print("Enter the limit of 2nd series:");
N1=sc.nextInt();
series(A,N);
series(N1);
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


x double Stores the value passed into the method.
f int Stores the factorial.
a int Stores the value passed into the method.
n int Stores the value passed into the method.
sum double Stores the sum of 1st series.
s double Stores the sum of 2nd series.
A,N,N1 int Stores the value given by user.

Page | 22
OUTPUT:

Page | 23
PROGRAM 8:

Write a program in Java to accept a string. Arrange all the letters of the string in alphabetical order.
Also, find the frequency of each character.

ALGORITHM:

Step 1: Start.
Step 2: A class of name ‘Freq_sort_string’ is made.
Step 3: A string variable ‘s’, ‘s1’ is created.
Step 4: A character variable ‘a[]’ is created.
Step 5: An int variable len is created.
Step 6: A constructer is called to initialize the global variable.
Step 7: A method called input() is made.
Step 8: The input is taken from the user and stored in the variable s.
Step 9: A method named sort() is made.
Step 10: The string is stored in an array and bubble sorting is done to arrange in alphabetical order.
Step 11: The sorted string is stored in variable s1.
Step 12: A method named frequency is made to find the frequency of each character in the string and
print it.
Step 13: A method named display() is made to print the original string and the sorted string
Step 14: A main method is made.
Step 15: All the methods are called here.
Step 16: Stop.

SOURCE CODE:

import java.util.*;
public class Freq_sort_string
{
String s,s1;
int len;
char a[];
Freq_sort_string()
{
s="";
s1="";
len=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
s=sc.nextLine();
}
void sort()
{
s=s.toLowerCase();
int len=s.length();

Page | 24
a=new char[len];
for(int i=0;i<len;i++)
{
a[i]=s.charAt(i);
}
for (int i=0;i<len;i++)
{
for (int j=0;j<len-i-1;j++)
{
if (a[j]>(a[j+1]))
{
char temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<len;i++)
{
s1=s1+a[i];
}
}
void frequency()
{
int count=0;
s1=s1.trim();
int l=s1.length();
char ch=' ';
for (char i='a';i<='z';i++)
{
for (int j=0;j<l;j++)
{
ch=s1.charAt(j);
if(ch==i)
count++;
}
if(count>0)
System.out.println("The frequency of "+i+" :"+count);
count=0;
}
}
void display()
{
System.out.println("The original : "+s);
System.out.println("The sorted : "+s1);
}
void main()
{
Freq_sort_string ob=new Freq_sort_string();

Page | 25
ob.input();
ob.sort();
ob.frequency();
ob.display();
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


a[] char Stores the string for sorting
s String Stores the input given by user.
len int Stores the length of the original string
s1 String Stores the sorted string.
s double Stores the sum of 2nd series.
l int Stores the length of the sorted string
count int Stores the frequency of each character in string
temp char Used for swapping during bubble sort

OUTPUT:

Page | 26
PROGRAM 9:

Write a program to accept an input and check if the number is smith number or not.

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘SmithNumber’ is created.
Step 3: Variables ‘number’,factor_sum’ and ‘digit_sum’ globally declared.
Step 4: A parameterized constructer is used to initialize number=n, digit_sum=0 and factor_sum=0;
Step 5: A ‘sumOfDigits()’ is used for doing the sum of the digits of the number and store it in the
variable ‘digitsum’ and return it.
Step 6: A ‘sumOfPrimeFactors ()’ is used for finding the sum of prime factors of the number.
Step 7: If any prime factor is of more than one digit then the sumofDigits() is called to find the sum of
digits of that factor. The result is stored in the variable ‘factor_sum’ and returned.
Step 8: A ‘isSmithNumber()’ method is used for checking if the sum of prime factors and the sum of
digits are same.
Step 9: The ‘main()’ method is created and the number is taken from the user.
Step 10: The number is passed through the constructor and the methods are called.
Step 11: If the method isSmithNumber() returns true then the input number is printed as smith
number.
Step 12: End.

SOURCE CODE:

import java.util.*;
class SmithNumber
{
static int n, factor_sum, digit_sum;
SmithNumber(int x)
{
n=x;
digit_sum=0;
factor_sum=0;
}
int sumOfDigits(int n)
{
while (n > 0)
{
digit_sum += n % 10;
n /= 10;
}
return digit_sum;
}
int sumOfPrimeFactors(int n)
{
for (int i = 2; i <= n; i++)
{
if (n % i == 0)

Page | 27
{
factor_sum += sumOfDigits(i);
n /= i;
}
}
return factor_sum;
}
boolean isSmithNumber(int n)
{
boolean r=false;
if (sumOfPrimeFactors(n) == sumOfDigits(n))
r=true;
return r;
}
static void main()
{
Scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int x = scanner.nextInt();
SmithNumber ob=new SmithNumber(x);
ob.sumOfDigits(n);
ob.sumOfPrimeFactors(n);
if (ob.isSmithNumber(n)==true)
System.out.println(number + " is a Smith number.");
else
System.out.println(number + " is not a Smith number.");
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


n int Stores the number given by user.
factor_sum int Stores the sum of prime factor.
digit_sum int Stores the sum of digits.
x int Stores the number given by the user to pass it through the
constructor.
R boolean Stores returning value of the function.
i Int Loop variable

Page | 28
OUTPUT:

Page | 29
PROGRAM 10:

Write a program to find the saddle-point.

ALGORITHM:

Step 1: Start.
Step 2: A class ‘SaddlePoint’ is created.
Step 3: Variables ‘rows’,’columns’ and a matrix ’A[][]’ is globally declared.
Step 4: Variables are initialized in a non-parameterized constructor.
Step 5: A method ‘input()’ is created to take input from user.
Step 6: The original matrix is then displayed.
Step 7: A method ‘findSaddlePoint()’ is to find the saddle point of the matrix.
Step 8: If saddle point found then the number is shown else and appropriate message is displayed for
the no saddle point.
Step 9: A ‘main()’ method is used to call all the methods.
Step 10: Stop.

SOURCE CODE:

import java.util.*;
class SaddlePoint
{
int A[][],rows,columns,min_Element;
SaddlePoint()
{
rows=0;
columns=0;
min_Element=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
rows=sc.nextInt();
System.out.print("Enter the number of columns: ");
columns=sc.nextInt();
A=new int[rows][columns];
System.out.println("Enter the values of A: ");
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
A[i][j]=sc.nextInt();
}
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)

Page | 30
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
}
void findSaddlePoint()
{
boolean isSaddlePoint = true;
for(int i = 0; i < rows; i++)
{
min_Element = A[i][0];
int minColumnIndex = 0;
for(int j = 1; j < columns; j++)
{
if(A[i][j] < min_Element)
{
min_Element = A[i][j];
minColumnIndex = j;
}
}
for (int k = 0; k < rows; k++)
{
if (A[k][minColumnIndex] > min_Element)
{
isSaddlePoint = false;
break;
}
}
}
if(isSaddlePoint)
System.out.println("The saddle point is: " + min_Element);
else
System.out.println("There is no saddle point");
}
void main()
{
SaddlePoint ob=new SaddlePoint();
ob.input();
ob.findSaddlePoint();
}
}

Page | 31
DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


rows int Stores the number of rows.
coulmns int Stores the number of columns.
A[][] int Stores the matrix.
min_Element Int Stores the minimum element.
minColumnIndex Int Stores the column of minimum elements
isSaddlePoint boolean Checks the saddle point

OUTPUT:

Page | 32
PROGRAM 11:

An angle can be measured in degrees and minutes. Addition of any two angle of can be performed by
adding and modifying the degrees and minutes. A class Angle is designed whose functions are shown
below:
Data members:
int d: Stores degrees
int m: Stores minutes
Methods:
Angle(….): A parameterized constructor to initiase d & m.
void display: To show data members as angles.
Angle AddAngle(Angle A,Angle B): To add two angles given as arguments and store it in current object
and return the object.

ALGORITHM:

Step 1: Start
Step 2: A class named “Angle” is created.
Step 3: The integer variables are globally initialized.
Step 4: A parameterized constructor is used to initialize d=x and m=y.
Step 5: A function display is created.
Step 6: The original angle is printed.
Step 7: A parameterized function ‘AddAngle(Angle A,Angle B)’ is created with an ‘int’ return-type.
Step 8: The sum of the two minutes of the angles are stored in a variable ‘min’ of ‘int’ datatype.
Step 9: If ‘min’ is more than 60, then the remainder is stored in a variable ‘r’ and the quotient is
stored in ‘q’.
Step 10: The sum of ‘q’ and the two degrees of the angles are stored in a variable ‘deg’ of ‘int’
datatype.
Step 11: The value of ’deg’ is stored in variable ‘A.d’ and value of ‘min’ is stored in ‘A.m’.
Step 12: Angle A is then returned.
Step 13: A static main function is created.
Step 14: The Angles are inputed.
Step 15: The two angles are displayed.
Step 16: The objects are then called by passing the required values through the functions.
Step 17: Stop.

SOURCE CODE:

import java.util.*;
class Angle
{
int d,m;
Angle(int x,int y)
{
d=x;
m=y;
}
void display()
{

Page | 33
System.out.println(d+" degrees "+m+" minutes");
}
Angle AddAngle(Angle A,Angle B)
{
int min=A.m+B.m;
int r=0,q=0;
if(min>=60)
{
r=min%60;
q=min/60;
}
min=r;
int deg=A.d+B.d+q;
A.m=min;
A.d=deg;
return A;
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter degree of 1st angle: ");
int d1=sc.nextInt();;
System.out.print("Enter minutes of 1st angle: ");
int m1=sc.nextInt();
System.out.print("Enter degree of 2nd angle: ");
int d2=sc.nextInt();
System.out.print("Enter minutes of 2nd angle: ");
int m2=sc.nextInt();
Angle a=new Angle(d1,m1);
Angle b=new Angle(d2,m2);
a.display();
b.display();
a.AddAngle(a,b);
a.display();
}
}
DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


d int Stores the degree of the angle
m int Stores the minute of the angle
min int Stores the sum of the minutes of two angles.
deg int Stores the sum of ‘q’ and the degrees of two angles.
q int Stores the quotient.
r int Stores the remainder.
A,B Angle Stores the value of 1st angle and 2nd angle.

Page | 34
OUTPUT:

Page | 35
PROGRAM 12:

Write program in Java to separate 2 sentences terminating with ‘.’,’?’ and ‘!’. Also find the frequency
of common words in both the sentences.

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘Paragraph’ is created.
Step 3: The variables ‘str’,’str1’,’str2’ with string data type and variable ‘l’ with int datatype is globally
declared.
Step 4: A parameterized constructor is used to initialize the variables as str=x, str1=””, str2=”” and
l=str.length().
Step 5: Method check() is created to check it the given string has 2 sentences or not. It returns ‘1’ if
there are 2 sentences else it returns ‘0’.
Step 6: Method split_Sentence() is created to separate the two sentences and store the 1st sentence
in ‘str1’ and 2nd sentence in ‘str2’.
Step 7: Method frequency_display() is created to check the frequency of common words.
Step 8: If common word is found, it is printed with its frequency.
Step 9: The ‘main()’ method is created to input the string from the user and pass it through the
constructors.
Step 10: The ‘check()’ method is called to check if the sentence has 2 sentences.
Step 11: If 2 sentences are found, Method ‘split_Sentence()’ and ‘frequency_display()’ are called. Else
and appropriate message is shown for the invalidity of the string.
Step 12: Stop.

SOURCE CODE:

import java.util.*;
class Paragraph
{
String str,str1,str2;
int l;
Paragraph(String x)
{
str=x;
str1="";
str2="";
l=str.length();
}
int check()
{
int c=0;
int a=0;
for(int i=0;i<l;i++)
{
char ch=str.charAt(i);
if(ch=='.' || ch=='!' || ch=='?')
c++;

Page | 36
}
if(c==2)
a=1;
return a;
}
void split_Sentence()
{
int index=0;
index = str.indexOf('.');
if (index == -1)
index = str.indexOf('!');
if (index == -1)
index = str.indexOf('?');
str1=str.substring(0,index+1);
str2=str.substring(index+2);
System.out.println(str1);
System.out.println(str2);
}
void frequency_display()
{
StringTokenizer st1 = new StringTokenizer(str1," ");
StringTokenizer st2 = new StringTokenizer(str2," ");
System.out.println("COMMON WORD \t FREQUENCY");
StringTokenizer temp = new StringTokenizer(str2," ");
while(st1.hasMoreTokens())
{
int freq=0;
String w1=st1.nextToken();
while(temp.hasMoreTokens())
{
String w2=temp.nextToken();
if(w1.equalsIgnoreCase(w2))
freq++;
}
if(freq>0)
System.out.println(w1+"\t\t"+(freq+1));
temp = new StringTokenizer(str2," ");
}
}
static void main()
{
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter 2 sentences in a string: ");
String x=sc.next()+sc.nextLine();
Paragraph ob=new Paragraph(x);
if(ob.check()==1)
{
ob.split_Sentence();

Page | 37
ob.frequency_display();
}
else
System.out.println("The string does not have 2 sentences: ");
}
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


str String Stores the input given by user.
str1 String Stores the 1st sentence.
str2 String Stores the 2nd sentence.
l int Stores the length of the string
count int Stores the frequency of each common words in both the strings
index int Stores the position of the first ‘.’ or ‘!’ or ‘?’.
w1 String Stores each word of str1 after using StringTokenizer
w2 String Stores each word of str2 after using StringTokenizer
X String Stores the value given by user for passing in the constructor.
temp String Stores the copied value of str2.

OUTPUT:

Page | 38
PROGRAM 13:

Write a program to check whether the given number is circular prime or not.

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘circular_prime_number’ is created.
Step 3: The variables ‘num’ with int datatype is globally declared.
Step 4: A parameterized constructor is used to initialize the variables as num=n.
Step 5: Method isPrime(int) is created to check if the given number prime or not. It returns ‘true’ if
prime else it returns ‘false’.
Step 6: Method circulate(int) is created to keep circulating the numbers.
Step 7: The passed value is stored in the variable a1 with int datatype.
Step 8: The number of digits of the given number is counted and stored in variable d.
Step 9: The number is circulated and is passed through the isPrime(int) function to check whether the
circulated number is prime or not.
Step 10: If the isPrime(int) returns false then the variable will get incremented by 1 and the process
will break and the value of f is returned.
Step 11: The display() function is created and checks the value returned by the function circulate(int).
Step 12: If the value is 1 then it’s not a circular prime else if 0 then it’s a circular prime.
Step 13: The main method is created.
Step 14: The number is taken from the user and passed in the constructor.
Step 15: All the other methods are called here.
Step 16: End.

SOURCE CODE:

import java.util.*;
class circular_prime_number
{
int num;
circular_prime_number(int n)
{
num=n;
}
boolean isPrime(int x)
{
int c=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
c++;
}
if(c==2)
return true;
return false;
}
int circulate(int a)

Page | 39
{
int a1=a;
int s=0;
int d=0;
int f=0;
while(a1>0)
{
a1=a1/10;
d++;
}
a1=a;
while(num!=s)
{
s=(a1%10)*(int)Math.pow(10,d-1)+a1/10;
a1=s;
if(isPrime(s)!=true)
{
f=1;
break;
}
}
return f;
}
void display()
{
if(circulate(num)==0)
System.out.println(num+" is a Circular prime number");
else
System.out.println(num+ "is not a circular prime number");
}
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
circular_prime_number ob=new circular_prime_number(n);
ob.display();
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


Num int Stores the input given by user.
n, x, a int The parameterised variables.
a1 Int Stores the value of a.
D Int Stores the number of digits.
S Int Stores the circulated number.
F Int Stores the flag value.

Page | 40
OUTPUT:

Page | 41
PROGRAM 14:

Write a program print mirror matrix of a given matrix.

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘Mirror_Matrix’ is created.
Step 3: The variables ‘A[][]’, ‘B[][]’, ‘r’, ‘c’ with int datatype is globally declared.
Step 4: A parameterized constructor is used to initialize the variables
Step 5: Method size() is created to take the size of the matrix and initialise ‘A’ and ‘B’.
Step 6: Method input() is created to take the input of the matrix from user.
Step 7: Method convert() is used to make the mirror matrix.
Step 8: The values of A[][] is stored in B[][] inversely.
Step 9: The display() function is created.
Step 10: The original matrix and the mirror matrix is displayed.
Step 11: The main method is created.
Step 12: All the other methods are called here.
Step 13: End.

SOURCE CODE:

import java.util.*;
class Mirror_Matrix
{
int A[][],B[][],r,c;
Mirror_Matrix()
{
r=0;
c=0;
}
void size()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows in matrix : ");
r=sc.nextInt();
System.out.print("Enter the number of columns in matrix : ");
c=sc.nextInt();
A=new int[r][c];
B=new int[r][c];
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values of matrix A: ");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{

Page | 42
A[i][j]=sc.nextInt();
}
}
}
void convert()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
B[i][j]=A[i][c-1-j];
}
}
}
void display()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("Mirror Matrix");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
}
void main()
{
Mirror_Matrix ob=new Mirror_Matrix();
ob.size();
ob.input();
ob.convert();
ob.display();
}
}

Page | 43
DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


A[][] int Stores the matrix from the user.
B[][] int Stores the mirror matrix.
R int Stores the number of rows
C int Stores the number of columns.

OUTPUT:

Page | 44
PROGRAM 15:

Write a menu driven program to convert a binary number to decimal number or vice versa using
recursion.

ALGORITHM:

Here's the algorithm for the Menu_Converter program in 13 steps:

Step 1: Start.
Step 2: Initialize a variable num to 0.
Step 3: Print the menu options: 1. Decimal to Binary, 2. Binary to Decimal.
Step 4: Prompt the user to input their choice (1 or 2).
Step 5: Read the user's choice into variable c.
Step 6: Prompt the user to input the number they wish to convert.
Step 7: Read the number into variable num.
Step 8: If the choice c is 1, execute the dec_to_bin function.
Step 9: In the dec_to_bin function, if the input number is 0, return 0.
Step 10: Otherwise, recursively divide the number by 2, add the remainder, and multiply by 10.
Step 11: If the choice c is 2, execute the bin_to_dec function.
Step 12: In the bin_to_dec function, if the input binary number is 0, return 0.
Step 13: Otherwise, recursively calculate the decimal value using powers of 2. End.

SOURCE CODE:

import java.util.*;
class Menu_Converter
{
int num;
Menu_Converter()
{
num=0;
}
int dec_to_bin(int a)
{
if(a==0)
return 0;
else
return ((a%2)+10*dec_to_bin(a/2));
}
int bin_to_dec(int a,int p)
{
if(a==0)
return 0;
else
return (((a%10)*(int)Math.pow(2,p)+bin_to_dec(a/10,++p)));
}
void main()
Page | 45
{
Scanner sc=new Scanner(System.in);
System.out.println("MENU: 1.DECIMAL TO BINARY 2.BINARY TO DECIMAL");

Page | 46
System.out.print("Enter your choice: ");
int c=sc.nextInt();
System.out.print("Enter the number to be converted: ");
num=sc.nextInt();
switch(c)
{
case 1:
System.out.print(dec_to_bin(num));
break;
case 2:
System.out.print(bin_to_dec(num,0));
break;
default:
System.out.print("Incorrect Choice");
}
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


num int Stores the input given by user.
a, p int The parameterized variables.
C int Stores the choice given by user

OUTPUT:

Page | 47
PROGRAM 16:

Write a program to check whether a number is diserium number or not using recursion.

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘Diserium_recursive’ is created.
Step 3: The variables ‘num’ with int datatype is globally declared.
Step 4: A constructor is used to initialize the variables
Step 5: Method input() is created to take the input from user.
Step 6: Method count_digit(x) is made to count the number of digits of the number given by user.
Step 7: Method isDiserium(n,p) to check if the number is a diserium number or not.
Step 8: The values of A[][] is stored in B[][] inversely.
Step 9: The display() function is created.
Step 10: The original matrix and the mirror matrix is displayed.
Step 11: The main method is created.
Step 12: All the other methods are called here.
Step 13: End.

SOURCE CODE:

import java.util.*;
class Diserium_recursive
{
int num;
Diserium_recursive()
{
num=0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
}
int count_digits(int x)
{
int c=0;
while(x>0)
{
c++;
x/=10;
}
return c;
}
int isDiserium(int n,int p)
{
int d=n%10;

Page | 48
if(n<=0)
return 0;
else
return (int)Math.pow(d,p)+isDiserium(n/10,--p);
}
void display()
{
int pos=count_digits(num);
if(isDiserium(num,pos)==num)
System.out.println("Diserium Number");
else
System.out.println("Not a Diserium Number");
}
void main()
{
Diserium_recursive ob=new Diserium_recursive();
ob.input();
ob.count_digits(num);
ob.display();
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


A[][] int Stores the matrix from the user.
B[][] int Stores the mirror matrix.
R int Stores the number of rows
C int Stores the number of columns.

OUTPUT:

Page | 49
PROGRAM 17:

Write a program to accept maximum of 1000 boxes from the user and insert them in cartons of
capacity 48, 24, 12, 6 with minimum number of cartons used.

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘Number_of_cartons’ is created.
Step 3: The variables ‘a,b,c,d,num’ with int datatype is globally declared.
Step 4: A constructor is used to initialize the variables
Step 5: Method input() is created to take the input from user.
Step 6: Method count(n) is made to count the number of cartons required based on the boxes given
by user.
Step 7: The remaining boxes are also counted and stored in one extra carton.
Step 8: : The display() function is created to display the output.
Step 9: The main method is created.
Step 10: All the other methods are called here.
Step 11: End.

SOURCE CODE:

import java.util.*;
class Number_of_cartons
{
static int a,b,c,d,num;
Number_of_cartons(int n)
{
a=0;
b=0;
c=0;
d=0;
num=n;
}
int count(int n)
{
if(n>=48)
{
a=n/48;
if(a>0)
System.out.println("48*"+a+"="+(48*a));
n=n%48;
}
if(n<48 && n>=24)
{
b=n/24;
if(b>0)
System.out.println("24*"+b+"="+(24*b));
n=n%24;

Page | 50
}
if(n<24 && n>=12)
{
c=n/12;
if(c>0)
System.out.println("12*"+c+"="+(12*c));
n=n%12;
}
if(n<12 && n>=6)
{
d=n/6;
n=n%6;
if(d>0)
System.out.println("6*"+d+"="+(6*d));
}
if(n>0 && n<6)
d=d+1;
int sum=a+b+c+d;
System.out.println("Remaining boxes:"+n);
return sum;
}
void display()
{
System.out.println("Total number of boxes to be inserted in the cartons: "+num);
System.out.println("Total number of cartons used: "+count(num));
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of boxes: ");
int n=sc.nextInt();
Number_of_cartons ob=new Number_of_cartons(n);
if(num<=1000)
ob.display();
else
System.out.println("Number of items are more than 1000");
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


a,b,c,d int Stores the number of each types of carton.
num int Stores the total number of boxes.
n int Stores the parameterized value.
sum int Stores the total number of cartons needed.

Page | 51
OUTPUT:

Page | 52
PROGRAM 18:

Write a program to check if the given matrixes are equal or not.

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘EqMat’ is created.
Step 3: The variables ‘A[][],m,n’ with int datatype is globally declared.
Step 4: A parameterized constructor is used to initialize the variables
Step 5: Method input() is created to take the input from user.
Step 6: Method count(n) is made to count the number of cartons required based on the boxes given
by user.
Step 7: The remaining boxes are also counted and stored in one extra carton.
Step 8: : The display() function is created to display the output.
Step 9: The main method is created.
Step 10: All the other methods are called here.
Step 11: End.

SOURCE CODE:

import java.util.*;
class EqMat
{
static Scanner sc=new Scanner(System.in);
int A[][],m,n;
EqMat(int mm,int nn)
{
m=mm;
n=nn;
A=new int[m][n];
}
void readArray()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
A[i][j]=sc.nextInt();
}
}
}
int check(EqMat p,EqMat q)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(p.A[i][j]!=q.A[i][j])

Page | 53
return 0;
}
break;
}
return 1;
}
void print()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
static void main()
{
System.out.println("Enter Matrix Size:");
int mm=sc.nextInt();
int nn=sc.nextInt();
System.out.print("Enter value of 1st matrix: ");
EqMat p=new EqMat(mm,nn);
p.readArray();
System.out.print("Enter value of 2nd matrix: ");
EqMat q=new EqMat(mm,nn);
q.readArray();
System.out.println("1st matrix: ");
p.print();
System.out.println("2nd matrix: ");
q.print();
if(p.check(p,q)==1)
System.out.print("Equal Matrixes");
else
System.out.print("Non-equal Matrixes");
}
}

Page | 54
DESCRIPTION TABLE:

VARIABLE DATA DESCRIPTION


NAME TYPE
A[][] int Stores matrix
m int Stores the rows.
n int Stores the columns.
mm,nn int Stores the parameterized value given by user.
p,q EqMat Stores the matrix of each object.

OUTPUT:

Page | 55
PROGRAM 19:

A class 'Author' contains details:


DATA MEMBERS
name: to store name of the author.
phoneno : to store phone number of the author.
MEMBER METHODS
Author(String N,long n): to initialize the data members of this class.
void show(): to display the data members of this class.

A class 'Booklist' contains details:


DATA MEMBERS
book_no: to store the number of the book.
book_name: to store the name of the book.
price: to store the price of the book.
edition: to store the edition of the book.
MEMBER METHODS
Booklist(String N,long n,String B_na,long B_no,double p,String edi): to initialize the
data members of both classes.
void show(): to display the data members of both classes.

Define the superclass 'Author'.


Create a main() method of the subclass 'Booklist' and call its functions.

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘Author’ is created.
Step 3: The variables ‘Author_No’ with int datatype and ‘name’ with string datatype is globally
declared.
Step 4: A constructor is used to initialize the variables of class ‘Author’.
Step 5: Method show() is created to show the variables.
Step 6: : A class named ‘Booklist’ is created which inherits the class ‘Author’.
Step 7: The variables ‘edition’, ’price’, ‘Book_Name’ and ‘Book_No’ is globally declared.
Step 8: A constructor is used to initialize the variables of class ‘Booklist’.
Step 9: Method show() is created to show the variables.
Step 10: The main method is created.
Step 11: The inputs are given by the user.
Step 12: These are then passed trough the parameterized constructor.
Step 13: All the other methods are called here.
Step 14: End.

SOURCE CODE:

class Author
{

Page | 56
String name;
long Author_No;
Author()
{
name="";
Author_No=0;
}
Author(String N,long n)
{
name=N;
Author_No=n;
}
void show()
{
System.out.println("Name of Author: "+name);
System.out.println("Author's Number: "+Author_No);
}
}

import java.util.*;
class Booklist extends Author
{
long Book_No;
String Book_Name;
float price;
int edition;
Booklist(String N,long n,String B_na,long B_no,float p,int edi)
{
super.name=N;
super.Author_No=n;
Book_Name=B_na;
Book_No=B_no;
price=p;
edition=edi;
}
void show()
{
super.show();
System.out.println("Name of Book: "+Book_Name);
System.out.println("Book Number: "+Book_No);
System.out.println("Price of Book: "+price);
System.out.println("Edition of Book: "+edition);
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter name of Author: ");
String N=sc.nextLine();
System.out.print("Enter Author's Number: ");

Page | 57
long n=sc.nextLong();
System.out.print("Enter name of Book: ");
String B_na=sc.next()+sc.nextLine();
System.out.print("Enter Book Number: ");
long B_no=sc.nextLong();
System.out.print("Enter price of Book: ");
float p=sc.nextFloat();
System.out.print("Enter edition of Book: ");
int edi=sc.nextInt();
Booklist ob=new Booklist(N,n,B_na,B_no,p,edi);
ob.show();
}
}

DESCRIPTION TABLE:

VARIABLE DATA DESCRIPTION


NAME TYPE
name String Stores the name of Author.
Author_No long Stores the Author number.
Book_No long Stores the Book Number.
Book_Name String Stores the Book Name.
price float Stores the price of Book.
edition int Stores the Edition of Book.
N , B_na String For Passing values through constructors.
n , B_no long For Passing values through constructors.

OUTPUT:

Page | 58
PROGRAM 20:

A class 'Bank' contains details:


Data members:
name: to store name of the customer.
accno: to store account number of the customer.
p: to store the net amount in the customer's account.
Member methods:
Bank(String na,long acc,double pr): to initialize the data members of this class.
void display(): to display the data members of this class.

A class 'Account' contains details:


Data members
amt: to store the amount to be deposited or withdrawn.
Member methods
Account(String na,long acc, double pr, double am): to initialize the data members of both classes.
void deposit(): to deposit the amount(amt).
void withdrawal(): to withdraw the amount(amt) else display “NOT ENOUGH BALANCE”.
void display(): to display the data members of both classes.

Define the superclass 'Bank'.


Create a main() method of the subclass 'Account' and call its functions..

ALGORITHM:

Step 1: Start.
Step 2: A superclass named ‘Bank’ is created.
Step 3: The variables ‘accno’ with int datatype, ‘name’ with string datatype and ‘p’ with datatype
double is globally declared.
Step 4: A constructor is used to initialize the variables of class ‘Bank’.
Step 5: Method display() is created to show the variables.
Step 6: : A class named ‘Account’ is created.
Step 7: The variables ‘amt’ is globally declared.
Step 8: A constructor is used to initialize the variables of class ‘Account’.
Step 9: Method display() is created to show the variables.
Step 10: The main method is created.
Step 11: All the other methods are called here.
Step 12: End.

SOURCE CODE:

class Bank
{
String name;
int accno;
double p;
Bank(String na,int acc,double pr)
{
name=na;

Page | 59
accno=acc;
p=pr;
}
void display()
{
System.out.println("Name: "+name);
System.out.println("Account Number: "+accno);
System.out.println("Principal Amount: "+p);
}
}

import java.util.*;
class Account extends Bank
{
double amt;
static Scanner sc=new Scanner(System.in);
Account(String n,int ac,double pri, double am)
{
super(n,ac,pri);
amt=am;
}
void deposit()
{
amt=p+amt;
}
void withdrawal()
{
if(amt>p)
System.out.print("Not enough balance available. Balance: "+p);
else
amt=p-amt;
}
void display()
{
super.display();
System.out.print("Balance in bank: "+amt);
}
static void main()
{
System.out.print("Enter the name: ");
String n=sc.next()+sc.nextLine();
System.out.print("Enter the Account number: ");
int ac=sc.nextInt();
System.out.print("Enter the Principal Amount: ");
double pri=sc.nextDouble();
System.out.print("Enter the Amount: ");
double am=sc.nextDouble();
Account ob=new Account(n,ac,pri,am);

Page | 60
System.out.println("Choose: \n 1:Withdraw 2:Deposit");
int ch=sc.nextInt();
if(ch==1)
ob.withdrawal();
else if(ch==2)
ob.deposit();
else
System.out.print("Invalid Choice");
ob.display();
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


name String Stores the Name.
acc int Stores the Account number.
p double Stores the Principal amount.
amt double Stores the amount given by user.

OUTPUT:

Page | 61
PROGRAM 21:

Interface name: Data


Data member:
double pi: initialize pi = 3.142
Member functions/methods:
double volume():

Class name: Base


Data member/instance variable:
rad: to store the radius in decimal
Member functions/methods:
Base(…): parameterized constructor to initialize the data member
void show(): displays the radius with an appropriate message

Class name: CalVol


Data member/instance variable:
ht: to store the height in decimal
Member functions/methods:
CalVol(…): parameterized constructor to initialize the data members of both the classes
double volume(): calculates the volume of a sphere by using the formula (pi × radius2 × height)
void show(): displays the data members of both the classes and the volume of the cylinder with
appropriate message.

Assume that the interface Data and the super class Base has been defined. Using the concept of
inheritance, specify the class CalVol giving the details of the constructor(…), double volume() and void
show().

ALGORITHM:

Step 1: An interface ‘Data is created.


Step 2: The variable ‘pi’ of double datatype is globally declared and initialised pi=3.142.
Step 3: An abstract method ‘volume()’ is created.
Step 4: A class named ‘Base’ is created.
Step 5: The variable ‘rad’ of double datatype is globally declared.
Step 6: A parameterized constructor ‘Base(..)’ is created to initialise rad=r.
Step 7: A method ‘show()’ is created to display the radius.
Step 8: A class named ‘CalVol’ is created which inherits the properties of the class ‘Base’.
Step 9: The variable ‘ht’ of double datatype is globally declared.
Step 10: A parameterised constructor is created to initialise the variables of both the class ‘Base’ and
‘CalVol’.
Step 11: A method named ‘volume()’ is created which returns the value after calculating it.
Step 12: A method named ‘display()’ is created to display the inputs and its corresponding output.
Step 13: A ‘main()’ method is created.
Step 14: The radius and height are taken as input from the user and then passed through the
constructor.
Step 15: All the methods are then call accordingly.
Step 16: Stop

Page | 62
SOURCE CODE:

public interface Data


{
double pi = 3.142;
double volume();
}

class Base
{
double rad;
Base(double r)
{
rad = r;
}
void show()
{
System.out.println("Radius: " + rad);
}
}

import java.util.*;
class CalVol extends Base implements Data
{
double ht;
CalVol(double radius, double h)
{
super(radius);
ht = h;
}
public double volume()
{
double vol=pi*ht*rad*rad;
return vol;
}
void display()
{
super.show();
System.out.println("Height: " + ht);
System.out.println("Volume of the cylinder: " + volume());
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Radius: ");
double radius=sc.nextInt();
System.out.print("Enter Height: ");
double h=sc.nextInt();
CalVol ob=new CalVol(radius,h);

Page | 63
ob.volume();
ob.display();
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


pi double Stores the value ‘3.142’.
rad double Stores the radius.
ht double Stores the height.
r, radius double Stores the radius given by user.
h double Stores the height given by user.

OUTPUT:

Page | 64
PROGRAM 22:

Register is an entity which can hold a total of 100 names. It enables a user to add and remove names
from the top most end only. Define a class ‘register’:
DATA MEMBERS:
stud[] : to store the names.
cap : to store the maximum capacity
top : to store the index of the top end
METHODS:
Register(int max): Initialise cap=max, top=-1;
void push(String n): To add names at top is possible else display ‘overflow’
String pop(): Removes and shows the names from top-most position if any else return ‘$$’
void display(): Display names in register.
Specify the class functions and the main method.

ALGORITHM:

Step 1: A class ‘register’ is created.


Step 2: Variables ‘cap’ and ‘top’ of int datatypes and ‘stud[]’ of String datatype are globally declared.
Step 3: A parameterized contructor is used for initialization as ‘cap=max’, ‘stud=new String[cap]’ and
‘top=-1’.
Step 4: A parameterized method ‘push()’ is created to store the names given by user.
Step 5: If the number of names exceed the maximum capacity, an appropriate message is shown.
Step 6: A method ‘pop()’ is created to display the name from the end of the array. If no element
present then an appropriate message is shown.
Step 7: A method ‘display()’ is created to show the names in the Stack.
Step 8: A ‘main()’ method is created to input the size of the Stack and passed through the constructor.
Step 9: A menu is shown to the user and his choice is asked.
Step 10: The appropriate options are done according to the user’s choice.
Step 11: Stop

SOURCE CODE:

import java.util.Scanner;
class register
{
String stud[];
int cap;
int top;
register(int max)
{
cap=max;
top=-1;
}
void push(String n)
{
Scanner sc=new Scanner (System.in);
if(top==cap-1)
{

Page | 65
System.out.println("overflow");
}
else
{
stud[++top]=n;
}
}
String pop()
{
String element="";
if(top==-1)
System.out.println("$$");
else
{
element=stud[top--];
System.out.println(element);
}
return element;
}
void display()
{
System.out.println("the names in the register are");
Scanner sc=new Scanner (System.in);
for(int i=0;i<=top;i++)
{
System.out.println(stud[i]);
}
}
static void main()
{
Scanner sc=new Scanner (System.in) ;
int n=0;
String name="";
stud=new String[cap];
System.out.println("Enter the size of the stack");
n=sc.nextInt();
register ob=new register(n);
int ch=0;
while(ch!=4)
{
System.out.println("1 for Push \t 2 for Pop \t 3 for Stack Status \t 4 for Exit");
System.out.println("Enter your choice");
ch=sc.nextInt();
switch (ch)
{
case 1:
System.out.println("Enter the names");
name=sc.next();
ob.push(name);

Page | 66
break;
case 2:
ob.pop();
break;
case 3:
ob.display();
break;
case 4:
System.exit(0);
}
}
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


ch int Stores the choice given by the user.
n int Stores the size to be passed to the constructor.
cap int Stores the maximum capacity of the Stack.
top int Stores the index of top end.
stud[] String Stores the Stack
name String Stores the names given by user.
element String Stores the names to be shown using pop().

Page | 67
OUTPUT:

Page | 68
PROGRAM 23:
Strange is an entity which can go at the most 20 integers. Its restriction is that the elements can only
be added from top or removed from top.
Class name: Strange
DATA MEMBERS:
ele[]: to store the elements.
capacity: to store the maximum capacity
top: to store the index of the top end
METHODS:
Strange(int max): Initialise cap=max, top=-1;
void push_element(int value): To add names at top is possible else display 'Strange is full’.
int pop_item(): Removes and shows the names from top-most position if any else return ‘-999’ and
show ‘Strange is empty’.
void display(): Display names in register.
Specify the class functions and the main method.
ALGORITHM:
Step 1: A class ‘register’ is created.
Step 2: Variables ‘cap’ and ‘top’ of int datatypes and ‘stud[]’ of String datatype are globally declared.
Step 3: A parameterized contructor is used for initialization as ‘cap=max’, ‘stud=new String[cap]’ and
‘top=-1’.
Step 4: A parameterized method ‘push()’ is created to store the names given by user.
Step 5: If the number of names exceed the maximum capacity, an appropriate message is shown.
Step 6: A method ‘pop()’ is created to display the name from the end of the array. If no element
present then an appropriate message is shown.
Step 7: A method ‘display()’ is created to show the names in the Stack.
Step 8: A ‘main()’ method is created to input the size of the Stack and passed through the constructor.
Step 9: A menu is shown to the user and his choice is asked.
Step 10: The appropriate options are done according to the user’s choice.
Step 11: Stop
SOURCE CODE:
import java.util.*;
class Strange
{
int ele[], capacity, top;
public Strange(int max)
{
capacity = max;
top = 0;
ele = new int[capacity];
}
void push_element(int value)
{
if (top < capacity )
ele[top++] = value;
else
System.out.println("Strange is full.");

Page | 69
}
int pop_item()
{
if (top >0)
return ele[--top];
else
{
System.out.println("Strange is empty.");
return -999;
}
}
void display()
{
for (int i = 0; i < top; i++)
System.out.print(ele[i] + " ");
System.out.println();
}
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the maximum capacity: ");
int max = sc.nextInt();
Strange ob = new Strange(max);
while (true)
{
System.out.println("1. Push element \t 2. Pop element \t 3. Display elements \t 4. Exit");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
switch (ch)
{
case 1:
System.out.print("Enter the element to push: ");
int value = sc.nextInt();
ob.push_element(value);
break;
case 2:
int x = ob.pop_item();
System.out.println("Popped: " + x);
break;
case 3:
ob.display();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}

Page | 70
}
}
DESCRIPTION TABLE:
VARIABLE NAME DATA TYPE DESCRIPTION
ch int Stores the choice given by the user.
max int Stores the size to be passed to the constructor.
capacity int Stores the maximum capacity of the Stack.
top int Stores the index of top end.
ele[] int Stores the Stack
value int Stores the element given by user.
x int Stores the names to be shown using pop().

OUTPUT:

Page | 71
PROGRAM 24:

A queue is a linear data structure in which the operations are performed based on FIFO(First in First
Out).
Class: Queue
Data Members:
dat[]: Array to hold integers
cap: Maximum capacity of the queue
front: Index of front
rear: Index of rear

Data function:
Queue(int max): cap=max, front=0, rear=0, initialise array
void add_dat(int v): to add integer from the rear if possible else diplay “Queue is full”.
int pop_dat(): to remove integer from the front if possible else return -999.
void display(): view elements of the queue.
Specify the class functions and main method.

ALGORITHM:

Step 1: Start
Step 2: A class 'Queue’ is created.
Step 3: Variables ‘cap', 'front’, ‘dat[]’ and 'rear’ of int datatypes are globally declared.
Step 4: A parameterized contructor is used for initialization as cap=max, dat=new int[cap], rear=0 and
front=0.
Step 5: A parameterized method 'add_dat()’ is created to store the elements given by user.
Step 6: If rear=cap then the message ‘Queue is full shown else the element is inserted.
Step 7: A method ‘pop_dat()’ is created to display the elements from the end of the array.
Step 8: If rear=front , return ‘-999’ else return the element from the front position.
Step 9: A method ‘display()’ is created to show the elements in the Queue.
Step 10: A ‘main()’ method is created to input the size of the Queue and passed through the
constructor.
Step 11: A menu is shown to the user and his choice is asked.
Step 12: The appropriate options are done according to the user’s choice.
Step 13: End

SOURCE CODE:

import java.util.*;
class Queue
{
int dat[], cap, front, rear;
Queue(int max)
{
cap = max;
front = 0;
rear = 0;
dat = new int[cap];
}

Page | 72
void add_dat(int v)
{
if (rear == cap)
System.out.println("Queue is full");
else
{
dat[rear] = v;
rear++;
}
}
int pop_dat()
{
if (front == rear)
return -999;
else
{
int temp = dat[front];
front++;
return temp;
}
}
void display()
{
if (front == rear)
System.out.println("Queue is empty");
else
{
for (int i = front; i < rear; i++)
System.out.print(dat[i] + " ");
System.out.println();
}
}
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the maximum capacity of the queue: ");
int max = sc.nextInt();
Queue ob = new Queue(max);
while (true)
{
System.out.println("1. Add element to the queue \t 2. Remove element from the queue \t 3.
Display the queue \t 4. Exit");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
switch (ch)
{
case 1:
System.out.print("Enter the element to add: ");
int x = sc.nextInt();

Page | 73
ob.add_dat(x);
break;
case 2:
int rev = ob.pop_dat();
if (rev != -999)
System.out.println("Removed element: " + rev);
else
System.out.println("Queue is empty");
break;
case 3:
ob.display();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


dat[] int Array to store the elements.
max int Stores the size to be passed to the constructor.
cap int Stores the maximum capacity of the Queue.
front int Stores the index of front end.
ch int Stores choice of the user.
rear int Stores the index of rear end.
x int Stores the to be insterted.
rev int Stores the returned value

Page | 74
OUTPUT:

Page | 75
PROGRAM 25:

A link is an entity which can hold a maximum of 100 elements. Link enables user to
Class: Link
Data Members:
lnk[]: Array to hold integers
max: Maximum capacity of the queue
begin: Index of front
end: Index of rear

Data function:
Link(int mm): max=mm, begin=0, end=0, initialise array
void add_link(int v): to add integer from the rear if possible else display “Out of Range”.
int del_link(): to remove integer from the front if possible else display ‘Empty’ and
return -999.
void display(): Display elements of the queue.
Specify the class functions and main method.

ALGORITHM:

Step 1: Start
Step 2: A class ‘Link’ is created.
Step 3: Variables ‘max’, ‘begin’, ‘lnk[]’ and ‘end’ of int datatypes are globally declared.
Step 4: A parameterized contructor is used for initialization as max=mm, lnk=new int[max], end=0 and
begin=0.
Step 5: A parameterized method 'add_link()’ is created to store the elements given by user.
Step 6: If end=max then the message ‘Out of Range’ shown else the element is inserted.
Step 7: A method ‘del_link()’ is created to display the elements from the end of the array.
Step 8: If begin=end , return ‘-999’ else return the element from the front position.
Step 9: A method ‘display()’ is created to show the elements in the entity.
Step 10: A ‘main()’ method is created to input the size of the entity and passed through the
constructor.
Step 11: A menu is shown to the user and his choice is asked.
Step 12: The appropriate options are done according to the user’s choice.
Step 13: End

SOURCE CODE:

import java.util.*;
class Link
{
int lnk[], max, begin, end;
Link(int mm)
{
max = mm;
begin = 0;
end = 0;

Page | 76
lnk = new int[max];
}
void add_lnk(int v)
{
if (end == max)
System.out.println("Out of Range");
else
{
lnk[end] = v;
end++;
}
}
int pop_lnk()
{
if (begin == end)
{
System.out.println("Empty");
return -999;
}
else
{
int temp = lnk[begin];
begin++;
return temp;
}
}
void display()
{
for (int i = begin; i < end; i++)
System.out.print(lnk[i] + " ");
System.out.println();
}
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the mmimum maxacity of the Link: ");
int mm = sc.nextInt();
if(mm>100 && mm<1)
{
Link ob = new Link(mm);
while (true)
{
System.out.println("1. Add element to the Link \t 2. Remove element from the Link \t 3.
Display the Link \t 4. Exit");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
switch (ch)
{
case 1:

Page | 77
System.out.print("Enter the element to add: ");
int x = sc.nextInt();
ob.add_lnk(x);
break;
case 2:
int rev = ob.pop_lnk();
if (rev != -999)
System.out.println("Removed element: " + rev);
else
System.out.println("Link is empty");
break;
case 3:
ob.display();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
else
System.out.println("Out of maximum capacity. Maximum Capacity: 100");
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


lnk[] int Array to store the elements.
mm int Stores the size to be passed to the constructor.
max int Stores the maximum capacity of the Queue.
begin int Stores the index of front end.
ch int Stores choice of the user.
end int Stores the index of rear end.
x int Stores the to be insterted.
rev int Stores the returned value

Page | 78
OUTPUT:

Page | 79
PROGRAM 26:

Class: DeQueue
Data Members:
qrr[]: Array to hold integers
lim: Maximum capacity of the queue
front: Index of front
rear: Index of rear

Data Function:
DeQueue(int l): lim=max, front=0, rear=0, size=0 initialise array
void add_front(int v): to add integer from the front end if possible else display “Full
from front”.
void add_rear(int v): to add integer from the rear end if possible else display “Full from
rear”.
int pop_front(): to remove and return integer from the front end if possible else return
-999.
int pop_rear(): to remove and return integer from the rear end if possible else return -
999.
void show(): view elements of the queue.
Specify the class functions and main method.

ALGORITHM:

Step 1: Start
Step 2: A class 'DeQueue’ is created.
Step 3: Variables ‘lim’, 'front’, ‘qrr[]’ and 'rear’ of int datatypes are globally declared.
Step 4: A parameterized contructor is used for initialization as lim=max, qrr=new int[lim], rear=0 and
front=0.
Step 5: A parameterized method ‘add_front(int v)’ is created to store the elements by the user from
the front end if possible else display “Full from front”.
Step 6: A parameterized method ‘add_rear(int v)’ is created to store the elements by the user from
the rear end if possible else display “Full from rear”.
Step 7: A method ‘pop_front()’ is created to remove and return the elements from the front end of
the array if possible else return -999.
Step 8: A method ‘pop_rear()’ is created to remove and return the elements from the rear end of the
array if possible else return -999.
Step 9: A method ‘show()’ is created to show the names in the Queue.
Step 10: A ‘main()’ method is created to input the size of the Queue and passed through the
constructor.
Step 11: A menu is shown to the user and his choice is asked.
Step 12: The appropriate functions are executed according to the user’s choice.
Step 13: End

Page | 80
SOURCE CODE:

import java.util.*;
class DeQueue
{
int qrr[], lim, front, rear;
DeQueue(int max)
{
lim = max;
qrr = new int[cap];
front = 0;
rear = 0;
}
void add_front(int v)
{
if (front == 0)
System.out.println("Full from front");
else
qrr[--front] = v;
}
int pop_front()
{
if (front == rear)
return -999;
else
return qrr[front++];
}
void add_rear(int v)
{
if (rear == lim)
System.out.println("Full from rear");
else
qrr[rear++] = v;
}
int pop_rear()
{
if (front == rear)
return -999;
else
return qrr[--rear];
}

void show()
{
for (int i = front; i < rear; i++)
System.out.print(qrr[i] + " ");
System.out.println();
}

Page | 81
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the maximum capacity: ");
int max = sc.nextInt();
DeQueue ob = new DeQueue(max);
while (true)
{
System.out.println("1. Push from Front \t 2. Push from rear \t 3. Pop from front \t 4. Pop from
rear \t 5. Display from rear \t 6. Exit");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
switch (ch)
{
case 1:
System.out.print("Enter the element to push: ");
int xf = sc.nextInt();
ob.add_front(xf);
break;
case 2:
System.out.print("Enter the element to push: ");
int xr = sc.nextInt();
ob.add_rear(xr);
break;
case 3:
System.out.println("Popped: " + ob.pop_front());
break;
case 4:
System.out.println("Popped: " + ob.pop_rear());
break;
case 5:
ob.show();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}

Page | 82
DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


qrr[] int Array to store the elements.
l int Stores the size to be passed to the constructor.
lim int Stores the maximum capacity of the Queue.
front int Stores the index of front end.
ch int Stores choice of the user.
rear int Stores the index of rear end.
xf, xr int Stores the element given by user.

OUTPUT:

Page | 83
PROGRAM 27:

Class: Cir_Queue
Data Members:
cQ[]: Array to hold integers
cap: Maximum capacity of the queue
front: Index of front
rear: Index of rear
size: No. of elements present in Queue
Data Function:
Cir_Queue(int max): cap=max, front=0, rear=0, size=0 initialise array
void push(int v): to add integer from the rear if possible else diplay “Queue is full”.
int pop(): to remove integer from the front if possible else return -999.
void show(): view elements of the queue.
Specify the class functions and main method.

ALGORITHM:

Step 1: Start
Step 2: A class 'Cir_Queue’ is created.
Step 3: Variables ‘cap', ‘size’, 'front’, ‘cQ[]’ and 'rear’ of int datatypes are globally declared.
Step 4: A parameterized contructor is used for initialization as cap=max, cQ=new int[cap], size=0,
rear=0 and front=0.
Step 5: A parameterized method ‘push()’ is created to store the elements by the user from the rear
end.
Step 6: If the number of names exceed the maximum capacity, an appropriate message is shown.
Step 7: A method ‘pop()’ is created to return the elements from the front end of the array.
Step 8: If no element present then an appropriate message is shown.
Step 9: A method ‘display()’ is created to show the names in the Queue.
Step 10: A ‘main()’ method is created to input the size of the Queue and passed through the
constructor.
Step 11: A menu is shown to the user and his choice is asked.
Step 12: The appropriate functions are executed according to the user’s choice.
Step 13: End

SOURCE CODE:

import java.util.*;
class Cir_Queue
{
int cQ[], cap, front, rear, size;
Cir_Queue(int max)
{
cap = max;
front = 0;
rear = 0;
size = 0;

Page | 84
cQ = new int[cap];
}
void push(int n)
{
if (size == cap)
System.out.println("Queue is full");
else
{
cQ[rear] = n;
rear = (rear + 1) % cap;
size++;
}
}
int pop()
{
if (size == 0)
return -999;
int temp = cQ[front];
front = (front + 1) % cap;
size--;
return temp;
}
void show()
{
if (size == 0)
{
System.out.println("Queue is empty");
return;
}
for (int i = 0; i < size; i++)
System.out.print(cQ[(front + i) % cap] + " ");
System.out.println();
}
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the capacity of the queue: ");
int max = sc.nextInt();
Cir_Queue ob = new Cir_Queue(max);
while (true)
{
System.out.println("1. Add element to the queue \t 2. Remove element from the queue \t 3.
Display the queue \t 4. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter an integer to push: ");

Page | 85
int n = sc.nextInt();
ob.push(n);
break;
case 2:
int popped = ob.pop();
if (popped != -999)
System.out.println("Popped: " + popped);
else
System.out.println("Queue is empty");
break;
case 3:
ob.show();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


cQ[] int Array to store the elements.
max int Stores the size to be passed to the constructor.
cap int Stores the maximum capacity of the Queue.
front int Stores the index of front end.
size int Stores the number of elements in the Queue.
ch int Stores choice of the user.
rear int Stores the index of rear end.
n int Stores the element given by user.
temp int Stores the returned value

Page | 86
OUTPUT:

Page | 87
PROGRAM 28:

Design a program to make a Date-Calender from no. of days 1-366 and year in 4-digits
from the user to generate and display the corresponding date and also display the
future date. (1<=N<=100)

ALGORITHM:

Step 1: Start.
Step 2: A class named ‘Date_Calender ‘ is created.
Step 3: A parameterized constructor ‘Date_Calender(….)’ is created.
Step 4: An int array ‘days[]’ is created to store the no. of days of the corresponding months in string
array ’months[]’.
Step 5: The int variable ‘year’, ‘N’, ‘ndays’ are initialised.
Step 6: A Boolean type function ‘leap_check()’ is formed.
Step 7: It is checked if the given year is divisible by 400 or only divisible by 4 but not 100.
Step 8: A method ‘Current_Date_Cal()’ is created to calculate the current date.
Step 9: It compares the ‘sum’ with ‘ndays’ and calculates the present date.
Step 10: A method ‘Future_Date_Cal()’ is created to calculate the future date.
Step 11: It compares the ‘sum’ with ‘x’ and calculates the future date.
Step 12: The main method is created.
Step 13: All the methods are called.
Step 14: Stop.

SOURCE CODE:

import java.util.*;
class Date_Calender
{
int days[]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int year, N, ndays, date;
String months[]= {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
Date_Calender(int y,int nn,int nd)
{
year=y;
N=nn;
ndays=nd;
date=0;
}
boolean leap_check()
{
boolean ret=false;
if((year%400==0) || (year%4==0 && year%100!=0))
{
days[1]=29;
ret=true;
}
return ret;

Page | 88
}
void Current_Date_Cal()
{
int i=-1,sum=0;
while(sum<ndays)
sum=sum+days[++i];
date=days[i]-(sum-ndays);
String l="";
if(date%10==1)
l="st";
else if(date%10==2)
l="nd";
else if(date%10==3)
l="rd";
else
l="th";
System.out.println("Current Date: "+date+""+l+" "+months[i]+" "+year);
}
void Future_Date_Cal()
{
int i=-1,sum=0;
int x=ndays+N;
while(sum<x)
{
sum=sum+days[++i];
if(leap_check()==true)
{
if(sum>366)
{
sum=sum-366;
x=x-366;
i=0;
year=year+1;
}
}
else
{
if(sum>365)
{
sum=sum-365;
x=x-365;
i=0;
year=year+1;
}
}
}
date=days[i]-(sum-x);
String l="";
if(date%10==1)

Page | 89
l="st";
else if(date%10==2)
l="nd";
else if(date%10==3)
l="rd";
else
l="th";
System.out.println("Future Date: "+date+""+l+" "+months[i]+" "+year);
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. days: ");
int nd=sc.nextInt();
System.out.print("Enter N days: ");
int nn=sc.nextInt();
System.out.print("Enter the year: ");
int y=sc.nextInt();
Date_Calender ob=new Date_Calender(y,nn,nd);
if(ndays>=1 && ndays<=366 && N>=1 && N<=100)
{
ob.Current_Date_Cal();
ob.Future_Date_Cal();
}
}
}

DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


days[] int Array to store the no. of Days.
months[] String Array to store the name of months.
year int Stores the year given by user.
N int Stores the value of N.
ndays int Stores choice of the user.
l String Stores the abbreviation of days.
date int Stores the actual date after calculating
x int Stores sum of ndays and N

Page | 90
OUTPUT:

Teacher’s Signature Examiner’s Signature

Page | 91

You might also like