0

Overview

Using DrJava, I have calculated the "Estimated corn plants damaged" based on an alien invasion that left a crop circle x diameter in feet (a user input). I made a calculation to determine an acre being that 43560 feet equals 1 acre. For every acre destroyed, 30000 corn plants are destroyed.

The output I am asking for clarification on why my program is getting a slightly different answer than my calculator is "Estimated corn plants damaged."

My output from my Java program when the user inputs 80

Estimated corn plants damaged: 3462

My calculator's and Google's final calculation of the same diameter

Estimated corn plants damaged: 3450

My Worded Solution

I am getting a different output because the dAcres variable is formatted to round to 3 decimal points for the output but the value it uses for the calculation is not rounded. For example, when the user enters 80 as the diameter, the dAcres variable outputs 0.115. However, the dAcre variable uses .11539 in its calculations instead.

Final Question

How do I make it so my calculation uses only 3 decimal places instead of it's default number of decimal places? Example: 0.115 instead of 0.11539?

My Code

import java.util.*;
import java.text.*;


public class Lab2_Problem2
{
  public static final double dPI = 3.14159;

  public static void main(String[] args )
  {
    // DECLARATIONS
    // Input-capture variables
    double dDiameter;

    // Expression-result variables
    double dArea;
    double dAcres;
    double dCornPlantsDamaged;

    // Other variables
    double dRadius;
    double dRadiusSquared;

    // Instantiations
    Scanner cin = new Scanner(System.in);
    DecimalFormat dfDiameter = new DecimalFormat("#######");
    DecimalFormat dfArea = new DecimalFormat("#######.00");
    DecimalFormat dfAcres = new DecimalFormat("##0.000");
    DecimalFormat dfCornPlantsDamaged = new DecimalFormat("#####");

    // INITIALIZE 
    // INPUT
    System.out.print("Enter crop circle diameter in feet: ");
    dDiameter = cin.nextDouble();

    // PROCESSING AND CALCULATIONS
    dRadius = dDiameter / (double) 2;
    dRadiusSquared = dRadius * dRadius;
    dArea = dPI * dRadiusSquared;
    dAcres = dArea / (double) 43560;
    dCornPlantsDamaged = dAcres * (double) 30000;

    // OUTPUT
    System.out.println("Crop circle diameter: " + dfDiameter.format(dDiameter) + " feet");
    System.out.println("Square feet: " + dfArea.format(dArea));
    System.out.println("Acres: " + dfAcres.format(dAcres));
    System.out.println("Estimated corn plants damaged: " + 
dfCornPlantsDamaged.format(dCornPlantsDamaged));
  }
}

Q&A

  1. Why would you want to calculate by 3 decimal places if your calculator wouldn't do it?
    1. Because I would like the output to match the calculations being made.
  • 2
    Use `Math.PI` for pi – OneCricketeer Feb 08 '20 at 16:56
  • 4
    If I type this in a calculator I get 3461.8... So your program outputs the correct value. Why do you want to introduce rounding errors? And what exactly do you mean when you say Google outputs a different result? Using the calculator in the search field or where? – Markus Feb 08 '20 at 16:57
  • 1
    Do you really want to make your program bug-for-bug compatible with something less correct? – NomadMaker Feb 08 '20 at 17:06
  • @NomadMaker You are correct, but at the same time, I would like to still know the answer to my question. It's for practice and information purposes ore so than accuracy. I posted an answer regarding what you had to say, but I would still like a coded answer. – Nicholas Zachariah Feb 08 '20 at 17:37
  • 1
    You could write a class similar to BigDecimal, but I seriously suggest finding something productive to do. – NomadMaker Feb 08 '20 at 20:41

2 Answers2

0

Here is the working code in example form from the answer @cricket_007 gave.

dTruncOne = dAcres * 1000;
dTruncTwo = (int) dTruncOne * (double) 30000; // Truncated acres
dCornPlantsDamaged = dTruncTwo / (double) 1000;

dBushelsPerAcre = 211;
dCostPerBushel = 3.90;
-1

How do I make it so my calculation uses only 3 decimal places

Multiply it up to a full integer, truncating the decimal, then divide it back down to a decimal

((int) (0.11539 * 1000)) / 1000.0

Alternatively, there's Math.round method

Your calculator wouldn't use 3 decimal places, though, so I'm not sure why you'd want to

And, for simplicity, you can write out the formulas like so

destroyed_acres = ((pi * (d/2)^2) ft) * (acre/ft) 
ans = (corn/acre) * destroyed_acres

FWIW, wolframalpha says 3462

https://www.wolframalpha.com/input/?i=+%28%28pi+*+%2880%2F2%29%5E2%29+square+feet%29+to+acres+*+30000

Google does too (π × (80 ÷ 2)^2) × (1 ÷ 43560) × 30000

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245