please read directions first. this is supposed to be a hybrid programe add comme
ID: 3576949 • Letter: P
Question
please read directions first. this is supposed to be a hybrid programe
add comments please
JAVA
please add comments
Programming Project 1 and Programming Project 2 "Hybrid"
Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate values and determine the smallest, largest and average of the remaining values. Your program should lastly output the smallest, largest and average values to an output file (results.txt).
Name your project " RealNumbersNoDuplicatesHighLowAverageText" Your program must check for and handle exceptions including (FileNotFoundException when opening the file, and IOException when opening the files, writing numbers to a file and when closing a file). Numbers.txt contains the data that your program should process. Remember to use appropriate naming conventions, include a well written program header comment, package your solution files into a single compressed/ZIP file, and submit the resulting file using this Assignment tool by the submission due date.
Programming Project 1
Write a program that searches a file of numbers and displays the largest number, the smallest number and the average of all the number in the file. Do not assume that the number in the file are in any special order. Your program should obtain the file name from the user. Use either a text file or a binary file. For the text-file version, assume one number per line. For the binary-file version, use numbers of type double that are written using writeDouble.
Programming Project 2
Write a program that reads a file of numbers of type int and outputs all the numbers to another file, but without any duplicate numbers. Assume that the numbers in the input file is already ordered from smallest to largest. After the program is run, the new file will contain all the numbers in the original file, but no number will appear more than once in the file. The numbers in the output file should also be sorted from smallest to largest. Your program should obtain both file names from the user. Use either a text file or a binary file. for the text-file version, assume one number per line. for the binary-file version, use numbers of typeint that are written using writeInt.
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
public class Hybrid {
public static void main(String a[]){
try{
//open file for reading using bufferedreader
BufferedReader br=new BufferedReader(new FileReader("F:\Corsera1\"
+ "Chegg\src\Numbers.txt"));
String line="";
List<Double> num=new ArrayList<Double>();
//read file line by line
while((line=br.readLine())!=null){
//convert each string nto double
double d=Double.parseDouble(line);
//if not duplicate number
if(!num.contains(d))
num.add(d);
}
br.close();
/*
* since the numbers are sorted already in the file.
* the first number will be the smallest and the last number will be
* the highest.
*/
double min=num.get(0);
double max=num.get(num.size()-1);
double sum=0.0;
/*
* average of remaining i.e. except smallest and highest is calculated
*/
for(int i=1;i<num.size()-1;i++){
sum+=num.get(i);
}
double avg=sum/(num.size()-2);//save the average
//open file to write using bufferedwriter
BufferedWriter bw=new BufferedWriter(new FileWriter("F:\Corsera1\"
+ "Chegg\src esults.txt"));
/*
* write smallest,highest and average values to file.
*/
bw.write("Smallest: "+min+" "
+ "Highest: "+max+" "
+ "Average of the remaining: "+avg);
bw.close();
}catch(Exception e){e.printStackTrace();}
}
}
Input:(Numbers.txt)
50.68463429677369
51.015415401825074
55.387507375973584
55.387507375973584
59.812141432305964
60.90211446057276
61.31558692961303
62.525503536033355
63.10518619398068
63.10518619398068
67.55918766265316
68.2877250148396
70.02170809188907
70.16711271431048
71.71022879358881
74.5756068653888
74.95389367902283
82.98477485876612
87.0189998776595
87.2483919971531
100.03381203887423
100.16584062440808
100.49000284452225
103.37402210868935
103.91973335045498
106.05391914460347
106.4914403029983
106.60944817534156
110.13456499453304
112.26832735156931
114.04236120631744
114.96284345568138
115.88302861995714
117.29240522958068
120.91606638212991
121.29918725315127
121.29918725315127
128.00121112787932
132.8269268120801
136.13414980226864
137.30938327434944
137.44840831397934
141.08133293737012
141.34824627440946
142.7539538730651
143.161304865631
145.7630602522524
145.7630602522524
147.16078377116355
148.6433054641271
149.27509966667873
Output(results.txt):
Smallest: 50.68463429677369
Highest: 149.27509966667873
Average of the remaining: 102.31467010526586
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.