Problem 1 (Name this Lab3_Problem1) Write a program that obtains an integer valu
ID: 669833 • Letter: P
Question
Problem 1 (Name this Lab3_Problem1)
Write a program that obtains an integer value from the end user named iValue. Then, using a single-selection "if" test, determine whether the value is equal to zero. If so, display Equal to 0. Otherwise, display nothing.
Input capture dialog sample:
Enter an integer value: 14
Output (for a non-zero value): nothing (there is no alternative branch; it is single-selection.
Once you get this working, then change your data type to double, and modify the prompt and variable names accordingly. Comment out the previous lines of code using int and add your new statements.
Next, apply the "epsilon" method to compare floating-point values for equality. Set an epsilon of .0001 as your target for "nearly equal." Then, test it out with some end-user input whereby the user might type in 0.0000001, 0.001 etc. Compare the user input against the hard-coded value of 0 as you apply "epsilon" in your "if" test.
Finally, add a false branch to your program so that the output will handle non-zero values with the output Not Equal to 0. Retain the true branch you already coded; this will give you a double-selection structure as your final code for this problem.
Explanation / Answer
ANSWER:
import java.util.Scanner;
public class Testing {
public static void main(String[] args) {
final double EPSILON = 0.0001;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Value :");
// int aa = in.nextInt();
double aa = in.nextDouble();
if (aa == 0)
System.out.println("Equal To Zero");
if ((aa - 0) < EPSILON)
System.out.println("Nearly Equal");
else
System.out.println("Not Equal To Zero");
}
}
OUTPUT:
Enter the Value :
0.0001
Not Equal To Zero
Enter the Value :
0
Equal To Zero
Nearly Equal
Enter the Value :
0.00001
Nearly Equal
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.