Case Study I - Solution

From CompSciWiki
Revision as of 01:54, 28 November 2007 by DavidW (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Solution Code

import javax.swing.*;
import java.util.Date;


public class CaseStudy1_ISBN{

    public static void main (String [] args) {


        String temp;
        int isbn;
        int total;



        temp = JOptionPane.showInputDialog("");
        isbn = Integer.parseInt(temp);


        System.out.println ("You have entered " + temp + ".\n\nThe weighted value of the ISBN is:");


        int digit1 = (isbn % 10);
        total = digit1 * 9;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 9 + " for a running total of " + total);


        int digit2 = (isbn % 10);
        total = total + digit2 * 8;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 8 + " for a running total of " + total);


        int digit3 = (isbn % 10);
        total = total + digit3 * 7;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 7 + " for a running total of " + total);


        int digit4 = (isbn % 10);
        total = total + digit4 * 6;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 6 + " for a running total of " + total);


        int digit5 = (isbn % 10);
        total = total + digit5 * 5;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 5 + " for a running total of " + total);


        int digit6 = (isbn % 10);
        total = total + digit6 * 4;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 4 + " for a running total of " + total);


        int digit7 = (isbn % 10);
        total = total + digit7 * 3;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 3 + " for a running total of " + total);


        int digit8 = (isbn % 10);
        total = total + digit8 * 2;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 2 + " for a running total of " + total);


        int digit9 = (isbn % 10);
        total = total + digit9 * 1;
        isbn = isbn / 10;
        //System.out.println (digit + " * " + 1 + " for a running total of " + total);


        int checkDigit = total % 11;


        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 ***");

        }
}