Difference between revisions of "Numerology with Methods"

From CompSciWiki
Jump to: navigation, search
m
m
Line 43: Line 43:
 
{
 
{
 
     // calculate sum from name
 
     // calculate sum from name
public static int calculateString(String origName)
+
    public static int calculateString(String origName)
 +
    {
 +
        String upperName = origName.toUpperCase();
 +
int sum = 0;
 +
char curr = ' ';
 +
 
 +
for (int r = 0; r < upperName.length(); r++)
 
{
 
{
String upperName = origName.toUpperCase();
+
        curr = upperName.charAt(r);
int sum = 0;
+
char curr = ' ';
+
  
for (int r = 0; r < upperName.length(); r++)
+
    if (curr == 'A' || curr == 'J' || curr == 'S')
{
+
sum += 1;
curr = upperName.charAt(r);
+
    else if (curr == 'B' || curr == 'K' || curr == 'T')
 
+
sum += 2;
if (curr == 'A' || curr == 'J' || curr == 'S')
+
    else if (curr == 'C' || curr == 'L' || curr == 'U')
sum += 1;
+
sum += 3;
else if (curr == 'B' || curr == 'K' || curr == 'T')
+
    else if (curr == 'D' || curr == 'M' || curr == 'V')
sum += 2;
+
sum += 4;
else if (curr == 'C' || curr == 'L' || curr == 'U')
+
    else if (curr == 'E' || curr == 'N' || curr == 'W')
sum += 3;
+
sum += 5;
else if (curr == 'D' || curr == 'M' || curr == 'V')
+
    else if (curr == 'F' || curr == 'O' || curr == 'X')
sum += 4;
+
sum += 6;
else if (curr == 'E' || curr == 'N' || curr == 'W')
+
    else if (curr == 'G' || curr == 'P' || curr == 'Y')
sum += 5;
+
sum += 7;
else if (curr == 'F' || curr == 'O' || curr == 'X')
+
    else if (curr == 'H' || curr == 'Q' || curr == 'Z')
sum += 6;
+
sum += 8;
else if (curr == 'G' || curr == 'P' || curr == 'Y')
+
    else if (curr == 'I' || curr == 'R')
sum += 7;
+
sum += 9;
else if (curr == 'H' || curr == 'Q' || curr == 'Z')
+
        }
sum += 8;
+
else if (curr == 'I' || curr == 'R')
+
sum += 9;
+
}
+
  
 
return(sum);
 
return(sum);
}
+
    }
  
// collapse sum into destiny number
+
    // collapse sum into destiny number
public static int collapseInt(int origNum)
+
    public static int collapseInt(int origNum)
 +
    {
 +
int result = 0;
 +
int remaining = origNum;
 +
 
 +
while(remaining > 0)
 
{
 
{
int result = 0;
+
    result += remaining % 10;
int remaining = origNum;
+
    remaining /= 10;
 +
}
  
while(remaining > 0)
+
return(result);
{
+
    }
result += remaining % 10;
+
remaining /= 10;
+
}
+
  
return(result);
+
    // display destiny message
}
+
    public static void displayDestiny(String name,int destiny)
 
+
    {
// display destiny message
+
if(destiny == 1)
public static void displayDestiny(String name,int destiny)
+
    System.out.println(name + " (1) is ambitious, independent, and self-sufficient.");
{
+
else if(destiny == 2)
if(destiny == 1)
+
    System.out.println(name + " (2) is supportive, diplomatic, and analytica.");
System.out.println(name + " (1) is ambitious, independent, and self-sufficient.");
+
else if(destiny == 3)
else if(destiny == 2)
+
    System.out.println(name + " (3) is enthusiastic, optimistic, and fun-lovin.");
System.out.println(name + " (2) is supportive, diplomatic, and analytica.");
+
else if(destiny == 4)
else if(destiny == 3)
+
    System.out.println(name + " (4) is practical, traditional, and serious.");
System.out.println(name + " (3) is enthusiastic, optimistic, and fun-lovin.");
+
else if(destiny == 5)
else if(destiny == 4)
+
    System.out.println(name + " (5) is adventurous, mercurial, and sensual.");
System.out.println(name + " (4) is practical, traditional, and serious.");
+
else if(destiny == 6)
else if(destiny == 5)
+
    System.out.println(name + " (6) is responsible, careful, and domestic.");
System.out.println(name + " (5) is adventurous, mercurial, and sensual.");
+
else if(destiny == 7)
else if(destiny == 6)
+
    System.out.println(name + " (7) is spiritual, eccentric, and a bit of a loner.");
System.out.println(name + " (6) is responsible, careful, and domestic.");
+
else if(destiny == 8)
else if(destiny == 7)
+
    System.out.println(name + " (8) is money-oriented, decisive, and stern.");
System.out.println(name + " (7) is spiritual, eccentric, and a bit of a loner.");
+
else if(destiny == 9)
else if(destiny == 8)
+
    System.out.println(name + " (9) is multi-talented, compassionate, and global.";
System.out.println(name + " (8) is money-oriented, decisive, and stern.");
+
else
else if(destiny == 9)
+
    System.out.println(name + " (?) is nothing.");
System.out.println(name + " (9) is multi-talented, compassionate, and global.";
+
    }
else
+
System.out.println(name + " (?) is nothing.");
+
}
+
  
 
     // main method
 
     // main method
public static void main(String [] args)
+
    public static void main(String [] args)
{
+
    {
// declare and initialize variables
+
// declare and initialize variables
String name = "";
+
String name = "";
int sum = 0;
+
int sum = 0;
int    destiny = 0;
+
int    destiny = 0;
  
// read in user's name as a string
+
// read in user's name as a string
name = JOptionPane.showInputDialog("Please enter your name.");
+
name = JOptionPane.showInputDialog("Please enter your name.");
  
// calculate sum from name
+
// calculate sum from name
sum = calculateString(name);
+
sum = calculateString(name);
  
// collapse sum into destiny number
+
// collapse sum into destiny number
destiny = collapseInt(sum);
+
destiny = collapseInt(sum);
while(destiny >= 10)
+
while(destiny >= 10)
destiny = collapseInt(destiny);
+
    destiny = collapseInt(destiny);
  
// display destiny message
+
// display destiny message
displayDestiny(name,destiny);
+
displayDestiny(name,destiny);
}// end main
+
    }// end main
 
}// end class
 
}// end class
 
|SideSectionTitle=Numerology with Methods
 
|SideSectionTitle=Numerology with Methods

Revision as of 13:03, 3 April 2011

Back to the Case Studies homepage

Problem

Write a complete Java program, similar to the problem Numerology in Case Studies Level Two. If you have not completed the previous problem click here to view problem details. Your goal is to rewrite the solution to use methods.


In particular, you should at least have methods with the following headers:

1. Calculate String Method

public static int calculateString (String name) 

This method will accept the name as a String and return the int which directly (i.e., without further collapsing)
corresponds to the name. So if name = “Alan Turing”, the method should return 45.


2. Collapse Integer Method

public static int collapseInt (int currentValue)

This method totally collapses the integer parameter currentValue to a single-digit number. So if currentValue = 88,
then the method should return 7 (and not 16).


3. Print Message Method

public static void printMessage (String name, int magicNumber)

This method should print the final message of the program. The magicNumber is between 1 and 9.


Remember to follow COMP 1010 coding standards.

 

Numerology with Methods

Wiki start01.jpg

Solution

Code

Solution Code

Back to the Case Studies homepage