Numerology
Back to the Case Studies homepage
ProblemWrite a complete Java program that will convert a name into an integer using the pseudo-mathematics (otherwise known as “malarkey”) of numerology. This is just an introduction on the use of numerology in field of Computer Science. Numerology is most commonly used in Computer Science for cryptography and to implement hashing functions. For good programming practices follow the COMP 1010 coding standards.
Your Java program will prompt a user for a name as follows:
For each alphabetic character in the name, you will add a value to a running sum based upon the following table:
For example, if the name entered is “Alan Turing”, the running sum would ultimately be equal to: 1 + 3 + 1 + 5 + 2 + 3 + 9 + 9 + 5 + 7 = 45 This sum needs to be “collapsed” by adding all its digits together. Thus 45 becomes 4 + 5 = 9. This collapsing process needs to continue until the final result is a single digit (obviously between 1 and 9). This single digit is known as the “destiny number” and is used to assign certain characteristics to the name as follows: 1 – is ambitious, independent, and self-sufficient 2 – is supportive, diplomatic, and analytical 3 – is enthusiastic, optimistic, and fun-loving 4 – is practical, traditional, and serious 5 – is adventurous, mercurial, and sensual 6 – is responsible, careful, and domestic 7 – is spiritual, eccentric, and a bit of a loner 8 – is money-oriented, decisive, and stern 9 – is multi-talented, compassionate, and global Assuming the input of “Alan Turing”, your program will generate output of: Alan Turing (9) is multi-talented, compassionate, and global Output need only be generated by System.out. Generate output from one run of your program where the name is:
|
Intro to Numerology | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SolutionHow to Approach the ProblemThere is a proper series of steps to follow before actually writing the code to create the conversion program. Thinking about each step in the problem and how it can be solved before writing the code will lead to cleaner, and more efficient code; not to mention that it follows proper coding standards. Some questions to think about for coming up with a solution to this specific problem are:
Correct Solution ProcedureTo correctly implement the procedure for this program there are a couple of things to think about. You do not want to have code scattered all over the program randomly to magically produce the correct output. Instead, you want to break the code up into smaller sections to easily distinguish what is happening and where. In a few lectures, if not already, you will learn that Java makes use of user-defined methods that set a template for coding in small simple sections. If you know how to use methods properly already, try approaching this problem using the Numerology with Methods Case Study. If not, you are in good company and you can write all the code in the main method for this exercise. Methods take a bit of practice and understanding on why they are used, but they are very effective and general good practice in the programming world. Methods are frequently used in programming languages when:
Consistent use of Loops and Conditional StatementsLoops and conditional statements should be used throughout the program as extensively as possible. Their use is essential to the general usability of any programming language. Without these statements it would be very difficult to perform some of the simple functionality we take advantage of them for. More specifically:
Proper DocumentationAs unpleasant as it may be, good code documentation is very important. Your comments should be written as you create the code so you can use them as a guideline for writing the section properly. Each method should include it's purpose, type of parameters and why they are needed, as well as the return value if there is one. Documentation may seem unnecessary as the current programs are very simple, small, and the code itself is readable. By applying documentation and comments to all code, whether it be Microsoft Office or a simple numerology program, the purpose is still the same; a quick understanding of each code block. Even working through long sections of code, anywhere the code is not clear as to its purpose, comment the code block so it is easy to follow and there is no confusion.
Creating the Code in SectionsAfter understanding how to approach the problem, you are ready to start building the program. As recommended in the Solution Procedure the program and explanation will be broken down into four sections. For each section pseudocode will be provided so an understanding of what has to be done can be grasped, without directly giving you the code. Where's the fun in that? Variable Initialization and User InputThe main method is the engine of the program. As proper procedure, you should always create and initalize variables at the beginning of a class. The problem states you will need to use JOptionPane to prompt the user for a name. Remember that this also means that the swing package import statement must be provided before the class, but within the file. import javax.swing.*;//import JOptionPane package public class numerologyProblem{ public static void main (String[] args){ //VARIABLE CREATION AND INITIALIZATION String name = NULL; //variable to hold the name we are going to prompt the user for String nameAllCaps = NULL; //name after it has been converted to all caps char ch = NULL;//the letter peeled off the name that is to be converted to a numeric value int sum = 0;//sum of the converted name int sumDigits = 0;//sum of the digits from the converted name boolean doneFindingSumDigits = false;//needed for a while loop to collapse the sum until we have the destiny number //name = result from the users input (using JOptionPane Prompt) Converting Characters Into Their Corresponding NumbersYou need to peel apart the name string and convert each character into a number to be added to the running sum. The use of a for loop makes sense to compare each character of the known length name with the chart of values to find its corresponding numerical value. For the conversion of a character to a numerical value you should be thinking of using conditional statements like if and else if for each letter in the alphabet. If the character matches the letter, the corresponding numerical value should be added onto the running sum. After the entire name has been converted, you have the total running sum value which will be collapsed to calculate the destiny number. To avoid extra work, before using any comparing conditional statements convert the name to either upper case or lower case letters so only half the conditional checks are needed. //nameAllCaps = (name converted to all caps) /*for(int i = 0; i < length of the name; i++){ ch = nameAllCaps.charAt(i); if (ch = A or J or S){ sum = sum + 1;//the value corresponding with the three conditional values } else if (ch = B or K or T){ sum = sum + 2; } //... etc. for all of the tables conversion values ... }*///end for loop Calculate the Destiny NumberWith an unknown valued (as well as unknown length) sum you need to calculate the destiny number. A recommended procedure to do this is to add the last digit from the total sum to a "sum of digits" variable, and then remove the last digit from the total sum. Repeat this process until the sum is a valid destiny number. To follow this procedure the use of the division operator as well as the remainder operator (mod) is required, so a good understanding of how they work is strongly advised. while(!doneFindingSumDigits){ /*while(currentValue is still a valid positive number){ sumDigits += the last digit from the integer (remainder operation) remove the last digit from currentValue (divisor operation) }*/ //if (the sum is not a valid destiny number){ sum = sumDigits; sumDigits = 0; //} else{ doneFindingSumDigits = true; } }//end while loop Print the Destiny Number and CharacteristicsFinally, you want to print the name and characteristics of the corresponding destiny number to standard output. After this procedure, the program requirements have been completed. Congratulations, you are almost done! Compile and run your program and see if your output matches the sample output below. if(sumDigits == 1){ //print name and message for (1) characteristics to standard output } else if(sumDigits == 2){ //print name and message for (2) characteristics to standard output } //.... etc. complete for all numbers one through nine ... }//end main method return 0; }//end class Output SolutionsHere are the correct answers to the output required from the problem description. Compare these with your outputs to see if you have generated code to solve the exercise. If your output does not match the given solutions and you can see visible discrepancies in the answers, then take a quick look at the sample solution code and see if there is a simple quick fix. 1. John von Neumann John von Neumann (9) is multi-talented, compassionate, and global. 2. Edsgter Dijkstra Edsgter Dijkstra (8) is money-oriented, decisive, and stern. 3. (of your choosing) Blaise Pascal Blaise Pascal (1) is ambitious, independent, and self-sufficient. Sample SolutionRemember, this is not the only "correct" coding solution to the problem. This is only one of many proper ways to implement the problem with your basic understanding of Java. There can be many different correct solutions based on how the vital points of the program were interpreted and coded.
| |||||||||||||||||||||||||||||||||||||
Code |