How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Kotlin programming language

Table of Contents

Problem Statement

Obtain a valid   Y   or   N   response from the keyboard. The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing   Y   or   N   key-press from being evaluated. The response should be obtained as soon as   Y   or   N   are pressed, and there should be no need to press an   enter   key.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Kotlin programming language

Explanation:

This code is a Java application that prompts the user to enter 'Y' or 'N' to confirm quitting the program. It uses the Java AWT (Abstract Window Toolkit) libraries to display a simple window and handle user input from the keyboard. Here's a detailed breakdown:

  1. Imports: The code imports necessary classes from the Java library for handling window creation, keyboard input, and system operations.

  2. Test Class: This class extends JFrame, which is a Swing component for creating a window. It contains:

    • Constructor (init block):
      • It clears any pending input in the standard input stream (System.in`) to prevent any interference.
      • It prompts the user to enter 'Y' or 'N' to quit the program.
      • It adds a KeyAdapter to handle keyboard input.
    • quit Method: This method is called when the user presses 'Y' to quit. It hides the window, disposes of it, and exits the program with a status code of 0.
  3. main Function: This is the entry point of the program.

    • It uses SwingUtilities.invokeLater to ensure that the window creation and event handling happen in the Java Swing Event Dispatch Thread (EDT).
    • It creates an instance of the Test class (f).
    • It sets the focusability of the window to true and makes it visible.

Execution Flow:

  1. The program starts and prompts the user to enter 'Y' or 'N' to quit.
  2. The user presses 'Y' or 'N'.
  3. The key press event is handled by the KeyAdapter in the Test class:
    • If 'Y' is pressed, the quit method is called to end the program.
    • If 'N' is pressed, a message is printed indicating that the program will end anyway.
    • If any other key is pressed, a message is printed instructing the user to only press 'Y' or 'N'.
  4. The program exits when the quit method is called or when the user presses a non-valid key.

Source code in the kotlin programming language

// version 1.0.6

import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
import javax.swing.JFrame
import javax.swing.SwingUtilities

class Test: JFrame() {
    init {
        while (System.`in`.available() > 0) System.`in`.read()
        println("Do you want to quit Y/N")
        addKeyListener(object: KeyAdapter() {
            override fun keyPressed(e: KeyEvent) {
                if (e.keyCode == KeyEvent.VK_Y) {
                    println("OK, quitting")
                    quit()
                } else if (e.keyCode == KeyEvent.VK_N) {
                    println("N was pressed but the program is about to end anyway")
                    quit()
                } else {
                    println("Only Y/N are acceptable, please try again")
                }
            }
        })
    }

    private fun quit() {
        isVisible = false
        dispose()
        System.exit(0)
    }
}

fun main(args: Array<String>) {
    SwingUtilities.invokeLater {
        val f = Test()
        f.isFocusable = true
        f.isVisible = true
    }
}


  

You may also check:How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the RPL programming language
You may also check:How to resolve the algorithm FASTA format step by step in the TMG programming language
You may also check:How to resolve the algorithm Mayan calendar step by step in the 11l programming language
You may also check:How to resolve the algorithm Gapful numbers step by step in the V (Vlang) programming language