Anda di halaman 1dari 79

EXP NO: 1

DATE:

IMPLEMENTATION OF WATER JUG PROBLEM

AIM:

To create a Water Jug Program in Java to check for the final state.

ALGORITHM:

Start

Ask the size of the jugs and the final state.


Fill the small jug from the tap (0,3)
Pour this into the larger jug (3,0)
Fill the larger jug from the tap (3,3)

Pour from the small jug into the larger jug until full. This leaves difference in the small jug
(4,2)

Continue till the final step is reached.


Stop.
PROGRAM:

import java.util.Scanner; public class waterjug

{
public static void main(String args[])
{

Scanner sc = new Scanner(System.in);


System.out.println("Enter the size of the jugs"); int a =
sc.nextInt();
int b = sc.nextInt();

int i,jug1,jug2,j1 = 0,j2 = 0; boolean flag = false;

int state[] = new int[2]; System.out.println("Enter the states of


the jugs"); state[0] = sc.nextInt();

state[1] = sc.nextInt(); if(a > b)

jug1 = a; jug2 = b;
}
else
{

jug1 = b; jug2 = a;
}
for(i = 0;i < 50;i++)
{
if(j1 == state[0] && j2 == state[1])
{

flag = true; break;


}
else
{

System.out.println(j1 + " " + j2); if(j1 == 0 && j2 < jug2)

{
j1 = jug1;
}
else if(j2 == jug2)
{
j2 = 0;
}

else if(j1 == jug1)


{
j1 = j1 - jug2 + j2;
j2 = jug2;
}
else
{
j2 = j1;
j1 = 0;
}
}
}
for(i = 0;i < 50;i++)

{
if(j1 == state[0] && j2 == state[1])
{

flag = true; break;


}
else
{

System.out.println(j1 + " " + j2); if(j1 == 0 && j2


< jug2)

{
j1 = jug1;
}
else if(j2 == jug2)
{
j2 = 0;
}
else if(j1 == jug1)
{
j1 = j1 - jug2 + j2;
j2 = jug2;
}

else
{
j2 = j1;

j1 = 0;
}
}
}
if(flag == true)
{

System.out.println(j1 + " " + j2);


System.out.println("Final Stage Reached");
}
else

System.out.println("Final Stage Not Possible"); sc.close();

}
}
OUTPUT:
RESULT:
ove
T program
h has been
e successfull
a y executed
b and
verified.
EXP NO: 2

DATE:

IMPLEMENTATION OF WATER JUG PROBLEM USING

BREADTH FIRST SEARCH

AIM:

To search water jug problem in Java using Breadth First Search.

ALGORITHM:

Start

Ask the user the size of the jug.

Ask the user the final state.

Calculate all possible combinations of the given water jug problem.

Store all the results in a queue.

Use Breadth First Technique to find the final solved state breadth wise.

If found display the nodes traversed and the path to the final state.

If not found, display an appropriate message to the user.

Stop.
PROGRAM:
import java.util.*; public class BFS
{
public static void main(String args[])
{

Scanner sc = new Scanner(System.in); int jug1,jug2;

System.out.println("Enter the Capacity of Jug1: "); jug1 = sc.nextInt();

System.out.println("Enter the Capacity of Jug2: "); jug2 = sc.nextInt();

int state[] = new int[2];

System.out.println("Enter the Final State seperated by spaces: "); state[0] =


sc.nextInt();

state[1] = sc.nextInt();

System.out.println("Enter 1 to search using BFS"); System.out.println("Enter 2 to


Exit");

int option = sc.nextInt(); switch(option)


{

case 1: BFS(jug1,jug2,state); break;


case 2: System.exit(0);
default: System.out.println("Wrong Choice...!!!");
}
}
public static boolean isGoalState(int j1, int j2, int state[])
{

if(j1 == state[0] && j2 == state[1]) return true;

return false;
}

public static ArrayList<String> getSuccessors(int jug1, int jug2, String node)

ArrayList<String> succ = new ArrayList<String>(); int j1 =


Character.getNumericValue(node.charAt(1)); int j2 =
Character.getNumericValue(node.charAt(3)); String s1 = null;
if(j1 == 0 && j2 == 0)
{

s1 = "(" + (char)('0' + jug1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + jug2) + ")"; succ.add(s1);

}
else if(j1 == jug1 && j2 == 0)
{

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + jug2) + ")";


succ.add(s1);

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

j2 = jug2;
j1 = j1 - j2;

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")";


succ.add(s1);

}
else if(j1 == 0 && j2 == jug2)

s1 = "(" + (char)('0' + jug1) + "," + (char)('0' + j2) + ")";


succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

j1 = j2;
j2 = 0;

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")";


succ.add(s1);

}
else if(j1 == jug1 && j2 == jug2)
{

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

}
else if(j1 < jug1 && j2 == jug2)
{

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);


