I need help compiling this Java code.. The code works but I need the code to out
ID: 3913238 • Letter: I
Question
I need help compiling this Java code.. The code works but I need the code to output the Paint Needed (0.5142857142857142) and Cans Needed (1 can(s))
thanks!
import java.util.Scanner; // Needed for user input
import java.lang.Math; // Needed for math functions
/**
* @author Marlene Azevedo
*/
public class PaintEstimator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight = 0.0; //12ft
double wallWidth = 0.0; //15ft
double wallArea = 0.0; //180.0 square feet
double gallonsPaintNeeded = 0.0; //0.5142857142857142 gallons
int cansNeeded = 0; //1 can(s)
final double squareFeetPerGallons = 350.0; //h*w = sqrt footage => 12x15=180.0 sqrt footage
final double gallonsPerCan = 1.0;
System.out.println("Enter wall height (feet): "); //12ft
wallHeight = scnr.nextDouble();
// Prompt user to input wall's width
System.out.println("Enter wall width (feet): "); //12ft
wallWidth = scnr.nextDouble();
// Calculate and output wall area
wallArea = wallHeight * wallWidth; // Math 180.0 = 12 x 15 area = height * width
System.out.println("Wall area: " + wallArea + " square feet"); // outputs wall area
wallArea = scnr.nextDouble();
//code stops here unless 350.0 is inserted manually
gallonsPaintNeeded = wallArea / squareFeetPerGallons; // Math 180.0 / 350.0 = 0.5142857142857142 gallons
System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons"); // should output gallonsPaintNeeded
gallonsPaintNeeded = scnr.nextInt();
cansNeeded = (int)((gallonsPaintNeeded / gallonsPerCan) +1); //had to fix this part by adding (int) and (+1)
System.out.println("Cans needed: " + cansNeeded + " can(s)"); // Needs to print out cansNeeded
cansNeeded = scnr.nextInt();
return;
}
}
Explanation / Answer
Most of your code is correct. I have same two simple modifications to the code so that it outputs the required number of cans and the volume of paint. After you have asked the user to input the wallHeigth and wallWidth you don't need to ask for anything else, as area and paint needed can be calculated by your code. Hence I have removed the lines wallArea =scnr.nextDouble(); and cansNeeded =scnr.nextDouble() and made some formatting changes. Here is the modified code:
import java.util.Scanner; // Needed for user input
import java.lang.Math; // Needed for math functions
/**
* @author Marlene Azevedo
*/
public class PaintEstimator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight = 0.0; //12ft
double wallWidth = 0.0; //15ft
double wallArea = 0.0; //180.0 square feet
double gallonsPaintNeeded = 0.0; //0.5142857142857142 gallons
int cansNeeded = 0; //1 can(s)
final double squareFeetPerGallons = 350.0; //h*w = sqrt footage => 12x15=180.0 sqrt footage
final double gallonsPerCan = 1.0;
System.out.print("Enter wall height (feet): "); //12ft
wallHeight = scnr.nextDouble();
// Prompt user to input wall's width
System.out.print(" Enter wall width (feet): "); //12ft
wallWidth = scnr.nextDouble();
// Calculate and output wall area
wallArea = wallHeight * wallWidth; // Math 180.0 = 12 x 15 area = height * width
System.out.print(" Wall area: " + wallArea + " square feet"); // outputs wall area
//code stops here unless 350.0 is inserted manually
gallonsPaintNeeded = wallArea / squareFeetPerGallons; // Math 180.0 / 350.0 = 0.5142857142857142 gallons
System.out.println(" Paint needed: " + gallonsPaintNeeded + " gallons"); // should output gallonsPaintNeeded
cansNeeded = (int)((gallonsPaintNeeded / gallonsPerCan) +1); //had to fix this part by adding (int) and (+1)
System.out.println("Cans needed: " + cansNeeded + " can(s)"); // Needs to print out cansNeeded
return;
}
}
Sample Output :-
~/chegg ??? javac PaintEstimator.java 13:57:09
~/chegg ??? java PaintEstimator
Enter wall height (feet): 12
Enter wall width (feet): 15
Wall area: 180.0 square feet
Paint needed: 0.5142857142857142 gallons
Cans needed: 1 can(s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.