Search code examples
javafor-looparea

For loop to figure out how much paint to buy to paint a room


These are the instructions that I was given to make my program: Write a program that calculates the number of buckets of paint to use for a room and the optimal number of cans to purchase. You need to ask the height of the room and the length and width of the room. The room is rectangular. You must paint the walls and the ceiling but not the floor. There are no windows or skylights. You can purchase the following size buckets of paint. • 5-liter bucket costs $15 each and covers 1500 square feet. • 1-liter bucket costs $4 and covers 300 square feet.

I already have most of my code written, I just need help figuring out how many buckets to buy using the for loop. Here is my program:

public class BrandonLatimerS5L1TryItSolveIt7 {
public static void main(String[] args){
    //declares variables
    double length;
    double width;
    double height;
    double ceilingArea;
    double wallsArea;

    //initializes bucket variables
    int fiveLiterBucket = 1500;
    int oneLiterBucket = 300;

    //Prompts user and gets input for length
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter the length of the room in feet: ");
    length = input.nextDouble();

    //prompts user and gets input for width
    System.out.println("Please enter the width of the room in feet: ");
    width = input.nextDouble();

    //Prompts for input and gets input for height
    System.out.println("And lastly, please enter the height of the room in feet: ");
    height = input.nextDouble();

    //figures out the total area that needs to be painted
    ceilingArea = length * width;
    wallsArea = (2 * (width * height) + (2 * (length * height)));
    double totalArea = ceilingArea + wallsArea;

    //For loop to figure out how much paint will be needed.
    for(int numOfBuckets = 0; totalArea > 1; numOfBuckets++){
        totalArea = totalArea - (totalArea / 1500);
        System.out.println("You will need " + numOfBuckets + " buckets.");
        continue;

    /*
     * This program taught me to use the for loop. I just can't seem to figure out how to find the amount of paint I need to buy. 
    */
    }
}

Any help is greatly appreciated!


Solution

  • A for loop is not the right tool for this job. Use a for loop when your program knows in advance how many times the loop should execute. If you don't know, or can't calculate how many times the loop body should execute, use a while loop, or in this case, just use arithmetic.

    Just do these steps in sequence, without using a loop:

    • Divide the total area by 1500 to find out how many big buckets of paint to buy.
    • Multiply that number by 1500 to find out the area that will be covered.
    • Subtract that area from the total area to find out how much blank wall space is left over.
    • Divide the left over wall space by 300 to find out how many small buckets to buy.
    • Use the same method as above to decide if you need an extra small bucket for any remaining blank wall space.