Case Study II

From CompSciWiki
Jump to: navigation, search

Main_Page > Back to Chapter Topics


Introduction

It is your second day as Junior Software Developer for Funky Books Incorporated. Yesterday you debugged broken code for a program that determined ISBNs for the company's books (see Case Study I). The Quality Assurance Team was very pleased with your work fixing up and commenting code. Today your boss would like you to continue to optimize the same program that you worked on yesterday, because it could be improved.

You remember that when you were fixing the broken code you realized that there was a lot of repetition that could be easily removed using a loop. As well, you noticed that the program did not print an 'X' as it should when the 'check digit' is 10.

Complete the following tasks on the code below:

  • Use a loop to minimize repeated code.
  • Use an if statement to print out check digits of 10 as X.

Remember to follow the company's coding standards while editing the code.





Overview

Continuing from Case Study I, we will introduce topics covered in the previous chapters. We will introduce a loop structure to remove the repetition of code. As well, we will use an if statement to print out an 'X' when the check digit is 10. A solution will be provided at the end.

Template loop detected: Template loop detected:

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

Solution

Case Study II - Solution

Resources

The resources needed to solve this case study can be found through the following links.

Internal Links

Chapter 5: Control Structures
Chapter 6: Loops

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 III: Day Three at Funky Books Inc.
Case Study IV: Day Four at Funky Books Inc.