Chapter 10

!

Input/Output





Historically, the phrase "input-output" (or, "I/O") referred to the operations involved in securing input data from a keyboard and displaying information on a screen or printer. As file processing became prevalent in the early years of computing, I/O was expanded to encompass using disk files for input and output. In today's world of graphical user interfaces, the term I/O has been expanded even further to include all of the operations involved in communicating between a computer and its users. As we have already seen, these operations include retrieving text that has been entered into text containers and processing the text using Java's built-in String class.
In this lablet we present you with a remarkably simple, but remarkably realistic, word-processing program (described as class WordPro) that illustrates many of these facets of I/O. This program also illustrates the use of three additional Java features that we have yet to explore. First, the class WordPro is not an applet, but is another type of container called a Frame. This means that we describe the class in terms of a constructor function and a main() member function. It also means that the WordPro program cannot be embedded in a Web page, for example, but can be run as a stand-alone "application." Second, we chose to make WordPro a frame so that we could attach a menu bar to it (which we can't do to an applet). So, the program shows you how to describe and respond to menu choices. Finally, because we wanted to make this program handle file I/O in a familiar way, we make use of Java's class that implements file dialog boxes.


Lab Objectives

In this lab, you will:
 o

Run the Chapter 10 lablet, WordPro, and exercise its text handling capabilities.

 o

Introduce some common errors into WordPro to explore its use of frames, menus, files, and dialogs.

 o

Extend the program to implement a variety of common word-processing operations.

Exercises

  1. Maybe you expect this by now, but when we first ran this lablet we were struck by how (relatively) easy it was to produce a realistic version of what appears to be a very complex program in only a few pages of Java code. Whether you're impressed or not, there is no denying that the WordPro program looks familiar to anyone who uses a modern WYSIWYG (short for "What You See Is What You Get") word-processing program.

    1. Compile and run WordPro now. Compare what you see on the screen with the code for the WordPro class's constructor function, shown here. Make sure you understand which parts of the code are responsible for which parts of the program's interface. If you need more context, take a look at the source code for our program.
         public WordPro()
         {
             setTitle("p.j WordPro");
             MenuBar mbar = new MenuBar();
      
             Menu mf = new Menu("File");
             mf.add(new MenuItem("New"));
             mf.add(new MenuItem("Open..."));
             mf.add(new MenuItem("Close")); 
             mf.addSeparator();
             mf.add(new MenuItem("Save"));
             mf.add(new MenuItem("Save As..."));
             mf.addSeparator();
             mf.add(new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q)));          
             mbar.add(mf);  
              
             Menu med = new Menu("Edit");
             med.add(new MenuItem("Cut"));
             med.add(new MenuItem("Copy"));
             med.add(new MenuItem("Paste"));
             med.add(new MenuItem("Clear"));
             mbar.add(med);  
      
             setMenuBar(mbar);
             add("South", display);
             add("Center", text);
              
             mf.addActionListener(this);
             med.addActionListener(this);
             addWindowListener(new WindowMinder(this)); 
         }
      
    2. Now, try using the program. It operates much as you would expect, with a few minor exceptions. You can type text into the window and edit it using the keyboard. You can cut, copy, delete, and paste text by selecting text and using the commands in the Edit menu. Choosing New from the File menu clears the text window. Experiment with these features of WordPro until you are comfortable with them.

    3. Select Open... from the File menu and choose any text file (it could even be one of your Java source files) to open. Edit the file; make some modifications to its text. Then, chose Save from the File menu to save your edited file. Choose Open... again to look at the new, edited file.

  2. Let's make some simple changes to WordPro now, and observe the effects of these changes. As you have done in other labs, make each change listed to the original program, and record in the space provided what error or change in behavior occurred when you tried to compile and run the program.

    1. Change the header of function main() to read:
      private static void main(String args[])
      Result:


    2. Change the header of function main() to read:
      public static void main()
      Result:


    3. Remove (or comment out) the statement in function main() that reads:
      myFrame.show();
      Result:


    4. Remove (or comment out) the statement in the WordPro constructor that reads:
      mbar.add(mf);
      Result:


    5. Remove (or comment out) the statement in the WordPro constructor that reads:
      setMenuBar(mbar);
      Result:


    6. Remove (or comment out) the statement in the function doOpen() that reads:
      myFD.show();
      Result:


    7. Remove (or comment out) the statement in the function doOpen() that reads:
      processNewMenu();
      Result:


    8. Remove (or comment out) the statement in the function doOpen() that reads:
      inStream.close();
      Result:


    9. Change the line in function doOpen() that reads:
      text.appendText(textLine + "\n");
      to read
      text.appendText(textLine);
      Result:


    10. Remove (or comment out) the statement in function doSave() that reads:
      DataOutputStream outStream = new DataOutputStream(fos);
      Result:


    11. Change the line in function doSave() that reads:
      fileName = dir + File.separator + name;
      to read
      fileName = dir + name;
      Result:


    12. Change the line in function doClear() that reads:
      String s1 = new String (all.substring(0, text.getSelectionStart()));
      to read
      String s1 = new String (all.substring(1,text.getSelectionStart()));
      Result:


    13. Reverse the order of the two statements in method actionPerformed() that occur immediately after the statement:
      else if (itemText.equals("Cut"))
      Result:


  3. Return now to the original version of WordPro. It provides us with examples of code that can be used to implement a number of features that will render our word-processor even more realistic--and useful--than it already is. Some of the changes we propose can be made without changing the existing user interface at all. Others require that you first add some components to the interface, and then implement them. Given that these extensions tend to build upon one another, you should attempt them in the prescribed order.

    1. Most word processors change the title of their text window to reflect the name of the file currently being edited (and use "Untitled" for documents that have not previously been saved). Fix WordPro so that it does this.

    2. Observe that we've provided a key equivalent for the Quit command in the File menu. Take a look at the WordPro constructor to see how we did it.
      You've almost certainly noticed that many modern applications provide key equivalents for some of their menu items, and you may have noticed that these equivalents have become more or less standardized. For programs intended for English-speaking users, we have "N" for New, "O" for Open, "W" for Close, "S" for Save, "X" for Cut, "C" for Copy, and (for two obscure reasons we leave to you to deduce) "V" for Paste. Add these "menu shortcuts," using whatever key equivalents are appropriate for your locale.

    3. Implement the Close command from WordPro's File menu. For now, this operation can have the same effect as the New menu command does.

    4. Add to the menu bar a Font menu that specifies a few type faces. Then, implement it so that when a type face is chosen, all text in the window is displayed in the chosen font.

    5. Implement the Save as... command from the File menu. This should present the user with a file dialog box that allows the user to name the file to be used for saving the document.

    6. Add a Find... command to the Edit menu. Implement Find... so that it prompts the user via a dialog box for a string, and then searches the current document for the first occurrence of that string.

    7. Most modern word processors are "safe" in the sense that, for example, if you choose New, Open, or Close from the File menu, and there is currently "dirty" text in the text window (that is, text that has been changed since the current file was last saved), you will be asked (via a dialog box) if you want to save the current text before performing any of these operations. Extend WordPro to act more safely when responding to these commands.

      Help: We covered dialogs in Section 4.3 of the text.

    8. Another safety feature of some programs is a Revert command in the File menu. This option works by restoring the document to the state it was in the last time it was saved, so that if the user makes a hopeless mess of a document, there's always the option of reverting to the last saved version (if any). Implement a Revert command. (While the placement of this command is less standardized than those of the other File commands, it is often placed immediately after Save as....)


