Case Study III - Solution
COMP 1010 Home > Java Fundamentals
IntroductionIn Case Study III you were asked to seperate a program into several different User-Defined Methods. QuickCreateWithTotals calculates a tenth digit for an ISBN, given the first 9 digits. This method prints out all of the steps involved in calculating the tenth digit. QuickCreateWithoutTotals does the same thing as the previous methos, except that it does not print out all of the steps involved in the calculation. Verify ISBN takes a 10 digit ISBN number and checks to see if it is a valid ISBN number. If it is a valid ISBN number it will print a message that says"Valid ISBN". Otherwise a message will be printed telling the user that the ISBN number that they entered is not valid. A solution to that case study is provided here.
|
---|
{{{Body}}}
Sample Solution
Sample solution using three methods and a main method:
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"); QuickCreateWithTotals(inputString); } // if (option = 'C') else // Create a 10 "digit" ISBN given the first 9 "digits", but do not print any running totals if (option == 'Q' || option == 'q') { inputString = JOptionPane.showInputDialog ("Enter 9 digit ISBN"); QuickCreateWithoutTotals(inputString); } // if (option == 'Q') else // Verify a 10 "digit" IBSN as valid or not - if invalid, print out the valid ISBN if (option == 'V' || option == 'v') { inputString = JOptionPane.showInputDialog ("Enter 10 digit ISBN"); VerifyISBN(inputString); } // if (option == 'V') // Invalid option else System.out.println ("You have entered an invalid option"); System.out.println ("\nDeveloped by COMP 1010 Instructor"); System.out.println ("Date: " + new Date()); System.out.println ("*** End of Processing ***"); }//close main /** * QuickCreateWithTotals calculates a valid 10th digit for an ISBN, given the first 9 digits. It also shows all of * the steps * * @param firstParameter inputString is the 9 digit ISBN number * * @return void. It doesn't return anything. */ public static void QuickCreateWithTotals(String inputString){ 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 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 }//close QuickCreateWithTotals /** * QuickCreateWithoutTotals calculates a valid 10th digit for an ISBN, given the first 9 digits. It does NOT show all * of the steps * * @param firstParameter inputString is the 9 digit ISBN number * * @return void. It doesn't return anything. */ public static void QuickCreateWithoutTotals(String inputString){ 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 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 }//close QuickCreateWithoutTotals /** * VerifyISBN checks to see if the 10 digit ISBN number that you entered is a valid ISBN number. * * @param firstParameter inputString is the 10 digit ISBN number * * @return void. It doesn't return anything. */ public static void VerifyISBN(String inputString){ 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 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 else{ // Convert check digit to a character. Note the little shorthand trick in doing this conversion. checkChar = (char)('0' + sum % 11) ; }//close else 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 }//close VerifyISBN }//close ISBN
Links To Case Studies
Case Study I: Day One at Funky Books Inc.
Case Study II: Day Two at Funky Books Inc.
Case Study III: Day Three at Funky Books Inc.
Case Study IV: Day Four at Funky Books Inc.