Facebook Pixel

Exercise: Interface Segregation

This exercise puts the Interface Segregation Principle into code. You will define three focused interfaces and two device classes, then drive them with a command sequence.

Scenario

An office-device registry manages two kinds of hardware: a simple printer that can only print, and an all-in-one device that can print, scan, and fax. The registry receives a sequence of commands and returns one output line per command.

Commands

The program reads a list of commands, one per line, and prints one line of output per command.

CommandBehaviorOutput
["add", type, name]Add a device; type is simple or allinone"Added <name>", or "Unknown type: <type>"
["print", name]Print on the named device"<name> printed", or "<name> not found"
["scan", name]Scan on the named device"<name> scanned" for all-in-one, "<name> cannot scan" for simple, or "<name> not found"
["fax", name]Fax on the named device"<name> faxed" for all-in-one, "<name> cannot fax" for simple, or "<name> not found"
Example
Input
7
add simple Laser
add allinone Xerox
print Laser
scan Laser
fax Xerox
scan Xerox
print Ghost
Output
Added Laser
Added Xerox
Laser printed
Laser cannot scan
Xerox faxed
Xerox scanned
Ghost not found
Explanation
`Laser` is a simple printer, so printing succeeds but scanning returns "cannot scan" — the registry checks the interface before calling any method. `Xerox` is an all-in-one, so both fax and scan succeed. The final print command names a device that was never registered, so the registry returns "not found".

Your task

Create SimplePrinter (implements Printer) and AllInOne (implements Printer, Scanner, and Fax) so each command produces the output above. The starter marks where to add the classes with a TODO comment and shows the exact method signatures and return values to implement.

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro