Question
Subject + registerObserver(Observer observer): void + removeObserver(Observer observer): void + notifyObservers(): void Baby - observers: ArrayList - cry: Cry + Baby() + registerObserver(Observer observer): void
<> Subject + registerObserver(Observer observer): void + removeObserver(Observer observer): void + notifyObservers(): void
Baby - observers: ArrayList - cry: Cry + Baby() + registerObserver(Observer observer): void + removeObserver(Observer observer): void + notifyObservers(): void + angryCry(): void + hungryCry(): void + wetCry(): void
<> Observer + update(Cry cry): void
Mon
-baby: Subject
+Mom(Subject baby)
+update(Cry cry): void
Dad
-baby: Subject
+update(Cry cry): void
public class CribDriver { public void playBabySim() { Baby baby = new Baby(); Dad dad = new Dad(baby); Mom mom = new Mom(); baby.hungryCry(); baby.wetCry(); baby.hungryCry(); baby.hungryCry(); baby.wetCry(); } public static void main(String[] args) { CribDriver cd = new CribDriver(); cd.playBabySim(); }
}
Output
A new baby is born
This baby is hungry Dad feeds baby Mom cuddles and feeds baby
This baby is wet Dad calls mom Mom changes baby
This baby is angry! Dad puts in earplugs
This baby is hungry Mom cuddles and feeds baby
This baby is wet Mom changes baby
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started