Design and implement the class CupDispenser. Each CupDispenser object has a loca
ID: 3540623 • Letter: D
Question
Design and implement the class CupDispenser. Each CupDispenser object has a location and keeps track of the number of cups it currently contains. Define two constructors, set and get methods for each data field, and the method toString. Also, define a method getOneCup that simply decrements the number of cups in the dispenser by 1. Demonstrate your class by creating several CupDispenser objects.
We are also asked to define the method takeCupsFrom that removes all the cups from its argument, a CupDispenser object, and adds them to its recieving object. For example, if cd1 contains 10 cups, and cd2 contains 20 cups, after the call cd1.takeCupsFrom(cd2), cd1 will contain 30 cups and cd2 will be empty. --That's the part i'm having the most trouble with. Everything else looks alright, but I've been stuck on this for ages!!
This is what I have so far... http://ideone.com/pHpBmB (I'm just learning so I know it isn't very good!) Any help is greatly appreciated because I have been confused over this for tooo long!!!!!
Explanation / Answer
Your code is fine except for the takeCupsFrom methods, i changed it to this. You can send a object as argument to a method :)Also you will be needing a new methods emptyDispenser()
public void takeCupsFrom(CupDispenser d) { numberCups = numberCups + d.getNumberCups(); d.emptyDispenser(); // use directly d.numberCups=0 here if u are not supposed to create more methods. } public void emptyDispenser() { numberCups = 0; }
In the main function call it in this way:
d2.takeCupsFrom(d1);
Cheers!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.