Write the program to calculate the amount of paint needed to paint the walls of
ID: 3666585 • Letter: W
Question
Write the program to calculate the amount of paint needed to paint the walls of a room of the given length and width. It assumes that the paint covers 350 square feet per gallon. Declare COVERAGE as integer constant and initialize to 350 Declare integers length, width, and height; Declare double totalSqFt; Declare double paintNeeded; Declare and initialize Scanner object Prompt for and read in the length of the room Prompt for and read in the width of the room Prompt for and read in the height of the room Compute the total square feet to be painted-think about the dimensions of each wall Compute the amount of paint needed Print the length, width, and height of the room and the number of gallons of paint needed.Explanation / Answer
Paint.java
import java.util.Scanner;
public class Paint
{
/**
* The number of square feet that a gallon of paint can cover
*/
public static final int COVERAGE = 350;
/**
* The instance of {@link Scanner} used by the program
*/
public static Scanner scanner = new Scanner(System.in);
/**
return the boolean
*/
public static boolean getBool()
{
// Gets the next string from the scanner and assigns it to a field.
final String string = Paint.scanner.next();
// Determines whether or not the string is a valid entry. If the string
// is not, it prompts for another string.
{
if (string.equalsIgnoreCase("yes")
|| string.equalsIgnoreCase("true")
|| string.equalsIgnoreCase("y"))
{
return true;
}
else if (string.equalsIgnoreCase("no")
|| string.equalsIgnoreCase("false")
|| string.equalsIgnoreCase("n"))
{
return false;
}
else
{
Paint.print("That's not a valid answer! Please enter either "yes" or "no"");
return Paint.getBool();
}
}
}
/**
* return the integer
*/
public static int getInt()
{
try
{
// Gets the next integer from the scanner, by calling scanner.next()
// and parsing an integer from it.
return Integer.parseInt(Paint.scanner.next());
}
catch (final NumberFormatException e)
{
// error message to the user
Paint.print("That's not an integer!");
// Prompts it for another integer
Paint.print("Please enter a valid integer.");
// Gets the integer
return Paint.getInt();
}
}
public static void main(final String[] args)
{
// Intialize the variables we will be using
int length, width, height;
double totalSqFt, paintNeeded;
boolean paintCeiling;
// Explain the nature of the program, and ask whether or not the ceiling
// will be painted
// Get the users response
//paintCeiling = Paint.getBool();
// Prompt for the length
Paint.print("Please enter the length of the room in feet.");
// Get the users response
length = Paint.getInt();
// Prompt for the width
Paint.print("Please enter the width of the room in feet.");
// Get the users response
width = Paint.getInt();
// Prompt for the height
Paint.print("Please enter the height of the room in feet.");
// Get the users response
height = Paint.getInt();
// Make the calculation for the surface area of the room
{
// Calculate the surface area of the walls.
totalSqFt = ((length * width) + (length * height)) * 2;
// Add the surface area of the ceiling if the user wanted us to do
if (paintCeiling)
{
totalSqFt = totalSqFt + (height * width);
}
}
// Calculate how many gallons of paint will be needed
paintNeeded = totalSqFt / Paint.COVERAGE;
// Output the dimensions of the room
Paint.print("The length of the room is " + length
+ " feet. The width of the room is " + width
+ " feet. The height of the room is " + height + " feet.");
// Output whether or not the ceiling will be painted
{
if (paintCeiling)
{
Paint.print("We will be painting the ceiling.");
}
else
{
Paint.print("We will not be painting the ceiling");
}
}
// Output the number of gallons of paint needed
Paint.print("To complete this task, you would need " + paintNeeded
+ " gallons of paint.");
}
/**
* the string to be printed
*/
public static void print(final String string)
{
// Prints the string to the console
System.out.println(string);
}
}
Sample Output
Please enter the length of the room in feet.
25
Please enter the width of the room in feet.
20
Please enter the height of the room in feet.
25
The length of the room is 25 feet.
The width of the room is 20 feet.
The height of the room is 25 feet.
We will be painting the ceiling.
To complete this task, you would need 7.857142857142857 gallons of paint.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.