Difference between revisions of "Numerology with Methods"

From CompSciWiki
Jump to: navigation, search
m
m
Line 58: Line 58:
 
====What is a Method?====
 
====What is a Method?====
 
<p>
 
<p>
A <b>Method</b> 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.
+
A <b>Method</b> 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 signature, code, and sometimes a return value. A method's <b>Signature</b> includes the name and input parameters that are passed to the method. 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.
 
</p>
 
</p>
 
<br>
 
<br>
Line 88: Line 88:
 
==Step 3: Determine The Logic For Each Item==
 
==Step 3: Determine The Logic For Each Item==
 
<p>
 
<p>
The goal in this step is to determine how each task or method will execute.  We need to figure out what the logic, the inputs, the return values required for each method.  A good way to layout the logic is to write [http://en.wikipedia.org/wiki/Pseudocode <b>pseudocode</b>]. <b>Pseudocode</b> a way to explain how an algorithm executes using plain English that is structured similar to code.
+
The goal in this step is to determine how each task or method will execute.  We need to figure out what the logic, the input parameters, the return values required for each method.  A good way to layout the logic is to write [http://en.wikipedia.org/wiki/Pseudocode <b>pseudocode</b>]. <b>Pseudocode</b> a way to explain how an algorithm executes using plain English that is structured similar to code.
 
</p>
 
</p>
 
<br>
 
<br>
Line 95: Line 95:
 
<br>
 
<br>
 
<p>
 
<p>
We will not worry about writing our own method for reading input.  The <b>JOptionPane.showInputDialog()</b> method will do the work for us.  This method displays a form, similar to the image above, which prompts the user for input.  The input is stored as a String object which is similar to an array of characters. For more information on <b>JOptionPane</b> click [http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JOptionPane.html here].
+
We will not worry about writing our own method for reading input.  The <b>JOptionPane.showInputDialog()</b> method will do the work for us.  This method displays a form, similar to the following image, which prompts the user for input.  The input is stored as a String object which is similar to an array of characters. For more information on <b>JOptionPane</b> click [http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JOptionPane.html here].
 
</p>
 
</p>
 
<p>
 
<p>

Revision as of 19:55, 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

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 for
  • 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.


What is 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 signature, code, and sometimes a return value. A method's Signature includes the name and input parameters that are passed to the method. 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 For.

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 on your own. This list is a good place start when deciding what methods you need.


Here is an overview of the problem:

  • Prompt the user for a name.
  • Calculate a running sum based on the letters in the name and the values assigned to the letters.
  • Collapse the sum into a destiny number.
  • Assign and display characteristics to the name based on the destiny number


As you can see, I bolded certain words within the list. These words can be used to name the methods we need.


Step 3: Determine The Logic For Each Item

The goal in this step is to determine how each task or method will execute. We need to figure out what the logic, the input parameters, the return values required for each method. A good way to layout the logic is to write pseudocode. Pseudocode a way to explain how an algorithm executes using plain English that is structured similar to code.


Prompt a User for a Name

Numerology Calc.png


We will not worry about writing our own method for reading input. The JOptionPane.showInputDialog() method will do the work for us. This method displays a form, similar to the following image, which prompts the user for input. The input is stored as a String object which is similar to an array of characters. For more information on JOptionPane click here.

Remember to import the library javax.swing at the top of the file. More information can be found here.


Calculate a Running Sum

This step requires you to take each value assigned to each letter, from the following table, and add to a variable storing the sum. You will need the name string as input parameter and the sum integer as the return value. To process each letter we need to use a loop structure such as the While or For loop. Since we can get the length by calling the Length() in the name String we will use a For loop. Each letter must now be compared with all the letters in the table.


1 2 3 4 5 6 7 8 9
A B C D E F G H I
J K K M N O P Q R
S T U V W X Y Z

Step 4: Organize The Code.

Step 5: Type The Rest Of The Code

Code

Solution Code

Back to the Case Studies homepage