154k views
2 votes
Assume that a gallon of paint covers about 350 square feet of wall space. Create an

application with a main() method that prompts the user for the length, width, and

height of a rectangular room. Pass these three values to a method that does the following:

- Calculates the wall area of a room

- Passes the calculated wall area to another method that calculates and returns the

number of gallons of paint needed.

- Displays the number of gallons needed.

- Computes the price based on a paint price of $32 per gallon, assuming that the painter

can buy any fraction of a gallon of paint at the same price as a whole gallon.

- Returns the price to the main() method

The main() method displays the final price. For example, the cost to paint a 15-by-20-foot

room with 10-foot cielings is $64 dollars. Save as PaintClculator.java

**Note: I can prompt the user, but I can't get a return from my code.

import java.util.Scanner;

public class PaintCalculator {

public static void main(String [] args)

{

Scanner keyboard = new Scanner(System.in);

double wallArea;

double height;

double length;

double width;

double price;

double WallArea;

double paintQuantity;

//Prompts user for the dimensions of the room

System.out.print("Please enter the height of the room: ");

height = keyboard.nextDouble();

System.out.print("Please enter the length of the room: ");

length = keyboard.nextDouble();

System.out.print("Please enter the width of the room: ");

width = keyboard.nextDouble();

WallAreaMethod(height, length, width);

}

//Calulates the area of the wall in a room

public static double WallAreaMethod(double height, double length, double width)

{

double wallArea;

wallArea = length * height * width * height;

return wallArea;

}

//Computes the quanity of paint needed

public static double paintFormula(double wallAreaMethod, double price, double height, double length,double width)

{

double wallArea;

double paintQuantity;

paintQuantity = wallAreaMethod * 2 / 350;

System.out.println("For a room of height " + height + "feet, length " +

length + " feet, and width " + width + " feet you need to purchase "

+ paintQuantity + " gallons of paint.");

System.out.println("The price will be $" + price + ".");

price = paintQuantity * 32.0;

return price;

}

}

2 Answers

6 votes

Final answer:

The code provided needs a few modifications in order to achieve the desired output. The corrected version calculates the wall area, number of gallons of paint needed, and the price. It also follows proper Java naming conventions.

Step-by-step explanation:

The code provided is almost correct, but it needs a few modifications in order to achieve the desired output. Here is the corrected version:

import java.util.Scanner;

public class PaintCalculator {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double height;
double length;
double width;
double wallArea;
double paintQuantity;
double price;

System.out.print("Please enter the height of the room: ");
height = keyboard.nextDouble();
System.out.print("Please enter the length of the room: ");
length = keyboard.nextDouble();
System.out.print("Please enter the width of the room: ");
width = keyboard.nextDouble();

wallArea = calculateWallArea(height, length, width);
paintQuantity = calculatePaintQuantity(wallArea);
price = calculatePrice(paintQuantity);

System.out.println("For a room of height " + height + " feet, length " + length + " feet, and width " + width + " feet, you need to purchase " + paintQuantity + " gallons of paint.");
System.out.println("The price will be $" + price + ".");
}

public static double calculateWallArea(double height, double length, double width) {
return 2 * (height * length + height * width);
}

public static double calculatePaintQuantity(double wallArea) {
return wallArea / 350;
}

public static double calculatePrice(double paintQuantity) {
return Math.ceil(paintQuantity) * 32;
}
}

First, the method names in the main() method have been changed to match the actual method names. Additionally, the methods have been renamed to follow Java naming conventions. The calculateWallArea() method now calculates the total wall area of the room, while the calculatePaintQuantity() method calculates the number of gallons of paint needed. The calculatePrice() method rounds up the paint quantity to the nearest whole gallon and calculates the price accordingly.

User Rocksfrow
by
7.8k points
5 votes

Below is the modified code that assume that a gallon of paint covers about 350 square feet of wall space.

java

import java.util.Scanner;

public class PaintCalculator {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

double height;

double length;

double width;

double price;

// Prompts user for the dimensions of the room

System.out.print("Please enter the height of the room: ");

height = keyboard.nextDouble();

System.out.print("Please enter the length of the room: ");

length = keyboard.nextDouble();

System.out.print("Please enter the width of the room: ");

width = keyboard.nextDouble();

// Call WallAreaMethod to calculate wall area

double wallArea = WallAreaMethod(height, length, width);

// Call paintFormula to calculate paint quantity and price

price = paintFormula(wallArea, height, length, width);

// Display the final price

System.out.println("The total cost to paint the room is: $" + price);

keyboard.close();

}

// Calculates the area of the wall in a room

public static double WallAreaMethod(double height, double length, double width) {

double wallArea = 2 * height * (length + width);

return wallArea;

}

// Computes the quantity of paint needed and the price

public static double paintFormula(double wallArea, double height, double length, double width) {

double paintQuantity = wallArea / 350.0;

double price = paintQuantity * 32.0;

System.out.println("For a room of height " + height + " feet, length " + length + " feet, and width " + width

+ " feet you need to purchase " + paintQuantity + " gallons of paint.");

System.out.println("The price will be $" + price + ".");

return price;

}

}

User Nadendla
by
8.4k points

No related questions found