Question
--------------------- NumberFile.java import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStreamReader; public class NumberFile { private int []arr = new int[5000]; public void writeToFile(String
---------------------
NumberFile.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
public class NumberFile {
private int []arr = new int[5000];
public void writeToFile(String filepath,
int lowerBound,
int upperBound) throws Exception {
// constructing the writer
BufferedWriter writer =
new BufferedWriter(new FileWriter(filepath));
// writing the numbers in the range
for(int number = lowerBound; number
writer.write(Integer.toString(number) + ' ');
}
// closing the writer
writer.close();
}
public void readData(String filepath) throws Exception {
// constructing the reader
BufferedReader reader = new BufferedReader(new FileReader(filepath));
// Reading and storing numbers to the array
String line;
int i=0;
while((line = reader.readLine()) != null && i
arr[i] = Integer.parseInt(line);
i++;
}
// Closing the reader
reader.close();
}
public int calcSum() {
int sum = 0; // initializing sum to zero
// reading and summing each number from the array
for (int i=0; i sum += arr[i]; // Return the sum return sum; } public double calcAverage() { // Call calcSum to calculate the sum of the elements in the array int sum = calcSum(); // Find the length of the array int size = arr.length; // Calculate the average double average = sum / size; // Return the average return average; } public void displayArray() { // Display the contents of the array System.out.println("Array contents:"); for (int i=0; i System.out.print(arr[i] + " "); System.out.println(); } public static void main(String[] args) { // constructing the NumberFile object NumberFile numberFile = new NumberFile(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // usage loop boolean isRunning = true; while(isRunning) { try { // Display menu to the user System.out.println("Options:"); System.out.println("1. Write to file"); System.out.println("2. Read file content to array"); System.out.println("3. Calculate and display total from the array"); System.out.println("4. Calculate and display average of the array content"); System.out.println("5. Display the array content"); System.out.println("6. Exit"); System.out.println("Choice:"); // parsing choice int choice = Integer.parseInt(reader.readLine()); // variables used in switch case String filepath; int lowerBound; int upperBound; switch(choice) { case 1: // writing the numbers in the range System.out.println("Enter filepath:"); filepath = reader.readLine(); System.out.println("Enter lower bound:"); lowerBound = Integer.parseInt(reader.readLine()); System.out.println("Enter upper bound:"); upperBound = Integer.parseInt(reader.readLine()); numberFile.writeToFile(filepath, lowerBound, upperBound); break; case 2: // reading data from the file and writing to the array System.out.println("Enter filepath:"); filepath = reader.readLine(); numberFile.readData(filepath); break; case 3: // Summing each number from the array int sum = numberFile.calcSum(); System.out.println("Sum of the elements in the array: " + sum); break; case 4: // Finding average of the array contents double average = numberFile.calcAverage(); System.out.println("Average of the elements in the array: " + average); break; case 5: // Displaying the contents of the array numberFile.displayArray(); break; case 6: // exiting System.out.println("Exiting"); isRunning = false; break; default: // choice out of range throw new Exception("Choice out of range"); } } catch(Exception e) { // printing error message System.out.println(e.getMessage()); } } } } ----------------------------------------- FYI info for week 8 if needed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started