Difference between revisions of "Case Study IV"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
 
{{
 
{{
 
1010Topic
 
1010Topic
|Introduction=Now that you have [[Case_Study_I | fixed your code]], added loops to make it [[Case_Study_II| more compact]], and broken your code into logical tasks called [[Case_Study_III| methods]], we will take a look at further organizing your code on day four at Funky Books Inc. These modifications will be made to your previous Case Studies' code using the Array structures.
+
|Introduction=Now that you have [[Case_Study_I | fixed your code]], added loops to make it [[Case_Study_II| more compact]], and broken your code into logical tasks called [[Case_Study_III| methods]], we will take a look at further organizing your code using arrays on day four at Funky Books Inc. to make a more useful program.
 +
 
 +
When you get to work on the fourth day, your boss tells you that she likes what you have done with the ISBN Check Digit program so far. But she asks that she be able to enter multiple 9-digit ISBNs, and then have it calculate the check digits all at once rather than repeatedly running the program. Your boss also mentions that it is not necessary to see the weighted or running totals as the program operates.
  
 
|Overview=You will apply your knowledge gained from the previous [[Arrays | chapter on arrays]] to further organize your code.
 
|Overview=You will apply your knowledge gained from the previous [[Arrays | chapter on arrays]] to further organize your code.
Line 10: Line 12:
 
<br />
 
<br />
 
<br />
 
<br />
 +
 
=Array Overview=
 
=Array Overview=
 
<p>
 
<p>
Line 21: Line 24:
 
   system.out.println(intArray[i]);
 
   system.out.println(intArray[i]);
 
}
 
}
 +
</pre>
 +
 +
=Code to fix=
 +
<pre>
 +
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 CaseStudy3_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
 +
 +
        //get input
 +
        temp = JOptionPane.showInputDialog
 +
        ("Enter the first 9 digits of a 10-digit ISBN number.");
 +
 +
        //confirm input
 +
        System.out.println ("You have entered " + temp + ".");
 +
 +
//method call to calculate the checkDigit
 +
//send the method the isbn
 +
calcISBN(temp);
 +
 +
        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 ("\nThe 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 ("The weighted total is: " + total +
 +
                            "\nThe check digit is : " + checkDigit +
 +
                            "\n\nThe 10-digit ISBN is: " + temp + checkDigit);
 +
 +
}//end calcTotal
 +
 +
}//end class
 
</pre>
 
</pre>
  

Revision as of 21:12, 5 December 2007

COMP 1010 Home > Java Fundamentals


Introduction

Now that you have fixed your code, added loops to make it more compact, and broken your code into logical tasks called methods, we will take a look at further organizing your code using arrays on day four at Funky Books Inc. to make a more useful program.

When you get to work on the fourth day, your boss tells you that she likes what you have done with the ISBN Check Digit program so far. But she asks that she be able to enter multiple 9-digit ISBNs, and then have it calculate the check digits all at once rather than repeatedly running the program. Your boss also mentions that it is not necessary to see the weighted or running totals as the program operates.

   

{{{Body}}}



Array Overview

This is not done yet, testing things atm.

int intArray[] = new int[5];

for(int i=0; i <= intArray.length; i++)
{
  system.out.println(intArray[i]);
}

Code to fix

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 CaseStudy3_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

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

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

		//method call to calculate the checkDigit
		//send the method the isbn
		calcISBN(temp);

        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 ("\nThe 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 ("The weighted total is: " + total +
                            "\nThe check digit is : " + checkDigit +
                            "\n\nThe 10-digit ISBN is: " + temp + checkDigit);

	}//end calcTotal

}//end class

Solution

Case Study IV - Solution

Links To Other 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.