The object is to calculate the cost of a paint job A painting company has determ
ID: 639016 • Letter: T
Question
The object is to calculate the cost of a paint job A painting company has determined that for every 112 square feet of wall space, one gallon of paint and eight hours of labor will be required. The company charges $35.00 per hour for labor. Modify your program so that it prints all values of money as numbers with exactly 2 decimal places.
Expected Output:
Enter the number of square feet: 224
Enter the price of a gallon of paint: 20
To paint 224.00 square feet, with paint that costs 20.00 per gallon,
you will need 2 gallons of paint and 16.00 hours of labor.
The cost of the paint is: 40.00
The cost of the labor is: 560.00 The total cost of the job is: 600.00
Explanation / Answer
import java.util.Scanner;
public class PaintJobEstimator {
// square feet per one gallon of paint.
public static final double AREA_PER_GALLON = 112.0;
// hours of labor needed to paint AREA_PER_GALLON square feet.
public static final double HOURS_PER_UNIT_AREA = 8.0;
// charge to customer for one hour of labor.
public static final double LABOR_COST_PER_HOUR = 35.0;
// main declares a Scanner that is passed to
// the input methods. main also controls the
// order of calculations.
public static void main( String[] args ) {
Scanner keyboard = new Scanner( System.in );
// How many square feet do we need to paint?
double sqft = getInput( keyboard, "Enter the number of square feet: " );
double gallonCost = getInput( keyboard,
"Enter the price of a gallon of paint: " );
int numGallons = calculateGallons( sqft );
double hoursLabor = calculateHours( sqft );
double paintCost = calculatePaintCost( numGallons, gallonCost );
double laborCost = calculateLaborCost( hoursLabor );
double totalCost = calculateTotalCost( paintCost, laborCost );
// Print the results.
generateReport( sqft, gallonCost, numGallons, hoursLabor,
paintCost, laborCost, totalCost);
}
public static double getInput( Scanner input, String prompt ) {
System.out.print( prompt );
while ( !input.hasNextDouble() ) {
input.nextLine(); // get rid of bad input.
System.out.print( prompt );
}
double inValue = input.nextDouble();
input.nextLine(); // clear the input line.
return inValue;
}
public static int calculateGallons(double sqft)
{
return (int)(sqft/AREA_PER_GALLON);
}
public static double calculateHours(double sqft)
{
return (sqft/AREA_PER_GALLON)*HOURS_PER_UNIT_AREA;
}
public static double calculatePaintCost(int n, double c)
{
return n*c;
}
public static double calculateLaborCost(double hl)
{
return hl*LABOR_COST_PER_HOUR;
}
public static double calculateTotalCost(double pc, double lc)
{
return pc+lc;
}
public static void generateReport(double sqft,
double gallonCost, int numGallons, double hoursLabor,
double paintCost, double laborCost, double totalCost)
{
System.out.println(" To paint "+sqft+" square feet, with paint that costs "+gallonCost+" per gallon,");
System.out.println("you will need "+numGallons+" gallons of paint and "+hoursLabor+" hours of labor.");
System.out.println("The cost of the paint is: "+paintCost+"");
System.out.println("The cost of the labor is: "+laborCost+" The total cost of the job is: "+totalCost);
}
}