Difference between revisions of "Case Study IV"

From CompSciWiki
Jump to: navigation, search
(fixed an off by one error in your first code example)
Line 30: Line 30:
  
 
<pre>
 
<pre>
for(int i=0; i <= intArray.length; i++)
+
for(int i=0; i < intArray.length; i++)
 
{
 
{
 
   system.out.println(intArray[i]);
 
   system.out.println(intArray[i]);

Revision as of 22:00, 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

An array is a homogeneous collection of variables, and can be of any variable type (int, bool, float, String, etc.) To create an array, you must first declare it, and then instantiate it's size.
Note: once the array has been created, neither its type nor size can be modified

int intArray[] = new int[5];

The best way to loop through an array, is to use .length, this will give you the length of the array.
Note: an array's index begins at zero. This means that while an array X contains X.length items, X[array.length] does not exist and will give you an error!

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.