Question: In this assignment, you need to write a Java program to use recursion and solve math puzzles like the following: Assume that variables T ,
In this assignment, you need to write a Java program to use recursion and solve math puzzles like the following:
"Assume that variables T H E B S P and O denote distinct numerical values from to inclusive If THE specifies the decimal representation of a threedigit integer, BEST and SPOT specify decimal representations of two four digit integers, and SPOTS specifies the decimal representation of a fivedigit integer, then, find the values of every variable and so that the following addition operation becomes true:
The main method of your program must call a static recursive method with the following signature:
public static void solveArrayListString operands, String result, int digitValues
The first parameter is a list of operands that are going to be added together to generate the second parameter String result The int digitValues is an array of integer initialized to new int and is supposed to store the values of each variable say if is then digitValuesT For the first call to this method, digitValues has cells, each are set to which represents an empty cell
How to Find the List of Variables
After the program receives a puzzle and stores it in a string say String puzzle we need to figure out the participating variables letters in the puzzle and store it in a list of characters. A suggestion is that you first define a static list of characters, like static ArrayList Character : and call the following method every time the user enters a puzzle:
Figure : Given String puzzle, the method finds the list of unique characters and store it in the list of variables, which is stored statically.
How to Convert String to Number
To convert strings like "THE", "BEST", "SPOT", and "SPOTS" to their equivalent integer values, you may use the following method:
Figure : Java implementation of method "numericalValue"
As you see in this figure, the first parameter is a word representing a number, second parameter, digitValues, is an array of size mapping each letter of the word to a digit from If a cell of digitValues is equal to it means that the mapping doesn't exist and method returns as a sign of error. For example, if digitValuesT it means that we haven't decided on the value of variable T yet. In this case, method returns
InputOutput of the Program
The program receives the puzzles from keyboard: "THEBESTSPOTSPOTS" followed by a new line. Please note that each line represents one puzzle. The output is the set of ALL mappings of letters to digits that make the equation true. For example, the output for the given input is:
T: H: E: B: S: P: O:
which makes the equation true: Assuming that numbers can have leading zeros ie T B and S can get value zero then we have the following set of answers:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
T: H: E: B: S: P: O:
Grading Criteria and Submissions
If you print all mappings of letters to digits that make the equation true, you'll receive the full credit If you can exclude the mappings that map multiple letters to the same digit, you will receive extra credit And finally, if you can exclude the mappings that assign zero to letters that are initial letters of the operands or the result, you will receive another extra credit You need to submit a zip file compressing all java files of the program. If you want to attempt the extra credit part, please submit a readme.txt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
