Starting from:
$30

$24

Data Structures and Algorithms Assignment #8 Solution

Assignment Description
This assignment provides the opportunity to work with a 2D array, array lists, queues, and iterators.  The layout of two solved crosswords is given in two files - letters and slots. For each crossword you will read the letters into the crossword layout (2D array) and print the crossword.

There are 2 files for each crossword (4 files total): 
    • Letter File – contains the list of letters for the crossword (listLetters.txt & queueLetters.txt)
    • Slot File – contains the array dimensions and location for each letter (listSlot.txt & queueSlot.txt)

When writing the code, first create the classes.  Write as much of the class code as possible so that you can create the necessary objects in your main.  This program contains the following two classes:
    • Slot
    • Crossword

Specifications
    1. Create a Java class called LastNameFirstNameAssignment8
    2. Follow "CS1450 Programming Assignments Policy"
    3. Write a test program (i.e. main) to do the following:
        a. Open all the files for reading
        b. Solve the 1st crossword puzzle using ArrayLists
            i. Create 2 ArrayLists
                • One to hold the letters.  I’m calling it letters. 
                • One to hold slot objects.  I’m calling it slots.

            ii. Fill array lists with the values in files
                • Array list: letters 
                    a. Read the letters in listLetters.txt into the letters array list.
                • Array list: slots 
                    a. Read 1st line in listSlots.txt – pair of integers
                        i. These 2 values are the dimensions for the crossword
                        ii. Save these values for use in step iii when creating the crossword
                    b. Read remaining lines in listSlots.txt - pairs of integers 
                        i. Create a Slot object for each pair of integers (row and column)
                        ii. Add the Slot object to the slots array list.

            iii. Create and fill the crossword 
                • Create a crossword object using the dimensions from step ii
                • Create 2 iterators - one for each ArrayList 
                    a. Iterators were discussed in lecture #9 in the Collections module
                • Put the letters into the appropriate slot in the crossword
                    a. Use the crossword’s enterLetters() method passing it the two iterators (one with the letters, one with the slots).

            iv. Print the completed crossword
                • Use the crossword’s printCrossword() method.

    c. Solve the 2nd crossword puzzle using Queues
    i. REPEAT steps (i – iv) using Queues instead of ArrayLists to hold letters and slots.
    ii. Read the letters from queueLetters.txt and the slots from queueSlots.txt

Test File Information
There is a total of 4 files – 2 files for each crossword: 
    • Letter Files  – each line contains a single character
    • Slot Files – the first line contains the size of the crossword (2D array), the remaining lines contain the location in the crossword for each letter in the Letters file.

Crossword #1 Files 

listLetters.txt
listSlots.txt
Meaning

12 12
The array (crossword) has 12 rows and 12 columns
A
3 4
Put the letter A in row 3, column 4
A
9 6
Put another A in row 9,vcolumn 6
B
4 11
Put a B in row 4, column 11
B
10 8
Put another B in row 10, column 8
etc.
etc.





Crossword #2 Files
    
queueLetters.txt
queueSlots.txt
Meaning

8 15
The array (crossword) has 8 rows and 15 columns

5 13
Put the letter ‘ (single quote) in row 5, column 13
A
5 3
Put the letter A in row 5,column 3
A
6 2
Put another A in row 6, column 2
B
0 1
Put the letter B in row 0, column 1
etc.
etc.





Note: The files used for grading will NOT be the same as the files that you are given. The array sizes and crosswords will all be different to ensure you did not make any assumptions. 

Classes
Slot Class
    • Description
        ◦ Represents the position of one slot (or square) in a crossword.

    • Private Data fields
        ◦ row – the row number of the slot
        ◦ column – the column number of the slot

    • Public Methods
        ◦ Constructor 
              public Slot (int row, int column) 
            ▪ Sets private fields row and column to incoming values

        ◦ Getters: for data fields row and column
        ◦ Setters: none

Crossword Class
    • Description
        ◦ Class that represents a crossword.

    • Private Data Fields
        ◦ array – 2D array of characters that contains the completed crossword
        ◦ numRows – integer for number of rows in the array
        ◦ numCols – integer for number of columns in the array
        ◦ EMPTY_CHARACTER – a constant for the character to put in the empty slots.  This is a constant, it will always be the underscore character: ‘_’

    • Public Methods
        ◦ Constructor 
public Crossword (int numRows, int numCols) 
            ▪ Sets private fields numRows/numCols to incoming numRows/numCols
            ▪ Creates a 2D array of size numRows by numCols
            ▪ Fills every slot of the array with the EMPTY_CHARACTER 

        ◦ Getters/Setters: None are needed because the 2D array is used only inside this class.

        ◦ public void enterLetters (Iterator<Character> letterIterator, Iterator<Slot>slotIterator) 
            ▪ This method will enter the letters into the slots in the crossword. 
            ▪ Fill 2D array with letters in letter iterator - letterIterator
                • Get a letter from the letter iterator
                • Get a slot object from the slot iterator
                • Get the row and column from the slot object
                • Place the letter in that specific row and column in the array
                • Repeat these steps until the iterators have reached end of array lists.
 
        ◦ public void printCrossword () 
            ▪ Print every slot in the crossword.
            ▪ Starting at row 0 and column 0, print all the slots in each row.
            ▪ Each row of letter should be printed on its own line. 


Must Do and Tips
Must Do
    • Use 2 ArrayLists to store the letters and slots for 1st crossword
        ◦ listLetters.txt and listSlots.txt

    • Use 2 Queues to store the letters and slots for 2nd crossword
        ◦ queueLetters.txt and queueSlots.txt


Output

For the 1st set of files (listLetters.txt and listSlots.txt):

***************
Crossword #1
***************

_ W _ _ _ _ _ _ _ _ _ _ 
_ O _ _ _ _ _ _ _ _ _ _ 
U N T I L _ _ _ _ _ _ _ 
_ T _ _ A N D _ _ _ _ _ 
_ _ _ _ P _ _ _ _ _ _ B 
_ _ _ _ T H I S _ _ _ R 
_ _ _ _ O _ _ _ _ _ _ O 
_ _ _ _ P _ _ _ _ F _ K 
_ _ _ _ S O M E T I M E 
_ _ _ _ _ _ A _ _ X _ _ 
_ _ _ _ _ M Y _ B E _ _ 
_ _ _ _ _ _ _ _ _ D _ _ 

The words in this crossword make this sentence:
MY LAPTOPS BROKE AND WONT BE FIXED UNTIL SOMETIME THIS MAY

For the 2nd set of files (queueLetters.txt and queueSlots.txt,):

***************
Crossword #2
***************

_ B U T _ _ _ _ _ G _ _ _ _ _ 
M Y _ U _ _ D O N E _ _ _ _ _ 
_ _ _ E _ _ U _ _ T _ N _ _ _ 
_ _ _ S O M E _ _ _ _ O _ _ _ 
_ _ _ D _ _ _ _ _ _ _ O _ I _ 
_ _ _ A S S I G N M E N T ' S 
_ W A Y _ _ T _ _ _ _ _ _ L _ 
_ _ _ _ _ _ _ _ _ _ _ _ _ L _ 

The words in this crossword make this sentence:
BUT MY ASSIGNMENT'S DUE BY TUESDAY NOON I'LL GET IT DONE SOME WAY

More products