Numerology

From CompSciWiki
Jump to: navigation, search

Back to the Case Studies homepage

Problem

Write 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:

Numerology Calc.png


For each alphabetic character in the name, you will add a value to a running sum based upon the following table:

123456789
ABCDEFGHI
JKLMNOPQR
STUVWXYZ


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:

  1. John von Neumann
  2. Edsger Dijkstra
  3. one of your choosing
 

Intro to Numerology

Number2 casestudy.jpg

Solution

How to Approach the Problem

There 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:

  • What procedure needs to be followed to implement the program correctly?
-What type of variables are needed and what should they be initialized to?
  • Where should loops and conditional statements be used?
-Where does it make sense to use reserved words like if/else, for and while within the program?
  • When should the code be commented and what should it include?
-Should comments and documentation be performed before, during or after I have written the program?


Make sure your code complies with the COMP 1010 coding standards. Following proper coding standards allows for an easier understanding of your code in the future as well as the present. Remember markers for assignments as well as other programmers may have to modify or understand your code in the future. The more practice you have in proper documentation and code syntax, the easier it will be in the future to follow any required coding guidelines in the workplace.

Correct Solution Procedure

To 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:

  • A protocol of steps or an algorithm will be used repetitively
  • The code section serves a different purpose than the current method/code block


Remember, programmers are lazy and never want rewrite the same code, so methods are an ideal solution.
For this problem we will break the code up into sections which will make it easier to follow what is happening and why. This problem can be broken up as follows:

  1. Create an input dialog box and prompt the user for a name to be converted
  2. Calculate the running sum of the name by converting each character value into its corresponding numeric value
  3. Calculate the collapsed digit from the total of the running sum
  4. Using the collapsed digit, print the destiny number and associated characteristics to output


From the procedure of steps, it is apparent that there are (at least) four appropriate code sections needed to properly implement the problem.

  • A starting section to initialize variables and prompt the user for a name
  • A section to convert each letter from the name into a corresponding number and calculate the running sum
  • A section to calculate the collapsed digit from the total running sum
  • A finishing section to print the name's destiny number and associated characteristics

Consistent use of Loops and Conditional Statements

Loops 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:

  • For loops should be used anywhere the number of looping cycles is known
Eg: Converting each character of a name (where the length is known) to its corresponding digit
  • While loops should be used where the number of looping cycles is unknown and only stops when the condition(s) are met
Eg: Adding each digit of the collapsing sum (which is of an unknown length) to discover its destiny number
  • If/else and else if statements should be used where there is a number of potential options and only one is met depending on the variable's value
Eg: Depending on a characters value/letter, there is a corresponding numeric value

Proper Documentation

As 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 Sections

After 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 Input

The 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 Numbers

You 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 Number

With 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 Characteristics

Finally, 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 Solutions

Here 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 Solution

Remember, 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.


After you have completely finished writing and testing your code I recommend taking a glance at the sample solution. Comparing similarities and differences between your code will let you know what is a good practice from what is a bad habit.

Code

Solution Code

Back to the Case Studies homepage