In: Computer Science
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of the file. You can type and save the text file with any text editor, such as Notepad or Notepad++. NotePad++ is a good program to have on your computer, It can be downloaded freely from: https://notepad-plus-plus.org (Links to an external site.) It should be saved or copied to the same folder as your Java source code file within you Intellij project. Contain a second for loop, calculate the average score, highest score, and lowest score in the array. This part of your program does not need to change from the previous assignment. output the results to a new text file. You can decide the name of the file. You should only use a local file name and the file will then be created in your IntelliJ project folder.
Your task is to modify the program from the Java Text files programming assignment to contain several methods for different parts of the program.I suggest you save a copy of the original before modifying the software.
Your modified program should have the following six methods:
Unlike Python functions, methods do not need to appear before they are called in a program file. In fact, it is common practice in Java to put the main() method as the first method in Java program, similar to what is done in the payMethods sample program in the chapter.
/*
* The java program that reads input file scores.txt that contains
the 5 scores. Then find the average score, highest score and lowest
score. Then write the data to the new file , output.txt.
* */
//Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
String
inputFile="scores.txt";
int[] nums=new int[5];
int location=0;
float total_score=0;
float average_score=0;
float highest_score=0;
float lowest_score=0;
Scanner filereader=null;
try
{
//Open
file for reading input file, scores.txt
filereader=new
Scanner(new File(inputFile));
//read
file until the end of the file is reached and location value is
less than 5
while(filereader.hasNextLine() &&
location<nums.length)
{
//parset to integer value
nums[location]=Integer.parseInt(filereader.nextLine());
//increment the location
location++;
}
filereader.close();
//Assume
the first element is highest score
highest_score=nums[0];
//Assume
the first element is lowest score
lowest_score=nums[0];
for (int i = 1;
i < nums.length; i++)
{
total_score=total_score+nums[i];
if(nums[i]>highest_score)
highest_score=nums[i];
if(nums[i]<lowest_score)
lowest_score=nums[i];
}
//calculate the average score
average_score=total_score/nums.length;
System.out.printf("Average score : %.2f\n",average_score);
System.out.printf("Highest score : %.2f\n",highest_score);
System.out.printf("Lowest score : %.2f\n",lowest_score);
//Create a variable of PrintWriter class
PrintWriter
filewriter=null;
try
{
//Create a PrintWriter file object of file
output.txt file
filewriter=new PrintWriter(new
File("output.txt"));
//write data to the output.txt
filewriter.printf("Average score :
%.2f\r\n",average_score);
filewriter.printf("Highest score :
%.2f\r\n",highest_score);
filewriter.printf("Lowest score :
%.2f\r\n",lowest_score);
//close the PrintWriter file
object
filewriter.close();
}
catch (Exception
e)
{
System.out.println("Exception");
}
}
catch (FileNotFoundException
e)
{
System.out.println(inputFile+" file not found...");
}
}
} //end of the class
------------------------------------------------------------------------------------------------------------------------------
Input file : scores.txt
85
99
96
95
75
------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Average score : 73.00
Highest score : 99.00
Lowest score : 75.00
------------------------------------------------------------------------------------------------------------------------------
output.txt