0% found this document useful (0 votes)
8 views

Computer Proj Final

Uploaded by

Nutan Srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Computer Proj Final

Uploaded by

Nutan Srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Program NO.

22:
Wonder is an entity which can hold maximum of 100 integers. The restriction is
that an integer can only be added at the rear and removed from the front. This
is like a line of cinema viewers at the “cinema hall”, in front of the ticket
booking window during distribution of tickets where the first person stands at
the front and next person stands at the rear of the first person and so on. As
soon as the first person gets the tickets, the person exits from front and the
next person becomes the first person and so on.
Define a class Wonder with the following details:
Class name: Wonder
Data members/instance variables:
ele[]: the array to hold the integer elements
size: the maximum storage capacity of the array
front: to point to the index of front of the wonder
rear: to point to the index of rear of the wonder
Member functions/methods:
Wonder(int limit): constructor to initialize the data members
size=limit, rear = 0 and front =0
void pushitem(int value) : adds the integer value to the rear of wonder if
possible, otherwise outputs a message “Wonder is full, cannot push item”
int popitem(): removes the integer from the front of wonder and
returns it if wonder is not empty, otherwise outputs a message “Wonder is
empty” and returns -9999
void printWonder(): to display the elements of wonder if it is not
empty, otherwise display a message “Wonder is underflow”.

Specify the class Wonder, giving details of the constructor(), void pushitem(int),
int popitem() and void printWonder(). Define the main function to create an
object and call the functions accordingly to enable the task.
Algorithm:

Step 1: Start
Step 2: Start class Wonder
Step 3: Create constructor Wonder(int)
size-> limit
front-> 0
rear-> 0
elenew int[size+1]
Step 4: Creating void pushitem(int value)
If front-> size
System.out.println Wonder is full cannot push item
else
if front-> 0 && rear-> 0
front-> rear-> 1
ele[front] -> value
else
ele[++front] -> value
Step 5: Creating int popitem()
int item
if(rear==0)
return -9999;
else
item=ele[rear]
if(front==rear)
front=rear=0;
else
rear=rear+1;
return item;

Step 6: Creating void printWonder()


int i
if front 0
System.out.println("Wonder is underflow");
else
for ifront i>=rear i--
System.out.println ele[i]
Step 7: case 1:
System.out.println Enter element
n1=sc.nextInt
obj.pushitem(n1)
break

Step 8: case 2:
double res=obj.popitem()
if(res!=-9999)
System.out.println The deleted value is res
else
System.out.println Wonder is empty
break
Step 9: case 3:
obj.printWonder()
break
default:
System.out.println Wrong choice
Step 10: default:
System.out.println Wrong choice
Step 11: End class
Step 12: End
Program:

import java.util.*;
class Wonder
{
int ele[],front,size,rear;
Wonder(int limit)
{
size=limit;
front=0;
rear=0;
ele=new int[size+1];//array to store integers
}

void pushitem(int value)


{
if(front==size)
System.out.println("Wonder is full,cannot push item");//condition for
not pushing
else
{
if(front==0 && rear==0)
{
front=rear=1;
ele[front]=value;
}
else
ele[++front]=value;//accepts elements
}
}

int popitem()
{
int item;
if(rear==0)
return -9999;//garbage value
else
{
item=ele[rear];
if(front==rear)
{
front=rear=0;
}
else
rear=rear+1;//deletes elements
}
return item;
}

void printWonder()
{
int i;
if(front==0)
System.out.println("Wonder is underflow");//prints message
else
{
for(i=front;i>=rear;i--)
System.out.println(ele[i]);
}
}

public static void main(String args[])//main method


{
Scanner sc=new Scanner(System.in);
int siz;
System.out.println("Enter the size of the array");
siz=sc.nextInt();
if(siz<=100)
{
Wonder obj=new Wonder(siz);//acceots integers
int ch,n1;
do
{
System.out.println("1.Insert\n2.Delete\n3.Display\nEnter your
choice?");
ch=sc.nextInt();
switch(ch)//switch case starts
{
case 1:
System.out.println("\nEnter element");
n1=sc.nextInt();
obj.pushitem(n1);
break;
case 2:
double res=obj.popitem();
if(res!=-9999)//can delete
System.out.println("The deleted value is="+res);
Else//cannot delete
System.out.println("Wonder is empty");
break;
case 3:
obj.printWonder();
break;
default://default message
System.out.println("\nWrong choice");
}
System.out.println("\nDo you continue??(1==YES) or (0==NO)....??");
ch=sc.nextInt();
}while(ch!=0);
}
else
{
System.out.println("Array is out of size");
System.exit(0);//ends program
}
}
}
Variable chart:

Name Type Purpose


front int Stores integer
rear int Stores integer
n1 int Stores integer
i int Stores integer
ch char Stores integer
siz int Stores integer
sc Scanner Accepts inputs
Output:
Program NO. 23:

Circular Queue is a linear data structure in which the operations are performed
based on FIFO(First In First Out) principle and the last position is connected
back to the first position to make a circle. A class CQ has been designed as
follows:
Class name : CQ
Data members/instance variables :
ar[] : integer array
n : capacity of the circular queue
front : to point to the index of front
rear : to point to the index of rear
Member functions/methods:
CQ(int size) : constructor to initialize the data members
n=size, rear = 0 and front =0
void push(int v) : adds the integer value to the rear of circular queue
if possible, otherwise outputs a message “Circular queue is full”
int pop() : removes the integer from the front of circular
queue and returns it if circular queue is not empty, otherwise returns -9999
void display() :to display the elements of circular queue if it is not
empty otherwise display a message “Circular queue is empty”
Specify the class CQ, giving details of the constructor(), void push(int), int pop()
and void display(). Define the main function to create an object and call the
functions accordingly to complete the task.
Algorithm:
Step 1: Start
Step 2: Start class CQ
Step 3: Create constructor CQ(int)
n=size
front=0
rear=0
ar=new int[n+1]//array to store integers
Step 4: Create void push(int v)
if(front==0 && rear==0)
front=rear=1
ar[rear]=v
Step 5: Create int pop()
if(front==0)
return -9999
Step 6: if(front==n && rear==n)
item=ar[front]
front=0
rear=0
Step 7: else
itemar[front]
front=(front+1)%n
if(front==0)
front++
Step 8: return item
Step 9: Creating void display()
If front==0 && rear==0
System.out.println Circular queue is empty
Step 10: else

int i
for int i=n i>=front i--
Step 11: for(i=rear;i>0;i--)
System.out.println(ar[i])
Step 12: Creating public static void main(String args[])
Scanner sc=new Scanner(System.in)
int siz;
System.out.println Enter the size of the array
siz=sc.nextInt()
CQ obj=new CQ(siz)
Step 13: switch(ch)
case 1:
System.out.println("\nEnter element");
n1=sc.nextInt();
case 2:
int res=obj.pop();
if(res==-9999)
System.out.println("Circular queue is empty");
else
System.out.println("Deleted element is:"+res);
case 3:
obj.display()
Step 14: End class
Step 15: End
Program:
import java.util.*;
class CQ
{
int ar[],n,front,rear;
CQ(int size)
{
n=size;
front=0;
rear=0;
ar=new int[n+1];
}

void push(int v)
{
if(rear==n && front==1)
System.out.println("Circular queue is full");
else
{
if(front==0 && rear==0)
{
front=rear=1;
ar[rear]=v;
}
else
{
rear=(rear+1)%(n+1);
if(rear==0)
rear=rear+1;
ar[rear]=v;
}
}
}

int pop()
{
int item;
if(front==0)
return -9999;
else
{
if(front==n && rear==n)
{
item=ar[front];
front=0;
rear=0;
}
else if(front==rear)
{
item=ar[front];
front=0;
rear=0;
}
else
{
item=ar[front];
front=(front+1)%n;
if(front==0)
front++;
}
}
return item;
}

void display()
{
if(front==0 && rear==0)
System.out.println("Circular queue is empty");
else
{
if(rear>front)
{
int i;
for(i=rear;i>=front;i--)
System.out.println(ar[i]);
}
else
{
int i;
for(i=n;i>=front;i--)
System.out.println(ar[i]);
for(i=rear;i>0;i--)
System.out.println(ar[i]);
}
}
}

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
int siz;
System.out.println("Enter the size of the array");
siz=sc.nextInt();
CQ obj=new CQ(siz);
int ch,n1;
do
{
System.out.println("1.Insert\n2.Delete\n3.Display\nEnter your
choice?");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("\nEnter element");
n1=sc.nextInt();
obj.push(n1);
break;
case 2:
int res=obj.pop();
if(res==-9999)
System.out.println("Circular queue is empty");
else
System.out.println("Deleted element is:"+res);
break;
case 3:
obj.display();
break;
default :
System.out.println("\nWrong Choice");
}
System.out.println("\nDo you want to continue??(1==YES) or
(0==NO)....??");
ch=sc.nextInt();
}while(ch!=0);
}
}
Variable chart:
Name Type Purpose
ar[] int Stores integers
n int Stores integer
front int Stores integer
rear int Stores integer
v int Stores integer
item int Stores integer
i int Runs a loop
Output:
PROGRAM NO. 24:

A double-ended queue is a linear data structure which enables the user to add
and remove integers from either ends, i.e. from front or rear. Define a class
with the following details:

Class name : Dequeue


arr[ ] : array to hold up to 100 integer elements
lim : stores the limit of the dequeue
front : to point to the index of front end
rear : to point to the index of the rear end

Member functions/methods :
Dequeue(int l) : constructor to initialize the data members
lim=l ; front=rear=0
void addfront(int val) : to add integer from the front if possible
else
display the message “Overflow from
front”
void addrear(int val) : to add integer from the rear if possible
else
display the message “Overflow from
rear”
int popfront( ) : returns element fromfront, if possible
otherwise returns – 9999
int poprear( ) : returns element from rear, if possible
otherwise returns – 9999
void display() : to display the elements of dequeue if it is
not
empty otherwise display a message
“Dequeue is empty”

Specify the class Dequeue, giving details of the constructor(), void addfront(int
val), void addrear(int val), int popfront(), int poprear() and void display(). Define
main function to create an object and call the functions accordingly to
complete the task.
ALGORITHM:

STEP - 1: Start
STEP - 2: Declaring class Dequeue
STEP - 3: Declaring a 1D-Array arr[] of integer type and variables lim, front
and rear of integer type
STEP - 4: Creating main() for the creation of objects of class Lucky_Num in
order to call member functions.
STEP - 5: Creating object obj when the parameterized constructor is getting
invoked.
Definition of parameterized constructor
limßl
frontß0
rearß0
arrßnew int[lim]
End of parameterized constructor
STEP - 6: Calling addfront using obj
Definition of void addfront()
if front=0 and rear=0
frontß1
rearß1
arr[front]ßval
else if front=1
Printing ‘Overflow from front’
else
arr[--front]ßval
End if
End of addfront()
STEP - 7: Calling addrear using obj
Definition of void addrear()
if front=0 and rear=0
frontß1
rearß1
arr[front]ßval
else if rear=lim-1
Printing ‘Overflow from rear’
else
arr[++rear]ßval
End if
End of addrear()
STEP - 8: Calling popfront using obj
Definition of int popfront()
Declaring variable temp of integer type
if front=0
tempß-9999
else if front=rear
tempßarr[front]
frontß0
rearß0
else
tempßarr[front]
frontßfront+1
End if
return temp
End of popfront()
STEP - 9: Calling poprear using obj
Definition of int poprear()
Declaring variable temp of integer type
if rear=0
tempß-9999
else if front=rear
tempßarr[rear]
frontß0
rearß0
else
tempßarr[rear]
rearßrear-1
End if
return temp
End of poprear()
STEP - 10: Calling display using obj
Definition of void display()
Declaring variable i of integer type
for ißfront to rear step value 1
Printing arr[i]
End for
End of display()
STEP - 11: Definition of main
Declaring variables ll, ch, v1 and v2 of integer type
Creating Scanner class object sc to accept input from user
Printing ‘Enter limit: ‘
llßsc.nextInt()
Dequeue objßnew Dequeue(ll)
do
Printing ‘1: Addfront’
Printing ‘2: Addrear’
Printing ‘3: Popfront’
Printing ‘4: Poprear’
Printing ‘5: Display’
Printing ‘6: Exit’
Printing ‘Enter choice: ’
chßsc.nextInt()
switch(ch)
case 1:
Printing ‘Enter value: ’
v1ßsc.nextInt()
obj.addfront(v1)
break
case 2:
Printing ‘Enter value: ‘
v2ßsc.nextInt()
obj.addrear(v2)
break
case 3:
if obj.popfront()=-9999
Printing ‘Array is empty’
else
Printing ‘The value deleted is ‘+obj.popfront()
End if
break
case 4:
if obj.poprear=-9999
Printing ‘Array is empty’
else
Printing ‘The value deleted is ‘+obj.poprear()
End if
break
case 5:
obj.display()
break
case 6:
Printing ‘Exiting…’
System.exit(0)
default:
Printing ‘Invalid Input!!’
break
End of switch case
while ch not equal to 6
End do-while
End of main
STEP - 12: End of class Dequeue
STEP - 13: End
PROGRAM:

import java.util.*;
class Dequeue
{//beginning of class Dequeue
int arr[];
int lim;//data members
int front;
int rear;
Dequeue(int l)
{//beginning of parameterzied constrcutor
lim = l;//assigning l to lim
front = 0;//initializing to 0
rear = 0;
arr = new int[lim];
}//end of parameterzied constructor
void addfront(int val)
{//beginning of addfront(int val)
if(front==0 && rear==0)
{//checking if both front and rear = 0
front=rear=1;//assigning them to 1
arr[front] = val;
}
else if(front==1)
{
System.out.println("Overflow from front");
}
else
{
arr[--front] = val;
}
}//end of addfront(int val)
void addrear(int val)
{//front of addrear(int val)
if(front==0 && rear==0)
{//checking if both front and rear = 0
front=rear=1;//assigning them to 1
arr[front] = val;
}
else if(rear==lim-1)
{
System.out.println("Overflow from rear");
}
else
{
arr[++rear] = val;
}
}//end of addrear(int val)
int popfront()
{//beginning of int popfront()
int temp;
if(front==0)
{//checking if front = 0
temp = -9999;
}
else if(front==rear)
{
temp = arr[front];
front=rear=0;
}
else
{
temp = arr[front];
front++;
}
return temp;//returning temp
}//end of int popfront()
int poprear()
{//beginning of int poprear()
int temp;
if(rear==0)
{//checking if rear = 0
temp = -9999;
}
else if(front==rear)
{
temp = arr[rear];
front=rear=0;
}
else
{
temp = arr[rear];
rear--;
}
return temp;//returning temp
}//end of int poprear()
void display()
{//beginning of display()
for(int i=front;i<=rear;i++)
{ //printing the numbers
System.out.print(arr[i]);
}
}//end of display
public static void main(String[] args)
{//beginning of main
Scanner sc = new Scanner(System.in);
System.out.print("Enter limit: ");
int ll = sc.nextInt();//taking limit input from user
Dequeue obj = new Dequeue(ll);//creating object obj
int ch;
do
{//beginning of do-while loop
System.out.println("1: Addfront");//adding element from the front
System.out.println("2: Addrear");//adding element from the rear
System.out.println("3: Popfront");//popping element from front
System.out.println("4: Poprear");//popping element from rear
System.out.println("5: Display");
System.out.println("6: Exit");
System.out.print("Enter choice: ");
ch = sc.nextInt();
switch(ch)
{//beginning of switch case
case 1://taking value input from user
System.out.println("Enter value");
int v1 = sc.nextInt();
obj.addfront(v1);
break;
case 2://taking value input from user
System.out.println("Enter value");
int v2 = sc.nextInt();
obj.addrear(v2);
break;
case 3:
if(obj.popfront()==-9999)
{
System.out.println("Array is empty");
}
else
{
System.out.println("The value deleted is "+obj.popfront());
}
break;
case 4:
if(obj.poprear()==-9999)
{
System.out.println("Array is empty");
}
else
{
System.out.println("The value deleted is "+obj.poprear());
}
break;
case 5:
obj.display();
break;
case 6:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid Input!!");
break;
}//end of switch case
}while(ch!=6);//end of do-while loop
}//end of main
}//end of class Dequeue

OUTPUT:
VARIABLE CHART:

VARIABLES DATA TYPE PURPOSE


arr[] int A 1D-Array of size lim

lim int Stores the size of the


array

front, rear int Stores the front and


rear index respectively

add int Stores the value to be


added from front or
rear end

temp int Stores the value to be


popped from front or
rear end
ch int Stores the choice
inputted by the user

ll int Stores the limit inputted


by the user

PROGRAM NO. 25:

A linked list is formed from the objects of the class:

class Nodes
{
int num;
Nodes next;
}
Write an Algorithm OR a Method to print the sum of nodes that contains only
odd integers of an existing linked list.
The method declaration is as follows:
void NodesCount( Nodes starPtr)

ALGORITHM:

Nodes:
STEP - 1: Start
STEP - 2: Declaring class Nodes
STEP - 3: Declaring variable num of integer type as data members
STEP - 4: Definition of Nodes(int m)
this.numm
this.nextnull
End of Nodes

PR25:
STEP - 1: Start
STEP - 2: Declaring class PR25
STEP - 3: Creating main for the creation of objects of class PR25 in order to call
member functions
STEP - 4: Calling NodesCount() using obj
Definition of void NodesCount(Nodes starPtr)
Declaring variable s of integer type
s0
Nodes currentstarPtr
while current not equal to null
if current.num%2 not equal to 0
ss+current.num
End if
currentcurrent.next
End while
Printing ‘Sum of nodes with odd integers: ‘+s
End of NodesCount
STEP - 5: Definition of main
Declaring variables n, value and i of integer type
Creating Scanner class object sc to accept input from user
Printing ‘Enter the number of nodes in the linked list: ‘
nsc.nextInt()
if n less than or equal to 0
Printing ‘Invalid Input’
System.exit(0)
End if
Printing ‘Enter value for node 1: ‘
valuesc.nextInt()
Nodes fnew Nodes(value)
Nodes currentf
for i2 to n step value 1
Printing ‘Enter value for node ’+i+’: ‘
valuesc.nextInt()
Nodes newNodenew Nodes(value)
current.nextnewNode
currentnewNode
End for
obj.NodesCount(f)
End of main
STEP - 6: End of class PR25
STEP - 7: End
PROGRAM:

class Nodes
{//class to define structure of each node
int num;
Nodes next;
Nodes(int m)
{ //constructor to initialize the node
this.num = m;
this.next = null;
}
}
import java.util.*;
class PR25
{//beginning of class PR25
public static void main(String[] args)
{//beginning of main
Scanner sc = new Scanner(System.in);
PR25 obj = new PR25();//creating object obj
System.out.print("Enter the number of nodes in the linked list: ");
int n = sc.nextInt();//taking no. of nodes input from user
if(n<=0)
{//checking if n<=0 or not
System.out.println("Invalid Input");
System.exit(0);
}
System.out.print("Enter value for node 1: ");
int value = sc.nextInt();//taking value for first node
Nodes f = new Nodes(value);//first node
Nodes current = f; //to keep track of the current node
//to take input for the remaining nodes
for(int i=2; i<=n; i++)
{ //taking values of other nodes
System.out.print("Enter value for node "+i+": ");
value = sc.nextInt();
Nodes newNode = new Nodes(value);
current.next = newNode;
current = newNode;
}
obj.NodesCount(f);//to call NodesCount() function
}//end of main
void NodesCount(Nodes starPtr)
{//beginning of NodesCount
int s=0;
Nodes current = starPtr;
while(current!=null)//traversing the linked list
{ //checking if number is odd or not
if(current.num%2!=0)
{
s += current.num;//adding that number
}
current = current.next;//moving to the next number
}//printing the result
System.out.println("Sum of nodes with odd integers: "+s);
}//end of NodesCount
}//end of class PR25
OUTPUT:
VARIABLE CHART:
VARIABLES DATA TYPE PURPOSE
num int Stores the number
inputted by the user
n int Stores the size of linked
list inputted by the user
value int Stores the value for
every node
i int Used in loop for storing
the value of each node
inputted by the user
s int Stores the sum of odd
integers

PROGRAM NO. 26:


Lucky numbers are a sequence of natural numbers that remain after removing
second, third, fourth, fifth and so on numbers respectively from a sequence of
consecutive natural numbers.
Consider the sequence of first 20 natural numbers:
Removing every second number produces the sequence: 1, 3, 5, 7, 9, 11, 13,
15, 17, 19
Next removing every third number produces the sequence: 1, 3, 7, 9, 13, 15, 19
Next removing every fourth number produces the sequence: 1, 3, 7, 13, 15, 19
Further deleting every fifth number we get the sequence: 1, 3, 7, 13, 19
Deletion of every sixth number is not possible and the five numbers that are
lucky to escape deletion remain indefinitely.
Write a program to enter any positive natural number ‘N’ where (1<=N<=50)
and generate lucky numbers less than the given natural number.
Test your program with the following set of data
Example 1
INPUT: N=10
OUTPUT: LUCKY NUMBERS LESS THAN 10 ARE: 1, 3, 7
Example 2
INPUT: N=25
OUTPUT: LUCKY NUMBERS LESS THAN 25 ARE: 1, 3, 7, 13, 19
Example 3
INPUT: N=100
OUTPUT: NUMBER NOT IN RANGE. INVALID ENTRY
Algorithm:
Step 1: START
Step 2: importing java.util.Scanner
Step 3: Starting class
Step 4: class Lucky
Step 5: int arr[]

Int N
Step 6: Creating constructor Lucky(int n)
N<- n
arrnew<- int [N]
Step 7: Creating method void sigma()

int n1<-N
m<-0
int a[]<-new int[n1]
int k<-2
Step 8: for int i<-0 i<n step value1

Arr[i]=i+1
Step 9: while k<=n
For int i<-0 i<n1 step value 1
Step 10: if (i+1) % k<-0
Continue

A[m++]<- arr[i]
Step 11: n1<-n1-n1/k
k++
m<-0
Step 12: for i<-0 i<n1 i++

arr[i] <-a[i]
System.out.print LUCKY NUMBERS LESS THAN N ARE:
Step 13: for int i0 i<n1 i++

if i! <-n1-1

System.out.print a[i],
else
System.out.print a[i];
Step 14: starting main method()
Step 15: Scanner sc<-new Scanner(System.in);

System.out.println("INPUT");
System.out.print
int n<-sc.nextInt()
if n2<1 ||n2>50
System.out.println NUMBER NOT IN RANGE. INVALID ENTRY

System.exit(0);
Lucky lc new Lucky(n2);
lc.sigma();
Step 16 :End main()
Step 17: End class

Step 18: END

Program:
import java.util.*;
class Lucky
{
int arr[];//to store integers
int N;
Lucky (int n)
{
N=n;
arr= new int[N];//initialise
}

void sigma()
{
int n1=N,m=0;//declare
int a[]= new int[n1];
int k=2;
for (int i=0; i<N; i++)
{
arr[i]= i+1;
}
while (k<=n1)//runs a loop
{
for (int i=0; i<n1; i++)
{
if ((i+1)%k==0)
{
continue;
}
else
a[m++]=arr[i];
}
n1= n1- (n1/k);
k++;
m=0;
for (int i=0; i<n1;i++)
{
arr[i]=a[i];
}
}
System.out.print("LUCKY NUMBERS LESS THAN "+N+" ARE:");//displays
for (int i=0; i<n1;i++)
{
if (i!=n1-1)
System.out.print(a[i]+",");
else
System.out.print(a[i]);
}
System.out.println();
}
public static void main(String args[])//main method
{
Scanner sc= new Scanner(System.in);
System.out.println("INPUT");//accepts inputs
System.out.print("N=");
int n2=sc.nextInt();
if (n2<1 ||n2>50)
{
System.out.println("NUMBER NOT IN RANGE. INVALID ENTRY");
System.exit(0);//ends program
}
Lucky lc= new Lucky(n2);
lc.sigma();
}
}

OUTPUT:
Variable Description Table:
Variable name Data Type Description
arr int Array to store
numbers
N int Store a no
n1 int Duplicates a no
a int Stores a no
k int Runs a loop
m int Runs a loop
i int Runs a loop
Program No 27:-

Keyword cipher is a form of encryption technique. A keyword is used as the key,


and it determines the letter matching the cipher alphabet to the plain
alphabet. Repeats of letters in the word are removed, then the cipher alphabet
is generated with the keyword matching to A, B, C, etc. until the keyword is
used up, whereupon the rest of the cipher text letters are used in alphabetical
order, excluding those already used in the key.
Encryption: The first line of input contains the keyword which you wish to
enter. The second line of input contains the string which you have to encrypt.
Plaintext: Encrypted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z K R Y P
TOSABCDEFGHIJLMNQUVWXZ
With KRYPTOS as the keyword, all A’s become K’s, all B’s becoming R’s, and so
on.
Example:
Encrypting the message: KNOWLEDGE IS POWER when, Keyword is KRYPTOS
Decoded message: DGHVETPST BM IHVTL

Write a program to accept a Coded text in upper case and a Keyword. Using the
above technique decrypt the text and display. Note: All the messages are
encoded in uppercase. Whitespace, special characters, and numbers remain
unchanged. Test your program for the following inputs:
Example 1
INPUT: ENTER KEYWORD: SECRET
ENTER TEXT TO BE DECODED: ZLJEFT DTOT
OUTPUT: DECODED TEXT: ZOMBIE HERE

Example 2
INPUT: ENTER KEYWORD: STAR WARS
ENTER TEXT TO BE DECODED: SPPSAG SP RSVJ
OUTPUT: DECODED TEXT: ATTACK AT DAWN

Example 3:
INPUT : ENTER KEYWORD: PLAYERS
ENTER TEXT TO BE DECODED: Haln de yokl
OUTPUT: INVALID TEXT

Algorithm:
Step 1: Start
Step 2: Start class Keyword_cipher

Step 3: Creating constructor Keyword_cipher()


String key_w
String text
Keyword_cipher()
key_w -> ""

text -> ""


Step 4: Creating void accept()
Scanner sc -> new Scanner(System.in);
System.out.print ENTER KEYWORD
key_w -> sc.nextLine().toUpperCase().replaceAll

System.out.print ENTER TEXT TO BE DECODED


text -> sc.nextLine
Step 4: Creating public static String createCipher_alp(String x)
StringBuilder cipher_alp = new StringBuilder
For int i -> 0 i<x.length() i++

char ch = x.charAt(i);
if cipher_alp.indexOf(String.valueOf(ch) -> -1
cipher_alp.append ch
Step 5: Creating public static String decodeMessage(String t, String c_alp, String alp)
StringBuilder deco_m -> new StringBuilder

For int I -> 0 i<t.length() i++


char ch -> t.charAt(i)
if Character.isLetter(ch)
int index -> c_alp.indexOf(ch)
deco_m.append(alp.charAt(index))
Step 6: else
deco_m.append(ch)
return deco m.toString()
Step 7: Creating void disp()

if(!ValidText(text))
System.out.println INVALID TEXT
System.exit(0)
text -> text.toUpperCase
Step 8: String cip_alp -> createCipher_alp key_w

String a -> ABCDEFGHIJKLMNOPQRSTUVWXYZ


String decode_m -> decodeMessage text, cip_alp, a
System.out.println DECODED TEXT: decode_m
Step 9: Creating public static void main(String[] args)
Keyword_cipher obj -> new Keyword_cipher

obj.accept()
obj.disp()

Step 10: End class


Step 11: End

Program:
import java.util.Scanner;
public class Keyword_cipher
{//beginning of Keyword_cipher
String key_w;//data members
String text;
Keyword_cipher()
{//beginning of non-parameterized constructor
key_w = "";
text = "";
}//end of non-parameterized constructor
void accept()
{//beginning of accept
Scanner sc = new Scanner(System.in);
System.out.print("ENTER KEYWORD: ");//taking keyword input from user
key_w = sc.nextLine().toUpperCase().replaceAll("\\s", "");
System.out.print("ENTER TEXT TO BE DECODED : ");
text = sc.nextLine();//taking text input from user
}//end of accept
public static String createCipher_alp(String x)
{//beginning of createCipher_alp()
StringBuilder cipher_alp = new StringBuilder();//creating StrinBuilder
cipher_alp
for(int i=0; i<x.length(); i++)
{//adding the characters of the keyword skipping repeats
char ch = x.charAt(i);
if(cipher_alp.indexOf(String.valueOf(ch))==-1)
{
cipher_alp.append(ch);
}
}
//adding the remaining characters of alpha
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0; i<alpha.length(); i++)
{
char ch = alpha.charAt(i);
if(cipher_alp.indexOf(String.valueOf(ch))==-1)
{
cipher_alp.append(ch);
}
}
return cipher_alp.toString(); //returning the cipher alphabet created
}//end of createCipher_alp()
public static String decodeMessage(String t, String c_alp, String alp)
{//beginning of decodeMessage()
StringBuilder deco_m = new StringBuilder();//creating StrinBuilder deco_m
for(int i=0; i<t.length(); i++)
{//only decoding letters, leaving other characters unchanged
char ch = t.charAt(i);
if(Character.isLetter(ch))
{
int index = c_alp.indexOf(ch);
deco_m.append(alp.charAt(index));
}
else
{
deco_m.append(ch);//keeping spaces and other characters
}
}
return deco_m.toString(); //returning the decoded text
}//end of decodeMessage()
public static boolean ValidText(String k)
{//beginning of ValidText()
for(int i=0; i<k.length(); i++)
{ //removing text in lowercase, blank spaces and special characters
char ch = k.charAt(i);
if(!Character.isUpperCase(ch) && !Character.isWhitespace(ch) &&
!isSpecialCharacter(ch))
{
return false;
}
}
return true;
}//end of ValidText()
public static boolean isSpecialCharacter(char ch)
{ //checking if text has special characters or not(excuding letters)
return ((ch>=33 && ch<=64)||(ch>=91 && ch<=96)||(ch>=123 &&
ch<=126));
}
void disp()
{//beginning of disp
if(!ValidText(text))
{//checking if text is valid or not
System.out.println("INVALID TEXT");
System.exit(0);
}
text = text.toUpperCase();//converting to uppercase
String cip_alp = createCipher_alp(key_w);//storing the cipher alphabet
String a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String decode_m = decodeMessage(text, cip_alp, a);//storing the decoded
text
System.out.println("DECODED TEXT: "+decode_m);//printing decoded text
}//end of disp
public static void main(String[] args)
{//beginning of main
Keyword_cipher obj = new Keyword_cipher();//creating object obj
obj.accept();//calling accept through obj
obj.disp();//calling disp through obj
}//end of main
}//end of Keyword_cipher
Variable chart:

Name Type Purpose


key_w String Stores a string
text String Stores a string
sc Scanner Accepts inputs
i int Runs a loop
ch char Stores a
character
a String Stroes a string
Output:

You might also like