Question is: Imagine a program that compresses files to 80 percent of their orig
ID: 3667955 • Letter: Q
Question
Question is: Imagine a program that compresses files to 80 percent of their original size and stores them on storage media. Before the compressed file is stored, it must be divided into blocks of 512 bytes each. Write a program that first reads the number of blocks available on the storage media. Then, in a loop, read the uncompressed size of the 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.
Code:
import java.util.Scanner;
public class HW2Q2
{
public static void main(String[] args)
{
int size;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of blocks available");
size=keyboard.nextInt();
System.out.println("Available number of blocks is " + size);
int filesize, compsize, sizenew;
while(size!=0)
{
System.out.println("Enter the size of the file that is to be stored");
filesize=keyboard.nextInt();
filesize=filesize/512;
compsize=filesize*(80/100);
if(compsize>size)
{
System.out.println("the number of blocks available, " + size + " is less than the required number, " + compsize);
break;
}
else
{
sizenew=size-compsize;
System.out.println("available number of blocks is " + sizenew);
}
}
}}
The program will run, but it returns size instead of sizenew at the end. Can you please help me identify my error?
Explanation / Answer
/**The java program HW2Q2 that prompts user to enter the number
* of blocks are available . Then prompts for the size of the file
* then calculates the 80 percentage of file and calculate the remianing
* blocks and continue prompt until there is no space available to
* store the user enter file size.*/
//HW2Q2.java
import java.util.Scanner;
public class HW2Q2
{
public static void main(String[] args)
{
int size;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of blocks available");
size=keyboard.nextInt();
System.out.println("Available number of blocks is " + size);
int filesize;
double compsize;
while(size!=0)
{
System.out.println("Enter the size of the file that is to be stored");
filesize=keyboard.nextInt();
//calculate the size of compressed file
compsize=filesize*(80.0/100.0);
//calcualte the number of blocks that the compsize occupies
int blocks=(int) (compsize/512);
//break the loop if blocks is greaterthan size
if(blocks>size)
{
System.out.println("the number of blocks available, "
+ size + " is less than the required number, " + compsize);
break;
}
else
{
//subtract the blocks from size and assign to size
size=size-blocks;
//print the avaialble blocks
System.out.println("available number of blocks is " + size);
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Enter the number of blocks available
10
Available number of blocks is 10
Enter the size of the file that is to be stored
2048
available number of blocks is 7
Enter the size of the file that is to be stored
2048
available number of blocks is 4
Enter the size of the file that is to be stored
2048
available number of blocks is 1
Enter the size of the file that is to be stored
2048
the number of blocks available, 1 is less than the required number, 1638.4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.