Issue
Assume that a gallon of paint cover's about 350 square feet of wall space, ask a user to enter length, width and height. The methods should do the following:
- Calculate the wall area for a room
- Passes the calculated wall area to another method that calculates and returns the number of gallons of paint needed
- Display the number of gallons needed
- Computes the price based on a paint price $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. Example: the cost to paint a 15-by-20 room with 10-foot ceilings is $64.
Here us what I did, and I'm failing to get that $64
public static void main(String[] args){
double l,h,w;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the height: ");
h=sc.nextDouble();
System.out.print("Enter the width: ");
w=sc.nextDouble();
System.out.print("Enter the length: ");
l=sc.nextDouble();
disGallons(calArea(h, w, l));
disPrice(price(calGallon(calArea(h, w, l))));
}
public static double calArea(double h,double w, double l){
double area=2*((w*h)+(l*w)+(l*h));
return area;
}
public static double calGallon(double area){
double gallons= area/350;
return gallons;
}
public static void disGallons(double gallons){
System.out.println("Gallons needed: "+gallons);
}
public static double price(double gallon){
final double gallPrice=32;
return (int)(gallPrice*gallon);
}
public static void disPrice(double price){
System.out.println("Total Price is: $"+price);
}
Solution
Here's how I'd do it.
package io.duffymo;
/**
* Size the amount of paint needed
* @link <a href="https://stackoverflow.com/questions/72404779/i-dont-understand-where-does-64-comes-from">...</a>
*/
public class PaintSizing {
public static final double PRICE_PER_GALLON_USD = 32.0;
public static final double SQ_FEET_PER_GALLON = 350.0;
public static double area(double h, double w, double l) {
return 2.0*(w + l)*h;
}
public static double gallons(double area) {
return area / SQ_FEET_PER_GALLON;
}
public static double price(double gallons) {
return gallons * PRICE_PER_GALLON_USD;
}
}
It's never too soon to learn about JUnit:
package io.duffymo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class PaintSizingTest {
@Test
public void testSizing() {
// setup
double l = 15.0;
double w = 20.0;
double h = 10.0;
double expectedArea = 700.0;
double expectedGallons = 2.0;
double expectedPrice = 64.0;
// exercise
double actualArea = PaintSizing.area(h, w, l);
double actualGallons = PaintSizing.gallons(actualArea);
double actualPrice = PaintSizing.price(actualGallons);
// assert
Assertions.assertEquals(expectedArea, actualArea, 0.001);
Assertions.assertEquals(expectedGallons, actualGallons, 0.001);
Assertions.assertEquals(expectedPrice, actualPrice, 0.001);
}
}
Answered By - duffymo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.