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
  • Create a LIFO class with following methods in java - public LifoList() The default constructor for...

    Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...

  • 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...

  • 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...

  • 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...

  • A) Please implement a Python script to define a student class with the following attributes (instance...

    A) Please implement a Python script to define a student class with the following attributes (instance attributes): cwid: student’s CWID first_name : student’s first name last_name: student’s last name gender: student’s gender gpa: student’s gpa Please make these attributes as private ones so they have to be accessed via getter/setter methods or property. For this purpose, please define a getter/setter method and property for each of the attributes. Another thing you need to do is to define a constructor that...

  • 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...

  • 1- Create the base class Book that has the following instance variables, constructor, and methods title...

    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 ( String) isbn...

  • Design and implement a set of classes that define the employees of a hospital. Start by...

    Design and implement a set of classes that define the employees of a hospital. Start by creating a HospitalEmployee super class, from which three other classes will inherit. The HospitalEmployee class will have the following components: • name - a private String instance variable containing the employee's name (taken in the object constructor) • field - a private String instance variable containing the employee's field (e.g. cardiology, oncology) (taken in the object constructor) • onCall - a private boolean instance...

  • 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...

  • Our teacher wants us to use a class and a driver class for this problem. Band...

    Our teacher wants us to use a class and a driver class for this problem. Band Booster Class In this exercise, you will write a class that models a band booster and use your class to update sales of band candy. . Write the BandBooster class assuming a band booster object is described by two pieces of instanced name (a String) and boxes Sold (an integer that represents the number of boxes of band candy the booster has sold in...

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