Question: USING JAVA: Follow the steps below to modify the JAVA Code: -A Paint company has determined that for every 112 square feet of wall space,
USING JAVA: Follow the steps below to modify the JAVA Code:
-A Paint company has determined that for every 112 square feet of wall space, one gallon of paint and 8 hours of labor will be required. The company charges $35.00 per hour for labor.
-For this assignment you will take pre-existing code, the Paint Job Estimator and modularize it by moving calculations to their own methods, calling those methods, validating data input and practicing other Java coding from past weeks.
-Part 1: Methodize/Modularize the code:
Right now we do all the calculations inside the main method but need to move those each into their own methods. Create the following methods:
-public static int calculateGallonsPaint(double paintSquareFeet)
This method takes the square footage and divides by 112 for how much a gallon covers. (This calculation is done for you)
Move the calculation from the gallonsOfPaint variable into this method.
Notice that the return type is an int, but we are calculating double. One thing this program needs to do/be modified is to round up. We can't buy a partial gallon of paint so for each job we ALWAYS need to round up. Use an appropriate rounding method to return the value rounded up AND make sure to return it as an integer.
Lastly refactor your code to call this method and save the value returned to a variable.
-public static int calculateLabor(double paintSquareFeet)
This method takes the square footage divides by 12 then multiplies by 8 for calculating the total hours of labor needed. (Again, this calculation is done in the main method already)
Move the calculation from the hoursOfLabor variable into this method.
Again notice we are returning and integer but currently calculating as a double. You need to also round this up to the nearest integer as we don't charge for partial hours. Then return the value as an integer.
Lastly refactor your code to call this method and save the value returned to a variable.
-public static double calculatePaintCost(int gallonsOfPaint, double gallonPaintCost)
This takes the gallons of paint needed (based on calculateGallonsPaint method) and the cost of each gallon multiplies them together for total cost of paint.(Again already done in main method)
Move the calculation from the totalPaintCost variable to this method.
Return the calculated cost from this method.
Lastly, refactor your code to call this method and save the value returned to a variable.
-public static double calculateLaborCharges(int laborHours)
This method takes hours and multiplies it by 35 which is the total hourly cost of the job. (Already calculated in the main method)
Move the calculations from laborCharges variable to this method.
Return the calculated cost form this method.
Lastly refactor your code to call this method and save the value returned to a variable.
-Part 2: Validate Data Input
Lastly we want to make sure that we get the correct input for our program so that it doesn't have any runtime exceptions. To do this we are going to use our data validation techniques and create a method to get our input for us.
Create the following methods and refactor our code to call it:
public static double getDouble(Scanner sc, String prompt)
This method takes on our Scanner object and a String for the prompt when getting input
Use these two variables to print a prompt, then ask for double input.
You can use any method you like to validate input
try/catch,
parsing from a String
hasNext____() methods on the Scanner
Once data has been received return it.
Lastly refactor your code to call this method and save the value returned to a variable. You will also have to remove a couple print statements. Do this for both double inputs that we get in the main method.

THE CODE TO MODIFY:
import java.util.Scanner; import java.text.NumberFormat; public class PaintJobEstimator { public static void main(String[] args) { Scanner input = new Scanner(System.in); NumberFormat currency = NumberFormat.getCurrencyInstance(); //Main Program Start System.out.println("Welcome to the Happy Accidents Paint Company Estimator "); String cont = "y"; // prime input for looping while (cont.equalsIgnoreCase("y")) { // get input for calculations System.out.print("Enter paint square footage: "); double paintSquareFootage = input.nextDouble(); System.out.print("Enter cost of paint(per gallon): "); double gallonPaintCost = input.nextDouble(); // calculate paint, labor, and costs associated with the job double gallonsOfPaint = paintSquareFootage / 112; // round this up. double hoursOfLabor = paintSquareFootage / 112 * 8; // round this up too. double totalPaintCost = gallonPaintCost * gallonsOfPaint; double laborCharges = hoursOfLabor * 35; double totalJob = laborCharges + totalPaintCost; // Output the results System.out.println("Gallons of Paing Required : " + gallonsOfPaint + " gallons"); System.out.println("Hours of Labor : " + hoursOfLabor + " hours"); System.out.println("Cost of Paint : " + currency.format(totalPaintCost)); System.out.println("Labor Charges : " + currency.format(laborCharges)); System.out.println(" Total Cost of the Job : " + currency.format(totalJob)); // asks to repeat or not System.out.print("Continue? (y): "); cont = input.next(); } } } Welcome to the Happy Accidents Paint Company Estimator Enter paint square footage: 525 Enter cost of paint(per gallon): 21 Gallons of Paint Required : 5 gallons Hours of Labor. : 38 hours Cost of Paint : $105.00 Labor Charges :$1330.00 Total Cost of the Job. : $1435.00 Continue? (y) : y Enter paint square footage: five hundred Error, enter a number. Enter paint square footage: alkg Error, enter a number. Enter paint square footage: 525 Enter cost of paint(per gallon): ytj Error, enter a number! Enter cost of paint(per gallon): twenty one Error, enter a number! Enter cost of paint(per gallon): 21 Gallons of Paint Required : 5 gallons Hours of Labor. : 38 hours Cost of Paint : $105.00 Labor Charges : $1330.00 Total Cost of the Job. : $1435.00 Continue? (y):n
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
