Sunteți pe pagina 1din 39

Question 1:

Define a class Dequeue with the following details:


Class name : Dequeue
Data members :
arr[] : integer array to hold upto 100 integer elements
lim : stores the limit of the dequeue
front : to point the index of front end
rear : to point the index of rear end

Member functions :
Dequeue(int L) : constructor to initialize the lim=L, front=0 and
rear=0.

void addfront(int val) : to add integer ‘val’ from front if


possible, otherwise display a message “OVERFLOW FROM FRONT”
void addrear(int val) : to add integer ‘val’ from rear if possible,
otherwise display a message “OVERFLOW FROM REAR”
void popfront() : to remove and return an integer from front if
possible, otherwise display a message “UNDERFLOW FROM FRONT” and
return -9999
void poprear() : to remove and return an integer from rear if
possible, otherwise display a message “UNDERFLOW FROM REAR” and
return -9999

void display() : to print elements currently in dequeue, if


possible otherwise display the message “UNDERFLOW DEQUEUE”

Specify the class Dequeue.

Algorithm:

1. Declare the class.


2. The initial values of variables are stored using a constructor.
3. Declare a function to add elements from rear of dequeue.
4. It is checked whether the dequeue is full from rear or not.
5. The value is inserted from rear of Dequeue.
6. Declare a function to add elements from front of Dequeue.
7. It is checked whether the Dequeue is full from the front and if not
the elements are inputted from front of the Dequeue.
8. Declare a function to pop elements from front of Dequeue.
9. It is checked whether the Dequeue is empty from front and if not,
pops an element from front of Dequeue
10. Declare a function to pop elements from rear of Dequeue.
11. It is checked whether the Dequeue is empty from rear and if not
an element is popped and returned.
12. Declare a function to display the Dequeue.
13. Declare the main function and call the member methods accordingly.
14. End the class.
import java.util.*;
public class Dequeue
{
int arr[]=new int[100]; //declaration of array that acts as dequeue
int lim,front,rear;
Dequeue(int L)
{
lim=L;
front=0;
rear=-1;
}
void addrear(int val)
{
if(rear==lim)//checks whether dequeue is full from rear or not
System.out.println("OVERFLOW FROM REAR");
else
{
if(rear==-1)
rear=0;
arr[rear]=val;//value is inserted from rear of dequeue
rear++;
}
}
void addfront(int val)
{
if(rear!=-1&&front>0&&front<=rear)
{
front--;
arr[front]=val;//value is inserted from front of dequeue
}
else
System.out.println("OVERFLOW FROM FRONT");//checks whether
dequeue is full from front or not
}
int popfront()
{
if(rear==-1||front>rear||arr[front]==0)
{
System.out.println("UNDERFLOW FROM FRONT");//checks
whether dequeue is empty from front or not
return (-9999);
}
else
{
//pops the element
int temp=arr[front];
front++;
return temp;//returns the popped element
}
}
int poprear()
{
//pops element from rear
if(rear>=0&&rear<lim-1)
{
int temp=arr[rear];
rear--;
return temp;//returns popped element
}
else
{
System.out.println("UNDERFLOW FROM REAR");//checks whether
dequeue is empty from rear or not
return(-9999);
}
}

void display()
{
if(rear==-1 || front>rear)
System.out.println("UNDERFLOW DEQUEUE");//prints if the
dequeue is empty or not
else
{
//dequeue is printed
System.out.println("The elements in the dequeue are as:");
for(int j=front;j<=rear;j++)
System.out.println(arr[j]+"\t");
}
}
public static void main(String args[])
{
Dequeue obj=new Dequeue(5);
//the required member methods are called accordingly
obj.display();
obj.addrear(12);
obj.addrear(56);
obj.addrear(7);
obj.display();
obj.addfront(74);
obj.display();
int x=obj.popfront();
obj.display();
int r=obj.poprear();
obj.display();
}
}
Variable description:

Variable name Data type Purpose

arr[] int Array variable storing


the Dequeue

lim int Stores size of Dequeue

front int Marks front of Dequeue

rear int Marks rear of Dequeue

val int Stores the number


to be pushed

