Hi i need help with adding the statistics part to this question and would apprec
ID: 3817882 • Letter: H
Question
Hi i need help with adding the statistics part to this question and would appreciate help figuring it out.
Write a program that will write 5000 random number to a file named RandomNumbers.txt. Then, read this file and do the following:
Write the even number to a file named EvenNumbers.txt
Write the odd number to a file named OddNumbers.txt
Write the following statistics to the file named Stats.txt
Largest number in RandomNumbers.txt
Smallest number in RandomNumbers.txt
The average of all the values in RandomNumbers.txt
Number of even values
Number of odd values
MY CODE IS BELOW, THE ONLY PART I NEED TO ADD IS WRITING THE STATISTICS TO STAT.TXT
import java.util.*;
import java.io.*;
public class FilesQuestion3{
public static void main(String [] args)throws IOException
{
int large = 0;
int num;
PrintWriter out = new PrintWriter(new File("random.txt"));
Random rand = new Random();
int number, count=0, count2=0;
while(count<5000)
{
while(count2<15)
{
number=rand.nextInt(5000)+1;
out.print(number);
count++;
count2++;
}
count2 = 0;
out.println();
}
out.close();
int i;
try{
FileOutputStream fodd = new FileOutputStream("dataodd.txt");
FileInputStream fin = new FileInputStream("random.txt");
FileOutputStream feven = new FileOutputStream("dataeven.txt");
while((i=fin.read()) != -1)
{
if(i%2==0)
feven.write(i);
else
fodd.write(i);
}
fodd.close();
fin.close();
feven.close();
}catch(Exception e){
}
}
}
Explanation / Answer
package org.students;
import java.util.*;
import java.io.*;
public class FilesQuestion3 {
public static void main(String[] args) throws IOException {
int large = 0;
int num;
int min = 999999, max = 0;
long sum = 0;
double avg = 0.0;
int evencount = 0, oddcount = 0;
PrintWriter out = new PrintWriter(new File("random.txt"));
Random rand = new Random();
int number, count = 0, count2 = 0;
while (count < 5000) {
while (count2 < 15) {
number = rand.nextInt(5000) + 1;
out.print(number);
count++;
count2++;
}
count2 = 0;
out.println();
}
out.close();
int i;
try {
FileOutputStream fodd = new FileOutputStream("D:\dataodd.txt");
FileInputStream fin = new FileInputStream("D: andom.txt");
FileOutputStream feven = new FileOutputStream("D:\dataeven.txt");
FileOutputStream fstats = new FileOutputStream("D:\stats.txt");
while ((i = fin.read()) != -1) {
if (i % 2 == 0) {
feven.write(i);
evencount++;
} else {
fodd.write(i);
oddcount++;
}
// finding max number
if (max < i)
max = i;
// finding min number
if (min > i)
min = i;
sum += i;
}
avg = (double) sum / 5000;
String lar = "Largest number in RandomNumbers.txt is " + max
+ " ";
String sml = "Smallest number in RandomNumbers.txt is " + min
+ " ";
String savg = "The average of all the values in RandomNumbers.txt is "
+ avg + " ";
String seven = "Number of even values :" + evencount + " ";
String sodd = "Number of odd values :" + oddcount + " ";
// Converting string to byte array
byte b1[] = lar.getBytes();
byte b2[] = sml.getBytes();
byte b3[] = savg.getBytes();
byte b4[] = seven.getBytes();
byte b5[] = sodd.getBytes();
// Writing data to stats.txt
fstats.write(b1);
fstats.write(b2);
fstats.write(b3);
fstats.write(b4);
fstats.write(b5);
fodd.close();
fin.close();
feven.close();
} catch (Exception e) {
}
}
}
______________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.