Difference between revisions of "Numerology with Methods"

From CompSciWiki
Jump to: navigation, search
m
Line 39: Line 39:
 
|SolutionCode=
 
|SolutionCode=
 
import javax.swing.*;
 
import javax.swing.*;
import java.util.Date;
 
  
/**
+
public class A3Q1
* calculate a check digit for ISBNs
+
*
+
* @author:    1010 Instructors
+
* @version:    2007-September
+
*/
+
 
+
public class CaseStudy1_ISBN_Solution
+
 
{
 
{
     /**
+
     // calculate sum from name
    * PURPOSE: inputs a 9-digit ISBN, calculates the check digit and outputs a 10-digit ISBN
+
public static int calculateString(String origName)
    */
+
{
    public static void main (String [] args)  
+
String upperName = origName.toUpperCase();
    {
+
int sum = 0;
 
+
char curr = ' ';
        //variables declared here
+
        String temp;        //temporary input string
+
        int isbn;            //9-digit ISBN
+
        int digit;          //isolated ISBN digit
+
        int total;          //total of isbn number when each digit is multiplied by check value
+
        int checkDigit;      //value of total%11(as per ISBN standard)
+
 
+
        //get input
+
        temp = JOptionPane.showInputDialog
+
        ("Enter the first 9 digits of a 10-digit ISBN number.");
+
        isbn = Integer.parseInt(temp);
+
 
+
        //confirm input
+
        System.out.println ("You have entered " + temp + ".\n\nThe weighted value of the ISBN is:");
+
 
+
        //isolate last digit and multiply by 9
+
        digit = (isbn % 10);
+
        total = digit * 9;
+
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 9 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 8
+
for (int r = 0; r < upperName.length(); r++)
        digit = (isbn % 10);
+
{
        total = total + digit * 8;
+
curr = upperName.charAt(r);
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 8 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 7
+
if (curr == 'A' || curr == 'J' || curr == 'S')
        digit = (isbn % 10);
+
sum += 1;
        total = total + digit * 7;
+
else if (curr == 'B' || curr == 'K' || curr == 'T')
        isbn = isbn / 10;
+
sum += 2;
        System.out.println (digit + " * " + 7 + " for a running total of " + total);
+
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;
 +
}
  
        //isolate next digit and multiply by 6
+
return(sum);
        digit = (isbn % 10);
+
}
        total = total + digit * 6;
+
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 6 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 5
+
// collapse sum into destiny number
        digit = (isbn % 10);
+
public static int collapseInt(int origNum)
        total = total + digit * 5;
+
{
        isbn = isbn / 10;
+
int result = 0;
        System.out.println (digit + " * " + 5 + " for a running total of " + total);
+
int remaining = origNum;
  
        //isolate next digit and multiply by 4
+
while(remaining > 0)
        digit = (isbn % 10);
+
{
        total = total + digit * 4;
+
result += remaining % 10;
        isbn = isbn / 10;
+
remaining /= 10;
        System.out.println (digit + " * " + 4 + " for a running total of " + total);
+
}
  
        //isolate next digit and multiply by 3
+
return(result);
        digit = (isbn % 10);
+
}
        total = total + digit * 3;
+
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 3 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 2
+
// display destiny message
        digit = (isbn % 10);
+
public static void displayDestiny(String name,int destiny)
        total = total + digit * 2;
+
{
        isbn = isbn / 10;
+
if(destiny == 1)
        System.out.println (digit + " * " + 2 + " for a running total of " + total);
+
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.");
 +
}
  
        //isolate next digit and multiply by 1
+
    // main method
        digit = (isbn % 10);
+
public static void main(String [] args)
        total = total + digit * 1;
+
{
        isbn = isbn / 10;
+
// declare and initialize variables
        System.out.println (digit + " * " + 1 + " for a running total of " + total);
+
String name = "";
 +
int sum = 0;
 +
int    destiny = 0;
  
        //calculate check digit
+
// read in user's name as a string
        checkDigit = total % 11;
+
name = JOptionPane.showInputDialog("Please enter your name.");
  
        //output weighted total and check digit
+
// calculate sum from name
        System.out.println ("The weighted total is: " + total +
+
sum = calculateString(name);
                            "\nThe check digit is : " + checkDigit +
+
                            "\n\nThe 10-digit ISBN is: " + temp + checkDigit);
+
  
        System.out.println("\nProgrammed by COMP 1010 Instructors");
+
// collapse sum into destiny number
        System.out.println("Date: " + new Date());
+
destiny = collapseInt(sum);
        System.out.println ("*** End of Processing ***");
+
while(destiny >= 10)
 +
destiny = collapseInt(destiny);
  
    }//end main
+
// display destiny message
}//end class
+
displayDestiny(name,destiny);
 +
}// end main
 +
}// end class
 
|SideSectionTitle=Numerology with Methods
 
|SideSectionTitle=Numerology with Methods
 
|SideSection=
 
|SideSection=

Revision as of 13:00, 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