x int Stores popped element

r int Stores popped element

Output:
Question 2:

Write a program to count the number of nodes present in an existing linked list.

Algorithm:

1. Create a class.
2. Assign initial values to instant variables using a constructor.
3. Declare a function to create a linked list.
4. The elements of the linked list are taken as input.
5. Declare a function to count the number of nodes.
6. The nodes are counted by using a while loop until the list comes to null.
7. Declare the display function to display the linked list.
8. Create the main function and call the member methods accordingly.
9. End the class.
import java.util.*;
class node2
{
int data;
node2 link;
node2()
{
data=0;
link=null;
}
void create()
{
//creating linked list structure
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the first data");
this.data=in.nextInt();
System.out.println("Enter number of nodes to be created");
n=in.nextInt();
node2 temp;
node2 ptr=this;
for(int i=1;i<n;i++)
{
temp=new node2();
System.out.println("Enter next data");
temp.data=in.nextInt();
temp.link=null;
ptr.link=temp;
temp=null;
ptr=ptr.link;
}
}
void countnodes(node2 start)
{
int count=0;
node2 temp=new node2();
temp=start;
while(temp!=null)
{
count++;
temp=temp.link;
}
System.out.println("Total number of nodes in linked list are = "+count);
}
void display()
{
node2 ptr=this;
while(ptr!=null)
{
System.out.println(ptr.data);
ptr=ptr.link;
}
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
node2 first=new node2();
first.create();
System.out.println("The linked list is");
first.display();
first.countnodes(first);
}
}
Variable description:

Variable name Data type Purpose

data int Stores elements in linked list

link node2 linking variable

Output:
Question 3:
Write a program to insert elements at the beginning of a linked list.

Algorithm:

1. Create a class.
2. Assign initial values to instant variables using a constructor.
3. Declare a function to create a linked list.
4. The elements of the linked list are taken as input.
5. Declare a function to insert elements at the beginning of linked list.
6. The element is inserted with the procedure.
7. Declare the display function to display the linked list.
8. Create the main function and call the member methods accordingly.
9. End the class.
import java.util.*;
class node
{
int data;
node link;
node()
{
data=0;
link=null;
}
void create()
{
//creating linked list structure
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the first data");//first data is inputted
this.data=in.nextInt();
System.out.println("Enter number of nodes to be created");
n=in.nextInt();
node temp;
node ptr=this;
for(int i=1;i<n;i++)
{
temp=new node();
System.out.println("Enter next data");
temp.data=in.nextInt();
temp.link=null;
ptr.link=temp;
temp=null;
ptr=ptr.link;
}
}
void insertbeg(node start,int x)
{
//inserting a list in the beginning
node temp=new node();//allocate memory to new node to object temp
temp=start;
node NewNode,num;//objects NewNode and num are declaredfor the
class node
NewNode=new node();//allocate memory to the object NewNode
NewNode.data=x;//assign x to variable data through NewNode
NewNode.link=temp;//link object temp containing linked list to
the next of object NewNode
temp=NewNode;//establish address link of NewNode to temp
start=temp;//refresh list in start
display(start);
}
void display()
{
//displaying the linked list
System.out.println("The linked list is:");
node NewNode=this;
while(NewNode!=null)
{
System.out.println(NewNode.data);
NewNode=NewNode.link;
}
}
public static void main(String args[])
{
int k;
Scanner in=new Scanner(System.in);
node first=new node();
first.create();
System.out.println("Enter data for new node");
k=in.nextInt();
first.insertbeg(first,k);
}
}

Variable description:

Variable name Data type Purpose

data int Stores elements of


linked list

link node Linking variable

n int Stores number of nodes

x int Formal parameter of the


value to be inserted

k int Actual parameter of the


value to be inserted

Output:
Question 4:
Write a program to delete elements at the given node number of a linked list.

Algorithm:

