Answered step by step
Verified Expert Solution
Link Copied!

Question

00
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

HW8 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 16 bytes (Java Strings use Unicode two-byte characters) and the second has 38 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 48 bytes. Then we would right pad the values of titles that have fewer than 48 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions