Write a program that sums the area of all lands and displays the total volume in
ID: 3595974 • Letter: W
Question
Write a program that sums the area of all lands and displays the total volume in acres. There are 0.404687261 hectares in an acre and 4 roods in an acre. Display the acreage with only three decimal digits of accuracy.
land.txt:
The program is to be written in Java.
COMP163 Acreage The county has a list of public lands as measured by surveyors. You are to write a program that calculates how much public land the county has. The data file land.txt contains a list of numbers representing the area of the land. Each line of the file contains a number with a decimal point followed by the unit of measurement. The units can be acre (represented by "ac"), hectare (represented as "ha") or rood (represented by "rood"). The units might be in a mixture of upper and lower case letters. A. [85 points] Write a program that sums the area of all lands and displays the total volume in acres. There are 0.404687261 hectares in an acre and 4 roods in an acre. B. [15 points] Display the acreage with only three decimal digits of accuracy Sample output The total area is 402062.0264791727 acres or The total area is 402062.026 acresExplanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
import javafx.scene.input.DataFormat;
/**
*
*/
/**
* @author XXXXX
*
*/
public class SumAcres {
/**
*
*/
public SumAcres() {
// TODO Auto-generated constructor stub
}
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) {
File file = new File("land.txt");
@SuppressWarnings("resource")
Scanner reader = null;
try {
reader = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double sum = 0;
while (reader.hasNextLine()) {
String line = reader.nextLine();
double input = Double.parseDouble(line.split(" ")[0]);
String unit = line.split(" ")[1];
if (unit.equalsIgnoreCase("ac")) {
sum += input;
} else if (unit.equalsIgnoreCase("rood")) {
input = (input / 4.0);// Converting to acre
sum += input;
} else if (unit.equalsIgnoreCase("ha")) {
input = (input / 0.404687261);// Converting to acre
sum += input;
} else {
//
}
}
System.out.println("The total area is " + sum + " acres");
System.out.println();
DecimalFormat df = new DecimalFormat("#.###");
System.out.println("The total area is " + df.format(sum) + " acres");
}
}
=========
The total area is 397351.35110632394 acres
The total area is 397351.351 acres
========
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.