Comp 249 – Object-Oriented Programming II Assignment 3 Solved

25.00 $

Category: Tags: , , , , , ,
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (2 votes)

Purpose: The purpose of this assignment is to allow you practice Exception Handling, and File I/O, as well as other previous object oriented concepts.

This assignment contains two parts. You need to complete part I before doing part II.

Part I

 The Book class has the following attributes: an ISBN (long type), a title (String type), an issue year (int type), an author(s) name (String type), a price (double type) and a number of pages (int type). It is assumed that both the title and the author(s) name are recorded as a continuous string with “_” to separate the different words if any (i.e. a title can be Java_Applications_for_Engineers, and author name can be Mike_Westman_and_Jack_Bartlett).

 

The file Initial_Book_Info.txt, which one of its versions is provided with this assignment, has the information of various books that a book store carries. However, this file is always created by the owner of the book store, who is not so careful, so errors in the ISBN numbers are expected to exist in this file. Specificaly, due to a cut-and-paste practice by the owner, some ISBN numbers of some books are usually re-recorded later in the file as the ISBN number of other following books in the file. Consequently, an ISBN can either appears once in the file, which is the correct case, or appears multiple times, in which case the second, and following, appearances are in error.

 

You must notice that the file Initial_Book_Info.txt changes regularly, and it may have many records, or no records at all, depending on the current inventory of the store. The file provided with this assignment is only one of possible versions of the file, and must not be considered as the general case when writing your code.

 

For this part, you are required to:

  1. Write the implementation of the Book class according to the above given specification.
  2. Write the implementation of an exception class called DuplicateISBNException, which extends the Exception Should a duplicate ISBN number is detected at any point, an object from this class will be thrown. More details will follow below.
  3. Write the implementation of a public class, called BookInventory1, which will utilize the Book class and the txt, as explained below. The class has a static array of books, called bkArr[], and few methods. Beside the main() method, the class must have the following methods:
    • A method called fixInventory(), which accepts two parameters, an input file stream name and an output file stream name. The first parameter is the stream related to the txt. The second parameter is related to an output file name, which will be entered by the user prior to calling this method (at the main() method). This output file will eventually store a correct version of the inventory. More information on this will follow below.
    • A method called displayFileContents(), which accepts an input file stream name, then displays the contents of this file to the standard output (the screen).
    • You can add any other methods to this class if you wish to.

 

 

Here are the details of how your program must behave:

  • In the main() method, your program must prompt the user to enter the name of the output file that will be created to hold the modified/correct inventory. This output file is theoretically a copy of the txt, but with all the duplicate ISBN numbers corrected.
  • If the entered name by the user matches the name of an existing file, then the program must reject that name indicating to the user that a file with that name already exists, display the size of this existing file (in bytes) to the user, then prompt the user to enter a new name. This process would repeat indefinitely, until the user finally enters a name for a non-existing file. See Figure1 for illustration.
  • Once the user finally enters a correct name for the output file, the program will attempt to establish an input and output streams for the txt and that output file accordingly. This process may surely throw specific exceptions, and your program must handle all these exceptions properly.
  • The fixInventory() method (see details below) will utilize the BkArr[] array as follows: All book objects recorded in the txt file must first be copied into that array. All detections and corrections of ISBN numbers will be conducted on that array. Once the array has finally all correct information, the objects will be recorded to the output file. However since it always unknown at the time the program starts how many records are in the Initial_Book_Info.txt file, you must first find this information. You may, and should, add a private helping method to the BookInventory1 class to do so. Once the number of records is know, the array must be set to that size.
  • If the number of records in the txt was detected to be zero or one; the case when the file is empty or has only one record, then the program must display a message indicating that, performs any needed operations (such as closing files), then exits since there is nothing to be fixed.
  • If the txt has more than one record, then finally the fixInventory() method will be called to create a the new output file with the correct information. The exact details of this method are as follows: o The method will accept two stream names for the input and output files, o The method must read each book record from the input file and creates a book object in the array bkArr[] based on that record,
    • Once the entire input file is read into the array (notice that the array has real book objects, and not just information), the method starts to trace that array from start to end looking for any ISBN duplicates,
    • If an ISBN duplicate is detected, then the method displays a message to the user indicating that, and prompt the user to enter a new ISBN number,
    • You should notice that this new number must not be a duplicate of any other existing record, and your program must guarantee that. Should the user enters an ISBN number that is still a duplicate, the program must throw a DuplicateISBNException, which must be catched to display a message indicating that, then user must be prompted again to enter a new ISBN. Further bad entries will result in the same action; that is throwing of the DuplicateISBNException object, catching it to display the message, and the user is prompted again. Effectively, this process will repeat indefinitely until the user enters a correct ISBN number. Again, you may, and should, create a private helping method to find if a duplicate exists in the array. See Figure 2 for illustration.
    • Finally, once all the duplicate ISBN numbers are removed, the new information of the objects in the bkArr[] must be copied to the output file. This output file has now the correct information.

 Upon the return of the fixInventory() call in the main() method, the program must use the displayFileContents() method to display the information of both the Initial_Book_Info.txt file, as well as the created output file. See Figure 3 for illustration.

