Problem 5 (Name this Lab3_Problem5) Write a program that shows what happens when
ID: 669837 • Letter: P
Question
Problem 5 (Name this Lab3_Problem5)
Write a program that shows what happens when comparing floating-point data for equality. The program will sum three floating-point values and then compare the sum to the "common sense" value you would get by "eyeballing" the data.
Declare these variables: dCalculatedSum, dActualSum, and dDifference.
Sum the values 0.1, 0.2, and 0.3 to dCalculatedSum.
Assign a value of 0.6 to the variable dActualSum.
Subtract dActualSum from dCalculatedSum and store the difference to dDifference.
Display your output as follows. The actual numeric values may vary depending on your Java compiler and OS platform:
Calculated sum: 0.6000000000000001
Actual sum: 0.6
Calculated sum – actual sum: 1.1102230246251565E-16
After the output above, add an if-else test to determine if the calculated sum and actual sum are equal using the == operator:
If the values are equal using the == operator display:
The comparison evaluates to EQUAL using ==
Otherwise, display:
The comparison evaluates to NOT EQUAL using ==
Next append this method BELOW the closing brace for the public static void main(String[] args) method and ABOVE the closing brace for the primary class, Lab1_Problem2. Clean up any indentation irregularities after you paste it into your code.
Tip: highlight your entire program code from beginning to end, then hit Tab to force DrJava to automatically fix or clean up the indentation level.
// A method to determine if two values of data type double are
// nearly equal with a user-defined epsilon:
public static boolean bEquals(double pdVal1, double pdVal2, double pdEpsilon)
{
// Declare a variable to store the return value to be passed
// back to the calling method:
boolean bReturnValue;
// If it evaluates to truly equal, the outcome is true:
if (pdVal1 == pdVal2)
{
bReturnValue = true;
}
// Test for being within an acceptable range of error; if within,
// the outcome is also true:
else if (Math.abs(pdVal1 - pdVal2) < pdEpsilon)
{
bReturnValue = true;
}
else
{
// It is neither truly equal or acceptably within range; return false:
bReturnValue = false;
}
// Return the boolean outcome to the calling method:
return bReturnValue;
}
} // This brace is the end of the primary class Lab1_Problem2
In your main class, add another if-else comparison to invoke the new method and use the method to determine if the values fall within an acceptable error range (in this case, the error is less than .00001). The if clause to use follows:
if (bEquals(dCalculatedSum, dActualSum, .001) == true)
For the true branch, display
:
The values fall within the acceptable epsilon for near equality
For the false branch, display:
The values fall outside the acceptable epsilon for near equality
Explanation / Answer
public class PrinterApp
{
// A method to determine if two values of data type double are
// nearly equal with a user-defined epsilon:
public static boolean bEquals(double pdVal1, double pdVal2, double pdEpsilon)
{
// Declare a variable to store the return value to be passed
// back to the calling method:
boolean bReturnValue;
// If it evaluates to truly equal, the outcome is true:
if (pdVal1 == pdVal2)
{
bReturnValue = true;
}
// Test for being within an acceptable range of error; if within,
// the outcome is also true:
else if (Math.abs(pdVal1 - pdVal2) < pdEpsilon)
{
bReturnValue = true;
}
else
{
// It is neither truly equal or acceptably within range; return false:
bReturnValue = false;
}
// Return the boolean outcome to the calling method:
return bReturnValue;
}
// This brace is the end of the primary class Lab1_Problem2
public static void main(String[] args){
double dCalculatedSum,dActualSum,dDifference;
dCalculatedSum=0.1+0.2+0.3;
dActualSum=0.6;
dDifference=dCalculatedSum-dActualSum;
System.out.println("Calculated Sum: "+dCalculatedSum);
System.out.println("Actual sum: "+dActualSum);
System.out.println("Calculated sum-actual sum "+dDifference);
if(dCalculatedSum==dActualSum){
System.out.println("The comparison evaluates to EQUAL using ==");
}else{
System.out.println("The comparison evaluates to NOT EQUAL using ==");
}
if (bEquals(dCalculatedSum, dActualSum, .001) == true){
System.out.println("The values fall within the acceptable epsilon for near equality");
}else{
System.out.println("The values fall outside the acceptable epsilon for near equality");
}
}
}