Numerology

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

Jump to: navigation, search

{{1010CaseStudy

|ProblemName=Numerology

|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


|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 Method Procedure

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 you should approach this problem using [[ Separate methods are used when a protocol of steps or an algorithm will be used repetitively, or 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 protocol 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 protocol steps it is apparent that there are (at least) four appropriate code sections needed to properly implement the problem.

  • A main method to prompt the user for a name as well as call the other methods appropriately.
  • A method to convert each letter from the name into a corresponding number and calculate the running sum.
  • A method to calculate the collapsed digit from the total running sum.
  • A method 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. Documenting and commenting code may seem unnecessary as the current programs being written are very simple, small and the code is readable; this that is not the point. By applying documentation to all code, whether it be Microsoft Office or a simple numerology program the purpose is still the same; a quick understanding of each methods purpose, what it does and how it is used, as well as its parameters and possible return variable.


Creating the Code

After understanding how to approach the problem and what should be in each method, we are ready to start building each method of the program. For each method pseudo-code will be provided so an understanding of what has to be done can be grasped. The method signature (parameters and return type) will be provided for this example to avoid confusion and promote proper use in the future!

The Main Method

The main method is the engine of the program. Only try to use it to call other methods and create the structure and template for the entire program, not clutter it will little functionality details. 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.

public static void main (String[] args){
   
    String name = NULL; //variable to hold the name we are going to prompt the user for
    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)

    //sum = result from converting the name into a numeric value (method call)

    //collapse the sum one digit at a time until it is a potential destiny number
    while(!doneFindingSumDigits){
       //sumDigits = result from collapsing the sum by one digit (method call)
       //if (the sum is not a valid destiny number){
            sum = sumDigits; 
            sumDigits = 0;
       //}
         else{
            doneFindingSumDigits = true;
          }       
    }
    //print out the compassionate message associated with the destiny number and name (method call)

}

The Conversion Method

This method peals apart the string and convert each method to the running sum. After the entire name has been converted, return the converted value. 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.

public static int calculateString (String name){
    
    //String caseInsensitiveName = name converted to all caps
    int runningSum = 0; //the sum of the letters converted
    char ch = NULL;//the letter peeled off the name that is to be converted to a numeric value

    /*for(int i = 0; i < length of the name; i++){
           ch = caseInsensitiveName.charAt(i);
           if (ch = A or J or S){
               runningSum = runningSum + 1;//the value corresponding with the three conditional values
           }
           else if (ch = B or K or T){
               runningSum = runningSum + 2;
           }
            
           ... etc. for all of the tables conversion values ...

     }*/
     return runningSum;
  
}

Calculating the Destiny Number Method

This method 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.

public static int collapseInt(int currentValue){

    int sumDigits = 0;
    
    /*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)
    }*/
    return sumDigits;
}

Printing the Destiny Number Characteristics

This method is used to print the name and characteristics of the corresponding destiny number to standard output.

public static void printMessage (String name, int magicNumber){

    if(magicNumber == 1){
        //print name and message for (1) characteristics to standard output
    }
    else if(magicNumber == 2){
        //print name and message for (2) characteristics to standard output
    }

    //.... etc. complete for all numbers one through nine ...

}

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.

|SolutionCode=


|SideSectionTitle=Intro to Numerology |SideSection= KYLE PICTURE
}}