Explore BrainMass
Share

Explore BrainMass

    A Brief Explanation of Pseudo-Code

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    public class WordSearch {

    private static ArrayList<String> board = new ArrayList<String>();
    private static Scanner scanner;

    public static void readBoard() {
    String line;
    while (true) {
    line = scanner.nextLine();
    if (line.equals(""))

    break;
    board.add(line.replaceAll(" ","").toUpperCase());
    }
    }

    public static void printBoard() {
    int rows = getRows();
    for (int row=0; row<rows; row++)
    System.out.println(board.get(row));
    }

    public static int getRows() {
    return board.size();
    }

    public static int getCols() {
    return board.get(0).length();
    }

    public static void createScanner() {
    try {
    scanner = new Scanner(new java.io.File("WordSearchInput.txt"));
    } catch (Exception e) {
    System.out.println();
    System.exit(1);
    }
    }

    public static void processWords() {
    while (scanner.hasNext()) {
    String word = scanner.next();
    findWord(word);
    }
    }

    public static void findWord(String word) {
    int rows = getRows();
    int cols = getCols();
    for (int row=0; row<rows; row++)
    for (int col=0; col<cols; col++)
    findWord(word,row,col);
    }

    public static void findWord(String word, int row, int col) {
    for (int drow=-1; drow<=1; drow++)
    for (int dcol=-1; dcol<=1; dcol++)
    findWord(word,row,col,drow,dcol);
    }

    public static void findWord(String word, int row, int col, int drow, int dcol) {
    int rows = getRows();
    int cols = getCols();
    for (int offset=0; offset<word.length(); offset++) {
    int targetRow = row + offset*drow;
    int targetCol = col + offset*dcol;
    if ((targetRow < 0) ||
    (targetRow >= rows) ||
    (targetCol < 0) ||
    (targetCol >= cols))

    return;
    char boardChar = board.get(targetRow).charAt(targetCol);
    char wordChar = word.charAt(offset);
    if (boardChar != wordChar)
    // mismatch, so we're done
    return;
    }
    System.out.printf("%s at %d,%d direction %d,%dn",
    word, row, col, drow, dcol);
    }

    public static void main(String[] args) {
    createScanner();
    readBoard();

    © BrainMass Inc. brainmass.com October 10, 2019, 7:30 am ad1c9bdddf
    https://brainmass.com/computer-science/pseudocode/brief-explanation-pseudo-code-584058

    Attachments

    Solution Preview

    Hello,

    My name is Jim. I hope I will answer any questions you have.

    Pseudo-code is just an informal way of writing out the logic used to solve a problem, without getting involved in the details of any specific computer language.

    Here's an example of pseudo-code for the readBoard function:

    READ ALL LINES FROM THE FILE.
    WHILE READING THEM, ELIMINATE ALL CARRAIGE RETURNS, MAP TO UPPERCASE, AND ADD EACH LINE TO THE ...

    Solution Summary

    This brief answer will describe what pseudo-code is and convert Java code to it. 293 words.

    $2.19