Chapter 5 |
Tasks (to be submitted by Mon 16.03)
Task 1. Inheritance tree
Create at least 5 classes which are related to each other in a tree-form hierarchy. The classes can be on any topic: for example geometric shapes, animals, sport, food, etc.
With the class hierarchy created, demonstrate solving tasks which require:
- overriding
- polymorphism
Add comments where overriding and polymorphism are used.
In MS Paint or any other editor, draw the inheritance tree of the classes. The drawing must also contain the Object
class.
Task 2. Radio listener
Create a class RadioListener
that represents radio listener. Add a void
method listen that takes a radio broadcast as its argument (String
) and remembers it. Add a second method recall that returns all broadcasts that a particular RadioListener
has heard, as a list of strings.
Create a class RadioTransmitter
that is able to transmit broadcasts to several listeners. On creation of the transmitter, the list of its listeners is empty. To add a new listener, the class should have a method addListener that remembers the RadioListener
given as an argument. The class also has the method broadcast that takes the broadcast given to it as a String
argument and transmits it to all its listeners (use the listen method from the RadioListener
class).
Create a subclass of RadioTransmitter
called BingoNumberInformer
. Add a drawAndBroadcast method (without parameters) which generates lucky numbers for a bingo game (10 random numbers in the range between 1 and 100) and broadcasts them as a String
message to the listeners attached to the transmitter. The numbers should be delimited by spaces and sent as a single broadcast (Hint: to broadcast the message, use the broadcast method from the RadioTransmitter
class).
Create a subclass of RadioTransmitter
called LoudRadio
. The class should have a method called broadcast (use overriding), with a parameter. The method broadcasts the message given as an argument in capital letters to all listeners.
Create a subclass of RadioTransmitter
called NewsRadio
. The class should have a method with the signature void currentNews(ArrayList<String> news), which sets the list of current news. To broadcast the news, the class should have a method without parameters called broadcastNews, which broadcasts each piece of news as a separate broadcast (Hint: use the broadcast method from the RadioTransmitter
).
Create a subclass of RadioListener
called CrazyListener
who remembers the broadcasts over one (i.e., remembers only the first, third, fifth etc. broadcast). This method should call the listen method from its parent class.
Write the main class which creates the instances of the classes BingoNumberInformer
, LoudRadio
and NewsRadio
and binds some normal radio listeners and some crazy listeners to them. Update the list of current news with some your own news. Demonstrate the broadcasting from BingoNumberInformer
, LoudRadio
and NewsRadio
. Print the broadcasts that each listener remembers.
Chapter 5 |