Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Imagine a program that compresses files to 80 percent of their original size and

ID: 3667643 • Letter: I

Question

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.

Please do not use advanced commands. I am ony a beginner at this point so I am up to while, if type statements. This is for Java. Thanks.

Explanation / Answer

import java.util.Scanner;

class FileCompress

{

public static void main(String args[])

{

double size;

Scanner s=new Scanner(System.in);

System.out.println("Enter the number of blocks that are available on the storage media");

size=s.nextInt();

  int filesize;

while(size!=0)

{

System.out.println("Enter the size of the file to be compressed in mb");

filesize=s.nextInt();

filesize=1024000/512;

if(filesize>size)

{

System.out.println("the file cannot be fitted");

break;

}

else

{

filesize=filesize-(80/100)*filesize;

System.out.println("The compressed file siz is "+filesize);

System.out.println("The file is compressed and saved");

size=size-filesize;

System.out.println("the size of the file after compression is"+filesize);

}

}

}

}