Need help writing a Java programe?please don\'t write anyhing fancy, it is a beg
ID: 3568513 • Letter: N
Question
Need help writing a Java programe?please don't write anyhing fancy, it is a beginner java class project. I will RATE ASAP
A particular data compression algorithm can reduce a file's size to 80 percent of its original size (so, for
example, a 100-byte file will compress to 80 bytes). Before the compressed file can be stored on disk, it
must be divided into blocks of 512 bytes each (so a 1000-byte compressed file will require two 512-byte
blocks for storage). Write a program that does the following:
1. Create a new Scanner
2. Prompt for and read in the number of empty/available blocks on disk
3. While the number of empty blocks is greater than 0 and the user has not entered -1 to exit:
a. Print the number of free blocks available
b. Prompt for and read in the size of an uncompressed file, in bytes (an integer value), or -1 to quit.
c. If the user enters a value greater than -1:
i. Calculate the size in bytes of the file when it is compressed (to keep the compressed size an
integer, first multiply the original size by 8 and then divide the product by 10)
ii. Print out the size of the compressed file, in bytes
iii. Calculate the number of blocks required to hold the compressed file. Start by dividing the
compressed size by 512; the quotient is the starting number of blocks. If the remainder of that
division is greater than 0, add 1 block to the total required.
iv. Print out the number of blocks required
v. If the number of blocks needed is greater than the number of free blocks:
1. Print out a message reporting that not enough blocks are available
2. Set the total number of free blocks to 0 (this will cause your loop to end)
vi. Otherwise:
1. Print a message indicating that the file has been saved
2. Subtract the number of blocks required from the number of free blocks
Sample Execution
(Program output is shown in bold, while user input is shown in italics)
Enter number of free blocks: 500
500 free blocks available
Enter a file size in bytes: 20000
Compressed: 16000 bytes
32 blocks required
Saved file in 32 blocks
468 free blocks available
Enter a file size in bytes: 124000
Compressed: 99200 bytes
194 blocks required
Saved file in 194 blocks
274 free blocks available
Enter a file size in bytes: 360
Compressed: 288 bytes
1 blocks required
Saved file in 1 blocks
273 free blocks available
Enter a file size in bytes: 40000
Compressed: 32000 bytes
63 blocks required
Saved file in 63 blocks
210 free blocks available
Last step:
Modify your program so that, when it ends, it prints out the total number of files that were
successfully saved, along with the total savings in bytes (for example, a 20,000-byte file would be
compressed to 16,000 bytes, saving 4000 bytes).
Explanation / Answer
import java.util.Scanner;
public class Compression {
public static void main(String args[])
{
Scanner keyboard = new Scanner( System.in );
System.out.print("Enter number of free blocks:");
int freeBlocks=keyboard.nextInt();
int success=0;
int saving=0;
while(freeBlocks>0)
{
System.out.println(freeBlocks+" free blocks available");
System.out.print("Enter a file size in bytes:");
int uncompressedFiles=keyboard.nextInt();
if(uncompressedFiles<0)
{
break;
}
int compressed=uncompressedFiles*8/10;
System.out.println("Compressed:"+compressed+" bytes");
int blockrequired=(int)Math.ceil((double)compressed/512.0);
System.out.println(blockrequired+" blocks required");
if(freeBlocks<blockrequired)
{
freeBlocks=0;
System.out.println("Not enough space available");
}
else
{
freeBlocks=freeBlocks-blockrequired;
System.out.println("Saved file in "+blockrequired+" blocks");
saving+=uncompressedFiles-compressed;
success++;
}
}
System.out.println("Total files saved:"+success);
System.out.println("Total saving:"+saving);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.