Need to write pseudocode for this. I have included my attempt after this code. p
ID: 3558325 • Letter: N
Question
Need to write pseudocode for this. I have included my attempt after this code.
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,%d ",
word, row, col, drow, dcol);
}
public static void main(String[] args) {
createScanner();
readBoard();
printBoard();
processWords();
}
}
My attempt at it:
public class WordSearch
private static ArrayList board = new ArrayList
private static Scanner scanner;
public static void readBoard
String line
while
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
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
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)
return;
System.out.print direction
word, row, col, drow, dcol
public static void main(String[] args)
createScanner
readBoard
printBoard
processWords
Explanation / Answer
public class WordSearch {
private static ArrayList board = new ArrayList()
private static Scanner scanner
public static void readBoard() {
while (true) {
line = input next line
if (line equals NULL)
exit from loop
Delete all spaces in line, convert to Uppercase and add to board } }
public static void printBoard() {
rows = getRows();
for (row=0 to rows)
print board.get(row); }
public static int getRows() {
return the number of rows }
public static int getCols() {
return the number of columns }
public static void createScanner() {
try {
scanner = new Scanner(new java.io.File("WordSearchInput.txt")); }
catch (Exception e) {
exit from program } }
public static void processWords() {
while (scanner has a next word) {
word = scanner.next();
findWord(word); }
public static void findWord(String word) {
rows = get the number of rows
cols = get the number of columns
for (row=0 to rows)
for (col=0 to cols)
findWord(word,row,col); }
public static void findWord(String word, int row, int col) {
for (drow=-1 to 1)
for (dcol=-1 to 1)
findWord(word,row,col,drow,dcol); }
public static void findWord(String word, int row, int col, int drow, int dcol) {
rows = get the number of rows
cols = get the number of columns
for (offset=0 to word.length) {
targetRow = row + offset*drow;
targetCol = col + offset*dcol;
if ((targetRow is less than 0) OR
(targetRow is greater than equal to rows) OR
(targetCol is less than 0) OR
(targetCol is greater than equal to cols))
return
boardChar = board.get(targetRow).charAt(targetCol);
wordChar = word.charAt(offset);
if (boardChar not equal to wordChar)
// mismatch, so we're done
return }
print (word, row, col, drow, dcol); }
public static void main(String[] args) {
createScanner();
readBoard();
printBoard();
processWords(); } }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.