## 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

#### 15 comments:

1. wow.....you are GENIUS....great coder

1. Thanks vinay raghuvanshi

2. Great Article android based projects

Java Training in Chennai

Project Center in Chennai

Java Training in Chennai

projects for cse

The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

2. Design and implement the solution for water jug problem using two jugs, which is stated as follows:
“You are given two jugs, a 4‐litre one and a 3‐litre one. Neither have any measuring markers on it. There is a pump that can be used to fill the jugs with water. How can you get exactly 2 litres of water into 4‐litre jug.”
Program in java solutions

3. myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

4. Informative Article… I want to read your articles in light of the fact that your composition style is too great... Customer Reconciliation
Warehouse Audit
Chartered Accountant

5. Amazing & Useful post.Thanks for sharing this post.Keep in blogging. Continuous Monitoring
Duplicate Payment Audit
AR Customer Helpdesk

6. The AI I've encountered (and this is essential, scratching just the outside of the profundity of concentrates into AI) is undeniably increasingly particular, concentrating on only one part of an issue that could conceivably be a lot bigger. artificial intelligence course

7. This comment has been removed by the author.

8. Many experts and industry analysts argue that AI or machine learning is the future - but if we look around, we are convinced that it's not the future - it is the present. artificial intelligence training in hyderabad

9. IBM had achieved reenacting 10 billion neurons to speak to 100 trillion neurotransmitters.cyber security course in hyderabad