1. Create a class.
2. Assign initial values to instant variables using a constructor.
3. Declare a function to create a linked list.
4. The elements of the linked list are taken as input.
5. Declare a function to delete elements at the given node number of
linked list.
6. The element is deleted with the procedure.
7. Declare the display function to display the linked list.
8. Create the main function and call the member methods accordingly.
9. End the class.
import java.util.*;
class node1
{
int data;
node1 link;
node1()
{
data=0;
link=null;
}
void create()
{
//creating linked list structure
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the first data");
this.data=in.nextInt();
System.out.println("Enter number of nodes to be created");
n=in.nextInt();
node1 temp;
node1 ptr=this;
for(int i=1;i<n;i++)
{
temp=new node1();
System.out.println("Enter next data");
temp.data=in.nextInt();
temp.link=null;
ptr.link=temp;
temp=null;
ptr=ptr.link;
}
}
void delete(node1 start,int m)
{
//deleting a list from linked list structure
node1 ptr=start;
node1 ptr1=ptr;
int c=0;
while(c<=m)
{
ptr1=ptr;
ptr=ptr.link;
c++;
}
ptr1.link=ptr.link;
ptr.link=null;
ptr=null;
ptr1=null;
}
void display()
{
//displaying the linked list
System.out.println("The linked list is");
node1 ptr=this;
while(ptr!=null)
{
System.out.println(ptr.data);
ptr=ptr.link;
}
}
public static void main(String args[])
{
int k;
Scanner in=new Scanner(System.in);
node1 first=new node1();
first.create();
System.out.println("Enter node number to be deleted");
k=in.nextInt();
first.delete(first,k);
first.display();
}
}
Variable description:

Variable name Data type Purpose

data int Stores elements of linked list

link node Linking variable

n int Stores number of nodes

i int for loop variable

c int Counter variable

m int Formal parameter of the


node number to be delete

k int Actual parameter of the


node number to be deleted

Output:
Question 5:

Write a program in java to display the mirror image of a matrix.


Algorithm:

