Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need assistance with the following java assignment. please include comments within your code, thank you! Here is the primary Searcher class which is

Hello, I need assistance with the following java assignment. please include comments within your code, thank you!

image text in transcribed

image text in transcribed

image text in transcribed

Here is the primary Searcher class which is to be ran with Letter.

import java.util.*; import java.io.*; class Searcher { // Read a line from the argument BufferedReader, and return it. // Return null if there is an exception. public static String doRead(BufferedReader rdr) { try{ return rdr.readLine(); } catch(Exception e) { return null; } } // Search the puzzle starting at first, and announce the result. public static void search(Letter first, String tofind) { // Direction names for printing. String[] dirs = { "E", "SE", "S", "SW", "W", "NW", "N", "NE" }; // Try each of the puzzle positions. We do this with two // fingers. The rowscan moves across each row, and rowstart // moves down the left side of the puzzle, to provide the starting // position for each row. int row = 0; int col = 0; Letter rowstart = first; Letter rowscan = rowstart; int direction = Letter.NODIR; while(direction == Letter.NODIR && rowstart != null) { // See if the word is at the current location. direction = rowscan.matches(tofind); if(direction == Letter.NODIR) { // If we didnt' find it, move to the next square // in the scan. Reading order: right one step, until // the end of the row, then to the start of the next. // Move the row scanner to the right. rowscan = rowscan.getNeighbor(Letter.E); ++col; if(rowscan == null) { // If that produced null, we're at the row end. Start on // the next row. rowstart = rowstart.getNeighbor(Letter.S); rowscan = rowstart; ++row; col = 0; } } } // Announce result. if(direction == Letter.NODIR) { System.out.println("Word " + tofind + " not found."); } else { System.out.println("Word " + tofind + " found at row " + row + ", column " + col + ", heading " + dirs[direction]); } } public static void main(String[] args) { // Prompt for the name of the puzzle file, and open the file. Scanner in = new Scanner(System.in); System.out.print("What file contains the puzzle? "); String fn; fn = in.nextLine(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(fn)); } catch(Exception e) { System.out.println("Cannot open file: " + e); return; } System.out.println(); // This holds the first letter in the puzzle, upper left corner. Letter first = null; // Read the puzzle file and fill in the puzzle list. Vector lastrow = null; // Previous row (null at first) Vector thisrow = // Row under construction. new Vector(); String line; // One line of file input. int lineno = 0; // Line number, for printing. while ((line = doRead(reader)) != null) { // Force the line to upper case. line = line.toUpperCase(); // Print the line number. System.out.printf("%3d ", lineno); for(int i = 0; i 0) { thisrow.lastElement().setNeighbor(Letter.E, let); } // Connect to the letters in the previous row (NW, N, NE). if(lastrow != null) { // This loop goes through the positions of items in the // previous row relative to position. That is, let is // in position position in its row, and connects to // position-1, position and position+1 in the row above. for(int j = -1; j = 0 && last_row_pos (); ++lineno; } System.out.println(); // This prints the column numbers at the bottom. Assumes that // there are no more than 99 columns. int rowsize = lastrow.size(); if(rowsize >= 10) { // If there are more than 10 columns, we print the digits // one or larger. First space over so they line up correctly. System.out.print(" "); for(int i = 10; i Word Search This lab involves solving a word search puzzle, but in a perhaps unusual way Suppose we want to search the following puzzle CART LEVE We could be boring and use a conventional two-dimensional array to store the puzzle But suppose we decide on something interesting: A grid of | P objects, one for each letter. Then, we would represent the puzzle something like the figure on the right. Each object LS knows its letter, and keeps a record of its neighbor objects Now, since each letter has become an object, it can do things for us. In particular, we can ask any letter if it is the start of a word. For instance, this puzzle contains the word CART starting at the left on the second row. Instead of going to the trouble of scanning through the puzzle, we might just ask the node second down on the left (the one holding C), "Excuse me, sir, are you at the start of the word CART extending to the east?" And it would answer politely "why, yes!". Then we could search for a word by simply asking each letter about each direction until the word is found Since this is such a cool idea, we're going to try it. I'll write the main program, and you write the class that holds each letter. The Letter Class You must create a class to represent a letter in the puzzle. It needs to have the interface given here. Note that the main program which you download will read the puzzle, build the grid, and run searches against it. All you have to do is create the class Letter, as described here. It must have these public methods and constants Constant:s Your class needs public constants to represent the eight directions, and one more for the null direction. The directions are a way to specify which neighbor we're talking about. The required constants and their required values are these public static final int E 0; public static final int SE 1; public static final int s = 2; public static final int SW -3; public static final int W 4; public static final int NW -5; public static final int N 6; public static final int NE -7 public static final int NOD1R -1; Letter (char let) The constructor takes a character, which is the letter the object represents void setNeighbor(int dir, Letter let) This makes two Letter objects neighbors. The object let becomes the dir neighbor of the object that runs the method. Dir is one of the eight (valid) direction constants above. For instance, suppose p is the object holding p in the upper left corner of the example puzzle above, and objects next to it are m, c and a, likewise holding M, c and A. Then the neighbor settings for p could be created by p.setNeighbor (Letter.E, m), p.setNeighbor (Letter.SE, a), and p.setNeighbor (Letter.s, c) Since this makes both nodes neighbors of each other, the exact same relation is established by calling either p.setNeighbor (Letter.SE, a) or a.setNeighbor (Letter.NW, p) Letter getNeighbor (int dir) Return the neighbor in the specified direction, or null if there is no neighbor there After x.setNeighbor(Letter.SE, y), then x.getNeighbor(Letter.SE) will return y, and y.getNeighbor (Letter.Nw) will return x. If there have been no other setNeighbor calls on x, then .getNeighbor (Letter.N) (or any direction other than southeast) wll return null boolean matches (String tofind, int dir) This returns true if the puzzle contains the word given by tofind starting with with the object it is called on, extending in the given direction, dir. The dir is one of the valid directions. For instance, under the assumptions made for the setNeighbor examples, p.matches("PAVE", Letter.SE) will return true, p.matches("PAVE", Letter.s l return false, as will p.matches("PAVE", Letter.N). Likewise, m.matches ("MAP, Letter.s) produces true, but m.matches ("MAPI", Letter.s) produces false a.matches("ARKS", Letter.E) is false as well int matches (String tofind) If the string tofind matches starting in any direction, that direction is returned. If not, return NoDIR. If it happens that tofind matches in more than one direction, the method may return any of them. How To Make Letter The above section describes how your class Letter should behave. This gives some hints how you might create the class 1. Create Letter.java. Make a class by the same name, and copy the constant declarations into it. 2. Add a private char variable to hold the character that the object represents, and have the constructor save that value there 3. The object must keep track of its neighbor in each direction so it can talk to it when needed. You could create eight private variables (type Letter) to store each neighbor object, but I think you'll be much happier if you create an array of Letter objects, subscripted by direction. Suppose you declare it as Letter neighbors Then, neighbors [Nw] would contain the neighbor to the northwest, or null if there is no neighbor there. Make sure to create this array with 8 positions, all null initially, of course. (The nodes are made neighbors after they are created.) 4. Create a private method rev, which takes an integer representing one of the eight directions and returns the integer for the opposite direction. For instance, p.rev(N) returns s, or p.rev (sw) returns NE. To compute this, you could use a switch or bank of ifs, or you could use modular arithmetic to go four steps around an eight-step circle 5. Create the setNeighbor method. As described, a call like p.setNeighbor (E,q) makes p and q neighbors, with q east of p. and p west of q. This means that each of p and q must be linked to the other, so you need to set your own neighbors[E to q, and set q's neighbors [W) to p. Since you are p, your name will be this 6. Create getNeighbor. It's straightforward. 7. Create the first matches method, the one with two arguments. There is a very simple way to do it: a. Check if the first character of tofind is the character you are holding. If not, return false. If so, you match the first letter b. Check the length of the tofind. If it has only one character, then you've matched the whole thing. Return true. Otherwise, you need to check the rest of tofind c. Look for the neighbor in the specified dir. If you don't have any neighbor in that direction, the string ran off the edge of the puzzle and didn't match. Return false d. Otherwise, ask that neighbor if it matches, in the same direction, the remainder of tofind after removing the first character (because you already matched that) Return whatever it tells you For instance, back to the pictured example, for the p in the upper left corner, if you have to process p.matches( "PAVE, SE), first check that you are holding "P. Since you are, find if you have a SE neighbor. You do, the Letter a. So ask a.matches "AVE", SE). Since it returns true, you return true. If you are asked about c.matches( "DATA", E), you will return false immediately, since you don't hold D. If asked about m.matches("MADE", s), you will notice that the M matches, then ask a.matches"ADE", S), which will return false, so you wll too. Don't even think about writing a loop for this method. 8. Create the second matches. Just loop through the eight directions and call the first matches each time. If any direction matches, return that direction. If none does return NODIR. You should call the first matches and let it talk to your neighbors for you. Don't do it yourself Feel free to create any private methods or additional private data which might be Word Search This lab involves solving a word search puzzle, but in a perhaps unusual way Suppose we want to search the following puzzle CART LEVE We could be boring and use a conventional two-dimensional array to store the puzzle But suppose we decide on something interesting: A grid of | P objects, one for each letter. Then, we would represent the puzzle something like the figure on the right. Each object LS knows its letter, and keeps a record of its neighbor objects Now, since each letter has become an object, it can do things for us. In particular, we can ask any letter if it is the start of a word. For instance, this puzzle contains the word CART starting at the left on the second row. Instead of going to the trouble of scanning through the puzzle, we might just ask the node second down on the left (the one holding C), "Excuse me, sir, are you at the start of the word CART extending to the east?" And it would answer politely "why, yes!". Then we could search for a word by simply asking each letter about each direction until the word is found Since this is such a cool idea, we're going to try it. I'll write the main program, and you write the class that holds each letter. The Letter Class You must create a class to represent a letter in the puzzle. It needs to have the interface given here. Note that the main program which you download will read the puzzle, build the grid, and run searches against it. All you have to do is create the class Letter, as described here. It must have these public methods and constants Constant:s Your class needs public constants to represent the eight directions, and one more for the null direction. The directions are a way to specify which neighbor we're talking about. The required constants and their required values are these public static final int E 0; public static final int SE 1; public static final int s = 2; public static final int SW -3; public static final int W 4; public static final int NW -5; public static final int N 6; public static final int NE -7 public static final int NOD1R -1; Letter (char let) The constructor takes a character, which is the letter the object represents void setNeighbor(int dir, Letter let) This makes two Letter objects neighbors. The object let becomes the dir neighbor of the object that runs the method. Dir is one of the eight (valid) direction constants above. For instance, suppose p is the object holding p in the upper left corner of the example puzzle above, and objects next to it are m, c and a, likewise holding M, c and A. Then the neighbor settings for p could be created by p.setNeighbor (Letter.E, m), p.setNeighbor (Letter.SE, a), and p.setNeighbor (Letter.s, c) Since this makes both nodes neighbors of each other, the exact same relation is established by calling either p.setNeighbor (Letter.SE, a) or a.setNeighbor (Letter.NW, p) Letter getNeighbor (int dir) Return the neighbor in the specified direction, or null if there is no neighbor there After x.setNeighbor(Letter.SE, y), then x.getNeighbor(Letter.SE) will return y, and y.getNeighbor (Letter.Nw) will return x. If there have been no other setNeighbor calls on x, then .getNeighbor (Letter.N) (or any direction other than southeast) wll return null boolean matches (String tofind, int dir) This returns true if the puzzle contains the word given by tofind starting with with the object it is called on, extending in the given direction, dir. The dir is one of the valid directions. For instance, under the assumptions made for the setNeighbor examples, p.matches("PAVE", Letter.SE) will return true, p.matches("PAVE", Letter.s l return false, as will p.matches("PAVE", Letter.N). Likewise, m.matches ("MAP, Letter.s) produces true, but m.matches ("MAPI", Letter.s) produces false a.matches("ARKS", Letter.E) is false as well int matches (String tofind) If the string tofind matches starting in any direction, that direction is returned. If not, return NoDIR. If it happens that tofind matches in more than one direction, the method may return any of them. How To Make Letter The above section describes how your class Letter should behave. This gives some hints how you might create the class 1. Create Letter.java. Make a class by the same name, and copy the constant declarations into it. 2. Add a private char variable to hold the character that the object represents, and have the constructor save that value there 3. The object must keep track of its neighbor in each direction so it can talk to it when needed. You could create eight private variables (type Letter) to store each neighbor object, but I think you'll be much happier if you create an array of Letter objects, subscripted by direction. Suppose you declare it as Letter neighbors Then, neighbors [Nw] would contain the neighbor to the northwest, or null if there is no neighbor there. Make sure to create this array with 8 positions, all null initially, of course. (The nodes are made neighbors after they are created.) 4. Create a private method rev, which takes an integer representing one of the eight directions and returns the integer for the opposite direction. For instance, p.rev(N) returns s, or p.rev (sw) returns NE. To compute this, you could use a switch or bank of ifs, or you could use modular arithmetic to go four steps around an eight-step circle 5. Create the setNeighbor method. As described, a call like p.setNeighbor (E,q) makes p and q neighbors, with q east of p. and p west of q. This means that each of p and q must be linked to the other, so you need to set your own neighbors[E to q, and set q's neighbors [W) to p. Since you are p, your name will be this 6. Create getNeighbor. It's straightforward. 7. Create the first matches method, the one with two arguments. There is a very simple way to do it: a. Check if the first character of tofind is the character you are holding. If not, return false. If so, you match the first letter b. Check the length of the tofind. If it has only one character, then you've matched the whole thing. Return true. Otherwise, you need to check the rest of tofind c. Look for the neighbor in the specified dir. If you don't have any neighbor in that direction, the string ran off the edge of the puzzle and didn't match. Return false d. Otherwise, ask that neighbor if it matches, in the same direction, the remainder of tofind after removing the first character (because you already matched that) Return whatever it tells you For instance, back to the pictured example, for the p in the upper left corner, if you have to process p.matches( "PAVE, SE), first check that you are holding "P. Since you are, find if you have a SE neighbor. You do, the Letter a. So ask a.matches "AVE", SE). Since it returns true, you return true. If you are asked about c.matches( "DATA", E), you will return false immediately, since you don't hold D. If asked about m.matches("MADE", s), you will notice that the M matches, then ask a.matches"ADE", S), which will return false, so you wll too. Don't even think about writing a loop for this method. 8. Create the second matches. Just loop through the eight directions and call the first matches each time. If any direction matches, return that direction. If none does return NODIR. You should call the first matches and let it talk to your neighbors for you. Don't do it yourself Feel free to create any private methods or additional private data which might be

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions