Sunday, 20 August 2017

Program For Single Columnar Transposition

Program:

import java.util.*;
class simpleColumnar{
      public static void main(String sap[]){
Scanner sc = new Scanner(System.in);

System.out.print("\nEnter plaintext(enter in lower case): ");
String message = sc.next();
System.out.print("\nEnter key in numbers: ");
String key = sc.next();

/* columnCount would keep track of columns */
int columnCount = key.length();

/* rowCount will keep of track of rows...no of rows = (plaintextlength + keylength) / keylength */
int rowCount = (message.length()+columnCount)/columnCount;

/*plainText and cipherText would be array containing ASCII values for respective alphabets */
int plainText[][] = new int[rowCount][columnCount];
int cipherText[][] = new int[rowCount][columnCount];

/*Encryption Process*/
System.out.print("\n-----Encryption-----\n");
cipherText = encrypt(plainText, cipherText, message, rowCount, columnCount, key);

// prepare final string
String ct = "";
for(int i=0; i<columnCount; i++)
{
for(int j=0; j<rowCount; j++)
{
if(cipherText[j][i] == 0)
ct = ct + 'x';
else{
ct = ct + (char)cipherText[j][i];
}
}
}
System.out.print("\nCipher Text: " + ct);

/*Decryption Process*/
System.out.print("\n\n\n-----Decryption-----\n");

plainText = decrypt(plainText, cipherText, ct, rowCount, columnCount, key);

// prepare final string
String pt = "";
for(int i=0; i<rowCount; i++)
{
for(int j=0; j<columnCount; j++)
{
if(plainText[i][j] == 0)
pt = pt + "";
else{
pt = pt + (char)plainText[i][j];
}
}
}
System.out.print("\nPlain Text: " + pt);

System.out.println();
}

static int[][] encrypt(int plainText[][], int cipherText[][], String message, int rowCount, int                                                                                                           columnCount, String key){
int i,j;
int k=0;

/* here array would be filled row by row  */
for(i=0; i<rowCount; i++)
{
for(j=0; j<columnCount; j++)
{
/* terminating condition...as string length can be smaller than 2-D array */
if(k < message.length())
{
/* respective ASCII characters would be placed */
plainText[i][j] = (int)message.charAt(k);
k++;
}
else
{
break;
}
}
}

/* here array would be filled according to the key column by column */
for(i=0; i<columnCount; i++)
{
/* currentCol would have current column number i.e. to be read...as there would be ASCII value stored in key so we would subtract it by 48 so that we can get the original number...and -1 would be subtract as array position starts from 0*/
int currentCol= ( (int)key.charAt(i) - 48 ) -1;
for(j=0; j<rowCount; j++)
{
cipherText[j][i] = plainText[j][currentCol];
}

}

System.out.print("Cipher Array(read column by column): \n");
for(i=0;i<rowCount;i++){
for(j=0;j<columnCount;j++){
System.out.print((char)cipherText[i][j]+"\t");
}
System.out.println();
}

return cipherText;
     }

static int[][] decrypt(int plainText[][], int cipherText[][], String message, int rowCount, int                                                                                                          columnCount, String key){
int i,j;
int k=0;

for(i=0; i<columnCount; i++)
{
int currentCol= ( (int)key.charAt(i) - 48 ) -1;
for(j=0; j<rowCount; j++)
{
plainText[j][currentCol] = cipherText[j][i];
}
}

System.out.print("Plain Array(read row by row): \n");
for(i=0;i<rowCount;i++){
for(j=0;j<columnCount;j++){
System.out.print((char)plainText[i][j]+"\t");
}
System.out.println();
}

return plainText;
}
}

Output: 

Enter plaintext(enter in lower case): networksecurity

Enter key in numbers: 31452

-----Encryption-----
Cipher Array(read column by column):
t       n       w       o       e
s       r       e       c       k
i       u       t       y       r

Cipher Text: tsixnruxwetxocyxekrx


-----Decryption-----
Plain Array(read row by row):
n       e       t       w       o
r       k       s       e       c
u       r       i       t       y

Plain Text: networksecurity

Wednesday, 19 July 2017

WaterJug Problem in Java: Artificial Intelligence

Problem Statement: There are two jugs (suppose capacity of 3 and 5) and we need to fill the jug in such a way that 5 litres capacity jug should contain 4 litres of water.