if(j1+j2 <= jug1)
{

j1 = j1 + j2;
j2 = 0;
}
else
{
j2 = j1 + j2;
j1 = jug1;
j2 = j2 - j1;
}
s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

}
else if(j1 == jug1 && j2 < jug2)
{

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

j1 = j1 - jug2 + j2;
j2 = jug2;

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

}
else if(j1 < jug1 && j2 == 0)
{

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + jug2) + ")"; succ.add(s1);

if(j1 > jug2)


{
j2 = j1; j1 = j1 - jug2; j2 = j2 - j1;
}
else
{
j2 = j1; j1 = 0;
}

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

}
else if(j1 == 0 && j2 < jug2)
{

s1 = "(" + (char)('0' + jug1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

j1 = j2; j2 = 0;

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")"; succ.add(s1);


}
return succ;
}
public static void BFS(int jug1, int jug2, int state[])
{
Queue<String> q = new LinkedList<String>();
ArrayList<String> path = new ArrayList<String>(); Set<String> closed = new
HashSet<String>(); ArrayList<String> succ = new ArrayList<String>();
ArrayList<String> a1 = new ArrayList<String>(); ArrayList<String> a2 = new
ArrayList<String>(); q.add("(0,0)");

String node; char s1, s2;

int j1, j2, i = 0; String str = ""; j1 = j2 = 0;

System.out.println("Expanded Nodes = "); do

node = q.poll(); System.out.print(node + " "); s1 = node.charAt(1);


j1 = Character.getNumericValue(s1);
s2 = node.charAt(3);
j2 = Character.getNumericValue(s2);

str = "(" + (char)('0' + state[0]) + "," + (char)('0' + state[1]) + ")"; boolean x =


isGoalState(j1, j2, state);

if(x == true)
{

System.out.println("\n\n"); sequence(a1, a2, str,path);

System.out.println("\n*****Final State Reached*****"); break;

}
if(!(closed.contains(node)))
{
closed.add(node);

succ = getSuccessors(jug1,jug2,node); for(String child:succ)

{
q.add(child);
if(!(closed.contains(child)) && !(a2.contains(child)))

{
a1.add(node);
a2.add(child);
}
}
}
i++; }while(i < 100);

}
public static void sequence(ArrayList<String> a1, ArrayList<String> a2,String
state,ArrayList<String> path)

{
int x;
if(state == "(0,0)")
{
path.add(state);

System.out.println("The Solution Path is:"); Collections.reverse(path);


System.out.println(path);
}
else

x = a2.indexOf(state); path.add(state);

sequence(a1, a2, a1.get(x),path);


}
}
}
OUTPUT:
RESULT:
The above program has been successfully executed and verified.
EXP NO: 3

DATE:

IMPLEMENTATION OF WATER JUG PROBLEM USING

DEPTH FIRST SEARCH

AIM:

To search water jug problem in Java using Depth First Search

ALGORITHM:

Start

Ask the user the size of the jug.

Ask the user the final state.

Calculate all possible combinations of the given water jug problem.

Store all the results in a queue.

Use Depth First Technique to find the final solved state Depth wise.

If found display the nodes traversed and the path to the final state.

If not found, display an appropriate message to the user.

