Question

Programming Problem: define a C++ Bucket class with the following methods: Bucket() constructor that takes two parameters, aImplement a main method that creates two bucket objects A and B with capacities of 3 and 5 gallons. In a loop, print the curr

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi there,

Following is the answer if you still have any queries,feel free to ask in the comments box.

Code:

#include <iostream>

using namespace std;

class Bucket

{public:

string name;

int capacity;

int contents;

Bucket(string name,int capacity)//parameterised constructor

{

this->name=name;

this->capacity=capacity;

this->contents=0;

}

void print()//print the details

{

cout<<"Bucket name : "<<name<<endl;

cout<<"Bucket capacity : "<<capacity<<"units"<<endl;

cout<<"Bucket contents : "<<contents<<"units"<<endl;

}

void fill()//fiill the bucket

{

contents=capacity;

}

void empty()//empty thr bucket

{

contents=0;

}

int pour_into(Bucket *b)//pour into another

{

if(this->contents+b->contents<=b->capacity)

{

b->contents+=this->contents;

int temp=this->contents;

empty();

return temp;

}

else if(b->capacity-b->contents<=contents)

{

int needed=(b->capacity-b->contents);

if(contents>=needed)

{

b->contents+=needed;

contents-=needed;

return needed;

}

else if (contents<needed)

{ int temp=contents;

b->contents+=contents;

empty();

return temp;

}

}

return 0;

}

};

int main()

{

Bucket a("A",3);

Bucket b("B",5);

int c=1;

while(c==1)

{

a.print();

b.print();

cout<<endl;

int ch;

cout<<"0-Exit\n";

cout<<"1-Fill Bucket A\n";

cout<<"2-Empty Bucket A\n";

cout<<"3-Pour A into B\n";

cout<<"4-Fill Bucket B\n";

cout<<"5-Empty Bucket B\n";

cout<<"6-Pour B into A\n";

cout<<"Enter your choice\n";

cin>>c;

switch(c)

{

case 0:

break;

case 1:

a.fill();

break;

case 2:

a.empty();

break;

case 3:

cout<<a.pour_into(&b)<<" units transferred from A to B\n";

break;

case 4:

b.fill();

break;

case 5:

b.empty();

break;

case 6:

cout<<b.pour_into(&a)<<" units transferred from B to A\n";

break;

}

cout<<"Do you wish to continue(1 for yes 0 for no)\n";

cin>>c;

}

return 0;

}

Output:

clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Bucket name : A
Bucket capacity : 3units
Bucket contents : 0units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 0units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
1
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 3units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 0units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
3
3 units transferred from A to B
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 0units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 3units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
4
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 0units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 5units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
6
3 units transferred from B to A
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 3units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 2units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
6
0 units transferred from B to A
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 3units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 2units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
5
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 3units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 0units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
4
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 3units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 5units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
3
0 units transferred from A to B
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 3units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 5units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
5
Do you wish to continue(1 for yes 0 for no)
1
Bucket name : A
Bucket capacity : 3units
Bucket contents : 3units
Bucket name : B
Bucket capacity : 5units
Bucket contents : 0units

0-Exit
1-Fill Bucket A
2-Empty Bucket A
3-Pour A into B
4-Fill Bucket B
5-Empty Bucket B
6-Pour B into A
Enter your choice
0
Do you wish to continue(1 for yes 0 for no)
0

Add a comment
Know the answer?
Add Answer to:
Programming Problem: define a C++ Bucket class with the following methods: Bucket() constructor that takes two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Java programming The purpose of this problem is to practice using a generic Urn class. NOTE:...

    Java programming The purpose of this problem is to practice using a generic Urn class. NOTE: Refer to the code for the ArrayStack class from Chapter 12. Use that code as a starting point to create the Urn class, modifying it to remove the methods in the ArrayStack class (push, pop, etc) then add the methods described below. Also change the variable names to reflect the new class. For example the array name should NOT be stack, instead it should...

  • C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class...

    C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class called Player that represents a player in the National Hockey League. Player class Use the following class definition: class Player { public: Player(); Player( const char [], int, int, int ); void printPlayer(); void setName( const char [] ); void setNumber( int ); void changeGoals( int ); void changeAssists( int ); int getNumber(); int getGoals(); int getAssists(); private: char name[50]; int number; int goals;...

  • Code in C++. Can someone make it so that the code below can be compiled? ▪...

    Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...

  • C++ This exercise will introduce static member variables and static methods in class to you. Class...

    C++ This exercise will introduce static member variables and static methods in class to you. Class Department contain information about universities departments: name students amount in addition it also stores information about overall amount of departments at the university: departments amount class Department { public: Department(string i_name, int i_num_students); ~Department(); int get_students(); string get_name(); static int get_total(); private: string name; int num_students; static int total_departments; }; Carefully read and modify the template. You have to implement private static variable "total...

  • Class River describes river’s name and its length in miles. It provides accessor methods (getters) for...

    Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables and toString() method that returns String representation of the river. Class CTRivers describes collection of CT rivers. It has no data, and it provides the following service methods. None of the methods prints anything, except method printListRec, which prints all rivers. // Prints all rivers recursively. Print them is same order as they were in the list . List can be empy...

  • c++ please need help with this question Consider a class Employee with data members: age(an integer),...

    c++ please need help with this question Consider a class Employee with data members: age(an integer), id (an integer) and salary (a float), and their corresponding member functions as follows: class Employee {        private:             int age;             int id;             float salary;        public:             Employee( ); // default constructor: age=0, id=0, and salary=0             Employee(Employee &x);   // copy constructor           Employee& operator = (Employee &x); // equal sign operator             void setAge(int x);    // let age = x...

  • C++ implement and use the methods for a class called Seller that represents information about a...

    C++ implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; }; Data Members The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT