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
|
Solution
I suggest you plan and create an outline on how to accomplish the solution before you code. An outline can benefit you in many ways. For example an outline can help you determine and layout the logic of your solution, variables, and methods. Additionally, you can find possible problems in the logic before writing the code. If an error exists or the code does not execute as expected, locating the problem in a few hundred lines of code is not always easy. If you know in advance what exactly you want to do, writing the code will be easier.
Here are steps you can follow:
- Make sure you understand the problem
- List exactly what the problem is asking you to do
- Determine the logic for each item
- Organize the code
- Type the rest of the code
Step 1: Make Sure You Understand The Problem
This step is important. You definitely want to avoid a situation where you have been working on an assignment for 3 days and suddenly find out you misinterpreted the question.
The Problem
In this case, the problem is to re-write the Numerology solution to use methods. What is the actual problem? According to the Numerology problem your task is to convert a name into an integer using the pseudo-mathematics of numerology.
What is a Method?
A Method is a section of code, similar to a sub procedure, which can be referred by name from anywhere in your program. The structure of an method includes a name, signature, and sometimes a return value. Methods benefit the programmer because time is saved from repeating sections of code by simply calling the method's name. Methods can be saved and used in other programs as well.
Here are some general rules you can follow when using methods:
- Method name should be meaningful, usually a verb describing the method's function.
- Each method should have one purpose.
- The length of the method is no longer than 100 lines or the size of your display.
Step 2: List Exactly What The Problem Is Asking To Do.
Most COMP 1010 assignments layout the steps for the solution in the problem. Although, as you advance to 2nd or 3rd year, you need to determine the steps yourselves. This list is a good place start when deciding what methods you need.
Step 3: Determine The Logic For Each Item
Step 4: Organize The Code.
Step 5: Type The Rest Of The Code
|
Code
Solution Code
import javax.swing.*;
public class A3Q1
{
// calculate sum from name
public static int calculateString(String origName)
{
String upperName = origName.toUpperCase();
int sum = 0;
char curr = ' ';
for (int r = 0; r < upperName.length(); r++)
{
curr = upperName.charAt(r);
if (curr == 'A' || curr == 'J' || curr == 'S')
sum += 1;
else if (curr == 'B' || curr == 'K' || curr == 'T')
sum += 2;
else if (curr == 'C' || curr == 'L' || curr == 'U')
sum += 3;
else if (curr == 'D' || curr == 'M' || curr == 'V')
sum += 4;
else if (curr == 'E' || curr == 'N' || curr == 'W')
sum += 5;
else if (curr == 'F' || curr == 'O' || curr == 'X')
sum += 6;
else if (curr == 'G' || curr == 'P' || curr == 'Y')
sum += 7;
else if (curr == 'H' || curr == 'Q' || curr == 'Z')
sum += 8;
else if (curr == 'I' || curr == 'R')
sum += 9;
}
return(sum);
}
// collapse sum into destiny number
public static int collapseInt(int origNum)
{
int result = 0;
int remaining = origNum;
while(remaining > 0)
{
result += remaining % 10;
remaining /= 10;
}
return(result);
}
// display destiny message
public static void displayDestiny(String name,int destiny)
{
if(destiny == 1)
System.out.println(name + " (1) is ambitious, independent, and self-sufficient.");
else if(destiny == 2)
System.out.println(name + " (2) is supportive, diplomatic, and analytica.");
else if(destiny == 3)
System.out.println(name + " (3) is enthusiastic, optimistic, and fun-lovin.");
else if(destiny == 4)
System.out.println(name + " (4) is practical, traditional, and serious.");
else if(destiny == 5)
System.out.println(name + " (5) is adventurous, mercurial, and sensual.");
else if(destiny == 6)
System.out.println(name + " (6) is responsible, careful, and domestic.");
else if(destiny == 7)
System.out.println(name + " (7) is spiritual, eccentric, and a bit of a loner.");
else if(destiny == 8)
System.out.println(name + " (8) is money-oriented, decisive, and stern.");
else if(destiny == 9)
System.out.println(name + " (9) is multi-talented, compassionate, and global.";
else
System.out.println(name + " (?) is nothing.");
}
// main method
public static void main(String [] args)
{
// declare and initialize variables
String name = "";
int sum = 0;
int destiny = 0;
// read in user's name as a string
name = JOptionPane.showInputDialog("Please enter your name.");
// calculate sum from name
sum = calculateString(name);
// collapse sum into destiny number
destiny = collapseInt(sum);
while(destiny >= 10)
destiny = collapseInt(destiny);
// display destiny message
displayDestiny(name,destiny);
}// end main
}// end class
|
Back to the Case Studies homepage