Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(JAVA - NEED ASAP PLEASE, will rate right away) The purpose of this problem is to gain familiarity with stacks and queues. You have three

(JAVA - NEED ASAP PLEASE, will rate right away)

The purpose of this problem is to gain familiarity with stacks and queues. You have three jugs that can hold c1, c2, and c3 liters of water, respectively. Initially, jug 1 is full and the other two jugs are empty. You can repeat the following procedure any number of times: Choose two of the jugs and pour the contents of one into the other until either the first is empty or the second is full. Your goal is to end up with exactly d liters in one of the jugs. Write a program called WaterJugs to determine the transfers required to reach the goal. The input is a single line containing four integers between 2 and 100 (inclusive) representing c1, c2, c3, and d. The output is a minimal sequence of jug contents, starting with the initial contents and ending with one of the jugs containing d liters. Each line of the output should consist of 3 integers separated by spaces. If no solution exists, then your program should produce no output. Good test case: 10 5 3 4 ; from movie Die hard: with a vengeance; For example, if the input is 20 5 3 4 then a valid output is 20 0 0 15 5 0 15 2 3 18 2 0 18 0 2 13 5 2 13 4 3 There may be other solutions, but none with fewer than this one (6 transfers)Use States.java as part of the solution:

/* * Define states to be used in solving the water jugs problem. */ class State { static int[] capacity; int[] contents; State pred; // predecessor state static void setCapacities(int[] c) { } State(int[] c) { } State move(int source, int destination) { } boolean reachedGoal(int d) { } void display() { System.out.println(contents[0] + " " + contents[1] + " " + contents[2]); } } 

//Waterjugs.java

import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import java.util.Scanner; public class WaterJugs { public static void main(String args[]) { Queue q = new LinkedList(); Stack st = new Stack(); ArrayList visited = new ArrayList(); Scanner scan = new Scanner(System.in); int[] cap = new int[3]; for (int i = 0; i < 3; ++i) { cap[i] = scan.nextInt(); } int d = scan.nextInt(); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

What is American Polity and Governance ?

Answered: 1 week ago

Question

Proficiency with Microsoft Word, Excel, PowerPoint

Answered: 1 week ago