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

The purpose of this exercise is for you to practice file I/O. Work with a partne

ID: 3557156 • Letter: T

Question

The purpose of this exercise is for you to practice file I/O. Work with a partner to write a program that will Two files are provided for you to test: CTempsl.txt and CTemps2.txt. After the program has run, the file "results.txt" should contain a message such as: You may assume the user provides a correct file name, but if you have time, improve your program so that it can recognize a bad file name and ask the user to enter a better one. For computing the minimum and maximum, consider using the special values Double. POSITIVE INFINITY and Double. NEGATIVE_INFINITY as the starting values of your min and max variables respectively. You may use the "throws" clause in the first line of main (or any other function) as a way of avoiding compiler errors related to the File Not Found Exception, but if you have time, you should try to use a try/catch block instead. Add a comment to the top of your program that includes your name. Test your program until you are convinced it is working correctly and then both partners should turn it in electronically on the course web site by using the 'Lab 6 submission' link.

Explanation / Answer

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

?

public class MaxMinNumber {

public static void main(String[] args) {

BufferedReader br = null;

PrintWriter writer;

Scanner scan=new Scanner(System.in);

int count=0;

double[] nums=new double[100];

double max=Double.POSITIVE_INFINITY;

double min=Double.NEGATIVE_INFINITY;

try {

String sCurrentLine;

System.out.println("Enter the file name");

String fileName=scan.nextLine();

br = new BufferedReader(new FileReader(fileName));

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

nums[count++]=Double.parseDouble(sCurrentLine);

System.out.println(sCurrentLine);

}

for (int i = 0; i < count - 1; i++)

{

int index = i;

for (int j = i + 1; j < count; j++)

if (nums[j] < nums[index])

index = j;

double smallerNumber = nums[index];

nums[index] = nums[i];

nums[i] = smallerNumber;

}

writer = new PrintWriter(new FileWriter("result.txt", false));

writer.println("contained " + count + " numbers");

writer.println("The maximum was :"+nums[count-1] );

writer.println("The minimum was :"+nums[0]);

writer.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (br != null)br.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote