Question
Write a Java program that recursively processes a two-dimensional array of characters and replaces all of the characters of the same value in any specified
Write a Java program that recursively processes a two-dimensional array of characters and replaces all of the characters of the same value in any specified region with another specified character. Specifically, your program should prompt the user for the name of a text file, then read the data stored in that file. The file should contain (on its first two lines) the number of rows and columns of a collection of characters which are stored in subsequent lines of the text file (one row per line). The program should then (as many times as the user desires) prompt the user for a row and column location, for a neighborhood method, and for a (new) character (by ASCII/Unicode value) and recursively locate and replace all characters that are the same as the original character, beginning at the specified location and continuing until all characters having the same value as the original and in the same region (and only in that region) have been converted to the new character. Note that a region is defined as a collection of identical characters bounded by characters of different value (or by the limits of the array). The allowable neighborhoods are either a 4-neighborhood (i.e., N S E W), or an 8-neighborhood. Use TextFileClass to get information from the text file. Remember that valid row and column values begin at 0.
TextFileClass:
class TextFileClass { String fileName; static final int MAX_TEXT_ROWS = 2000; KeyboardInputClass keyboardInput = new KeyboardInputClass(); TextFileClass TextFileClass() { text = new String[MAX_TEXT_ROWS]; lineCount = 0; } TextFileClass TextFileClass(int maxTextRows) { if (maxTextRows > 0) text = new String[maxTextRows]; lineCount = 0; } KeyboardInputClass public void getFileName(String prompt) { fileName=keyboardInput.getKeyboardInput(prompt); return; }
getFileContents public int getFileContents() { String s; int maxRows = MAX_TEXT_ROWS; int passStart = 2; if (text == null) { passStart = 1; maxRows = 50000000; } for (int pass = passStart; pass <= 2; pass++) { lineCount = 0; try { FileReader reader = new FileReader(fileName); BufferedReader buffer = new BufferedReader(reader); s = buffer.readLine(); while ((s != null) && (lineCount < maxRows)) { if (pass == 2) text[lineCount] = s; lineCount++; s = buffer.readLine(); } reader.close(); } catch (Exception e) { keyboardInput.getKeyboardInput("Problem trying to access or read file. Press ENTER to continue..."); pass = 3; } if(pass==1) text = new String[lineCount]; } return lineCount; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started