Hint: You must carefully keep track of the opening and closing of these files.

 

 

Below are sample snapshots of the program behavior for further illustrations:

 

 

 

Figure 1: Detecting Existing Files

 

 

 

 

 

Figure 2: Detecting Existing ISBN

 

 

Figure 3: Displaying File Contents

 

Part II

 

 

For this second part, you will still use the same Book class from Part I (some little modifications may be needed, and you should find that out). Other code segments from Part I can still be used when appropriate.

Implement a public class called BookInventory2, which will utilize the Book class and the file called Sorted_Book_Info.txt. This file includes information about books inventory in a book store. The records in this file are sorted by ISBN, and the information in this file is assumed to always be correct.

The class has a static array of books, called bkArr[], and few methods. Beside the main() method, the class must have the following methods:

  • A method called addRecords(), which accept one parameter: an output file stream name; further details of this method are given below.
  • A method called displayFileContents(), which accepts an input file stream name, then displays the contents of this file to the standard output (the screen). This method however must use the BufferedReader class to read the file.
  • A method called binaryBookSearch(), which accepts four parameters: any array of books, a start index, an end index, and an ISBN number. The method then uses binary search to search the array segment (from start index to end index) for that ISBN. The method must also keep track and display how many iterations it needed to perform the search.
  • A method called sequentialBookSearch(), which accepts four parameters: any array of books, a start index, an end index, and an ISBN number. The method would then search the array, using sequential search, for that ISBN. You must analysis the performance aspect of your solution. Hence, the method must also display how many iterations it needed to perform the search.
  • You can add any other methods to that class if you wish to.

 

 

Here are the details of how your program must behave:

  • In the main() method, your program must open the txt file to append few records to it.
  • Call the addRecords() method to add few records to the file. The information of each of the new records must be entered by the user. The user can add as many records as he/she wishes (you must surely allow the user to have a stopping condition limiting the numbers of records that can be added). To simplify your task, you can assume that the user will always keep the file sorted; that is, no new record will have an ISBN# that is smaller than any of the previous records in the file., in a nother way a reference of the last record is kept to accept or reject the record you want to add.
  • Call the displayFileContents() method to show the contents of the file after modifications.
  • Now, the array bkArr[] is going to be used in a similar fashion to Part I. So, you need to find out what is the current number of records in the txt file, and set the size of the array to that number. You may, and should, have a private helping method to do so.
  • After setting the array size properly, the program must read the contents of the file to the array objects. You can use a separate method to do so.
  • Once the array has the objects information from the file, prompt the user to enter and ISBN number then use binaryBookSearch() to search for that ISBN.
  • Repeat the same search using sequentialBookSearch().
    • As an important exercise, you should run your code for different searches (i.e. attempt to find best and worst cases) of the two searches.
  • Finally, store all the objects from the bkArr[] into a binary file called dat. You can have a separate method to do so. If writing to the binary file requires any specific modifications to your code, you must apply these modifications.
    • As always, you must handle all exceptions as needed and you must keep track of the opening and closing of your files.

 

 

  • COMP249A3.zip