Clock factory

Report a typo

There is a hierarchy of clocks with the base interface Clock and the class ClockFactory to produce instances.

Implement the method produce of the factory. It should return a clock according to the specified type string:

  • "SAND" is for SandClock;
  • "DIGITAL" is for DigitalClock;
  • "MECH" is for MechanicalClock.

The single constructor of the factory takes the boolean parameter produceToyClock. It determines what the factory does when an unsuitable type of clock is passed. If it is true, the factory should produce an instance of ToyClock, otherwise, return null.

Do not change the provided code of the clock classes.

Write a program in Java 17
import java.util.Scanner;

class ClockFactory {

private boolean produceToyClock;

public ClockFactory(boolean produceToyClock) {
this.produceToyClock = produceToyClock;
}

/**
* It produces a clock according to a specified type: SAND, DIGITAL or MECH.
* If some other type is passed, the method produces ToyClock.
*/
public Clock produce(String type) {
// write your code here
}
}

/* Do not change code below */
interface Clock {

void tick();
}

class SandClock implements Clock {

@Override
public void tick() {
System.out.println("...sand noise...");
}
}

class DigitalClock implements Clock {

@Override
___

Create a free account to access the full topic