For java : Write a function called dist that takes two double values as paramete
ID: 3595710 • Letter: F
Question
For java : Write a function called dist that takes two double values as parameters and returns the absolute value of the difference of the two numbers (i.e. how far apart the numbers would be on a number lie). Do NOT use a library function for distance or for absolute value for this lab In main ask the user to enter the two values, then print out the numbers and result in a sentence The function dist should NOT print anything or receive any input from the user (no Scanner or print/printin in this function) Test case 1: Input a number: Input another number: The distance between 5.3 and 1.7 is 3.6 Test case 2: Input a number:Explanation / Answer
DistTest.java
import java.util.Scanner;
public class DistTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Input a number: ");
double first = scan.nextDouble();
System.out.println("Inout another number: ");
double second = scan.nextDouble();
System.out.printf("The distance between %.1f and %.1f is %.1f ",first, second, dist(first, second));
}
public static double dist(double first, double second) {
double d = first - second;
if(d < 0) {
d = -d;
}
return d;
}
}
Output:
Input a number:
1.7
Inout another number:
5.3
The distance between 1.7 and 5.3 is 3.6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.