DATE:
AIM:
To create a Water Jug Program in Java to check for the final state.
ALGORITHM:
Start
Pour from the small jug into the larger jug until full. This leaves difference in the small jug
(4,2)
{
public static void main(String args[])
{
jug1 = a; jug2 = b;
}
else
{
jug1 = b; jug2 = a;
}
for(i = 0;i < 50;i++)
{
if(j1 == state[0] && j2 == state[1])
{
{
j1 = jug1;
}
else if(j2 == jug2)
{
j2 = 0;
}
{
if(j1 == state[0] && j2 == state[1])
{
{
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)
{
}
}
OUTPUT:
RESULT:
ove
T program
h has been
e successfull
a y executed
b and
verified.
EXP NO: 2
DATE:
AIM:
ALGORITHM:
Start
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.
Stop.
PROGRAM:
import java.util.*; public class BFS
{
public static void main(String args[])
{
state[1] = sc.nextInt();
return false;
}
}
else if(j1 == jug1 && j2 == 0)
{
j2 = jug2;
j1 = j1 - j2;
}
else if(j1 == 0 && j2 == jug2)
j1 = j2;
j2 = 0;
}
else if(j1 == jug1 && j2 == jug2)
{
}
else if(j1 < jug1 && j2 == jug2)
{
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)
{
j1 = j1 - jug2 + j2;
j2 = jug2;
}
else if(j1 < jug1 && j2 == 0)
{
}
else if(j1 == 0 && j2 < jug2)
{
j1 = j2; j2 = 0;
if(x == true)
{
}
if(!(closed.contains(node)))
{
closed.add(node);
{
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);
x = a2.indexOf(state); path.add(state);
DATE:
AIM:
ALGORITHM:
Start
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.
Stop.
PROGRAM:
import java.util.*; public class DFS
{
public static void main(String args[])
{
state[1] = sc.nextInt();
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])
}
else if(j1 == jug1 && j2 == 0)
{
j2 = jug2;
j1 = j1 - j2;
}
else if(j1 == 0 && j2 == jug2)
{
j1 = j2;
j2 = 0;
}
else if(j1 == jug1 && j2 == jug2)
{
}
else if(j1 < jug1 && j2 == jug2)
{
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)
{
j1 = j1 - jug2 + j2;
j2 = jug2;
}
else if(j1 < jug1 && j2 == 0)
{
}
else if(j1 == 0 && j2 < jug2)
j1 = j2;
j2 = 0;
}
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)");
if(x == true)
{
}
if(!(closed.contains(node)))
{
closed.add(node);
q.add(child);
if(!(closed.contains(child)) && !(a2.contains(child)))
{
a1.add(node);
a2.add(child);
}
}
}
i++;
}while(i < 100);
}
int x;
if(state == "(0,0)")
{
path.add(state);
else
{
x = a2.indexOf(state); path.add(state);
DATE:
AIM:
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.
If this cell has already been assigned another integer, instead use the cell adjacently above.
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[])
{
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);
}
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;
}
}
display(arr,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);
}
{
for(int i=0;i<x;i++)
{
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:
AIM:
ALGORITHM:
Start .
Player 1 wins
Player 2 wins
Quit.
PROGRAM:
import java.io.*; public class PvsP
{
static char a[][]=new char[3][3];
public static void main(String args[])throws IOException
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
a[i][j]='*';
}
while(true)
{
n++;
display();
if(n==9)
break;
if(win('x')==true)
{
n++;
display();
if(n==9)
break;
if(win('o')==true)
{
}
}
if(n==9)
System.out.println("Draw!");
}
static Boolean win(char n)
{
int i; for(i=0;i<3;i++)
}
for(i=0;i<3;i++)
}
}
OUTPUT:
RESULT:
m was
T compiled
he and
Pr executed
ogsuccessful
ra ly.
EXP NO: 5(B)
DATE:
AIM:
ALGORITHM:
Start .
Computer wins
Player wins
Quit.
PROGRAM:
import java.util.Random; import java.util.Scanner;
public class PvsC { public enum eStatus {
IN_PROGRESS,
TIE_GAME,
COMPUTER_WINS,
PLAYER_WINS,
tic.computer();
else
tic.player();
}
eStatus status = tic.status();
if (status == eStatus.TIE_GAME)
System.out.println("Game is tied!");
System.out.println("Computer wins!");
System.out.println("Player wins!");
board[row][col] = '-';
board[1][1] = 'X';
System.out.println("------------------------------");
System.out.println();
}
private void printBoard() {
System.out.println("------------------------------");
System.out.println();
System.out.println();
System.out.println("\n\nx--> Computer\t\to-->Player\n\n");
while (true) {
for (int row = 0; row <= 2; ++row) for (int col = 0; col <= 2; +
+col)
if (tieGame)
if ((board[row][0] == c) &&
(board[row][1] == c) && (board[row]
[2] == c)) return true;
return false;
}
OUTPUT:
RESULT:
IMPLEMENTATION OF A* ALGORITHM
AIM:
To write a java program implementing A* algorithm using 8 puzzle problem as example.
ALGORITHM:
If the input string doesnt have any optional optimal solution point,the solution doesnt exist.
}
System.out.println("Solution doesn't exist");
}
void add(String newState, String oldState)
{
if(!stateDepth.containsKey(newState))
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)+currentState.substring(a+3,a+4)+
currentState.substring(a+1,a+3)+"0"+currentState.substring(a+4);
checkCompletion(currentState,nextState);
}
}
void left(String currentState)
{
StringnextState=currentState.substring(0,a)+
currentState.charAt(a+1)+"0"+currentState.substring(a+2); checkCompletion(currentState,
nextState);
}
}
private void checkCompletion(String oldState, String newState)
{
AO* ALGORITHM
AIM:
ALGORITHM:
Compare the string length to select an array of strings having same string length.
Scanner sc= new Scanner(System.in); final String word= sc.nextLine(); boolean found=false;
if(x.length()==word.length()){
first.add(x);
int m=0;
String s=first.get(i).substring(0,j);
String c= word.substring(0,j);
first.remove(i);
System.out.println(first);
m--;
i=0;
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:
AIM:
To write a java program for implementing predicate logic.
ALGORITHM:
Now check for negation, i.e. not and accordingly print further.
import java.util.Scanner;
Boolean all=false;
for(int i=0;i<words.length;i++)
{
if(words[i].equalsIgnoreCase("all"))
{
none=1;
}
else if(words[i].equalsIgnoreCase("some"))
{
none=1;
}
if(words[i].equalsIgnoreCase("not")&& all)
{
}
if(words[i].equalsIgnoreCase("not") && !all)
{
}
}
{
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:
AIM:
ALGORITHM:
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.
String fever,cough,headche,cold;
if (disease.contains("fever") )
else if(disease.contains("cough"))
stress\n");
else if (disease.contains("headache"))
else if(disease.contains("cold"))
{
breeze\n");
}}
OUTPUT:
RESULT:
The
abov
e
prog
ram
has
been
exec
uted
succ
essfu
lly
and
verif
ied.