Convert the following program into one that uses an array object, String a[], instead of an ArrayList object. Write a class [MyName]ArrayList, which can replace the ArrayList< String > type declarations in the code below. Be sure to support the same behavior currently provided by the ArrayList object, include:

  • automatic enlarge resizing, not need to auto shrink
  • default size initialization,
  • add, remove, and set methods
import java.util.*;
public class ArrayListExercise {
public static void main(String[] args) {
ArrayList< String > aList = new ArrayList< >();
Scanner fileReader;
fileReader = new Scanner(new ArrayListExercise().
getClass().getResourceAsStream("myWords.txt"));
Scanner keyboardReader = new Scanner(System.in);
String inFilePath, line, word;
int size = 0;
while (fileReader.hasNext()) {
line = fileReader.nextLine();
if (line == null) {
break;
}
aList.add(line);
} // while not end of file
System.out.print("\n\nPlease enter the word you want to search for: ");
word = keyboardReader.nextLine();
if (aList.indexOf(word) >= 0) {
System.out.println(word + " was found.\n\n");
} else {
System.out.println(word + " was not found.\n\n");
}
System.out.print("Please enter the word you want to remove: ");
word = keyboardReader.nextLine();
int removalCount = 0;
while (aList.remove(word)) {
removalCount++;
}
if (removalCount == 0) {
System.out.println(word + " was not found, so not removed.\n\n");
} else if (removalCount == 1) {
System.out.println("The only instance of " + word + " was removed.\n\n");
} else {
System.out.println("All " + removalCount + " instances of " + word
+ " were removed.\n\n");
}
System.out.print("Please enter the word you want to append: ");
word = keyboardReader.nextLine();
aList.add(word);
System.out.println(word + " was appended.\n\n");
System.out.print("Please enter the word you want to upper case: ");
word = keyboardReader.nextLine();
int position = aList.indexOf(word);
if (position >= 0) {
aList.set(position, word.toUpperCase());
System.out.println(word + " was converted to upper-case.\n\n");
} else {
System.out.println(word + " was not found, so not upper-cased.\n\n");
}
System.out.println("Here is the final version:\n" + aList);
} // method main
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.