Write the following 2 class programs – version one followed by version three. They are different because the layout of the text file has changed.
Version one has a single field on a single line (therefore no delimiter required.)
Version three has three fields on one line which is why we have to use a delimited to separate the fields.
Grade 12 The Octopus Assignment. Version One
In NetBeans create a new Project i.e. Grade12_2021. This will the only project you will create this year (besides your PAT). Into this project folder you will create new packages – each package being a new coding exercise. Reminder: a project folder is not part of Java; it is a device used by NetBeans to organise your work. This way all your coding work will be in one folder that you can move from one computer to another.
In Grade12_2021 create a new package i.e. anoctopus
Into this package create two new classes i.e. OctopusUI (with a main method) and OctopusManager (no main method)
Into the package folder (not scr) create a text file called “octopus.txt” with the following words one underneath the other (one per line) octopus, mermaid, frigate, island.
In OctopusManger create a constructor method that reads the words into a one-dimensional array using the Scanner class. In the main method you will need to instantiate a new OctopusManager object. Reminder: Because there is only one word per line you cannot break the single word up into tokens using a delimiter.
In OctopusManager create another method that reads the whole array into a single String. This method must return the String to the main class for printing. The main class must print the String to the monitor. Call this method from the main method.
Example . . .
octopus
mermaid
frigate
island
In OctopusManager create another method that also passes a single String back to the main method for printing to the monitor. This method must also iterate through the array and create a sentence around each word
Example . . .
Ahoy Captain, an octopus off the port bow
Ahoy Captain, a mermaid off the port bow
Ahoy Captain, a frigate off the port bow
Ahoy Captain, an island off the port bow
NOTE: Depending whether the word starts with a vowel or not the indefinite article “a” or “an” is used.
====================================================================
Grade 12 The Octopus Assignment. Version Three
In NetBeans create a new Project i.e. Grade12_2021. This will the only project you will create this year (besides your PAT). Into this project folder you will create new packages – each package being a new coding exercise. Reminder: a project folder is not part of Java; it is a device used by NetBeans to organise your work. This way all your coding work will be in one folder that you can move from one computer to another.
In Grade12_2021 create a new package i.e. anoctopus3
Into this package create two new classes i.e. OctopusUI (with a main method) and OctopusManager (no main method)
Into the package folder (not scr) create a text file called “octopus3.txt” with the following lines
mermaid#frigate#squall
cargo vessel#tanker#tug
container#life boats#debris
turtle#dolphin#penguin
Here each line has three separate fields which we need to read in separately. A special character called a delimiter separates each field from its neighbour. Note that the default delimiter is the space. Here we are using “#” instead.
For this project we are not going to store the words in the text file in an array. We are simply going to build the String and pass it back to the UI class for printing to the monitor
This is how we work with a delimiter . . . syntax has been left out and formatting has been ignored in the code snippet.
mermaid#frigade#squall – first, second and third respectively
1 String st = “”
2
3 try
4
5 Scanner scFile = new Scanner(newFile(“octopus3.txt”))
6
7 while (scFile.hasNext( ))
8 String line = scFile.nextLine( )
9 Scanner scLine = new Scanner(line).useDelimiter(“#”)
10
11 String first = scLine.next( )
12 String second = scLine.next( )
13 String third = scLine.next( )
14 st = st + first + second + third + “\n”
15 catch (FileNotFoundException ex)
16 JOptionPane.showMessageDialog(null, “File not found”)
17
18
In OctopusManger create a method that reads the words into a single, long, multi-line String which is passed back to the UI class for printing. Because the method returns a parameter you cannot use the constructor for this. NOTE: The only job of the constructor is to initialize the newly created child object.
Example . . .
Ahoy Captain, a mermaid, frigate and a squall off the port bow
Ahoy Captain, a cargo vessel, tanker and tug off the port bow
Ahoy Captain, a container, life boats and debris off the port bow
Ahoy Captain, a turtle, dolphin and penguin off the port bow
Your project Grade12_2021 has two packages each with a main method. You must tell NetBeans which one to use. You do this in the properties of the project i.e. Properties, Run. Browse and select the correct main class off the list.
Click here for the solution to version one.
Click here for the solutin to version three