Assignment #4

 

·         In each problem, you must demonstrate use of at least one specific Exception class. That is, do not catch the generic ‘Exception’ except as a last option.  So for example, look at the API in your FileReader class and determine which kind of exception might be thrown by the constructor you are using.  You do not have to catch every possible exception out there; simply demonstrate that you know how to look for particular type of exceptions that may come up. As I said, you only need to choose one for each of the two problems in this assignment.

·         For each of the two problems, add one GUI component that we have not discussed in class.  For example, you might add an image to a button.  Or you might try to add a scroll bar. Etc… 

 

Problem #1

Use the Book class you created in assignment #3.  Create a GUI in which you prompt the user for all of fields. Have 2 buttons:  One that says ‘Add New Book’ and another that says ‘Show Current Books’ (or it can also say ‘Finished’).  

 

Every time the user fills the information and clicks on ‘Add New Book, instantiate a Book object and add it to an ArrayList of Book objects. If the user leaves any of the fields blank, set those fields to whatever your default choices are (e.g. “unset” for name, etc).  For up to 4 bonus points, check to make sure they enter something in all of the text fields before instantiating the new object.

 

When the user clicks the ‘Finished’ button, go through your ArrayList and output the information for every Book object in the array – to a file. 

 

Problem #2

For this program you will need the file oneToTen.txt which can be found on the class page. Create a GUI in which you have buttons that say ‘Count’ and ‘Search’. Your GUI should also have a text field where you ask the user for a number between 1 and 10.  When the user enters a number (you may assume they will enter a number between 1 and 10) and clicks the count button, output on the GUI how many times that number was present in the file. If they enter ‘Search’ simply output if the number is present.

·         Note that your GUI should NOT re-read in all the values from the text file each time they click the button. Rather, when the program runs and you draw the GUI, also have code that will create an ArrayList and add all of the numbers to that array list.

·         Then use that ArrayList to react to the button that was pressed.  You may make the ArrayList a class-level variable.

 

Assignment and Submission Guidelines:

-          As always, submit all of your source code files as a ZIP file. 

-          You do NOT need to include any of the input or output files. The grader will have the input files from problem #1, and any needed output files will be generated when your program(s) are run.