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

jar.h

#include <algorithm>
#include <vector>

using namespace std;

class Jar
{
public:
Jar(int new_capacity =8);

bool isEmpty();
bool isFull();

int getvolume();
int getcapacity();

int pour(Jar&);

private:
int capacity;
int volume;


};

jar.cpp

#include "jar.h"


Jar::Jar(int new_capacity)
{
if(new_capacity <=0)
new_capacity =8;
capacity = new_capacity;
volume =0;
}
bool Jar::isEmpty()
{
return true;
}
bool Jar::isFull()
{
return true;
}

int Jar::getvolume()
{
return 0;
}
int Jar::getcapacity()
{
return 0;
}
int Jar::pour(Jar& jar)
{
int this_avail = capacity - volume;
//int jar_avail = jar.valume;
int pour_amt =0;
volume += pour_amt;
jar.volume -= pour_amt;
return pour_amt;
}
main.cpp

#include <iostream>
#include <vector>
#include "jar.h"

using namespace std;
int main()
{
int jar;
vector<Jar>Jar ={(8), (5), (3)};
Jar[0].pour(Jar[1]);
do
{
cout<<"read source " <<endl;
cout<<"read distination " <<endl;
int srce =0;
int dest = 1;
Jar[dest].pour(Jar[srce]);
cout<< Jar[0].getvolume()<< " "<< Jar[0].getvolume()<<Jar[0].getvolume()<<endl;

}while(Jar[1].getvolume()!=4);


return 0;


}

Solutions

Expert Solution

Ans: Solution

1) Empty Function

#include <iostream>

#include <stack>

using namespace std;

int main()

{

    stack<int> mystack;

    mystack.push(1);

    // Stack becomes 1

    if (mystack.empty()) {

        cout << "True";

    }

    else {

        cout << "False";

    }

    return 0;

}

2) Final Value Function

#include <iostream>

#include <stack>

using namespace std;

int main()

{

    int sum = 0;

    stack<int> mystack;

    mystack.push(1);

    mystack.push(8);

    mystack.push(3);

    mystack.push(6);

    mystack.push(2);

    // Stack becomes 1, 8, 3, 6, 2

    while (!mystack.empty()) {

        sum = sum + mystack.top();

        mystack.pop();

    }

    cout << sum;

    return 0;

}

3) Stack Size

#include <iostream>

#include <stack>

using namespace std;

int main()

{

    int sum = 0;

    stack<int> mystack;

    mystack.push(1);

    mystack.push(8);

    mystack.push(3);

    mystack.push(6);

    mystack.push(2);

    // Stack becomes 1, 8, 3, 6, 2

    cout << mystack.size();

    return 0;

}

4) Check till Stack Size = 0

#include <iostream>

#include <stack>

using namespace std;

int main()

{

    int sum = 0;

    stack<int> mystack;

    mystack.push(1);

    mystack.push(8);

    mystack.push(3);

    mystack.push(6);

    mystack.push(2);

    // Stack becomes 1, 8, 3, 6, 2

    while (mystack.size() > 0) {

        sum = sum + mystack.top();

        mystack.pop();

    }

    cout << sum;

    return 0;

}


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. /* A man goes to a bathtub with two jars, of which one holds exactly 3 pints and...
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