JAVA please add comments Programming Project 1 and Programming Project 2 \"Hybri
ID: 3576437 • Letter: J
Question
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 1 andProgramming 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.
numbers.txt
results.txt
sample output
244578843 133355567Explanation / Answer
Please find the required solution:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
*
* Programming Project 1
*/
public class RealNumbersNoDuplicatesHighLowAverageText {
public static void main(String[] args) {
// Read input from user
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter file name:");
String inputFilename = keyboard.nextLine();
System.out.print("Enter output name:");
String outputFilename = keyboard.nextLine();
// business logic to read double from file
Scanner file = null;
double max = 0, min = 0, sum = 0;
int count = 0;
try {
int i = 0;
file = new Scanner(new File(inputFilename));
// iterate through the file and compute max,min,avg
while (file.hasNext()) {
double number = file.nextDouble();
if (i == 0) {
max = number;
min = number;
i++;
}
else {
if (max < number)
max = number;
if (min > number)
min = number;
}
sum += number;
count++;
}
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
// business logic to write result from file
BufferedWriter writer = null;
try {
writer = new BufferedWriter(
new FileWriter(new File(outputFilename)));
writer.write("Largest Number=" + max);
writer.newLine();
writer.write("Smallest Number=" + min);
writer.newLine();
writer.write("Average=" + (sum / count));
}
catch (IOException e) {
e.printStackTrace();
}
try {
if (writer != null)
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
file.close();
keyboard.close();
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
/**
*
* Programming Project 2
*/
public class RealNumbersNoDuplicatesText {
public static void main(String[] args) {
// Read input from user
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter file name:");
String inputFilename = keyboard.nextLine();
System.out.print("Enter output name:");
String outputFilename = keyboard.nextLine();
Scanner file = null;
// use treeset to save integers in order
Set<Integer> set = new TreeSet<Integer>();
try {
file = new Scanner(new File(inputFilename));
while (file.hasNext()) {
set.add(file.nextInt());
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter(
new FileWriter(new File(outputFilename)));
Iterator<Integer> iter = set.iterator();
while (iter.hasNext()) {
writer.write("" + iter.next());
writer.newLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
try {
if (writer != null)
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
file.close();
keyboard.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.