CSC 230 UVic Elementary Data Structures and Algorithms Computer Science Task
Description
Unformatted Attachment Preview
Homework Two
Objectives:
To read text files
To allow a user to reenter a filename.
To search for a phrase in a text file
To use some of the methods in File class
To use the ArrayList and its methods
To use the Collections class to sort a list
Note: The textbook material for this assignment can come from chapters 1-12 & 17. Do not go
above the objectives.
Assignment:
Create a package called homeWork2. This is a simple program that has only the one main class.
The program will have two parts to it (but it is all in one program). This program has you reading
two files and writing to one file. You may choose whichever processes you wish to use to read
the two files, but they must be different. You may choose how you wish to write the file. The
files you will be reading will be text files. The file you will write to will be writing an array list.
The output shown on the screen and in the file must be readable.
The program must have error checking. In the main method, you will add a try-with-resources
exception. Feel free to use the Java exception catch and/or to write your own messages.
Part 1: Reading the file called bakebread.txt
1. In the first part of the program, under the programmer and program information, you will
ask a user to enter the name of the file to be opened. You will then test to see of the
filename that was entered by the user is valid. If it is invalid, you will let the user reenter
the filename. Once you have the correct name entered, you will be able to read the file.
The exists() method is most helpful.
2. The program will then print out the sentences that show the program information that will
be the size of the file in bytes and the absolute path to the file.
3. You will then be able to ask the user which word he or she wishes to find in the file. Your
program will then check to see if the word is in the file. If it is not, the program will say
so. If the file does contain the word, the program will say how many times the word
shows up in the file.
4. You will add a try to this part of the program before reading the contents of the file.
Although you do not need to explicitly close the bakebread.txt file due to the try with
resources, I would like you to do so, please. Under it, write the matching catch for the try
you have.
CSC 230
Homework Two
Page 1 of 12
The following comments are in my program and can help you to see the logic that this main
program needs. However, you do not have to follow this guideline. If your program is correct
and does what it is supposed to do, you may code it in any way you wish to as long as you do not
go above the objectives of the program.
// Reading the name of the file to open
// Reading the contents of the file
// Creating a File object
// Testing of the filename being correct
// Creating a Scanner object
// Print out file’s information
// Reading the word to be found from the user
// Counting the number of occurrences of the word
// Output to say if the word was found or not and if so, how many times
// An explicit close of the file
// The catch
Part 2 Reading the file called array.txt and writing to the file called arrayB.txt
1. In the second part of the program, you will create the ArrayList. You will then read the
array into the list. After the array has been read, you will then add 7 more names to the
list; 6 of these will be added to the end of the list (which is the default for the add()
method and the 7th name will be added as the first name in the list. You will then, using
the Collections class method, sort the array. Lastly, you will print the names to both the
screen and the file.
2. There will be two try blocks in this part of the program. One will be before creation of
the ArrayList and one will be before the reading the contents of the file. There will be
two catches at the end of this code, one to handle the file and one to handle the array
being out of bounds. Since you are using an ArrayList, this exception will not happen
(ArrayLists can be resized); I just want to see if you can add it.
The following comments will help you to see the logic that this main program needs. Again, feel
free to code this part as you wish to do so as long as you stay within the objectives.
// Create the ArrayList
// Reading the array via BufferedReader
// Adding more names to the list
// Sorting the array
// Calling the listIterator() constructor
// Printing the list to the screen
// Printing the list to file called arraysB.txt
// The catches
Note: How do you test an exception? Move the array.txt file so that the program cannot find it
and see what happens. Do not try moving the bakebread.txt file as your program is set up to keep
asking for the correct filename if it is not there.
CSC 230
Homework Two
Page 2 of 12
In the main class (Combination):
1. Above the class header, write a javadoc comment containing the integrity policy
statement. Under the statement, but still in the block, type your first and last name for the
signature, press Tab and type the date. You will not need to use underlining. You can see
the integrity policy statement by going to Canvas: Modules: Help Files> Integrity
Policy Statement.
2. Under the integrity statement, add comments on your program that show the title of the
program (homeWork2), your first and last name, and the course name (CSC 230, Sec #).
You may choose to use single line comments or a multiline docstring. Sec # will be your
section #.
3. As the first print lines for your program under the main method, make sure that the run
screen also prints the title of the program (homeWork2), your first and last name, and the
course name (CSC 230, Sec #), and the program information as shown in the sample run
screen.
To Submit this homework:
Go to your project folder, find the folder that is called homeWork2, and zip it.
Turn in, via the Assignments area: The homeWork2.zip file
Important Notes
1. If you do not submit the homeWork2.zip file that holds all of the directories and files
created when you create a new project, then there will be no way for us to review your
work, and sadly, your grade will be recorded as a zero.
a. The path to the homeWork2.java file will be homeWork2srchomework2.
b. The homeWork2 folder dropped down looks like this:
2. Your program must compile. Programs that do not compile will not be accepted and,
again, your grade will be recorded as a zero.
3. You can use any IDE you wish to use as long as your file can be opened, compiled, and
run in NetBeans. Use the tutoring lab to test that this can be done.
CSC 230
Homework Two
Page 3 of 12
SAMPLE RUN SCREEN #1
Christine Kikuchi
Homework 2
CSC 230, Sec #
This program has two parts.
The first part reads a text file.
The user will enter the file name.
The user is asked to enter a word in the file,
If the word is not in the file, the program will say that the word was not found.
If the word is found, the program will say how many times it is in the file.
The second part asks opens a file that holds names.
The names will be entered into an ArrayList.
The user will then be able to enter 6 more names into the list.
The list will then be sorted, and that sorted list will be printed to the screen.
This program uses exception handling.
Part 1: Working with a text file.
Enter a filename: dfgg
The file entered is an incorrect name.
Please reenter a file name: fggtyuu
The file entered is an incorrect name.
Please reenter a file name: bakebread
The file entered is an incorrect name.
Please reenter a file name: backbread.txt
The file entered is an incorrect name.
Please reenter a file name: bakebread.txt
I entered the filename incorrectly ! bunch of times.
Here it is finally correct.
Enter the word to be found: sugar
File information & the text
The file has 1419 bytes
The absolute path to the file is: D:ChristineCSC 230 Elementary Data Structures and
AlgorithmsProgramsAssignments Spring 2023homeWork2bakebread.txt
WHITE BREAD RECIPE
A white bread recipe that’s worth a blue ribbon!
6 1/2 to 7 cups all-purpose flour
2 packages “Instant Dry Yeast”
3 tablespoons sugar
1 tablespoon salt
1 1/4 cups water
1 cup milk
2 tablespoons shortening
CSC 230
Homework Two
Page 4 of 12
Oven 400
2 Loaves
In large mixer bowl, combine 3 cups flour, yeast, sugar and salt;
mix well. In saucepan, heat water, milk and shortening until warm
(120-130 degrees); shortening does not need to melt. Add to flour
mixture. Blend at low speed until moistened; beat 3 minutes at
medium speed. By hand, gradually stir in enough ramaining flour
to make a firm dough. Knead on floured surface until smooth and
elastic, 5 to 8 minutes. Place in greased bowl, turning to
grease top. Cover; allow to rise in warm place until light and
doubled, about 1 hour.
Punch down dough. Divide into 12 parts. On lightly floured surface
roll or pat each half to a 14 x 7 inch rectangle. Starting with
shorter side, roll up tightly, pressing dough into roll with each
turn. Pinch edges and ends to seal. Place in greased 9 x 5 or
8 x 4 inch bread pans. Cover; allow to rise in warm place until
double, about 30 minutes. Bake at 400 degrees for 35 to 40 minutes
until golden brown. Remove from pans and You know the rest!
Is the word found?
The file contains the word sugar.
The word sugar shows up 2 times.
Part 2: Working with a listarray and Buffered Reader.
The names are:
Amy
Anna
Cathy
Donna
Jenny
Jill
Kathy
Margo
Maria
Mary
Meredith
Sherry
Susan
Terry
Tina
CSC 230
Homework Two
Page 5 of 12
SAMPLE RUN SCREEN #2
Christine Kikuchi
Homework 2
CSC 230, Sec #
This program has two parts.
The first part reads a text file.
The user will enter the file name.
The user is asked to enter a word in the file,
If the word is not in the file, the program will say that the word was not found.
If the word is found, the program will say how many times it is in the file.
The second part asks opens a file that holds names.
The names will be entered into an ArrayList.
The user will then be able to enter 6 more names into the list.
The list will then be sorted, and that sorted list will be printed to the screen.
This program uses exception handling.
Part 1: Working with a text file.
Enter a filename: bakebread.txt
Enter the word to be found: cocoa
Entered a word that is not in the file
File information & the text
The file has 1419 bytes
The absolute path to the file is: D:ChristineCSC 230 Elementary Data Structures and
AlgorithmsProgramsAssignments Spring 2023homeWork2bakebread.txt
WHITE BREAD RECIPE
A white bread recipe that’s worth a blue ribbon!
6 1/2 to 7 cups all-purpose flour
2 packages “Instant Dry Yeast”
3 tablespoons sugar
1 tablespoon salt
1 1/4 cups water
1 cup milk
2 tablespoons shortening
Oven 400
2 Loaves
In large mixer bowl, combine 3 cups flour, yeast, sugar and salt;
mix well. In saucepan, heat water, milk and shortening until warm
(120-130 degrees); shortening does not need to melt. Add to flour
mixture. Blend at low speed until moistened; beat 3 minutes at
medium speed. By hand, gradually stir in enough ramaining flour
to make a firm dough. Knead on floured surface until smooth and
elastic, 5 to 8 minutes. Place in greased bowl, turning to
grease top. Cover; allow to rise in warm place until light and
CSC 230
Homework Two
Page 6 of 12
doubled, about 1 hour.
Punch down dough. Divide into 12 parts. On lightly floured surface
roll or pat each half to a 14 x 7 inch rectangle. Starting with
shorter side, roll up tightly, pressing dough into roll with each
turn. Pinch edges and ends to seal. Place in greased 9 x 5 or
8 x 4 inch bread pans. Cover; allow to rise in warm place until
double, about 30 minutes. Bake at 400 degrees for 35 to 40 minutes
until golden brown. Remove from pans and You know the rest!
Is the word found?
The file does not contain the word cocoa.
Part 2: Working with a listarray and Buffered Reader.
The names are:
Amy
Anna
Cathy
Donna
Jenny
Jill
Kathy
Margo
Maria
Mary
Meredith
Sherry
Susan
Terry
Tina
CSC 230
Homework Two
Page 7 of 12
SAMPLE RUN SCREEN #3
run:
Christine Kikuchi
Homework 2
CSC 230, Sec #
This program has two parts.
The first part reads a text file.
The user will enter the file name.
The user is asked to enter a word in the file,
If the word is not in the file, the program will say that the word was not found.
If the word is found, the program will say how many times it is in the file.
The second part asks opens a file that holds names.
The names will be entered into an ArrayList.
The user will then be able to enter 6 more names into the list.
The list will then be sorted, and that sorted list will be printed to the screen.
This program uses exception handling.
Part 1: Working with a text file.
Enter a filename: bakebread.txt
Enter the word to be found: and
File information & the text
The file has 1419 bytes
The absolute path to the file is: D:ChristineCSC 230 Elementary Data Structures and
AlgorithmsProgramsAssignments Spring 2023homeWork2bakebread.txt
WHITE BREAD RECIPE
A white bread recipe that’s worth a blue ribbon!
6 1/2 to 7 cups all-purpose flour
2 packages “Instant Dry Yeast”
3 tablespoons sugar
1 tablespoon salt
1 1/4 cups water
1 cup milk
2 tablespoons shortening
Oven 400
2 Loaves
In large mixer bowl, combine 3 cups flour, yeast, sugar and salt;
mix well. In saucepan, heat water, milk and shortening until warm
(120-130 degrees); shortening does not need to melt. Add to flour
mixture. Blend at low speed until moistened; beat 3 minutes at
medium speed. By hand, gradually stir in enough ramaining flour
to make a firm dough. Knead on floured surface until smooth and
elastic, 5 to 8 minutes. Place in greased bowl, turning to
CSC 230
Homework Two
Page 8 of 12
grease top. Cover; allow to rise in warm place until light and
doubled, about 1 hour.
Punch down dough. Divide into 12 parts. On lightly floured surface
roll or pat each half to a 14 x 7 inch rectangle. Starting with
shorter side, roll up tightly, pressing dough into roll with each
turn. Pinch edges and ends to seal. Place in greased 9 x 5 or
8 x 4 inch bread pans. Cover; allow to rise in warm place until
double, about 30 minutes. Bake at 400 degrees for 35 to 40 minutes
until golden brown. Remove from pans and You know the rest!
Is the word found?
The file contains the word and.
The word and shows up 7 times.
I moved the array.txt file so that it could not be found.
Part 2: Working with a listarray and Buffered Reader.
java.io.FileNotFoundException: array.txt (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.(FileInputStream.java:157)
at java.base/java.io.FileInputStream.(FileInputStream.java:111)
at java.base/java.io.FileReader.(FileReader.java:60)
at homework2.HomeWork2.main(HomeWork2.java:120)
array.txt (The system cannot find the file specified)
java.io.FileNotFoundException: array.txt (The system cannot find the file specified)
CSC 230
Homework Two
Page 9 of 12
Grading Rubric
Part 1
The class does not have the Javadoc comment
0
that is the Academic Integrity statement
The class starts with a Javadoc comment
that is the Academic Integrity statement
1
Under the Academic Integrity statement,
there are the comments that show the title
of the program, the programmer’s first
and last name, and the course name and
section #.
The class does not have the comments that
are the title of the program, the programmer’s
1
first and last name, and the course name and
section #.
0
The first print lines print the title of the
program, the programmer’s first and last
name, and the course name and section #,
and the program information as shown in
the sample run screen
The class does not have the print lines that
print the title of the program, the
programmer’s first and last name, and the
2
course name and section #, and the program
information as shown in the sample run
screen.
0
The main method incudes a try with
resources
1
The user is asked to enter the filename.
1 The user is not asked to enter the filename.
A loop lets the user reenter a file name
until it is correct
10
A loop does not let the user reenter a file name
until it is correct
0
A try clause is around the code for the file
being created and opened.
2
A try clause is not around the code for the file
being created and opened.
0
The user is asked to enter a word to
search for in the file.
1
The user is not asked to enter a word to search for
in the file.
0
The programs prints out file’s information
(the size in bytes and the absolute path) &
the text
The program does not print out file’s information
5 (the size in bytes and the absolute path) &/or the
text
0
The program checks to see if the word
searched for is found.
10
The program does not check to see if the word
searched for is found.
0
CSC 230
Homework Two
The main method does not include a try with
resources
Page 10 of 12
If the word is found, the program prints
out that the word was found and how
many times it was found and if the word
is not found, it prints out that the word
was not found.
If the word is found, the program does not print
out that the word was found and/or how many
4
times it was found and/or if the word is not found,
it does not print out that the word was not found.
0
The file is explicitly closed
1 The file is not explicitly closed
0
This part of the program ends with a
matching catch to the try.
3
This part of the program does not end with a
matching catch to the try.
A try clause is around the code for the list
being created and reading the array
2
A try clause is not around the code for the list
being created and reading the array
An ArrayList is created.
3 An ArrayList is not created.
0
The array is read into the ArrayList.
5 The array is read into the ArrayList.
0
Six names are read into the list at the end
of the list. (Sherry, Mary, Anna, Jenny,
Meredith, and Tina)
5
Six names are not read into the list at the end of
the list.
0
A seventh name is read into the list at the
beginning of the list. (Donna)
2
A seventh name is not read into the list or it is not
at the beginning of the list.
0
The array is sorted using the method from
the Collections class
4
The array is not sorted using the method from the
Collections class
0
The sorted list is printed to the screen
10 The sorted list is not printed to the screen
CSC 230
Homework Two
Part 2
0
0
Page 11 of 12
The sorted list is printed to the file called
arrayB.txt.
10
The file is explicitly closed
1 The file is not explicitly closed
This part of the program ends with a
matching catch to the try for the file
opening
3
This part of the program ends with a
matching catch to the try for the array being
out of bounds.
This part of the program does not end with a
3 matching catch to the try for the array being out
of bounds.
The run of the program to the screen looks
like the sample
10
CSC 230
The sorted list is not printed to the file called
arrayB.txt.
0
0
This part of the program does not end with a
matching catch to the try for the file opening
The run of the program to the screen does
not look like the sample
Homework Two
0
Page 12 of 12
WHITE BREAD RECIPE
A white bread recipe that’s worth a blue ribbon!
6 1/2 to 7 cups all-purpose flour
2 packages “Instant Dry Yeast”
3 tablespoons sugar
1 tablespoon salt
1 1/4 cups water
1 cup milk
2 tablespoons shortening
Oven 400 2 Loaves
In large mixer bowl, combine 3 cups flour, yeast, sugar and salt;
mix well. In saucepan, heat water, milk and shortening until warm
(120-130 degrees); shortening does not need to melt. Add to flour
mixture. Blend at low speed until moistened; beat 3 minutes at
medium speed. By hand, gradually stir in enough remaining flour
to make a firm dough. Knead on floured surface until smooth and
elastic, 5 to 8 minutes. Place in greased bowl, turning to
grease top. Cover; allow to rise in warm place until light and
doubled, about 1 hour.
Punch down dough. Divide into 12 parts. On lightly floured surface
roll or pat each half to a 14 x 7 inch rectangle. Starting with
shorter side, roll up tightly, pressing dough into roll with each
turn. Pinch edges and ends to seal. Place in greased 9 x 5 or
8 x 4 inch bread pans. Cover; allow to rise in warm place until
double, about 30 minutes. Bake at 400 degrees for 35 to 40 minutes
until golden brown. Remove from pans and You know the rest!
Jill
Margo
Amy
Terry
Susan
Maria
Cathy
Kathy
Purchase answer to see full
attachment

Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."