import java.util.*;
class WaterJug{
public static void main(String sap[]){
Scanner sc = new Scanner(System.in);

// j1 is capacity of small tank
System.out.print("\nEnter odd capacity of small tank: ");
int j1 = sc.nextInt();

// j2 is capacity of large tank
System.out.print("\nEnter odd capacity of large tank: ");
int j2 = sc.nextInt();

// count takes care of number of iterations
int count = j1 + j2;

/* jug1 array would hold the values for smaller tank and jug2 array would hold the values for larger     tank */
int jug1[] = new int[count];
int jug2[] = new int[count];

int i=0;

// initialzing jug1 and jug2 array
jug1[i] = j1;
jug2[i] = 0;
i++;

jug1[i] = 0;
jug2[i] = j1;
i++;

while(i < count){
if(jug1[i-1] > 0){
// if jug1 has any amount of water i.e. it is not empty
jug1[i] = jug1[i-1];
jug2[i] = 0;
}
else{
// jug1 is fully empty
jug1[i] = j1;
jug2[i] = jug2[i-1];
}
i++;

if(jug2[i-1] > 0){
// if jug2 has any amount of water i.e. it is not empty
if(jug1[i-1] + jug2[i-1] < j2){
// final result obtained
jug2[i] = jug1[i-1] + jug2[i-1];
jug1[i] = 0;
}
else{
int temp = jug2[i-1];
temp = j2 - temp;
jug2[i] = temp + jug2[i-1];
jug1[i] = jug1[i-1] - temp;
}
}
else{
// jug2 is fully empty
jug2[i] = jug1[i-1];
jug1[i] = 0; 
}
i++;
}

// display final result
for(i=0; i<count; i++){
System.out.print("\nJUG1: "+jug1[i]+"\tJUG2: "+jug2[i]);
}

System.out.println();
}
}

Output: 

Enter odd capacity of small tank: 3

Enter odd capacity of large tank: 5

JUG1: 3 JUG2: 0
JUG1: 0 JUG2: 3
JUG1: 3 JUG2: 3
JUG1: 1 JUG2: 5
JUG1: 1 JUG2: 0
JUG1: 0 JUG2: 1
JUG1: 3 JUG2: 1
JUG1: 0 JUG2: 4

Saturday, 25 March 2017

Program For Hundred Year Calender


Step1 - Firstly, select a year and then we need to find out the respective alphabet associated with it (Suppose we are selecting a year '2010' then the alphabet we would be getting is 'E')

Step2 - Secondly, the alphabet obtained from phase1 would act as your row index and the month you are looking would be your column index (now selecting month as 'march' then the column we are looking would be column3 and the row we are looking would be row5 as alphabet 'E' is at 5th row now the output of phase2 would be the intersection of column3 and row5 i.e. '1')

Step3 - Lastly, the number obtained from phase2 would act as a column index and then we need to traverse that column for the date we are looking for (the output from phase2 is '1' so we would be looking column1 and we are selecting date as '10' then we need to traverse column1 for date 10 and the day we would be getting would be 'Wednesday' i.e. final result)



Program -
import java.util.*;
class HundredYearCalender{
public static void main(String sap[]){
Scanner sc = new Scanner(System.in);
int i;
StringBuilder final_date = new StringBuilder();

/*
Phase1 - For phase input would be year and output would be the alphabet associated with that             year
*/
char alpha_28[] =                 {'A','B','C','K','F','G','A','I','D','E','F','N','B','C','D','L','G','A','B','J','E','F','G','H','C','D','E','M'};

//array for 100 years alphabets
char years[] = new char[100];
//variable 'k' keeps track of array alpha_28
int k = 0;
for(i=1;i<100;i++){
years[i] = alpha_28[k];
k++;
k = k % 28;
}

//taking input year
System.out.print("\nEnter the year(2001-2099): ");
int input_year = sc.nextInt();
final_date = final_date.append(input_year + "-");
input_year = input_year - 2000;

//setting the alphabet w.r.t specific year
char final_year = years[input_year];

/*
Phase2 - input would be the alphabet obtained from phase1 and the month you are searching                for and output would be the intersecting number
*/
//setting column values
int months[] = {1,4,4,7,2,5,7,3,6,1,4,6};

/*here subtracting the alphabet getting from phase1 with 64 to get the respective row to opt for*/
int row = (int)final_year - 64;

//taking input month
System.out.print("\nEnter the month: ");
int input_month = sc.nextInt();
final_date = final_date.append(input_month + "-");

input_month = input_month-1;
int initial_value = months[input_month];

int final_month=0;
for(i=0;i<row;i++){
final_month = initial_value;
initial_value++;
initial_value = initial_value % 7;
}

/*
Phase3 - number obtained from phase2 and the date would be input for phase3 and the output              would be the day we are looking for
*/
String weekdays[] =       {"Monday","Tuesday","Wednesday","Thrusday","Friday","Saturday","Sunday"};
System.out.print("\nEnter date: ");
int input_date = sc.nextInt();
final_date = final_date.append(input_date + "");

input_date = input_date + final_month - 1;
int result = -1;
for(i=0;i<input_date;i++){
result++;
result = result % 7;
}

//final result
System.out.print("\nDate(YYYY-MM-DD): "+ final_date + "\nDay: " + weekdays[result]);
}
}

Output -

Enter the year(2001-2099): 2017

Enter the month: 03

Enter date: 25

Date(YYYY-MM-DD): 2017-3-25
Day: Saturday