Documentos de Académico
Documentos de Profesional
Documentos de Cultura
DATE:
AIM:
To create a Water Jug Program in Java to check for the final state.
ALGORITHM:
1. Start
2. Ask the size of the jugs and the final state.
3. Fill the small jug from the tap (0,3)
4. Pour this into the larger jug (3,0)
5. Fill the larger jug from the tap (3,3)
6. Pour from the small jug into the larger jug until full. This leaves difference in the small jug
(4,2)
7. Continue till the final step is reached.
8. 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:
The above program has been successfully executed and verified.
EXP NO: 2
DATE:
AIM:
To search water jug problem in Java using Breadth First Search.
ALGORITHM:
1. Start
2. Ask the user the size of the jug.
3. Ask the user the final state.
4. Calculate all possible combinations of the given water jug problem.
5. Store all the results in a queue.
6. Use Breadth First Technique to find the final solved state breadth wise.
7. If found display the nodes traversed and the path to the final state.
8. If not found, display an appropriate message to the user.
9. 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:
AIM:
To search water jug problem in Java using Depth First Search
ALGORITHM:
1. Start
2. Ask the user the size of the jug.
3. Ask the user the final state.
4. Calculate all possible combinations of the given water jug problem.
5. Store all the results in a queue.
6. Use Depth First Technique to find the final solved state Depth wise.
7. If found display the nodes traversed and the path to the final state.
8. If not found, display an appropriate message to the user.
9. 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:
AIM:
To generate a magic square of order n.
ALGORITHM:
1. Start.
2. Get the input for order n from the user.
3. If n is odd, then assign the integers 1 to n^2 in ascending order, starting at the bottom,
middle cell.
4. Repeatedly assign the next integer to the right and down.
5. If this cell has already been assigned another integer, instead use the cell adjacently
above.
6. Use wrap-around to handle border cases.
7. If n is even, generate normal matrix in which the integers 1 to n^2 are listed across the
rows.
8. Then interchange the diagonals that are symmetrically located with respect to the center.
9. Print the generated matrix of order n by n.
10. 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);
}
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);
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++)
{
for(int j=0;j<x;j++)
System.out.print(" "+arr[i][j]);
System.out.println();
}
}
}
OUTPUT:
RESULT:
The magic square of order n is generated and the desired output is obtained.
EXP NO: 5(A)
DATE:
AIM:
To write a java program for Tic-Tac-Toe Player vs. Player
ALGORITHM:
1. Start .
2. Create the class and initialize the variables.
3. Choose the player 1 or 2.
4. Assign the moves for the respective players.
5. Repeat (4) until any of the following conditions are obtained
a. Player 1 wins
b. Player 2 wins
c. Game results in a tie.
6. Display every step (or move) in the output.
7. 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:
The Program was compiled and executed successfully.
EXP NO: 5(B)
DATE:
AIM:
To write a java program for Tic-Tac-Toe Player vs. Computer
ALGORITHM:
1. Start .
2. Create the class and initialize the variables.
3. Start with the Computer move as the (1,1) .
4. Get the player moves from user.
5. Repeat (4) until any of the following conditions are obtained
a. Computer wins
b. Player wins
c. Game results in a tie.
6. Display every step (or move) in the output.
7. 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) {
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:
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:
The program has been successfully executed and the output has been verified.
EXP NO: 7
DATE:
AO* ALGORITHM
AIM:
ALGORITHM:
}
}
}
System.out.println(found);
}
}
OUTPUT:
RESULT:
The above program was successfully executed and desired output was obtained.
EXP NO: 8
DATE:
Predicate Logic Generation
AIM:
To write a java program for implementing predicate logic.
ALGORITHM:
RESULT:
The program has been successfully executed and the output has been verified.
EXP NO: 9
DATE:
AIM:
To write a program to create an expert system for medical diagnosis.
ALGORITHM:
1. Start the program.
2. Create an array including some of the common diseases.
3. Accept the input from user asking which disease.
4. Check if the disease entered by the user matches any of the diseases from the array
created.
5. If the disease matches, then based on the input disease prescribe the diagnosis.
6. Display the diagnosis according to the disease.
7. 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 above program has been executed successfully and verified.