1. Create a class.
2. Declare the main function.
3. Take the number of rows and columns as input.
4. The matrix is declared.
5. The elements of the matrix are taken input.
6. The inputted matrix is printed
7. The mirror image of the matrix is printed by running the inner loop
in the opposite direction from that of time of input.
8. End the function.
9. End the class.
//matrix mirror image
import java.util.*;
public class mirror
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,n ,i,j;
System.out.println("Enter number of rows");//number of rows are inputted
m=in.nextInt();
System.out.println("Enter number of columns");//number of
columns are inputted
n=in.nextInt();
int arr[][]=new int [m][n];//matrix is declared
//the array elements are inputted
System.out.println("Enter matrix elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
arr[i][j]=in.nextInt();
}
//inputted array is printed
System.out.println("The inputted matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println("The mirror image of the matrix is");
//mirror image of the matrix is found and printed
for(i=0;i<m;i++)
{
for(j=n-1;j>=0;j--)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Variable desceiption:

Variable name Data type Purpose

i int for loop variable

j int for loop variable

m int Stores number of rows

n int Stores number of columns


Output:
Question 6:

Write a program in java to check whether two words are anagrams or not.

Algorithm:

1. Create a class.
2. Declare the main function.
3. Take two words as input.
4. Convert them to uppercase and find their lengths.
5. Arrange the letters of each word in alphabetical order using two for loops.
6. Also find the sum of ASCII values of each of the words.
7. If the words are same after arranging them in alphabetical order
and the sum of ASCII values of the two words are also equal, they are
anagrams else not.
8. End the function.
9. End the class.
import java.util.*;
public class anagram
{
public static void main(String args[])
{
int p1,p2,i,j,as1,as2,s1=0,s2=0;
String str1,str2,str3="",str4="";
char chr1,chr2;
Scanner in=new Scanner(System.in);
System.out.println("Enter first word");//first word is inputted
str1=in.next();
str1=str1.toUpperCase();//inputted word is converted to uppercase
System.out.println("Enter second word");//second word is inputted
str2=in.next();
str2=str2.toUpperCase();//inputted word is converted to uppercase
p1=str1.length();//length of first word is found out
p2=str2.length();//length of second word is found out
if(p1==p2)
{
for(i=65;i<=90;i++)
{
for(j=0;j<p2;j++)
{
//characters are extracted from each word
chr1=str1.charAt(j);
chr2=str2.charAt(j);
as1=(int)chr1;
as2=(int)chr2;
//the letters of each word are arranged in
alphabetical order and the sum of the ASCII values of each word is
found out
if(as1==i)
{
str3=str3+chr1;
s1=s1+as1;
}
if(as2==i)
{
str4=str4+chr2;
s2=s2+as2;
}
}
}
if(str3.equals(str4)&&(s1==s2))//required conditions are checked
System.out.println(str1+" & "+str2+" are Anagram words");
else
System.out.println(str1+" & "+str2+" are not Anagram words");
}
else
System.out.println("Wrong input");
}
}

Variable description:

Variable name Data type Purpose

i int for loop variable

j int for loop variable

p1 int Stores length of first word

p2 int Stores length of second word

as1 int Stores ASCII value of


each letter of first word

as2 int Stores ASCII value of


each letter of second word

s1 int Stores sum of ASCII values


of each letter of first word

s2 int Stores sum of ASCII values of each


letter of second word

str1 String Stores first word

str2 String Stores second word

str3 String Stores first word in


alphabetical order

str4 String Stores second word in


alphabetical order

chr1 char Stores each character of first word

chr2 char Stores each character of second word

Output:
For anagram words:

For not anagram words:


Question 7:

Write a program in java to enter a word in mixed case and display the
new token after deleting all the vowels. Finally arrange the token in
alphabetical order.

Algorithm:

1. Create a class.
2. Declare the main function.
3. Take the word as input.
4. Each character of the word is extracted and added to a new string
if it is not a vowel.
5. The new string is printed.
6. The new string is alphabetically arranged by using two for loops.
7. The alphabetically arranged string is printed.
8. End the function.
9. End the class.
import java.util.*;
public class dvowels
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,p,q;
char ch,ch1;
String st,st2="",st3="";
System.out.println("Enter a word");//word is taken as input
st=in.next();
p=st.length();
for(i=0;i<p;i++)
{
ch=st.charAt(i);//each character is extracted
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='U')
continue;
st2=st2+ch;//letters except the vowels are added to the new string
}
System.out.println("The new token formed after deleting vowels "+st2);
q=st2.length();
//the new string is arranged alphabetically
for(i=65;i<=90;i++)
{
for(j=0;j<q;j++)
{
ch1=st2.charAt(j);
if(ch1==(char)i||ch1==(char)(i+32))
st3=st3+ch1;
}
}
System.out.println("The new token after arranging in
alphabetical order: "+st3);
}
}
Variable description:

Variable name Data type Purpose

p int Stores length of original string

q int Stores length of string without vowels

i int for loop variable

j int for loop variable

ch char Stores characters of original string

ch1 char Stores characters of string without


vowels

st String Stores original string

st2 String Stores string without vowels

st3 String Stores string without vowels


in alphabetical order

Output:
Question 8:

Write a program in java to take a sentence as input and print the word
having maximum number of vowels.
Algorithm:

1. Create a class.
2. Declare the main function.
3. Take a sentence as input.
4. Store the number of characters in the sentence in a variable.
5. Run a for loop from 0 to length-1 and obtain each word of the
sentence using : word=word+ch
6. Check the number of vowels in each word and store the word having
maximum number of vowels by comparing.
7. Print the word having maximum number of vowels.
8. End the function.
9. End the class.

import java.util.*;
public class maxvowel
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String st;int l,l1,i,j,c=0,max=0;String maxword="",word="";char ch,ch1;
System.out.println("Enter a sentence");// sentence is inputted
st=in.next();
st=st+" ";
l=st.length();//length of sentence is found out
for(i=0;i<l;i++)
{
ch=st.charAt(i);
if(ch==' ')
{
l1=word.length();
for(j=0;j<l1;j++)
{
ch1=word.charAt(j);//characters of sentence are extracted

if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U'||ch1=='a'||ch1=='e'||ch1=='i'||ch1
=='o'||ch1=='u')
c++;//number of vowels are counted
}
if(c>max)
{
max=c;//maximum number of vowels is checked
maxword=word;//storing the word having maximum number of vowels
}
word="";
c=0;
}
else
word=word+ch;//each word of sentence is obtained
}
System.out.println("Word containing maximum number of vowels
is: "+maxword);
}
}

