Question: Lab 9 - Looping ( Exercise 1 6 on p . 2 5 6 ) Create, compile, and run a class that keeps track of

Lab 9-Looping (Exercise 16 on p.256)
Create, compile, and run a class that keeps track of compressed files.
Imagine a program that compresses files by 80 percent and stores them on storage media. Before the compressed file is stored, it must be divided into blocks of 512 bytes each. Develop an algorithm for this program that first reads the number of blocks available on the storage media. Then, in a loop, read the uncompressed size of a file and determine whether the compressed file will fit in the space left on the storage media. If so, the program should "compress" and "save" the file. It continues until it encounters a file that will exceed the available space on the media.
For example, suppose the media can hold 1000 blocks. A file of size 1100 bytes will compress to size 880 and require 2 blocks. The available space is now 998 blocks. A file of size 20,000 bytes will compress to size 16,000 and require 32 blocks. The available space is now 966.
Sample dialog:
How many 512-byte blocks are available on the storage device?10
How big is the file in bytes?1100
The file will be compressed to 880 bytes. That will take 2 blocks. You have 8 blocks available.
How big is the file in bytes?1800
The file will be compressed to 1440 bytes. That will take 3 blocks. You have 5 blocks available.
How big is the file in bytes?900
The file will be compressed to 720 bytes. That will take 2 blocks. You have 3 blocks available.
How big is the file in bytes?2500
The file will be compressed to 2000 bytes. That will take 4 blocks. You have only 3 blocks. The file cannot be compressed and stored.
(The program stops here.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!