Case Study II - Solution
From CompSciWiki
COMP 1010 Home > Java Fundamentals
IntroductionIntroduction goes here.
|
---|
{{{Body}}}
total = 0; total needs to be initialized before loop
Sample Solution Code
import javax.swing.*; import java.util.Date; /** * * This program will read in the first 9 digits of an ISBN and print out the 9 digits * along with the valid 10th check digit. This code uses a loop structure to remove redundant code * Note that this code will handle the possibility of the check digit being an 'X'. * * @author: 1010 Instructors * @version: 2007-September * */ public class CaseStudy2_ISBN_Solution { 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 = 0; //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 first 9 digits of ISBN"); isbn = Integer.parseInt (temp); System.out.println ("You have entered " + temp + ".\n\nThe weighted value of the ISBN is:"); for (int i=9; i>0; i--) { digit = isbn % 10; isbn = isbn / 10; total = total + digit * i; System.out.println (digit + " * " + 9 + " for a running total of " + total); } //calculate check digit checkDigit = total % 11; System.out.println ("The weighted total is: " + total); //if check digit is equal to 10 then print an 'X', otherwise print the checkDigit value if (total%11 == 10) { System.out.println ("The check digit is: 'X'"); System.out.println ("\nThe 10-digit ISBN is: " + temp + 'X'); } else { System.out.println ("The check digit is: " + total%11); System.out.println ("\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
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.