An input operation is any action that transfers data from the user to the computerβs main memory via one of the computerβs input devices. An output operation is any action that transfers data from the computerβs main memory to one of the computerβs output devices.
The user interface is that part of the program that handles the input and output interactions between the user and the program. As an interface, it limits or constrains the manner in which the user can interact with the program.
A buffer is a portion of main memory where input is held until it is needed by the program. Using a buffer between the keyboard and the program allows you to use the Backspace key to delete a character.
GUI programming involves a computational model known as event-driven programming, which means that GUI programs react to events that are generated mostly by the userβs interactions with elements in the GUI.
A top-level container is a GUI container that cannot be added to another container; it can only have components added to it. All GUI programs must be contained in a top-level container.
4.3A Command-Line Interface 4.3.4Designing a Command-Line Interface
Self-Study Exercises
4.3.4.1.HiLow Guessing Game.
Solution.
public class HighLowApp
{ private KeyboardReader reader;
private int secretNumber;
public HighLowApp()
{ reader = new KeyboardReader();
secretNumber = 1 + (int)(Math.random() * 100);
} // HighLowApp() constructor
public void run()
{ int userGuess = -1;
reader.display("Guess my secret number between 1 and 100.");
while (userGuess != secretNumber)
{ reader.prompt("Please input your guess here > ");
userGuess = reader.getKeyboardInteger();
if (userGuess > secretNumber)
reader.display("Your guess was too high.");
if (userGuess < secretNumber)
reader.display("Your guess was too low.");
} // while
reader.display("Congratulations. Your guess was correct.");
} // run()
public static void main(String args[])
{ HighLowApp app = new HighLowApp();
app.run();
} // main()
}// HighLowApp
4.6From the Java Library: java.io.File and File Input (Optional) 4.6.1File Input with the File and ScannerClasses
Self-Study Exercises
4.6.1.1.NumberFileReader.
Solution.
Java code that prints out the sum of the squares of a set of integers read from a file named "numbers.txt":
import java.io.*;
import java.util.Scanner;
public class NumberFileReader
{ private Scanner fileScan; // For file input
public NumberFileReader(String fName)
{ try
{ File theFile = new File(fName);
fileScan = new Scanner(theFile);
} catch (IOException e)
{ e.printStackTrace();
} //catch()
} //NumberFileReader()
public void readNumbers()
{ int num = 0; // To store integers read
int sum = 0: // To store sum of squares
while (fileScan.hasNextInt())
{ num = fileScan.nextInt();
sum = sum + num * num;
} // while
System.out.println("The sum of squares = " + sum);
} // readNumbers()
public static void main(String[] args)
{ NumberFileReader nfr =
new NumberFileReader("numbers.txt");
nfr.readNumbers()
} //main()
} //NumberFileReader