Software Design III (CS 340), Spring 2019 Assignment 01 (40 points)
Due by 5:00 PM, Friday, 15 February 2019
1 Word-Search Puzzle (20 points)
For this assignment, you will be using the provided WordPuzzle class. This class is almost entirely complete. When its main() method is executed, it will open up a window, featuring a single button; when that button is pressed a file-chooser dialogue window will open, allowing the user to select a text-file. Once a file is chosen, the program calls the solve() method, with the file as input.
Your job is to implement a new class, called WordPuzzleSolver. This new class must implement the provided interface, PuzzleSolver, which means:
• There will be a public method, readFile(), that takes in a file as input. This method must be called so that the implementing class knows which file it will use to read in the data about the puzzle it needs to solve. (It will not solve the puzzle yet, that will come later). Exactly how this method works is up to you.
• There will be a public method, solvePuzzle(), that solves the puzzle contained in the file that was sent as input to the previous method. Thus, this method will only work if the previous method has already been called, with a proper puzzle file as input (the format of puzzle files, and of correct output, is described below).
• Back in the WordPuzzle class, you will complete the solve() method so that when called it creates an instance of your new solver class, sends in the input file, and calls the two methods just described to solve the puzzle and print results.
A puzzle file is simply a text-file. The first line consists of two positive integers, giving the number of rows and number of columns, respectively, of a word-search puzzle. Following that will be a block of text of the given size (you can assume that the input is in the right format). After that block of text, there will be one or more words, each on a single line. As an example, the following specifies a (5 × 6) puzzle grid, along with 4 words for which to search:
56 turtle waterb elofem evodek pckrse turtle dove packers fee
1
Your program will read in the grid of letters (using any data-structures you choose). It will then proceed through the list of words, one at a time, in order. For each word, it will search the grid for that word, looking up, down, left, right, and diagonally. Words can run in any of the 8 possible directions, and cannot skip any letters, or run off the sides of the grid. For each word that can be found, the program will report that fact, using the exact format:
“
where the row-number and column-number are given as the starting position (first character) of the word, counting from the top of the grid of characters, left-to-right, and starting at 0, as in an array or String. The direction reported will be one of the following, depending upon what direction the word can be found: U, D, R, L, UR, UL, DR, DL (up, down, left, right, up-right, up-left, down-right, down-left). You can assume that each word occurs at most one time in the grid, so that if the word can be found, the output will be unique. When a word is not found anywhere in the grid, that will also be reported, using the exact format:
“
For the example puzzle shown on the previous page, then, the correct output would be as follows:
“turtle” was found at row 0, column 0, going R.
“dove” was found at row 3, column 3, going L.
“packers” was not found.
“fee” was found at row 2, column 3, going DR.
Note 01: the assignment download includes two sample word-puzzle files, along with a file that shows the correct output for each input file. For full points, your code should produce exactly the same output for these sample files.
Note 02: your code should be able to handle any input file that it is given, so long as that file is in the basic format described. When grading the work, I will be testing your code using files of my own. If you want to make more puzzle files for de-bugging and testing purposes, feel free to do so. You can create any such files you need using a simple text editor.
2
2 Assembly Language Simulator (20 points)
For this part of the assignment, you will be creating a program that simulates a very simple computing system. Your program will read in files consisting of code in a simple version of assembly language (that is, a simple set of basic instructions to follow); it will then simulate execution of that code, printing out the results at the end.
You should begin by creating a new class, called Assembler. This code should do the same thing as the WordPuzzle class for the first part of the assignment. That is, it should create a window with a button and label, and allow the user to choose text-files to open (feel free to cut and paste from the other class, making whatever changes needed). Once your code opens up a file chosen by the user, it should send that file for processing by another class (with whatever name you choose). That other class is where you will implement the simulation of the assembly language processor.
The system you are simulating has the following features:
1. It has two registers, A and B, each of which is a memory location that is capable of storing a non-negative integer value (that is, a number n such that 0 ≤ n ≤ Integer.MAX_VALUE). At the start of the execution of any program, each register starts off at zero (0).
2. A program is read from a text-file. Each line consists of one of the following instructions:
• ADD: this command adds a number to one of the registers; it takes the name of the register, along with an integer value to add, as arguments. For example, the command ADD A 3 adds 3 to the value currently stored in register A; the command ADD B -2 subtracts 2 from the value of register B (by adding −2). Remember that registers can only handle positive integer values; thus, any subtraction that would make the register value negative in fact only makes it hold the value 0. Similarly, any addition that would make the value too large has the effect that the register holds maximal value (Integer.MAX_VALUE).
• INC: this command increments the register that is named as an argument. For instance, the command INC A increments register A. The command behaves as if it were identical to ADD A 1, which means that if register A is already at the maximum possible value, executing INC A has no effect.
• JIG: this command executes a conditional jump in the code by comparing the two regis- ters; it takes the names of the two registers, in some order, along with an integer value, as arguments. When executed, it checks whether the value in the first named register is greater than or equal to the value of the second named register, and if so moves forwards or backwards by the number of lines given as the last argument. Thus, the command JIG A B 2 checks if the value of register A ≥ the value of register B, and if so moves 2 lines forward. The command JIG B A -1 checks if the value of B ≥ the value of A, and if so moves 1 line back in the code.
• SET: this command sets a named register to a given integer value. Thus, the command SET A 4 sets the value of register A to 4. You can assume that the integer value given is always in the possible range for any register.
3. Unless the program executes a conditional jump forward or back, it proceeds in an imperative sequence, one line at a time. Execution terminates either when the program reaches the end of the file, or when it executes a jump that causes it to move to a position that is outside of
3
the bounds of the program. That is, if we are at the second line of a 3-line program, then a jump forward or backwards 2 or more lines will cause the program to terminate.
As an example, consider the following program:
INC A
JIG A B 2
INC B
INC A
When this code executes, it starts with both registers set to 0, as always. The first line then increments register A to 1. At the second line, it checks whether the value of A ≥ the value of B, and since it now is, it jumps forward 2 lines, to the last line of code. That line then increments register A once again, to value 2, and the program terminates.
Your program will simulate this process. When the program terminates, it will produce a single line of output, giving the final values of the two registers, in the exact format:
RESULT => A:
That is, for the example program shown above, the output will be:
RESULT => A: 2, B: 0.
Note: as before, your code must handle any program in the given format, and will be tested on files beyond the two that are included with the assignment download. You can assume that every program will eventually terminate in a finite number of steps.
Handing in your work: you will hand in your code via D2L. Your work should consist of a single compressed folder containing all of your source code, for both parts of the assignment. Files should be named as specified in this document, where required. The class website contains more information about preparing compressed documents if you need a refresher on that.
Coding conventions: Each part of the program has a points total, given above. For full points, you should complete all those components, and observe the basic coding conventions as follows:
• Comments should appear at the start of any code-file you write or change, with your name, the date of your work, and a short explanation of what the code does.
• Each method should be preceded by a blank line, followed by at least one line of comments explaining what the method does.
• Methods should be private if possible, and public only if necessary.
• Class variables should be local to methods if possible, and should only appear as global
variables if they appear in more than one method of the class.
• Unless absolutely necessary, global instance variables should be private.
4
• Code can be broken up by blank lines, but keep this to a low level. There should be no more than a single blank line separating pieces of code. Within a line of code, white space should not be too wide, for clarity.
• Code should be properly indented and separated into lines.
• Standard naming conventions (capitalized class names, lower-case method/variable names, etc.) should be followed.
5