Case Study III - Solution

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Java Fundamentals


Introduction

In Case Study III you were asked to seperate a program into several different User-Defined Methods. QuickCreateWithTotals calculates a tenth digit for an ISBN, given the first 9 digits. This method prints out all of the steps involved in calculating the tenth digit. QuickCreateWithoutTotals does the same thing as the previous methos, except that it does not print out all of the steps involved in the calculation. Verify ISBN takes a 10 digit ISBN number and checks to see if it is a valid ISBN number. If it is a valid ISBN number it will print a message that says"Valid ISBN". Otherwise a message will be printed telling the user that the ISBN number that they entered is not valid. A solution to that case study is provided here.

   

{{{Body}}}



About Methods

The addition of methods to code not only makes the code more efficient, it makes it more reusable. In Computer Science it is important to consider how a program will be used in the future, and how it can be expanded to include more features. When the same segment of code can be used in many different parts of the application, you should consider creating a method that will do that particular task. Now the parts of the code that need to do that task only need to make a "call" to that method. This will eliminate repetition of code and make the code easier to understand.

Reality Check

The concept of User-Define Methods tends to be very abstract so it may help to think of it in terms of an every day application. Consider a kitchen appliance. A blender can be used for many things including making smoothies, making salad dressing and making pancake batter. It would be ridiculous to buy a new blender for every one of these tasks. The same thing applies to repetition of code. If the same method can be used for several tasks you should find a way to incorporate it into your code.

How To Create A Method

A method consists of a task, a return type and parameters. Once you have decided what the method is supposed to do you need to know what the method will need to carry out that task. For example, a String that you would like to modify. This or these will be your parameter(s). Then you need to consider what the calling method needs. For example, if you are calculating a number and you want to print it directly to the console your return type should be void because you will not be returning anything. However, if you need to make further calculations on that number you will want your return type to be an integer so that you can send that number back to the calling method.

Sample Solution

Sample solution using three methods and a main method:

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


public class ISBN {

	public static void main (String [] args) {
		String inputString;  // Used for JOP input
		char option;         // Will either be 'C' for "Create with running totals" or
							 // 'Q' for "Quick create without running totals" or
							 // 'V' for "Verify a 10-digit ISBN"
		int  isbn;
		int  sum = 0;        // Must be initialized
		int  digit;          // of input String
		char lastChar;       // of input String
		char checkChar ;     // correct check digit as character

		inputString = JOptionPane.showInputDialog ("Enter:" + "\nC (Create)," +
												   "\nQ (Quick Create), or" +
												   "\nV (Verify)");
		option = inputString.charAt(0);

		// Create a 10 "digit" ISBN given the first 9 "digits", and print the running totals
		if (option == 'C' || option == 'c') {
			inputString = JOptionPane.showInputDialog ("Enter 9 digit ISBN");
			QuickCreateWithTotals(inputString);

		} // if (option = 'C')
		else

		// Create a 10 "digit" ISBN given the first 9 "digits", but do not print any running totals
		if (option == 'Q' || option == 'q') {
			inputString = JOptionPane.showInputDialog ("Enter 9 digit ISBN");
		    QuickCreateWithoutTotals(inputString);
		} // if (option == 'Q')
		else

		// Verify a 10 "digit" IBSN as valid or not - if invalid, print out the valid ISBN
		if (option == 'V' || option == 'v') {
			inputString = JOptionPane.showInputDialog ("Enter 10 digit ISBN");
			VerifyISBN(inputString);
		} // if (option == 'V')

		// Invalid option
		else
			System.out.println ("You have entered an invalid option");

		System.out.println ("\nDeveloped by COMP 1010 Instructor");
		System.out.println ("Date: " + new Date());
		System.out.println ("*** End of Processing ***");
	}//close main
       
        /**
         * QuickCreateWithTotals calculates a valid 10th digit for an ISBN, given the first 9 digits. It also shows all of     
         * the  steps
         *
         * @param firstParameter  inputString is the 9 digit ISBN number
         *
         * @return                void. It doesn't return anything.
         */

	public static void QuickCreateWithTotals(String inputString){
		int  isbn;
		int  sum = 0;        // Must be initialized
		int  digit;          // of input String
		char lastChar;       // of input String
		char checkChar ;     // correct check digit as character
		System.out.println ("You entered the 9-digit ISBN: " + inputString);
		isbn = Integer.parseInt (inputString);

		for (int i=9; i>0; i--) {
				digit = isbn % 10;
				isbn = isbn / 10;
				sum = sum + digit * i;
				System.out.println ("Running total " + i + " * " + digit + " = " + sum);
		}//close for
		System.out.println ("Weighted sum = " + sum);

		if (sum%11 == 10) {
				System.out.println ("Check digit is 'X'");
				System.out.println ("The 10 digit ISBN is " + inputString + 'X');
		}//close if
		else {
				System.out.println ("Check digit is " + sum%11);
				System.out.println ("The 10 digit ISBN is " + inputString + sum%11);
		}//close else
	}//close QuickCreateWithTotals

         /**
         * QuickCreateWithoutTotals calculates a valid 10th digit for an ISBN, given the first 9 digits. It does NOT show all
         * of the  steps
         *
         * @param firstParameter  inputString is the 9 digit ISBN number
         *
         * @return                void. It doesn't return anything.
         */


	public static void QuickCreateWithoutTotals(String inputString){
		int  isbn;
		int  sum = 0;        // Must be initialized
		int  digit;          // of input String
		char lastChar;       // of input String
		char checkChar ;     // correct check digit as character
		System.out.println ("You entered the 9-digit ISBN: " + inputString);
		isbn = Integer.parseInt (inputString);

		for (int i=9; i>0; i--) {
			digit = isbn % 10;
			isbn = isbn / 10;
			sum = sum + digit * i;
		}//close for

		System.out.print ("The 10 digit ISBN is " + inputString);
		if (sum%11 == 10){
			System.out.println ('X');
		}//close if
		else{
			System.out.println (sum%11);
		}//close else
	}//close QuickCreateWithoutTotals

         /**
         * VerifyISBN checks to see if the 10 digit ISBN number that you entered is a valid ISBN number.
         *
         * @param firstParameter  inputString is the 10 digit ISBN number
         *
         * @return                void. It doesn't return anything.
         */


	public static void VerifyISBN(String inputString){
		int  isbn;
		int  sum = 0;        // Must be initialized
		int  digit;          // of input String
		char lastChar;       // of input String
		char checkChar ;     // correct check digit as character
		System.out.println ("You entered the 10 digit ISBN: " + inputString);

		// Separate the first 9 "digits" from the "last". Remember that the last "digit" may be an 'X'.
		lastChar = inputString.charAt(9);
		inputString = inputString.substring(0,9);
		isbn = Integer.parseInt (inputString);

		for (int i=9; i>0; i--) {
			digit = isbn % 10;
			isbn = isbn / 10;
			sum = sum + digit * i;
		}//close for

		if ( sum % 11 == 10 ){
			checkChar = 'X' ;
		}//close if
		else{
			// Convert check digit to a character. Note the little shorthand trick in doing this conversion.
			checkChar = (char)('0' + sum % 11) ;
		}//close else

		if (checkChar == lastChar){
			System.out.println ("Valid ISBN");
		}//close if
		else {
			System.out.println ("WARNING! INVALID ISBN. --- ");
			System.out.println ("A valid ISBN would be: " + inputString + checkChar);
		}//close else
	}//close VerifyISBN
}//close ISBN

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.