Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2024E

Section 4.7 Chapter Summary

Subsection 4.7.1 Technical Terms

abstract class input operation
abstract interface input stream
abstract method interface
AWT layout manager
binary file listener
buffer model-view-controller (MVC) architecture
command-line interface output operation
container output stream
control element stream
event-driven programming Swing
event loop text file
graphical user interface (GUI) top-level container
helper method user interface
inheritance wrapper class

Subsection 4.7.2 Important Points

  • 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.
  • In a command-line interface, user input is taken from the keyboard, and the program’s output is displayed on some kind of console.
  • 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.
  • A wrapper class contains methods for converting primitive data into objects and for converting data from one type to another.
  • Designing appropriate prompts is an important aspect of designing a good user interface.
  • I/O operations must watch out for certain types of I/O exceptions.
  • 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.
  • Java has two packages of GUIs, the older java.awt and the newer javax.swing.
  • Swing components are based on the object-oriented model-view-controller (MVC) architecture.
  • The extends keyword is used to specify subclass/superclass relationships in the Java class hierarchy.
  • 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.
  • There are generally three kinds of GUI components, corresponding to the three main functions of a user interface: input, output, and control.
  • Events are handled by special objects called listeners. A listener is a specialist that listens constantly for a certain type of event.
  • An interface is a special Java class that contains only methods and constants (final variables).

Solutions 4.7.3 Solutions to Self-Study Exercises

4.3 A Command-Line Interface
4.3.4 Designing 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.4 A Graphical User Interface (GUI)
4.4.8 Using the GUI in a Java Application

Self-Study Exercises
4.4.8.1. GreeterGUI.
Solution.
The following addition to GreeterGUI allows the button or the textfield (enter key) to be a an actionlistener.
inField.addActionListener(this);

4.6 From the Java Library: java.io.File and File Input (Optional)
4.6.1 File 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
You have attempted of activities on this page.