Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 4, Problem 16E

Explanation of Solution

Algorithm:

Step 1: First get the number of free block and store it in a variable “blocks”.

Step 2: Assign “true” to a Boolean variable “flag”.

Step 3: WHILE “flag”.

Step 4: Get the size of the file in bytes and store it in a variable “size”.

Step 5: Calculate the compressed size of the file and store it in a variable “compress”.

Step 6: Calculate number of blocks required for the file and store it in a variable “blocksNeeded”. This is done by dividing “compress” by 512.

Step 7: Check if the “blocksNeeded” is less than “blocks”.

Step 8: If the condition is true then reduce the number of free blocks. This is done by subtracting “blocksNeeded” from “blocks”.

  Step 9: If the condition is false then assign “false” to the variable “flag”.

Complete program:

//Import required packages

import static java.lang.Math.round;

import java.util.*;

//Define the main class

public class FileSize

{

    //Define the main method

    public static void main(String[] args)

    {

        //Declare required variables

        double blocks, size;

        boolean flag = true;

        //Create an object for scanner class

        Scanner sc = new Scanner(System.in);

        //Get the number of free blocks

System.out.print("Enter the number of free blocks: ");

        blocks = sc.nextInt();

        //While true

        while(flag)

        {

            //Declare required variables

            double compress = 0, blocksNeeded = 0;

            //Get the size of the file

System.out.print("\nEnter the size of file in bytes: ");

            size = sc...

Blurred answer
Students have asked these similar questions
Assume n is a positive integer. Consider the loop below: x := 0 for i:= 5 to 5n x = 2(x+i) - 7 next i There are the total number of operations performed in this loop is operations performed in each run of the loop and the loop runs times, so
Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following: A) Ask the user for the amount deposited into the account during the month. (Do not accept negative numbers.) This amount should be added to the balance. B) Ask the user for the amount withdrawn from the account during the month. (Do not accept negative numbers.) This amount should be subtracted from the balance. C) Calculate the monthly interest. The monthly interest rate is the annual interest rate divided by twelve. Multiply the monthly interest rate by the balance, and add the result to the balance. After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned.
Write a complete program that: 1. Uses a do...while loop to prompt the user to enter a count that is greater than 1 and less than or equal to 20 then reads in a count from the keyboard as an integer. a. If input error occurs or the count the user enters is less than or equal to 1 or greater than 20, output an error. After outputting an error, clear/ignore the failed input and continue do...while looping until a valid number is entered. 2. Writes a triangle to the display using for loops. The triangle will be upside down. To write this triangle to the display: amountOfStars = number entered in (1). a. b. Output amountOfStars followed by a new line. C. Decrement amountOfStars by one. d. Continue from (b) until amountOfStars is zero. 3. Writes a triangle to the display using while loops. The triangle will be rightside up. To write this triangle to the display: a. amountOfStars = 1. b. Output amountOfStars followed by a new line. C. Increment amountOfStars by one. d. Continue from (b)…

Chapter 4 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 4.1 - Prob. 11STQCh. 4.1 - Write a for statement that displays the even...Ch. 4.1 - Prob. 13STQCh. 4.2 - Write a Java loop that will display the phrase One...Ch. 4.2 - Write a Java loop that will set the variable...Ch. 4.2 - Write a Java loop that will read a list of numbers...Ch. 4.2 - What output is produced by the following code? for...Ch. 4.2 - What output is produced by the following code? for...Ch. 4.2 - What output is produced by the following code? for...Ch. 4.2 - Revise the loop shown in Listing 4.6 to use a...Ch. 4.2 - What is the bug in the code in the section Tracing...Ch. 4.2 - Add some suitable output statements to the...Ch. 4.2 - What is the bug in the code in the previous...Ch. 4.2 - Prob. 24STQCh. 4.2 - Suppose that you did not have assertion checking...Ch. 4.3 - Prob. 26STQCh. 4 - Write a fragment of code that will read words from...Ch. 4 - Develop an algorithm for computing the...Ch. 4 - Develop an algorithm for a simple game of guessing...Ch. 4 - Write a fragment of code that will compute the sum...Ch. 4 - Convert the following code so that it uses nested...Ch. 4 - Write a for statement to compute the sum 1 + 22 +...Ch. 4 - (Optional) Repeat the previous question, but use...Ch. 4 - Write a loop that will count the number of blank...Ch. 4 - Write a loop that will create a new string that is...Ch. 4 - Write a program that will compute statistics for...Ch. 4 - Suppose we attend a party. To be sociable, we will...Ch. 4 - Define an enumeration for each of the months in...Ch. 4 - Write a fragment of code that computes the final...Ch. 4 - Suppose that you work for a beverage company. The...Ch. 4 - Suppose that we want to compute the geometric mean...Ch. 4 - Prob. 16ECh. 4 - Create an applet that draws a pattern of circles...Ch. 4 - Prob. 18ECh. 4 - What does the following fragment of code display?...Ch. 4 - Repeat Practice Program 4 of Chapter 3, but use a...Ch. 4 - Write a program that implements your algorithm...Ch. 4 - Repeat Practice Program 5 of Chapter 3, but use a...Ch. 4 - Write a program to read a list of nonnegative...Ch. 4 - Write a program to read a list of exam scores...Ch. 4 - Combine the programs from Programming Projects 5...Ch. 4 - Write a program that simulates the Magic 8 Ball...Ch. 4 - Whats for dinner? Let the computer decide. Write a...Ch. 4 - Write a program that implements your algorithm...Ch. 4 - Prob. 2PPCh. 4 - Write a program that reads a bank account balance...Ch. 4 - Modify Programming Project 5 from Chapter 2 to...Ch. 4 - Write a program that asks the user to enter the...Ch. 4 - Write a program that simulates a bouncing ball by...Ch. 4 - You have three identical prizes to give away and a...Ch. 4 - Prob. 9PPCh. 4 - Holy digits Batman! The Riddler is planning his...Ch. 4 - Your country is at war and your enemies are using...Ch. 4 - Prob. 12PPCh. 4 - Prob. 13PPCh. 4 - Prob. 14PPCh. 4 - (Challenge) Repeat the previous project, but...Ch. 4 - Write a JavaFx application that displays a series...
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning