Question
1. Write a method called getNameArray that takes an integer parameter (numNames), returns an array of Strings, and is declared to throw an Exception. If
1. Write a method called getNameArray that takes an integer parameter (numNames), returns an array of Strings,
and is declared to throw an Exception. If the parameter passed is a negative value, throw
an Exception. Otherwise, loop numNames times prompting the user to enter another name
each pass through the loop. Return this array of names.
2. Write a method called writeNames that takes two parameters: an array of Strings (nameArray) and
a String (fileName), and returns nothing. Write the array of Strings (names) to a text file
named using the fileName parameter. Make sure to catch any Exception that
might be thrown.
3. Write a method called readNames that takes a String parameter (fileName) and
returns an ArrayList of Strings (nameArrayList). The method reads the text file (fileName) and
populates the ArrayList with an element for each line in the file. The method returns the
populated ArrayList. Make sure to catch any Exception that
might be thrown.
4. Write a method called printNames that takes an ArrayList of Strings and prints
the contents of the ArrayList to the command line, each on a separate line.
4. Call each of the methods above from the main method.
The pseudocode for the main method is as given below.
Make sure to catch any Exception that
might be thrown.
Call getNameArray passing the size of the array (any integer) that you want returned
Pass the array and a file name to the writeNames method
Pass the file name to the readNames method and return the ArrayList
Pass the ArrayList to the printNames method.
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