Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

--------------------------------------- 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

image text in transcribedimage text in transcribed

---------------------------------------

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());

}

}

}

}

Q2. NumberFile.java. This is a continuation of NumberFile.java from Week 7 & 8 practical exercises. Import a copy of NumberFile.java into your Week 9 project in Eclipse. Your program needs to be modified to (i) allow the user to read numbers from multiple input files, storing the combined numbers in a single fixed-size array that can hold a maximum of 5000 numbers (the use of ArrayList or other dynamic array types is not permitted); (ii) allow the user to search the array to find how many times a particular number occurs in the array, and (ii) sort the numbers in the array in ascending order. Furthermore, your program must have a text-based menu with the following options: Menu 1) Write new number range to file 2) Read numbers from file into the array 3) Write the array's numbers to file 4) Print the numbers in the array 5) Sum the numbers in the array 6) Average the numbers in the array 7) Find a number in the array 8) Sort the numbers in the array in ascending order 9) Exit the program Enter your choice: The functionality of each menu option is described below: 1) Writes a sequence of numbers to file (as per week 8). Ask the user for the starting number and ending number the starting number should be less than the ending number), then write that sequence of whole numbers to a file named by the user. If the file already exists, ask the user if they wish to overwrite it. 2) Ask the user for the name of the input file. Check that the file exists before attempting to read the numbers in the file. If the array already has numbers in it, ask the user if they wish to overwrite the contents of the array. If the user does not wish to overwrite the contents of the array, the numbers in the file should be added to the array, directly after the existing numbers in memory. For example, if the array contained the numbers 1 2 3 4 5", and the user chose a file with the numbers 10 11 12 13" and the user chose not to overwrite the contents of the array, the array contents would now be *1 2 3 4 5 10 11 12 13". Alternatively, if the user chose to overwrite the contents of the array, the new array contents would be 10 11 12 13". If the array contains no numbers (for example, when the program first starts), do not ask the user if they wish to overwrite the contents of the array. Inform the user if the array is full (5000 numbers). 3) Write the numbers in the array to a file of the user's choice. If the file already exists, ask the user if they wish to overwrite it. If the array contains no numbers (for example, when the program first starts), inform the user there are no numbers to be written to file, and do not allow the user to save the file. Remember that your array may be partially filled; only write to file the numbers that have been read using menu option 2. 4) Print the numbers stored in the array (as per week 8). Consider that the logical end of the array may not be the physical end of the array 5) Sum the numbers in the array and display the total (a s per week 8). 6) Find the average of the numbers and display it (as per Week 8). 7) Ask the user to enter a number. Search the contents of the array and display the number of times that number occurs in the array. Remember, due the functionality of menu option 2, it is possible that a number occurs more than once in the array. 8) If the user chooses this option, code should execute that ensures the numbers in the array are sorted in ascending order (see the Week 9 lecture slides for how to sort an array). 9) Exit the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions