Difference between revisions of "Case Study I - Solution"

From CompSciWiki
Jump to: navigation, search
m (Error One)
(Find the Errors in the Code)
Line 97: Line 97:
  
 
==Find the Errors in the Code==
 
==Find the Errors in the Code==
The [[Case Study I#Code|messy code]] contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the [[Case Study I#Code|messy code]].
+
The [[Case Study I#Code|messy code]] contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the [[Case Study I#Code|code]] from the [[Case Study I|case study]].
 
===Error One===
 
===Error One===
 
<pre>
 
<pre>
Line 111: Line 111:
 
int isbn = Temp;
 
int isbn = Temp;
 
</pre>
 
</pre>
 +
There are two problems with the above code sample. The first problem being that the [[Variables and Literals#Variables|variable]] "Temp" is of type [[Strings|string]] and not of the primitive type [[Common Primitive Variables#Primitive Type int|int]]. The second problem is the name of the [[Variables and Literals#Variables|variable]] "Temp". The [[Variables and Literals#Variables|variable]] was originally declared as "temp" and Java is a [[Your First Java Program#Case Sensitivity|case sensitive]] language. Both programs are repaired by replacing the code with the line below.
 +
<pre>
 +
isbn = Integer.parseInt(temp);
 +
</pre>
 +
 
===Error Three===
 
===Error Three===
 
<pre>
 
<pre>
 
isbn = isbn // 10;
 
isbn = isbn // 10;
 
</pre>
 
</pre>
 +
An extra front slash changes the [[Arithmetic Operators|division operation]] into a comment. Remove the extra front slash to correct the error.
 +
<pre>
 +
isbn = isbn / 10;
 +
</pre>
 +
 
===Error Four===
 
===Error Four===
 
<pre>
 
<pre>
 
digit = (isbn % 10)  
 
digit = (isbn % 10)  
 
</pre>
 
</pre>
 +
<pre>
 +
digit = (isbn % 10);
 +
</pre>
 +
 
===Error Five===
 
===Error Five===
 
<pre>
 
<pre>
total = total + digit + 6
+
total = total + digit + 6;
 
</pre>
 
</pre>
 +
<pre>
 +
total = total + digit * 6;
 +
</pre>
 +
 
===Error Six===
 
===Error Six===
 
<pre>
 
<pre>
 
digit = (isbn / 10);
 
digit = (isbn / 10);
 
</pre>
 
</pre>
 +
<pre>
 +
digit = (isbn % 10);
 +
</pre>
 +
 
===Error Seven===
 
===Error Seven===
 
<pre>
 
<pre>
 
digit = (isbn * 10);
 
digit = (isbn * 10);
 +
</pre>
 +
<pre>
 +
digit = (isbn % 10);
 
</pre>
 
</pre>
 
===Error Eight===
 
===Error Eight===
 
<pre>
 
<pre>
total = total + digit9 - 1;
+
total = total + digit - 1;
 +
</pre>
 +
<pre>
 +
total = total + digit * 1;
 
</pre>
 
</pre>
  

Revision as of 03:25, 29 November 2007

COMP 1010 Home > Java Fundamentals


Introduction

Introduction goes here

   

{{{Body}}}

Sample Solution

This section explains the Sample Solution Code in detail with respect to the list tasks from the case study.

Insert a section for each of the list items

  • Organize the code
  • Add Comments to Explain the Code
  • Optimize the Code by Removing Unnecessary Variables
  • Find the Errors in the Code
  • Add code to output progress reports as the program executes
  • Add code to output all the calculated results in a readable format

Organize the Code

Separate the Statements

The code file contains more than one statements on each line.

int digit1 = (isbn % 10);int total = digit1 * 9;isbn = isbn / 10;
int digit2 = (isbn % 10);total = total + digit2 * 8;isbn = isbn / 10;

Each of the above lines of code performs a similar function. Each line can be interpretted as a block of code. By placing each statements on a separate line and grouping the statements into appropriate code blocks, the result should look something like the following:

int digit1 = (isbn % 10);
int total = digit1 * 9;
isbn = isbn / 10;
int digit2 = (isbn % 10);
total = total + digit2 * 8;
isbn = isbn / 10;

By placing each of the statements on a separate line, the readability of the code increases dramatically.

Separate the Statements into Code Blocks

Once the statements are readable, the next step would be to organize them into code blocks as stated in COMP 1010 Coding Standards. Continuing with our previous example, the functionality of the code can be broken into two distinct code blocks.

int digit1 = (isbn % 10);
int total = digit1 * 9;
isbn = isbn / 10;

int digit2 = (isbn % 10);
total = total + digit2 * 8;
isbn = isbn / 10;

Almost each line of code in the messy code file can be considered a separate code block. Take the time to read the code and understand how everything works together before deciding which statements should be grouped together.

One of the code blocks that should be added is a variable declaration code block at the beginning of the main method. Throughout the messy code, integers are declared. All of the declaration statements should be placed at the beginning of the main method to ensure the code stays organized. Going to the previous example, two declaration statements can be moved to the top as depicted below.

int digit1;
int digit2;

digit1 = (isbn % 10);
int total = digit1 * 9;
isbn = isbn / 10;

digit2 = (isbn % 10);
total = total + digit2 * 8;
isbn = isbn / 10;

Add Comments to Explain the Code

The COMP 1010 coding standards, or the Funky Books Inc. coding standards, make numerous points concerning comments in code. To be specific, statements 1, 2, 4, and 7 can be applied to the code file for this case study. All major code blocks should be identified by now. Look over each code block and briefly explain what it does in a comment. Since all declaration statements have been moved to the top of the the main method , make sure to apply coding standard 7 from the COMP 1010 Coding Standards.

Optimize the code by removing unnecessary variables

At the top of the main method, there should now be a number of variables

declared. Notice that there are nine different "digit" variables which are only used once to store the same calculation. 
int digit1;
int digit2;

digit1 = (isbn % 10);
int total = digit1 * 9;
isbn = isbn / 10;

digit2 = (isbn % 10);
total = total + digit2 * 8;
isbn = isbn / 10;

The code sample from above shows two of the nine digit variables. These two variables can be replaced with a single one as follows:

int digit;

digit = (isbn % 10);
int total = digit1 * 9;
isbn = isbn / 10;

digit = (isbn % 10);
total = total + digit2 * 8;
isbn = isbn / 10;

Find the Errors in the Code

The messy code contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the code from the case study.

Error One

String temp = JOptionPane.showInputDialog("")

Technically, the above code does not break the functionality of the application as the method call still makes the input dialog appear to take input from the user. Although, from a usability standpoint the code does break the application. The user who is running the application needs to know what to enter as input into the input dialog. Without a proper message, the user cannot be expected to know what the application is expecting as input. An example of a proper message is as follows:

temp = JOptionPane.showInputDialog("Enter the first 9 digits of a 10-digit ISBN number.");

Error Two

int isbn = Temp;

There are two problems with the above code sample. The first problem being that the variable "Temp" is of type string and not of the primitive type int. The second problem is the name of the variable "Temp". The variable was originally declared as "temp" and Java is a case sensitive language. Both programs are repaired by replacing the code with the line below.

isbn = Integer.parseInt(temp);

Error Three

isbn = isbn // 10;

An extra front slash changes the division operation into a comment. Remove the extra front slash to correct the error.

isbn = isbn / 10;

Error Four

digit = (isbn % 10) 
digit = (isbn % 10);

Error Five

total = total + digit + 6;
total = total + digit * 6;

Error Six

digit = (isbn / 10);
digit = (isbn % 10);

Error Seven

digit = (isbn * 10);
digit = (isbn % 10);

Error Eight

total = total + digit - 1;
total = total + digit * 1;

Add code to output progress reports as the program executes

Add code to output all the calculated results in a readable format

Sample Solution 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

Links To Other Case Studies

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.