Numerology with Methods

From CompSciWiki
Revision as of 14:23, 3 April 2011 by EsmondP (Talk | contribs)

Jump to: navigation, search

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 to do.
  • Determine the logic for each item.
  • Organize the code.
  • Type the rest of the code.


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.

Code

Solution Code

Back to the Case Studies homepage