|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
In lab today, we will...
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 |
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 ) |
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.
new Scanner( new java.io.File("H:/my120folder/lab02a/someFile.java") ) |
void myFavoriteMethod() throws java.io.FileNotFoundException { … &helip;new Scanner( new java.io.File( … ) )… } |
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. ↩
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
©2008, Ian Barland, Radford University Last modified 2008.Dec.09 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |