The problem
One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of acres in a tract of land with 389,767 square feet.
Breaking it down
Create constant
private static final int FEET_PER_ACRE = 43560;
Write method to calculate acres
/**
* Should calculate the number of acres.
*
* @param tract
* @return double
*/
static double calculateAcres(double tract) {
return tract / FEET_PER_ACRE;
}
Create variables, scanner for keyboard input
double tract;
double acres;
Scanner keyboard = new Scanner(System.in);
System.out.println("This program will calculate number of acres.");
Get user input, calculate acres
System.out.print("Enter tract in feet: ");
tract = keyboard.nextDouble();
acres = calculateAcres(tract);
Display results
System.out.println("A tract of land with " + tract + " square feet has " + acres + " acres.");
Output
This program will calculate number of acres.
Enter tract in feed: 30
A tract of land with 30.0 square feet has 6.887052341597796E-4 acres.
Unit tests
@Test
public void calculateAcres () {
assertEquals(68.87105142332415, LandCalculation.calculateAcres(3000023), 0);
}
Level Up
- Allow user to tract in another user of measurement. What will you need to allow a user to do?
- Validate user input