Variable description:

Variable name Data type Purpose

st String Stores original string

maxword String Stores string having maximum


number of vowels

word String Stores each word of string

l int Stores length of original string

l1 int Stores length of each word of string

i int for loop variable

j int for loop variable

c int Counter variable

max int Stores maximum number of vowels

ch char Stores each character of string

ch1 char Stores each character of


extracted word
Output:
Question 9:

Write a program in java to accept a number and check whether it is a


sum of consecutive numbers or not.
Algorithm:

1. Create a class.
2. Declare the main function.
3. Take a number as input.
4. Using two for loops, find the sum of consecutive numbers till it is
equal to the inputted number or exceeds it.
5. If the condition is satisfied, print that it is a sum of
consecutive numbers else not.
6. End the function.
7. End the class.
import java.util.*;
public class consequtive
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,n,s=0,f=0;int a=0,b=0;
System.out.println("Enter a number to check");//number is inputted
n=in.nextInt();
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
s=s+j;//sum of consecutive numbers is found out
if(s==n)
{
f=1;
a=i;
b=j;
break;
}
}
}
if(f==1)
{
//sum of consecutive numbers is found out
System.out.println("The number "+n+" is sum of consecutive numbers:");
while(a<=b)
{
System.out.println(a);//the consecutive numbers are printed
a++;
}
}
else
System.out.println("The number "+n+" is not sum of consecutive
numbers");
}
}
Variable description:

Variable name Data type Purpose

i int for loop variable

j int for loop variable

n int Stores inputted number

f int Flag variable

s int Stores sum of consecutive numbers

a int Lower range of consecutive numbers

b int Upper range of consecutive numbers

Output:
Question 10:

Write a program in java to shift the elements of a matrix by one space.

Algorithm:

1. Create a class.
2. Declare the main function.
3. Enter the number of rows and columns.
4. Take the matrix as input.
5. Print the original matrix.
6. A while loop is used to go through each element of the matrix.
7. Using four for loops, we shift the boundary elements by one space
and then due to the while loop, the control enters the inner part of
matrix and shifts the inner elements by one space.
8. Print the shifted matrix.
9. End the function.
10. End the class.
import java.util.*;
class boundary
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int row=0,col=0,m,n,prev,curr,R,C,i,j;
System.out.println("Enter row and column");
m=in.nextInt();
n=in.nextInt();
R=m;
C=n;
int mat[][]=new int[m][n];
System.out.println("Enter matrix elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
mat[i][j]=in.nextInt();
}
System.out.println("Original matrix is:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
System.out.print(mat[i][j]+" ");
System.out.println();
}
while(row<m&&col<n)
{

if(row+1==m||col+1==n)
break;
//store the first element of next row and this element
will replace the first element of current row
prev=mat[row+1][col];
//move elements of first row from the remaining rows
for(i=col;i<n;i++)
{
curr=mat[row][i];
mat[row][i]=prev;
prev=curr;
}
row++;
//move elements of last column from the remaining columns
for(i=row;i<m;i++)
{
curr=mat[i][n-1];
mat[i][n-1]=prev;
prev=curr;
}
n--;
//move elements of last row from the remaining rows
if(row<m)
{
for(i=n-1;i>=col;i--)
{
curr=mat[m-1][i];
mat[m-1][i]=prev;
prev=curr;
}
}
m--;
//move elements of first column from the remaining rows
if(col<n)
{
for(i=m-1;i>=row;i--)
{
curr=mat[i][col];
mat[i][col]=prev;
prev=curr;
}
}
col++;
}
System.out.println("Shifted matrix is");//print new matrix
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
System.out.print(mat[i][j]+" ");
System.out.println();
}
}
}
Variable description:

Variable name Data type Purpose

row int Initial row number

col int Initial column number

i int for loop variable

j int for loop variable

m int Stores number of rows

n int Stores number of columns

prev int Stores previous array element

curr int Stores current array element

R int Final row number

C int Final column number

mat[][] int Stores matrix

Output:

S-ar putea să vă placă și