RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lab14a
A Library Catalog
File I/O, command-line, and double-clicking programs

In lab today, we will...

You will have to decide how to organize these tasks into individual methods, and how/when to call one method from another! Practice compiling and running your program from the command-line today, rather than using BlueJ's or Eclipse's compile button.


  1. Write a class which deals with Books: every Book has a title and an author. (You'll need a constructor and two getters, a toString method, but no setter methods.)
  2. First, your program should read the title and author of books from a file1, storing the result in a List<Book> (or, in a Book[]2).

    To create the data file, you can save this link to disk, or create the file yourself using any text editor (BlueJ or any other -- if using something like Microsoft Word be sure to File > Save as…, using the format “Text Only”! Be sure not to include any blank lines, or spaces at the start/end of each line). Here is a sample:

        Catch-22
        Joseph Heller
        Midnight's Children
        Salman Rushdie
        Fahrenheit 451
        Ray Bradbury
        Beloved
        Toni Morrison
        Satanic Verses
        Salman Rushdie
        The Ground Beneath Her Feet
        Salman Rushdie
        Song of Solomon
        Toni Morrison
        The Illustrated Man
        Ray Bradbury

  3. Then, prompt the user to type in an author's (full) name;
  4. Your program should create a list (or array3) containing all the Books whose author has the given name,
  5. and finally display that list on the screen.
    (For style points, print something like “no books by author”, if we don't have any books by that author.)

GUI I/O and Stand-alone applications

We can convert this program to one that we can double-click. However, first we'll need to make several changes. Create a second project, separate from the previous problem, and copy everything over.

Unfortuately, when starting a java program by double-clicking it, the program doesn't have a console window, so we can't use System.out nor System.in! Instead, we'll use GUI dialog boxes. We'll need to replace every System.out.println and every reading-from-the-keyboard with the following two methods instead:

/* Show a message.  (Always use null for the first argument.) */
void javax.swing.JOptionPane.showMessageDialog( null, String msg )

/* Read a line of input, after displaying a prompt: */
String javax.swing.JOptionPane.showInputDialog( String prompt )
Go ahead and try calling these from the Code Pad, first:
javax.swing.JOptionPane.showMessageDialog( null, "Hello there everybody!");

String answer;
answer = javax.swing.JOptionPane.showInputDialog( "What is your quest?");

answer  // look at the result, in Code Pad.

Be sure that main starts your whole program running — that's what will be called when your program is double-clicked.

From BlueJ (with the main Project window active), choose Project > Save Jar file…. You'll be prompted for which class's main to call upon double-clicking. BlueJ will then create a file (whose name ends in “.jar”); when you double-click that file, your program will run, ta-dum!


1Remember that reading from a file uses a Scanner, just like reading from the keyboard.

     

2 The problem with storing the Books in an array is that before reading the file, we don't know how big to make the array! The proper solution is make two passes over the file: First create a scanner to read the file and keep reading lines, just counting them; then you can compute how many Books will be created and allocate the array; finally, make another scanner reading from the same file, and actually populate(initialize) your array.      

3 Again, using an array requires two passes through our list — first just loop through your library and count how many Books match, then create the (properly-sized) array, the loop through the library again and actually gather the matching Books into the array.      

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Dec.09 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme