Create a program It should read 4 numbers with possible decimal places from a fi
ID: 3621852 • Letter: C
Question
Create a programIt should read 4 numbers with possible decimal places from a file named data.txt and find the range of the numbers.
The range is the maximum number minus the smallest number.
You can use the Math method Math.min and Math.max to find the smaller or larger of two numbers. Solve this problem by using these methods. DO NOT USE if statements from a future chapter.
use of Math.min
Math.min(22.0, 21.0) will return 21.0, the smaller of the two parameters.
Math.max(-23,0) will return 0, the larger number of the two parameters.
Your program should read the data.txt file and output a single statement like this example:
range of 22.0, 6.1, 2.2, 23.4 is 21.2
Explanation / Answer
please rate - thanks
import java.util.*;
import java.io.*;
public class FileIn
{public static void main(String[] args)throws FileNotFoundException
{double num,maxx,minn;
int i;
System.out.print("range of ");
Scanner input=new Scanner(new File("numbers.txt"));
num=input.nextDouble();
maxx=num;
minn=num;
System.out.print(num+", ");
for(i=1;i<4;i++)
{num=input.nextDouble();
System.out.print(num+", ");
maxx=Math.max(maxx,num);
minn=Math.min(minn,num);
}
System.out.println(" is "+(maxx-minn));
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.