Answered step by step
Verified Expert Solution
Question
1 Approved Answer
HW 8 File Operations Using the StockItemManager files in the File IO module, as a model, create a set of programs that: Instantiates and writes
HW File Operations
Using the StockItemManager files in the File IO module, as a model, create a set of programs that:
Instantiates and writes five Book objects to a file. Hardcode the creation of Book objects with their attribute values.
Reads the original data from the file and displays it
Updates the title for one Book
Updates the price for another Book
Reads the modified data from the updated file and displays it Your Book class must have the following attributes:
ISBN
title
yearPublished
price
It must also have set and get methods for these attributes, plus a toString method.
The most difficult part of using Random Access Files is handling Strings. The variable lengths of Strings cause problems. Random Access Files require that every record have the same length. This enables a program to move the file pointer to a particular location. When a program wants to move the pointer to record five, lets say, it has to know how many bytes record five is from the beginning of the file. If all the records have different lengths, this would not be possible, and the program would put the file pointer in the wrong place.
To relate this problem to this project: if one book has the title Java and another has the title Introduction to Java the first title has bytes Java Strings use Unicode twobyte characters and the second has bytes. We need to make Strings occupy the same amount of space in a file, so we must decide on a standard length for String fields like title. Lets say we determine that titles should have bytes. Then we would right pad the values of titles that have fewer than bytes and truncate those that have more. The sample program demonstrates this with its setCorrectLength method.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started