Difference between revisions of "Case Study IV"
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{ | ||
1010Topic | 1010Topic | ||
− | |Introduction= | + | |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. |
− | |Overview= | + | 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. | ||
|Chapter_TOC=[[Java Fundamentals]] | |Chapter_TOC=[[Java Fundamentals]] | ||
Line 10: | Line 12: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | |||
+ | =Array Overview= | ||
+ | <p> | ||
+ | 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. | ||
+ | <br/> | ||
+ | <i>Note: once the array has been created, neither its type nor size can be modified</i> | ||
+ | |||
+ | <pre> | ||
+ | int intArray[] = new int[5]; | ||
+ | </pre> | ||
+ | |||
+ | The best way to loop through an array, is to use .length, this will give you the length of the array. | ||
+ | <br/> | ||
+ | <i>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!</i> | ||
+ | |||
+ | <pre> | ||
+ | for(int i=0; i < intArray.length; i++) | ||
+ | { | ||
+ | system.out.println(intArray[i]); | ||
+ | } | ||
+ | </pre> | ||
+ | </p> | ||
+ | |||
+ | =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> | ||
=Solution= | =Solution= | ||
[[Case Study IV - Solution]] | [[Case Study IV - Solution]] | ||
+ | |||
+ | =Resources= | ||
+ | The resources needed to solve this case study can be found through the following links. | ||
+ | <br /> | ||
+ | ==Internal Links== | ||
+ | Chapter 8: [[Arrays]]<br /> | ||
+ | ==External Links== | ||
+ | [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 coding standards]<br /> | ||
+ | [http://www.cs.queensu.ca/~bradbury/checkdigit/isbncheck.htm ISBN Check Digit Queen's University]<br /> | ||
=Links To Other Case Studies= | =Links To Other Case Studies= |
Latest revision as of 22:25, 5 December 2007
COMP 1010 Home > Java Fundamentals
IntroductionNow 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
Resources
The resources needed to solve this case study can be found through the following links.
Internal Links
Chapter 8: Arrays
External Links
COMP 1010 coding standards
ISBN Check Digit Queen's University
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.