Case Study IV - Solution

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Java Fundamentals


Introduction

This page contains a sample solution to Case Study IV along with a detailed description on each step to go from the Case Study IV to the sample solution. All the steps apply knowledge that can be gained from the sources listed in the overview section of this page.

   

{{{Body}}}



Sample Solution

This section goes over, in detail, the changes made to the Code to fix.

Declare needed variables

The first thing you will want to do is declare an integer array and an integer. Do this by adding the following code to your variables list in the main method:

    String isbnArray[];       //array to hold 9-digit ISBN numbers
    int counter;              //a counter to keep track of how many times to loop the program
  

Add Input from the User

You will now need to ask the user how many check digits they would like to enter. I did this with the following code:

  temp = JOptionPane.showInputDialog
  ("How many ISBN check digits would you like to calculate?");
  counter = Integer.parseInt(temp);

As you can see, I reused the 'temp' variable since it is not needed for more after the input is saved as an integer.
After the input is taken, I then parse the String into an integer variable called 'counter'.

Create the Array

Now that we know how many numbers to read in, we can create the array with a size of 'counter':

  isbnArray = new String[counter];

Input Loop

We can now start taking data from the user
This time, instead of taking the user's input an calculating the check digit right away, we'll store it away for later (in an Array).

  //loop 'isbnArray.length' times
  for(int i=0; i < isbnArray.length; i++)
  {
    temp = JOptionPane.showInputDialog
    ("Enter the first 9 digits of a 10-digit ISBN number.");

    //confirm input
    System.out.println ("You have entered " + temp + ".");

    //store ISBN in an array for later calculation
    isbnArray[i] = temp;
  }//end for-loop

Calculation Loop

All that is left for the calculation is to take each number out of the array and pass it to the 'calcISBN' method.

  //run the calcISBN method on every value in the array
  for(int i=0; i < isbnArray.length; i++)
  {
    //method call to calculate the checkDigit
    //send the method the isbn in the array at position i
    calcISBN( isbnArray[i] );

    //line to separate entries
    System.out.println("\n---------------------------");
  }//end for-loop

Modify Output

To finish up, we will remove some of the output in the 'calcISBN' method by commenting it out.
I have found it to be a good practice to only comment out your output rather than delete it all together. This way, if you need it back later (say to debug code), you can save yourself a lot of time

  //System.out.println (digit + " * " + i + " for a running total of " + total);
  System.out.println ("\nFor " + temp +
		    /*"The weighted total is: " + total +*/
                      "\n\nThe check digit is : " + checkDigit +
                      "\n\nThe 10-digit ISBN is: " + temp + checkDigit);

After removing some of the output, I had added some new lines and other formatting to clean up the output.

Sample Solution Code

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

/**
 * calculate a check digit for ISBNs
 *
 * @author:      1010 Instructors
 * @modified by: Gregor McKenzie
 * @version:     2007-December
 */

public class CaseStudy4_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
        String isbnArray[];       //array to hold 9-digit ISBN numbers
        int counter;              //a counter to keep track of how many times to loop the program

		//get input
		temp = JOptionPane.showInputDialog
		("How many ISBN check digits would you like to calculate?");
		counter = Integer.parseInt(temp);

		isbnArray = new String[counter];

                //loop 'counter' times
		for(int i=0; i < isbnArray.length; i++)
		{
			temp = JOptionPane.showInputDialog
			("Enter the first 9 digits of a 10-digit ISBN number.");

			//confirm input
			System.out.println ("You have entered " + temp + ".");

			//store ISBN in an array for later calculation
			isbnArray[i] = temp;
		}//end for-loop

		//run the calcISBN method on every value in the array
		for(int i=0; i < isbnArray.length; i++)
		{
			//method call to calculate the checkDigit
			//send the method the isbn in the array at position i
			calcISBN( isbnArray[i] );

			//line to separate entries
			System.out.println("\n---------------------------");
		}//end for-loop

        System.out.println("\nProgrammed by COMP 1010 Instructors");
        System.out.println("Date: " + new Date());
        System.out.println ("*** End of Processing ***");

	}//end main


    public static void calcISBN(String temp)
    {

        //variables declared here
        int isbn;            //9-digit ISBN
        int total = 0;       //total of isbn number when each digit is multiplied by check value
        int digit;           //isolated ISBN digit
        int checkDigit;      //value of total%11(as per ISBN standard)

        isbn = Integer.parseInt(temp);

		//System.out.println ("\nFor " + temp + ", the weighted value of the ISBN is:");

		//the loop to calculate the sum of the products of numbers
		for(int i=9; i >= 1; i--)
		{
			//isolate next digit and multiply by i
			digit = (isbn % 10);
			total = total + digit * i;
			isbn = isbn / 10;
			//System.out.println (digit + " * " + i + " for a running total of " + total);
		}// end for-loop

        //calculate check digit
        checkDigit = total % 11;

        //output weighted total and check digit
        System.out.println ("\nFor " + temp +
			  /*"The weighted total is: " + total +*/
                            "\n\nThe check digit is : " + checkDigit +
                            "\n\nThe 10-digit ISBN is: " + temp + checkDigit);


	}//end calcISBN

}//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.