Reading in a text file into an array using the Scanner class and using OOP principals eg static variables and methods, creating objects, passing parameters. This program also uses String handling.
There are eight versions of this program. The first versions has no OOP while the last versions uses OOP throughout. The final version searches a string list instead of using multiple ORs.
Here is the text file
sloop
frigate
corvette
liner
icebreaker
outrigger
oil tanker
ketch
inflatable
albatross
whaler
armada
Here is the output. Note that if a word starts with a vowel “a” changes to “an”.
Ahoy Captain. There is a sloop off the port bow.
Ahoy Captain. There is a frigate off the port bow.
Ahoy Captain. There is a corvette off the port bow.
Ahoy Captain. There is a liner off the port bow.
Ahoy Captain. There is an icebreaker off the port bow.
Ahoy Captain. There is an outrigger off the port bow.
Ahoy Captain. There is an oil tanker off the port bow.
Ahoy Captain. There is a ketch off the port bow.
Ahoy Captain. There is an inflatable off the port bow.
Ahoy Captain. There is an albatross off the port bow.
Ahoy Captain. There is a whaler off the port bow.
Ahoy Captain. There is an armada off the port bow.
Version 2
Here the program is single class with only a main method. No OOP and no array.
Version 3
Here the program is single class but uses a single static method to read in the text file. No OOP and no array.
Version 4_5
Here the program is single class, uses static methods and static variable as well as an array for the words from the text file.
Version 6
OOP version one. This program has two classes i.e. a UI class with the main method and a class for the methods. The name of the text file is passed as a parameter.
Version 7
OOP version two. This program has two classes i.e. a UI class with the main method and a class for the methods. The name of the text file is passed as a parameter. The array is also passed between the UI class and the method class
Version 8
OOP version three. Here the confusing and messy if statement with multiple ORs has been replaced. Instead the vowels are searched for in a literal vowel string ie “aeiouAEIOU”.using indexOf (-1 if not found)