Case Study III
COMP 1010 Home > Case Study III
IntroductionOn your first day working as a Junior Software Developer at Funky Books Incorporated you were asked to fix some "broken code" (see Case Study I). Last week you were asked to modify some code, which is used for checking ISBN numbers, to make the code more compact. You did this by using loops (see Case Study II). The following Case Study will outline the events of your third day as a Junior Software Developer at Funky Books Incorporated. The company plans to update and expand it's ISBN checking code to include other options. It is important that other programmers have a thorough understanding of what a piece of code is supposed to do prior to making any modifications. Today you will be asked to modify this code to make it easier to maintain. You will do this by separating the tasks into three different methods and a main method. Please complete the following tasks on the code provided below:
Remember to follow the coding standards when writing your code.
|
---|
{{{Body}}}
Method Details
The "QuickCreateWithTotals" method will be used for showing the steps involved in creating a valid 10th digit for an ISBN, given the first 9 digits. This method should have no return value and one parameter, a String.
The "QuickCreateWithoutTotals" method will also be used for creating a valid 10th digit for an ISBN, given the first 9 digits. The difference will be that it will not show the steps used to create the 10th digit. Thiis method should have no return value and one parameter, a String.
The "VerifyISBN" method will check to see if the specified 10 digit ISBN is valid or not. This method should have no return value and one parameter, a String.
The main method will get the users choice method and call the method that will perform that task.
Code To Fix
import javax.swing.*; import java.util.Date; public class ISBN{ public static void main(String [] args){ String inputString; // Used for JOP input char option; // Will either be 'C' for "Create with running totals" or 'Q' for "Quick create without // running totals" or 'V' for "Verify a 10-digit ISBN" int isbn; int sum = 0; // Must be initialized int digit; // of input String char lastChar; // of input String char checkChar ; // correct check digit as character inputString = JOptionPane.showInputDialog ("Enter:" + "\nC (Create)," +"\nQ (Quick Create), or" + "\nV (Verify)"); option = inputString.charAt(0); // Create a 10 "digit" ISBN given the first 9 "digits", and print the running totals if (option == 'C' || option == 'c'){ inputString = JOptionPane.showInputDialog ("Enter 9 digit ISBN"); System.out.println ("You entered the 9-digit ISBN: " + inputString); isbn = Integer.parseInt (inputString); for (int i=9; i>0; i--){ digit = isbn % 10; isbn = isbn / 10; sum = sum + digit * i; System.out.println ("Running total " + i + " * " + digit + " = " + sum); }//close for System.out.println ("Weighted sum = " + sum); if (sum%11 == 10){ System.out.println ("Check digit is 'X'"); System.out.println ("The 10 digit ISBN is " + inputString + 'X'); }//close if else{ System.out.println ("Check digit is " + sum%11); System.out.println ("The 10 digit ISBN is " + inputString + sum%11); }//close else } // if (option = 'C') // Create a 10 "digit" ISBN given the first 9 "digits", but do not print any running totals else if (optIon == 'Q' || option == 'q'){ inputString = JOptionPane.showInputDialog ("Enter 9 digit ISBN"); System.out.println ("You entered the 9-digit ISBN: " + inputString); isbn = Integer.parseInt (inputString); for (int i=9; i>0; i--){ digit = isbn % 10; isbn = isbn / 10; sum = sum + digit * i; }//close for System.out.print ("The 10 digit ISBN is " + inputString); if (sum%11 == 10){ System.out.println ('X'); }//close if else{ System.out.println (sum%11); }//close else } // if (option == 'Q') // Verify a 10 "digit" IBSN as valid or not - if invalid, print out the valid ISBN else if (option == 'V' || option == 'v'){ inputString = JOptionPane.showInputDialog ("Enter 10 digit ISBN"); System.out.println ("You entered the 10 digit ISBN: " + inputString); // Separate the first 9 "digits" from the "last".Remember that the last "digit" may be an 'X'. lastChar = inputString.charAt(9); inputString = inputString.substring(0,9); isbn = Integer.parseInt (inputString); for (int i=9; i>0; i--){ digit = isbn % 10; isbn = isbn / 10; sum = sum + digit * i; }//close for if ( sum % 11 == 10 ){ checkChar = 'X' ; }//close if // Convert check digit to a character. Note the little shorthand trick in doing this conversion. else{ checkChar = (char)('0' + sum % 11) ; }//close if if (checkChar == lastChar){ System.out.println ("Valid ISBN"); }//close if else{ System.out.println ("WARNING! INVALID ISBN. --- "); System.out.println ("A valid ISBN would be: " + inputString + checkChar); }//close else } // if (option == 'V') // Invalid option else{ System.out.println ("You have entered an invalid option"); }//close else System.out.println ("\nDeveloped by COMP 1010 Instructor"); System.out.println ("Date: " + new Date()); System.out.println ("*** End of Processing ***"); }//close main }//close ISBN
Solution
Resources
The resources needed to solve this case study can be found through the following links.
Internal Links
Chapter 7: User-Defined Methods
External Links
COMP 1010 coding standards
ISBN Check Digit Queen's University
Links To Other Case Studies
Case Study I: Day One at Funky Books Inc.
Case Study II: Day Two at Funky Books Inc.
Case Study IV: Day Four at Funky Books Inc.