Question

In: Computer Science

Rewrite the Jars problem to use a Jar class and a vector of Jar objects The...

Rewrite the Jars problem to use a Jar class and a vector of Jar objects

The Jar class has 2 private members - volume, capacity, a default constructor, a 1 parameter constructor, 2 accessors but only 1 mutator, two boolean methods isEmpty and IsFull, a 1 Jar reference parameter method, pour, that pours the contents of its parameter into the method's Jar.

/*

A man goes to a bathtub with two jars, of which one holds exactly 3 pints and the other 5 pints.

How can be bring back exactly 4 pints of water. The solution presents no difficulty.

Empty(source, destination)

Fill(source, destination)

Pour(source, destination)

   while source is not empty and destination is not full

aubtract 1 from volume(source)

add 1 to volume(destination)

   end while

End Pour

tub(capacity) = 8

jar1(capacity) = 5

jar2(capacity) = 3

tub(volume) = 8

jar1(volume) = 0

jar2(volume) = 0

display(count, tub, jar1, jar2)

count = 0

repeat

read source, destination

pour source, destination

count = count + 1

display(count, tub, jar1, jar2)

until jar1(volume) = 4

*/

#include <iostream>

#include <string>

using namespace std;

const int TUB = 0;

const int JAR1 = 1;

const int JAR2 = 2;

const int capacity[] = {8, 5 , 3};

const string names[] = {"Tub", "Jar 1", "Jar 2"};

int volume[] = {8, 0, 0};

void display(int count);

void read(int& source, int& destination);

void pour(int source, int destination);

bool isEmpty(int container);

bool isFull(int container);

int main()

{

    int count = 0;

    display(count);

    do

    {

        int source, destination;

        read(source, destination);

        pour(source, destination);

        count++;

        display(count);

    }

    while(volume[JAR1] != 4);

    system("pause");

    return 0;

}

void pour(int source, int destination)

{

    while(!isEmpty(source) && !isFull(destination))

    {

        volume[source]--;

        volume[destination]++;

    }

}

bool isEmpty(int container)

{

    return volume[container] == 0;

}

bool isFull(int container)

{

    return volume[container] == capacity[container];

}

void read(int& source, int& destination)

{

    cout << "source? ";

    cin >> source;

    cout << names[source] << " is the source" << endl;

    cout << "destination? ";

    cin >> destination;

    cout << names[destination] << " is the destination" << endl;

}

void display(int count)

{

    cout << count << ": ";

    for(int index = TUB; index <= JAR2; index++)

        cout << volume[index] << ' ';

    cout << endl;

}

Solutions

Expert Solution

Here is the solution. Please do upvote thank you.

#include <iostream>
#include<vector>
using namespace std;

class Jar{
private:
int capacity;
int volume;
public:
// default constructor
Jar(){
volume=0;
}
// parametrized constructor
Jar(int c){
capacity=c;
volume=c;
}
// accessor for capacity
int getCapacity(){
return capacity;
}
// accessor for volume
int getVolume(){
return volume;
}
//setter for capacity
void setCapacity(int c){
capacity=c;
}
// check if jar is empty
bool isEmpty(){
return volume==0;
}
// check if jar is full
bool isFull(){
return volume==capacity;
}
// pour from given jar to method's jar
void pour(Jar &j){
while(!j.isEmpty()&&!this->isFull()){
j.volume--;
this->volume++;
}
}
};

// driver code
int main()
{
Jar j(10);
Jar j1;
j1.setCapacity(20);
Jar j2;
j2.pour(j);
j2.setCapacity(13);
vector<Jar>v;// vector of Jar objects
v.push_back(j);
v.push_back(j1);
v.push_back(j2);
for(int i=0;i<v.size();i++){
cout<<"Capacity: "<<v[i].getCapacity()<<" ,Volume: "<<v[i].getVolume()<<endl;;
}
return 0;
}

Output:


Related Solutions