Postlab Exercises

  1. Even after all the modifications in this lab, WordPro still suffers from a slightly annoying user interface problem, since the menu items are always enabled. For example, it really makes no sense to have the Paste item enabled if there's no text in the clipboard, and it's a waste of time to have Save enabled if the current text has been saved and isn't dirty. Play with a commercial-quality word processor, recording when menu items are enabled and disabled, and incorporate these features into WordPro. Go ahead--since you're not going to distribute your version of WordPro, you won't have to worry about getting embroiled in a "look and feel" lawsuit with Microsoft, Apple, or Corel.

  2. Return to the Chapter 9 lablet, OrderPlease. Whenever an exception was encountered in OrderPlease, a message was posted in the same text area used for display of the order. A more realistic way of reporting such errors is by means of a dialog box that must be acknowledged by "clicking" it away. Extend OrderPlease to make use of dialog boxes when reporting exceptions.

  3. Write a class Keyboard which performs basic, type-specific input operations through the standard system input-output window. That is, your class should implement the following functions, which could be used by any other program to accomplish a traditional style of console-based keyboard input.

    Hint: System.in is an InputStream amd System.out is a PrintStream.

       // Prompts the user with String s, 
       // in the standard I/O window.
       public static void prompt(String s);
    
       // Prompts the user with String s, 
       // then reads and returns a single integer.
       public static int readInt(String s);
    
       // prompts the user with String s, 
       // then reads and returns a double 
       public static double readDouble(String s);
    
       // Prompts the user with String s, 
       // then reads and returns a line of text up to a return.
       public static String readLine(String s);
    
       // Prompts the user with String s, 
       // then reads and returns a word as delimited 
       // by a space or a return.
       public static String readWord(String s);
    
  4. You're on your own for this one. Make WordPro into a fully-functioning word processor by giving it the ability to print a document. You'll want to look at the documentation of the PrintJob, Toolkit, and Properties classes.

Last updated: December 4, 1998
Rick Decker
Department of Computer Science
Hamilton College
Clinton, NY 13323