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

}

}

}

}

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

FYI info for week 8 if needed

image text in transcribed

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 a. This is a continuation of Exercise 1 from week 7 practical exercises. Import a copy of NumberFile.java (from week 7) into your week 8 project in Eclipse. Modify your solution code as follows: Modify the second method (the method that reads the data from the file into memory) so that the data is read from the file into an array of appropriate data type. Assume that the array can store a maximum of 5000 numbers. Your method will need to ensure that a maximum of 5000 numbers are read from the file even if there are more than 5000 values in the file. You cannot assume that there will always be 5000 values in the file. The method should no longer display the values to the screen. b. Modify the third method (the method that numerically added the values from the file) so that the sum is calculated from the array that was created in part a above. The method will need to return the sum. Display the total (sum) in your main program. c. Add a new method to calculate and return the average (mean) of the values in the array. Display the average in your main program. d. Add a new method to display the contents of the array. Consider that the logical end of the array may not be the physical end of the array. e. Your program must allow the user to choose if they want to create the file, read the file content into an array, calculate and display the total from the array, calculate and display the average of the array content, display the array content, or exit the program. The program should continue to run until the user chooses to exit the program. f. As per part b of exercise 1 in week 7 your program should i. allow the user to choose the starting and ending values to be stored in the file, ii. enter the file name of the file to be created, iii. and enter the file name for the file to be read by the program. Test the program with different sized files with different sets of numbers. Make sure your code works correctly if the file isn't found, if it exists but doesn't have any data in it, or, if the file has too many values to fit into the array. Your solution must incorporate appropriate methods utlising appropriate parameter passing

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

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago