How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Kotlin programming language
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:
-
Imports: The code imports necessary classes from the Java library for handling window creation, keyboard input, and system operations.
-
Test
Class: This class extendsJFrame
, 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.
- It clears any pending input in the standard input stream (
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.
- Constructor (
-
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.
- It uses
Execution Flow:
- The program starts and prompts the user to enter 'Y' or 'N' to quit.
- The user presses 'Y' or 'N'.
- The key press event is handled by the
KeyAdapter
in theTest
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'.
- If 'Y' is pressed, the
- 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