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
The original solution looks similar to this.
import javax.swing.*;
public class A2Q2
{
//Declare constants
.
.
.
// main method
public static void main(String [] args)
{
// declare and initialize variables
.
.
.
// read in user's name as a string
.
.
.
// calculate sum from name
for (int r = 0; r < strName.length(); r++)
{
.
.
.
}
// collapse sum into a destiny number
while(intDestiny >= 10)
{
.
.
.
}
// display destiny message
if(intDestiny == 1)
System.out.println(strName + DESTINY_ONE);
else if(intDestiny == 2)
System.out.println(strName + DESTINY_TWO);
.
.
.
else
System.out.println(strName + DESTINY_ERROR);
}
}
Organize the Code
Separate the Statements
Separate Statements into Code Blocks
Optimize the Code by Removing Unnecessary Variables
Fix the Errors in the Code
The messy code contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the code from the case study.
Add Code to Output Progress Reports as the Program Executes
|
Code
Solution Code
import javax.swing.*;
import java.util.Date;
/**
* calculate a check digit for ISBNs
*
* @author: 1010 Instructors
* @version: 2007-September
*/
public class CaseStudy1_ISBN_Solution
{
/**
* PURPOSE: inputs a 9-digit ISBN, calculates the check digit and outputs a 10-digit ISBN
*/
public static void main (String [] args)
{
//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
digit = (isbn % 10);
total = total + digit * 8;
isbn = isbn / 10;
System.out.println (digit + " * " + 8 + " for a running total of " + total);
//isolate next digit and multiply by 7
digit = (isbn % 10);
total = total + digit * 7;
isbn = isbn / 10;
System.out.println (digit + " * " + 7 + " for a running total of " + total);
//isolate next digit and multiply by 6
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
digit = (isbn % 10);
total = total + digit * 5;
isbn = isbn / 10;
System.out.println (digit + " * " + 5 + " for a running total of " + total);
//isolate next digit and multiply by 4
digit = (isbn % 10);
total = total + digit * 4;
isbn = isbn / 10;
System.out.println (digit + " * " + 4 + " for a running total of " + total);
//isolate next digit and multiply by 3
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
digit = (isbn % 10);
total = total + digit * 2;
isbn = isbn / 10;
System.out.println (digit + " * " + 2 + " for a running total of " + total);
//isolate next digit and multiply by 1
digit = (isbn % 10);
total = total + digit * 1;
isbn = isbn / 10;
System.out.println (digit + " * " + 1 + " for a running total of " + total);
//calculate check digit
checkDigit = total % 11;
//output weighted total and check digit
System.out.println ("The weighted total is: " + total +
"\nThe check digit is : " + checkDigit +
"\n\nThe 10-digit ISBN is: " + temp + checkDigit);
System.out.println("\nProgrammed by COMP 1010 Instructors");
System.out.println("Date: " + new Date());
System.out.println ("*** End of Processing ***");
}//end main
}//end class
|
Back to the Case Studies homepage