Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ problem: The program has a ```Jug``` class with missing code in the ```pourTo()``` method. Suppose there are two jug objects ```jugA``` and ```jugB```. The

C++ problem:

The program has a ```Jug``` class with missing code in the ```pourTo()``` method. Suppose there are two jug objects ```jugA``` and ```jugB```. The code ```jugA.pourTo(jugB)``` should pour all amount of tea from ```jugA``` to ```jugB```, or pour until ```jugB``` is full (i.e. its amount is equal to its capacity). There is no effect on both jugs when ```jugA``` is empty, or when ```jugB``` is full.

Fill in the missing code in the ```pourTo()``` method so that the program runs as intended.

#include

#include

using namespace std;

class Jug

{

float capacity_; // the maximum amount that this jug can contain.

float amount_; // the amount this jug currently contains.

public:

Jug(int capacity, int amount);

int getCapacity() const;

int getAmount() const;

void setAmount(int amount);

void pourTo(Jug& jug);

void show() const;

};

Jug::Jug(int capacity, int amount)

{

capacity_ = capacity;

amount_ = amount;

}

int Jug::getCapacity() const

{

return capacity_;

}

int Jug::getAmount() const

{

return amount_;

}

void Jug::setAmount(int amount)

{

amount_ = amount;

}

void Jug::pourTo(Jug& jug)

{

// Write your code here

}

void Jug::show() const

{

cout << "Capacity = " << capacity_ << ", "

<< "Amount = " << amount_ << endl;

}

void DisplayAllJugs(const vector& jugs)

{

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

{

cout << "- Jug[" << i << "] : ";

jugs[i].show();

}

}

int main()

{

cout << "Enter number of jugs -> ";

int m; cin >> m;

vector jugs;

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

{

cout << "Enter capacity and amount for Jug #" << i << " -> ";

float capacity, amount;

cin >> capacity >> amount;

Jug jug(capacity, amount);

jugs.push_back(jug);

}

cout << endl;

cout << "Enter number of pouring -> ";

int k; cin >> k;

int from, to;

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

{

cout << " Pouring #" << i << ":" << endl;

// This loop is written for fun.

// The test cases will not input similar values for 'from' and 'to'.

do {

cout << "From jug -> ";

cin >> from;

cout << "To jug -> ";

cin >> to;

} while (from == to);

cout << "Before pouring: " << endl;

DisplayAllJugs(jugs);

// pouring

jugs[from].pourTo(jugs[to]);

cout << "After pouring: " << endl;

DisplayAllJugs(jugs);

}

}

---------------------------------------------------------

## Sample Run 1

```

Enter number of jugs -> 5

Enter capacity and amount for Jug #0 -> 100 20

Enter capacity and amount for Jug #1 -> 200 150

Enter capacity and amount for Jug #2 -> 300 300

Enter capacity and amount for Jug #3 -> 400 0

Enter capacity and amount for Jug #4 -> 500 490

Enter number of pouring -> 6

Pouring #0:

From jug -> 0

To jug -> 2

Before pouring:

- Jug[0] : Capacity = 100, Amount = 20

- Jug[1] : Capacity = 200, Amount = 150

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 0

- Jug[4] : Capacity = 500, Amount = 490

After pouring:

- Jug[0] : Capacity = 100, Amount = 20

- Jug[1] : Capacity = 200, Amount = 150

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 0

- Jug[4] : Capacity = 500, Amount = 490

Pouring #1:

From jug -> 0

To jug -> 4

Before pouring:

- Jug[0] : Capacity = 100, Amount = 20

- Jug[1] : Capacity = 200, Amount = 150

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 0

- Jug[4] : Capacity = 500, Amount = 490

After pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 150

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 0

- Jug[4] : Capacity = 500, Amount = 500

Pouring #2:

From jug -> 4

To jug -> 3

Before pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 150

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 0

- Jug[4] : Capacity = 500, Amount = 500

After pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 150

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 400

- Jug[4] : Capacity = 500, Amount = 100

Pouring #3:

From jug -> 1

To jug -> 4

Before pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 150

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 400

- Jug[4] : Capacity = 500, Amount = 100

After pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 0

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 400

- Jug[4] : Capacity = 500, Amount = 250

Pouring #4:

From jug -> 1

To jug -> 0

Before pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 0

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 400

- Jug[4] : Capacity = 500, Amount = 250

After pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 0

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 400

- Jug[4] : Capacity = 500, Amount = 250

Pouring #5:

From jug -> 4

To jug -> 2

Before pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 0

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 400

- Jug[4] : Capacity = 500, Amount = 250

After pouring:

- Jug[0] : Capacity = 100, Amount = 10

- Jug[1] : Capacity = 200, Amount = 0

- Jug[2] : Capacity = 300, Amount = 300

- Jug[3] : Capacity = 400, Amount = 400

- Jug[4] : Capacity = 500, Amount = 250

```

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

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions

Question

what is the most common cause of preterm birth in twin pregnancies?

Answered: 1 week ago

Question

Which diagnostic test is most commonly used to confirm PROM?

Answered: 1 week ago

Question

What is the hallmark clinical feature of a molar pregnancy?

Answered: 1 week ago