Numerology

From CompSciWiki
Revision as of 14:02, 31 March 2011 by RileyP (Talk | contribs)

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

KYLE PICTURE

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. Some questions to think about for coming up with a solution to the program are:

  • What procedure needs to be followed to implement the program correctly?
-What type of variables are needed and what should they be initialized too?
  • Where should loops and conditional statements be used?
-Where does it make sense to use if/else if/else, for and while within the program??
  • When should the code be commented and what should it include?
-Should 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 and the future as well as the present. Remember markers 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 your company's required coding guidelines.

Correct Coding Procedure

To correctly code the program we have a couple of things to think about. Ideally with a bit more knowledge we would like to use methods to simplify this problem. As a rule of thumb, methods should generally not be longer then the screen width (40-50 lines). 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 for this exercise. Methods take a bit of understanding and knowledge of how to properly use them, but they are very effective and a general 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 then the current method.


Remember, programmers are lazy and never want to have to write duplicate code. After reading this particular problem we can break down the solution procedure into a couple of simple steps. The protocol for this problem can be broken up as follows:

  1. Create a 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 it's 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. There 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 exclusively:

  • 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 in unknown and only stops when condition is met.
Eg: Adding each digit of the collapsing sum (which is on an unknown length) to discover its destiny number.
  • If/else and else if statements should be used where there is a number of potential solutions and only one is met depending on the variables value.
Eg: Depending on a characters value/letter a numeric value is associated with it.

Proper Documentation

As unpleasing as it can be, good documentation of your code 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. Documenting and commenting code may seem unnecessary as the current programs being written are very simple, small and the code is readable; but this that is not the point. 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 sections purpose, what it does and how it is used, as well as its parameters and possible return variable.


Creating the Code in Sections

After understanding how to approach the problem where to break it into proper code blocks and sections, we are ready to start building the program. For each code block or section pseudo-code will be provided so an understanding of what has to be done can be grasped.

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. As the problem states we 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 outside of 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

This section peels apart the string and convert each character to the running sum. After the entire name has been converted we have the total running sum value which will be collapsed to calculate the destiny number. To avoid extra work, before using conditional statements convert all the letters to either upper case or lower case 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

This section is used to take an unknown valued (as well as unknown length) integer and calculate the destiny number by adding together each individual digit. Understanding of the division operator as well as the remainder operator (mod) is required to understand how this method works.


    
        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

This section is used to print the name and characteristics of the corresponding destiny number to standard output. After close the main method as we have completed the problem.


        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

Remember that this is not the only "correct" progress report. There can be many different progress reports based on how the vital points of the program were interpreted. Although, in this case study the calculations are exceptionally important points to the correct execution of the program.

Output Solutions

Here are answers to the output required from the problem description. Compare these with your solutions to see if you generate the correct output. If you are having notice any discrepancies your the answers, 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.

Code

Solution Code

Back to the Case Studies homepage