Difference between revisions of "Case Study II"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
{{Template:1010Solution|Introduction=It is your second day as Junior Software Developer for Funky Books Incorperated. Yesterday you debugged broken code for a program that determined ISBNs for the company's books (see [[Case Study I]]). Today your boss wants you to use loop structures to remove uneccesary repetition from the same program code.|Overview=Continuing from [[Case Study I]], we will introduce loop structures to remove the repetition of code. A solution will be provided at the end.}}
+
{{Template:1010Solution
 +
|Introduction=
 +
<p>
 +
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.
 +
</p>
 +
<p>
 +
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.
 +
</p>
 +
Complete the following tasks on the code below:
 +
<ul>
 +
<li>Use a loop to minimize repeated code.</li>
 +
<li>Use an if statement to print out check digits of 10 as X.</li>
 +
</ul>
 +
Remember to abide by the company coding standards while repairing 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.}}
 +
 
 +
=Code=
 +
<pre>
 +
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
 +
</pre>
  
 
=Solution=
 
=Solution=

Revision as of 20:45, 5 December 2007

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 abide by the company coding standards while repairing 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

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.