Stop.
PROGRAM:
import java.util.*; public class DFS
{
public static void main(String args[])
{

Scanner sc = new Scanner(System.in); int jug1,jug2;

System.out.println("Enter the Capacity of Jug1: "); jug1 = sc.nextInt();

System.out.println("Enter the Capacity of Jug2: "); jug2 = sc.nextInt();

int state[] = new int[2];

System.out.println("Enter the Final State seperated by spaces: "); state[0] = sc.nextInt();

state[1] = sc.nextInt();

System.out.println("Enter 1 to search using DFS"); System.out.println("Enter 2 to Exit");

int option = sc.nextInt(); switch(option)


{

case 1: DFS(jug1,jug2,state); break;

case 2: System.exit(0);
default: System.out.println("Wrong Choice...!!!");
}
sc.close();
}

public static boolean isGoalState(int j1, int j2, int state[]) { if(j1 == state[0] && j2 ==
state[1])

return true; return false;

public static ArrayList<String> getSuccessors(int jug1, int jug2, String node)


{ ArrayList<String> succ = new ArrayList<String>();

int j1 = Character.getNumericValue(node.charAt(1)); int j2 =


Character.getNumericValue(node.charAt(3)); String s1 = null;
if(j1 == 0 && j2 == 0)
{
s1 = "(" + (char)('0' + jug1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + jug2) + ")"; succ.add(s1);

}
else if(j1 == jug1 && j2 == 0)
{

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + jug2) + ")";


succ.add(s1);

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

j2 = jug2;
j1 = j1 - j2;

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")";


succ.add(s1);

}
else if(j1 == 0 && j2 == jug2)
{

s1 = "(" + (char)('0' + jug1) + "," + (char)('0' + j2) + ")";


succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

j1 = j2;
j2 = 0;

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")";


succ.add(s1);

}
else if(j1 == jug1 && j2 == jug2)
{

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

}
else if(j1 < jug1 && j2 == jug2)
{

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

if(j1+j2 <= jug1)


{
j1 = j1 + j2;

j2 = 0;
}
else
{
j2 = j1 + j2;
j1 = jug1;
j2 = j2 - j1;
}
s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")";
succ.add(s1);
}
else if(j1 == jug1 && j2 < jug2)
{

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

j1 = j1 - jug2 + j2;
j2 = jug2;

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

}
else if(j1 < jug1 && j2 == 0)
{

s1 = "(" + '0' + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + jug2) + ")"; succ.add(s1);

if(j1 > jug2)


{
j2 = j1;
j1 = j1 - jug2;
j2 = j2 - j1;
}
else
{
j2 = j1;
j1 = 0;
}

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

}
else if(j1 == 0 && j2 < jug2)

s1 = "(" + (char)('0' + jug1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

s1 = "(" + (char)('0' + j1) + "," + '0' + ")"; succ.add(s1);

j1 = j2;
j2 = 0;

s1 = "(" + (char)('0' + j1) + "," + (char)('0' + j2) + ")"; succ.add(s1);

}
return succ;
}
public static void DFS(int jug1, int jug2, int state[]) { Stack<String> q = new
Stack<String>(); ArrayList<String> path = new ArrayList<String>(); Set<String>
closed = new HashSet<String>(); ArrayList<String> succ = new
ArrayList<String>(); ArrayList<String> a1 = new ArrayList<String>();
ArrayList<String> a2 = new ArrayList<String>(); q.push("(0,0)");

String node; char s1, s2;

int j1, j2, i = 0; String str = ""; j1 = j2 = 0;

System.out.println("Expanded Nodes = "); do

node = q.pop(); System.out.print(node + " "); s1 = node.charAt(1);


j1 = Character.getNumericValue(s1);
s2 = node.charAt(3);
j2 = Character.getNumericValue(s2);

str = "(" + (char)('0' + state[0]) + "," + (char)('0' + state[1]) + ")"; boolean x =


isGoalState(j1, j2, state);

if(x == true)
{

System.out.println("\n\n"); sequence(a1, a2, str,path);

System.out.println("*****Final State Reached*****"); break;

}
if(!(closed.contains(node)))
{
closed.add(node);

succ = getSuccessors(jug1,jug2,node); for(String child:succ)

q.add(child);
if(!(closed.contains(child)) && !(a2.contains(child)))
{
a1.add(node);
a2.add(child);
}
}
}
i++;
}while(i < 100);
}

