Using JAVA In a class called Stats, given a text file of doubles (file name is f
ID: 3834228 • Letter: U
Question
Using JAVA
In a class called Stats, given a text file of doubles (file name is fileIn.txt), write a program that determines the average, the maximum, the minimum, and how many numbers are in the following buckets: less than 0, between 0 (inclusive) and 100 (exclusive), and greater than or equal to 100.
Your program should print the following message to another file called “fileOut.txt”:
Statistics for the numbers in fileIn.txt:
average: <average>
max: <max>
min: <min>
There are <negNum> negative numbers, <btw0and100> numbers between 0 (inclusive) and 100 (exclusive), and <geq100> numbers that are greater than or equal to 100.
Here is a template to help write the code:
import java.io.*;
import java.util.*;
public class Stats {
public static void main(String[] args) {
// Scanner and PrintWrite must be declared outside the try block
// otherwise their scope will be limited to within the block
Scanner input = null;
PrintWriter output = null;
try {
//initialize ithe variables nput and output here
input = new Scanner(new FileInputStream("fileIn.txt"));
} catch (FileNotFoundException e) {
//If an exception is caught, we need to handle it.
//Print a nice message telling the user the file was not found and
//exit the program normally, i.e. use the exit method.
System.out.println("Not able to find the file.");
System.exit(0); //exits the program if the file is not able to be found
}
//Your turn!
//Declare some useful variables here - the ones you will need to
//modify in the loop below to calculate the required statistics
//i.e. max, min, sum, count, etc.
//Hint: initialize max to be -infinity and min to be +infinity
//This is the loop where you read from the file and calculate some stats as you go.
while (input.hasNextDouble()) {
//Your turn!
}
//Let's print the results:
output.println("Statistics for the numbers in fileIn.txt:");
//Your turn! Make it look like the following:
//average: <average>
//max: <max>
//min: <min>
//there are <negNum> negative numbers, <btw0and100> numbers between 0 (inclusive) and 100 (exclusive), and <geq100> numbers that are greater than or equal to 100.
//one very important last thing to to...
//your program should print this information to another file called fileOut.txt
}
}
Explanation / Answer
Please find my implementation.
please let me know in case of any issue.
import java.io.*;
import java.util.*;
public class Stats {
public static void main(String[] args) {
// Scanner and PrintWrite must be declared outside the try block
// otherwise their scope will be limited to within the block
Scanner input = null;
PrintWriter output = null;
try {
//initialize ithe variables nput and output here
input = new Scanner(new FileInputStream("fileIn.txt"));
output = new PrintWriter(new File("fileOut.txt"));
} catch (FileNotFoundException e) {
//If an exception is caught, we need to handle it.
//Print a nice message telling the user the file was not found and
//exit the program normally, i.e. use the exit method.
System.out.println("Not able to find the file.");
System.exit(0); //exits the program if the file is not able to be found
}
//Your turn!
//Declare some useful variables here - the ones you will need to
//modify in the loop below to calculate the required statistics
//i.e. max, min, sum, count, etc.
//Hint: initialize max to be -infinity and min to be +infinity
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
double sum = 0;
int count = 0;
int lessZero = 0;
int gtZero = 0;
int gt100 = 0;
double num;
//This is the loop where you read from the file and calculate some stats as you go.
while (input.hasNextDouble()) {
num = input.nextDouble();
count++;
if(num < 0)
lessZero++;
else if(num < 100)
gtZero++;
else
gt100++;
sum = sum + num;
if(min > num)
min = num;
if(max < num)
max = num;
}
input.close();
//Let's print the results:
output.println("Statistics for the numbers in fileIn.txt:");
//Your turn! Make it look like the following:
//average: <average>
System.out.println("average: "+(sum/count));
//max: <max>
System.out.println("max: "+max);
//min: <min>
System.out.println("min: "+min);
//there are <negNum> negative numbers, <btw0and100> numbers between 0 (inclusive) and 100 (exclusive), and <geq100> numbers that are greater than or equal to 100.
System.out.println("there are "+lessZero+" negative numbers, "+gtZero+
" numbers between 0 (inclusive) and 100 (exclusive), and "+gt100+
" numbers that are greater than or equal to 100.");
//one very important last thing to to...
//your program should print this information to another file called fileOut.txt
output.write("average: "+(sum/count)+" ");
output.write("max: "+max+" ");
output.write("min: "+min+" ");
output.write("there are "+lessZero+" negative numbers, "+gtZero+
" numbers between 0 (inclusive) and 100 (exclusive), and "+gt100+
" numbers that are greater than or equal to 100. ");
output.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.