In: Computer Science
Question 1)
The Following OOP Problem must be completed in C++
Consider a bubble gum dispenser.The dispenser releases one bubble gum at a time until empty. Filling of the dispenser adds a positive number of bubble gums.
A) Write an Abstract Data Type for a bubble gum dispenser
B) Draw the UML class diagram
C) Define a C++ class for a bubble gum dispenser object
D) The number of bubble gums in the dispenser is private
E) Write an implementation for the class
F) Write a simple test program to demonstrate that your class is implemented correctly
We need to create a Bubble Gum Dispenser.
There is only one value that we need to keep track of, and that's the number of bubblegum inside the dispenser.
There are only functions available for a dispenser, either we can take out a bubble gum or we can fill it up. We can only obtain a bubble gum if the dispenser is not empty.
What is Abstract Data Type ?
An abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior (only behavior is defined but not implementation). Real life example: book is Abstract (Telephone Book is an implementation)
Hence an abstract data type for this question can be Dispenser object which has 1 function (to dispense whatever it holds), another function that is done on it (refill) and only one value to store (quantity of whatever it holds).
UML Diagram
We can also define a user class to show how our class interactd with the world
C++ Class
class bubblegum
{
private :
int quantity;
public :
bubblegum(int x = 0)
{
quantity = x;
}
void refill(int x)
{
quantity += x;
}
void dispense()
{
if(quantity > 0)
quantity--;
}
};
Example Program :
Hope this helps!