Rewrite the Jars problem to use a Jar class and a vector of Jar objects The...
Rewrite the Jars problem to use a Jar class and a vector of Jar objects The Jar class has 2 private members - volume, capacity, a default constructor, a 1 parameter constructor, 2 accessors but only 1 mutator, two boolean methods isEmpty and IsFull, a 1 Jar reference parameter method, pour, that pours the contents of its parameter into the method's Jar jar.h #include <algorithm> #include <vector> using namespace std; class Jar { public: Jar(int new_capacity =8); bool isEmpty(); bool...
Rewrite the Jars problem to use a Jar class and a vector of Jar objects The...
Rewrite the Jars problem to use a Jar class and a vector of Jar objects The Jar class has 2 private members - volume, capacity, a default constructor, a 1 parameter constructor, 2 accessors but only 1 mutator, two boolean methods isEmpty and IsFull, a 1 Jar reference parameter method, pour, that pours the contents of its parameter into the method's Jar. /* A man goes to a bathtub with two jars, of which one holds exactly 3 pints and...
The company is currently producing and selling jars of sauce The jars of sauce sell for $4 per jar.
Consider the production cost information for Mama Italiano Sauce given below:Mama Italian SauceProduction Cost BudgetApril 2008Production - Jars of sauce20,000Ingredient cost (variable)$16,000Labor cost (variable)9,000Rent (fixed)4,000Depreciation (fixed)6,000Other (fixed)1,000Total$36,000The company is currently producing and selling jars of sauce The jars of sauce sell for $4 per jar. The company is considering lowering the price to $3.70 per jar. Suppose this action will increase sales. What is the incremental costs associated with producing an extra 68,750 jars of sauce?Round to two decimal places.
2. [Uncertainty and risk] A DM is presented with two jars. Jar 1 has 50 red...
2. [Uncertainty and risk] A DM is presented with two jars. Jar 1 has 50 red and 50 blue balls. Jar 2 consists of 100 total balls each of which is either red or blue but the colors are in an unknown proportion. An experiment consists of drawing a single ball from each jar. The DM faces the following two choices. Choice 1 is between option 1a which pays $100 if the Jar 1 ball is red, and option 1b...
You have two jars of m&m's. Jar 1 has a defined proportion of yellow and orange...
You have two jars of m&m's. Jar 1 has a defined proportion of yellow and orange m&m's (10% of each). Jar 2 is either 5% orange, 5% yellow OR 14% orange, 17% yellow. If you draw one M&M from jar 2, what is the probability that it is orange or yellow?
You have two jars of m&m's. Jar 1 has a defined proportion of yellow and orange...
You have two jars of m&m's. Jar 1 has a defined proportion of yellow and orange m&m's (10% of each). Jar 2 is either 5% orange, 5% yellow OR 14% orange, 17% yellow. If you draw one M&M from jar 2, what is the probability that it is orange or yellow?
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem :...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem : The Rectangle class (Filename: TestRectangle.java) Design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height....
Mark Make-a-Buck sells jars of beach sand in Florida. His price per jar is $2.50. His...
Mark Make-a-Buck sells jars of beach sand in Florida. His price per jar is $2.50. His historical costs have been:             Jar                                                       $.40 per item             Cap                                                      $.10 per item             Label                                                   $.30 per item             Sales commission to retailers             $.20 per item             Excavating machine depreciation    $500 per month             Mark’s salary (President)                   $1,000 per month       a. What is Mark’s breakeven point for the month, in terms of both quantity and sales dollars...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
R problem 1. Student records. Create an S3 class studentRecord for objects that are a list...
R problem 1. Student records. Create an S3 class studentRecord for objects that are a list with the named elements ‘name’, ‘subjects completed’, ‘grades’, and ‘credit’. Write a studentRecord method for the generic function mean, which returns a weighted GPA, with subjects weighted by credit. Also write a studentRecord method for print, which employs some nice formatting, perhaps arranging subjects by year code. Finally create a further class for a cohort of students, and write methods for mean and print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT