Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Programming Project 1 and Programming Project 2 \"Hybrid\" Your goal is to

ID: 3575960 • Letter: J

Question

JAVA

Programming Project 1 and Programming Project 2 "Hybrid"

Your goal is to write a program that combines the functionality of both Programming Project 1 and Programming 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.

Nunbers.txt

results.txt

sample run

244578843 133355567

Explanation / Answer

HI, Please find my code. Please let me know in case of any issue.

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class RealNumbersNoDuplicatesHighLowAverageText {

  

   public static void main(String[] args) throws NumberFormatException, IOException {

      

       Scanner sc = new Scanner(System.in);

      

       System.out.print("Enter input file name: ");

       String inputFileName = sc.next();

      

       System.out.print("Enter output file name: ");

       String outputFileName = sc.next();

      

       FileReader fr = new FileReader(inputFileName);

       BufferedReader br = new BufferedReader(fr);

      

       FileWriter fw = new FileWriter(outputFileName);

       BufferedWriter bw = new BufferedWriter(fw);

      

       double min = 0 , max = 0, sum = 0, number, prev = 0;

       int count = 0;

       String line;

       while((line = br.readLine()) != null){

           number = Double.parseDouble(line.trim());

          

           // if this is the first number read from file, then initializie all

           if(count == 0){

               max = number;

               min = number;

               prev = number;

               count++;

               sum = sum + number;

           }else if(prev != number){

               if(max < number)

                   max = number;

               if(min > number)

                   min = number;

               count++;

               sum = sum + number;

               prev = number;

           }  

       }

      

       br.close();

       fr.close();

      

       bw.write("Largest: "+max);

       bw.newLine();

       bw.write("Smallest: "+min);

       bw.newLine();

       bw.write("Average: "+(sum/count));

       bw.close();

       fw.close();

   }

}

/*

Sample output:

Largest: 7.3

Smallest: 1.2

Average: 4.757142857142857*/