public static void sequence(ArrayList<String> a1, ArrayList<String> a2,String


state,ArrayList<String> path){

int x;
if(state == "(0,0)")
{
path.add(state);

System.out.println("The Solution Path is:"); Collections.reverse(path);


System.out.println(path);
}

else
{

x = a2.indexOf(state); path.add(state);

sequence(a1, a2, a1.get(x),path);


}}}
OUTPUT:
RESULT:
The above program has been successfully executed and verified.
EXP NO: 4

DATE:

IMPLEMENTATION OF MAGIC SQUARE

AIM:

To generate a magic square of order n.

ALGORITHM:

Start.
Get the input for order n from the user.

If n is odd, then assign the integers 1 to n^2 in ascending order, starting at the bottom, middle
cell.

Repeatedly assign the next integer to the right and down.

If this cell has already been assigned another integer, instead use the cell adjacently above.

Use wrap-around to handle border cases.

If n is even, generate normal matrix in which the integers 1 to n^2 are listed across the rows.

Then interchange the diagonals that are symmetrically located with respect to the center.
Print the generated matrix of order n by n.
End the program.
PROGRAM:
import java.io.*; import java.util.*;

class magicsquare
{
public static void main(String args[])
{

magicsquare obj= new magicsquare();

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

if(n%2==0) obj.create_even(n-1,n);

else
obj.create_odd(n-1,n);
}

public void create_odd(int n,int x)


{

int c=x/2; int r=0;

int arr[][]=new int[x][x]; int value=1;

for(int i=0;i<x;i++)
{
for(int j=0;j<x;j++)
{

if(arr[r][c]==0)

arr[r][c]=value;
else

if(c-1<0) c=x;

if(r-1<0) r=x;
c=c-1; r=r-1;
arr[r][c]=value;
}

if(r-1<0) r=x;

r=(r-1)%x; c=(c+1)%x; value++;

}
}

display(arr,x);

public void create_even(int n,int x)


{
int value=1;
int arr[][]=new int[x][x];

for(int i=0;i<x;i++)
{
for(int j=0;j<x;j++)
{
if((i==j)||(i+j)%n==0)
arr[i][j]=value;
value++;
}
}
value= x*x;

for(int i=0;i<x;i++)

{
for(int j=0;j<x;j++)
{
if(arr[i][j]==0)
{
arr[i][j]=value;
}
value--;
}
}
display(arr,x);
}

public void display(int arr[][],int x)

{
for(int i=0;i<x;i++)
{

for(int j=0;j<x;j++) System.out.print(" "+arr[i][j]);

System.out.println();

}
}

}
OUTPUT:
RESULT:
T square of
h order n is
e generated
mand the
a desired
g output is
ic obtained.
EXP NO: 5(A)

DATE:

TIC-TAC-TOE (Player vs. Player)

AIM:

To write a java program for Tic-Tac-Toe Player vs. Player

ALGORITHM:

Start .

Create the class and initialize the variables.

Choose the player 1 or 2.

Assign the moves for the respective players.

Repeat (4) until any of the following conditions are obtained

Player 1 wins

Player 2 wins

Game results in a tie.

Display every step (or move) in the output.

Quit.
PROGRAM:
import java.io.*; public class PvsP
{
static char a[][]=new char[3][3];
public static void main(String args[])throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int


i,j,n=0;

for(i=0;i<3;i++)

{
for(j=0;j<3;j++)
a[i][j]='*';

}
while(true)
{

System.out.println("Enter the Move for x:"); i=Integer.parseInt(br.readLine());


j=Integer.parseInt(br.readLine()); a[i-1][j-1]='x';

n++;
display();
if(n==9)
break;
if(win('x')==true)
{

System.out.println("x wins!"); break;

System.out.println("Enter the Move for o:"); i=Integer.parseInt(br.readLine());


j=Integer.parseInt(br.readLine()); a[i-1][j-1]='o';

n++;

display();
if(n==9)
break;
if(win('o')==true)
{

System.out.println("'o' wins!"); break;

}
}
if(n==9)
System.out.println("Draw!");
}
static Boolean win(char n)
{

int i; for(i=0;i<3;i++)

{if(a[i][0]==n&&a[i][1]==n&&a[i][2]==n) return true;

}
for(i=0;i<3;i++)

{if(a[0][i]==n&&a[1][i]==n&&a[2][i]==n) return true;

if(a[0][0]==n&&a[1][1]==n&&a[2][2]==n) return true;

if(a[0][2]==n&&a[1][1]==n&&a[2][0]==n) return true;


return false;
}
static void display()
{

int i,j; for(i=0;i<3;i++)

for(j=0;j<3;j++) System.out.print(a[i][j]+" ");


System.out.println();
}

}
}
OUTPUT:
RESULT:
m was
T compiled
he and
Pr executed
ogsuccessful
ra ly.
EXP NO: 5(B)

