Controls simulation

Report a typo

Imagine that you want to write a program to control a music player via wireless earbuds. The user can control the music player by tapping the earbuds according to a certain input pattern:

  • When the user taps an earbud once, the earbud enters command mode and awaits further input. If no input is registered within a certain amount of time, the earbud returns to standby mode.

  • In command mode, if the user taps the earbud once and waits a moment, the music player receives a command to change the current playing mode. If the player is playing at the moment, it will pause, and if paused, it will resume playing music.

  • In command mode, if the user taps the earbud twice and waits a moment, the player receives a command to fast forward the current track. After the execution of any command, the earbud returns to standby mode.

  • If the user switches an earbud to command mode and taps it three times, such input is considered invalid and discarded, and the earbud returns to standby mode.

The code template already has the Earbud class and an interface to represent its states, as well as a few classes implementing the State interface. Your task is to complete the implementations of the OneTap and TwoTaps classes so that the Earbud class would work as intended. When any of the commands (pause, resume and fast forward) is successfully passed, it must be printed to the console: Pausing, Resuming and Fast forwarding respectively.

See the examples for details.

Sample Input 1:

tap
wait
wait
tap
tap
wait
wait

Sample Output 1:

Paused
Resuming
Playing

Sample Input 2:

tap
tap
wait
wait
tap
tap
tap
wait
wait

Sample Output 2:

Resuming
Playing
Fast forwarding
Playing

Sample Input 3:

tap
tap
tap
tap
wait
tap
tap
wait
wait

Sample Output 3:

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

interface State {
void onTap(Earbud earbud);

void onDiscontinue(Earbud earbud);
}

class Earbud {
private boolean playing;
private State state = new Idle();

public void tap() {
state.onTap(this);
}

public void doNothing() {
state.onDiscontinue(this);
}

public boolean isPlaying() {
return playing;
}

public void setPlaying(boolean playing) {
this.playing = playing;
}

public void setState(State state) {
this.state = state;
}
}

class Idle implements State {

@Override
___

Create a free account to access the full topic