DATE:

TIC-TAC-TOE (Player vs. Computer)

AIM:

To write a java program for Tic-Tac-Toe Player vs. Computer

ALGORITHM:

Start .

Create the class and initialize the variables.

Start with the Computer move as the (1,1) .

Get the player moves from user.

Repeat (4) until any of the following conditions are obtained

Computer wins

Player wins

Game results in a tie.

Display every step (or move) in the output.

Quit.
PROGRAM:
import java.util.Random; import java.util.Scanner;
public class PvsC { public enum eStatus {

IN_PROGRESS,

TIE_GAME,

COMPUTER_WINS,

PLAYER_WINS,

public char board[][] = new char[3][3]; public


Scanner keyboard;

public Random random; public PvsC() {

keyboard = new Scanner(System.in); random = new


Random(); initBoard(); }

public static void main(String args[]) { PvsC tic =


new PvsC();

boolean nextTurn = false;

while (tic.status() == eStatus.IN_PROGRESS) { if


(nextTurn)

tic.computer();

else

tic.player();

tic.printBoard(); nextTurn = !nextTurn;

}
eStatus status = tic.status();

if (status == eStatus.TIE_GAME)

System.out.println("Game is tied!");

else if (status == eStatus.COMPUTER_WINS)

System.out.println("Computer wins!");

else if (status == eStatus.PLAYER_WINS)

System.out.println("Player wins!");

private void initBoard() {

for (int row = 0; row <= 2; ++row) {

for (int col = 0; col <= 2; ++col) {

board[row][col] = '-';

board[1][1] = 'X';

System.out.println("------------------------------");

for (int row = 0; row <= 2; ++row) {

for (int col = 0; col <= 2; ++col) {

System.out.print(" " + board[row][col]);

System.out.println();

}
private void printBoard() {

System.out.println("------------------------------");

for (int row = 0; row <= 2; ++row) {

for (int col = 0; col <= 2; ++col) {

System.out.print(" " + board[row][col]);


}

System.out.println();

System.out.println();

private void markSquare(int row, int col, char c) { board[row]


[col] = c;

private void computer() { while (true) {

int row = (int) random.nextInt(3); int col = (int)


random.nextInt(3); if (board[row][col] == '-') {

markSquare(row, col, 'X'); break;

private void player() {

System.out.println("\n\nx--> Computer\t\to-->Player\n\n");
while (true) {

System.out.print("Row: "); int row = keyboard.nextInt();


System.out.print("Column: ");

int col = keyboard.nextInt(); if ((row < 0) || (row > 2))


System.out.println("Row out of range, try again!"); else if ((col
< 0) || (col > 2))

System.out.println("Column out of range, try again!"); else if


(board[row][col] != '-')

System.out.println("Occupied square, try again!"); else {

markSquare(row, col, 'O'); break;

private eStatus status() { boolean tieGame = true;

for (int row = 0; row <= 2; ++row) for (int col = 0; col <= 2; +
+col)

if (board[row][col] == '-') tieGame = false;

if (tieGame)

return eStatus.TIE_GAME; else if (runOfThree('O'))

return eStatus.PLAYER_WINS; else if (runOfThree('X'))

return eStatus.COMPUTER_WINS; return


eStatus.IN_PROGRESS;

private boolean runOfThree(char c) { for (int row = 0; row <=


2; ++row) {

if ((board[row][0] == c) &&
(board[row][1] == c) && (board[row]
[2] == c)) return true;

for (int col = 0; col <= 2; ++col) { if


((board[0][col] == c) && (board[1]
[col] == c) &&

(board[2][col] == c)) return true;

if ((board[0][0] == c) && (board[1][1]


== c) && (board[2][2] == c)) return
true;

if ((board[2][0] == c) && (board[1][1]


== c) && (board[0][2] == c)) return
true;

return false;

}
OUTPUT:
RESULT:

The Program was compiled and executed successfully.


EXP NO: 6
DATE:

IMPLEMENTATION OF A* ALGORITHM

AIM:
To write a java program implementing A* algorithm using 8 puzzle problem as example.

ALGORITHM:

Start the program

Define the necessary variables.

Create a method for each operation of up,down,left and right.

Take any input string.

If the input string doesnt have any optional optimal solution point,the solution doesnt exist.

Check the completion

Print and end.


PROGRAM:

import java.io.BufferedReader; import java.io.IOException; import


java.io.InputStreamReader; import java.util.HashMap;

import java.util.LinkedList; import java.util.Map; import java.util.Queue; class astarrr

public static String stringy; public static String str;

Queue<String> agenda = new LinkedList<String>(); Map<String,Integer> stateDepth =


new HashMap<String, Integer>(); Map<String,String> stateHistory = new
HashMap<String,String>();

public static void main(String args[]) throws IOException


{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("Enter Initial State: "); str=br.readLine(); System.out.println("Enter Final
State: "); stringy=br.readLine();

astarrr e = new astarrr(); e.add(str, null); while(!e.agenda.isEmpty())

String currentState = e.agenda.remove(); e.up(currentState); e.down(currentState);


e.left(currentState); e.right(currentState);

}
System.out.println("Solution doesn't exist");
}
void add(String newState, String oldState)
{
if(!stateDepth.containsKey(newState))

int newValue = oldState == null ? 0 : stateDepth.get(oldState) + 1;


stateDepth.put(newState, newValue);

agenda.add(newState);
stateHistory.put(newState,oldState);
}
}
void up(String currentState)
{
int a = currentState.indexOf("0"); if(a>2)
{

String nextState=currentState.substring(0,a-3)+"0"+ currentState.substring(a-2,a)


+currentState.charAt(a-3)+currentState.substring(a+1);
checkCompletion(currentState,nextState);
}
}
void down(String currentState)
{

int a = currentState.indexOf("0"); if(a<6)

String nextState=currentState.substring(0,a)+currentState.substring(a+3,a+4)+
currentState.substring(a+1,a+3)+"0"+currentState.substring(a+4);

checkCompletion(currentState,nextState);
}
}
void left(String currentState)
{

int a = currentState.indexOf("0"); if(a!=0 && a!=3 && a!=6)


{

String nextState =currentState.substring(0,a-1)+"0"+ currentState.charAt(a-


1)+currentState.substring(a+1);
checkCompletion(currentState, nextState);
}
}
void right(String currentState)
{

int a = currentState.indexOf("0"); if(a!=2 && a!=5 && a!=8)


{

StringnextState=currentState.substring(0,a)+

currentState.charAt(a+1)+"0"+currentState.substring(a+2); checkCompletion(currentState,
nextState);

}
}
private void checkCompletion(String oldState, String newState)
{

add(newState, oldState); if(newState.equals(stringy))


{
System.out.println("Solution Exists at Level "+stateDepth.get(newState)+" of the
tree");
String traceState = newState; while (traceState != null)

System.out.println(traceState + " at " + stateDepth.get(traceState)); traceState =


stateHistory.get(traceState);
}
System.exit(0);
}
}
}
OUTPUT:
RESULT:
T am has
h been
e successfull
p y executed
r and the
o output has
g been
r verified.
EXP NO: 7
DATE:

AO* ALGORITHM

AIM:

To code a program implementing AO* algorithm.

ALGORITHM:

Start the program.

Enter the word that needs to be searched.

Predefine the dictionary array.

Compare the string length to select an array of strings having same string length.

Compare the substring iteratively and incrementally to eliminate the words.

Print true if word is found else print false.

Stop the program.


PROGRAM:

import java.util.*; public class aostar {

public static void main( String args[]){

String dictionary[]= {"hey","gaudyw","blue","yellow", "put",


"purple","ped","tem","red"}; System.out.println("Enter the word to search");

Scanner sc= new Scanner(System.in); final String word= sc.nextLine(); boolean found=false;

ArrayList<String> first = new ArrayList<String>(); for(String x : dictionary){

if(x.length()==word.length()){

first.add(x);

int m=0;

for(int j=1; j<word.length()+1; j++)

m=first.size(); for(int i=0; i<m; i++)

String s=first.get(i).substring(0,j);

String c= word.substring(0,j);

//System.out.println("c is now "+c); if(!s.equals(c))

first.remove(i);

System.out.println(first);

m--;
i=0;

if(first.size()==1 && first.get(i).equals(word))

found=true;

System.out.println(found);

}
OUTPUT:
RESULT:
e program
T was
h successfull
e y executed
a and
b desired
o output was
v obtained.
EXP NO: 8 DATE:

Predicate Logic Generation

AIM:
To write a java program for implementing predicate logic.

ALGORITHM:

Start the program

Get the statement from the user.

If the word all is there, print for all x.

If the word some is there, print there exist some x.

Next, print the first letter along with (x).

Now check for negation, i.e. not and accordingly print further.

End the program.


PROGRAM:

import java.util.Scanner;

public class predLogic {


public static void main(String args[])
{

Scanner scanner=new Scanner(System.in); String string =scanner.nextLine();

String words[]=string.split(" "); int flag=0,none=0;

Boolean all=false;

for(int i=0;i<words.length;i++)
{
if(words[i].equalsIgnoreCase("all"))
{

System.out.print("for all x("+words[++i].charAt(0)+"(x)"); all=true;

none=1;
}
else if(words[i].equalsIgnoreCase("some"))
{

System.out.print("There exists some x("+words[++i].charAt(0)+"(x)"); all=false;

none=1;
}
if(words[i].equalsIgnoreCase("not")&& all)
{

System.out.print("->not "+words[++i].charAt(0)+"(x))"); flag=1;

}
if(words[i].equalsIgnoreCase("not") && !all)
{

System.out.print("^not "+words[++i].charAt(0)+"(x))"); flag=1;

}
}

if(flag==0 && none==1)


{
System.out.print("->"+words[words.length-1]+"(x))");
}

Boolean isNot=true; if(none==0)

{
for(int i=0;i<words.length;i++)

{
if(words[i].equalsIgnoreCase("not"))
isNot=true;
}
if(!isNot)

System.out.print(" "+words[words.length-1]+"("+words[0]+")");
else{System.out.print(" "+words[words.length-1]+"("+words[0]+")");
}
}
}
}
OUTPUT:
RESULT:
T am has
h been
e successfull
p y executed
r and the
o output has
g been
r verified.
EXP NO: 9

DATE:

DEVELOP EXPERT SYSTEM FOR MEDICAL DIAGNOSIS

AIM:

To write a program to create an expert system for medical diagnosis.

ALGORITHM:

Start the program.

Create an array including some of the common diseases.

Accept the input from user asking which disease.

Check if the disease entered by the user matches any of the diseases from the array created.

If the disease matches, then based on the input disease prescribe the diagnosis.

Display the diagnosis according to the disease.

Stop the program.


PROGRAM:

import java.util.Scanner; import java.io.*;

public class medi{

public static void main(String args[])throws IOException{ Scanner scanner = new


Scanner(System.in); System.out.println("wat is ur disease");

String disease = scanner.nextLine();

String fever,cough,headche,cold;

if (disease.contains("fever") )

System.out.println(" daily 1 crocin\n take rest\n");

else if(disease.contains("cough"))

System.out.println("drink chericof at regular intervals\n reduce ur

stress\n");

else if (disease.contains("headache"))

System.out.println("apply iodex cream\n sleep for 1 hour\n");

else if(disease.contains("cold"))
{

System.out.println("daily 1 sinarest tab at nyt\n dont go out in cool

breeze\n");

}}
OUTPUT:
RESULT:
The
abov
e
prog
ram
has
been
exec
uted
succ
essfu
lly
and
verif
ied.

Anda mungkin juga menyukai