Vous êtes sur la page 1sur 6

# COMP

Page 1 of 6

## COMP 1010 Fall 2013 Assignment 2

Due date: Wednesday, October 23, 2013, before midnight

Material Covered
long integers if statements input validation using Scanner "has-" methods loops (while, do-while, for) File input using Scanner class String processing

Page 2 of 6

## Question 1 - Credit Card Validator (45%) 1

In this question you will implement an algorithm for checking a 16-digit credit card number for validity. This algorithm is useful for checking for common errors such as transposing two digits when entering a credit card number on a web page. (Of course, it can't tell whether or not the number is a legitimate account.) You can try it on your own credit card numbers if you like. The algorithm works as follows: Each of the 16 digits is in either an even or odd position; the leftmost digit is even, the next digit is odd, etc. From each digit we calculate a check value: for a digit in an odd position, the check value is the digit itself; for a digit in an even position we multiply it by 2 to get the check value, and if the check value is greater than 9 we subtract 9. The check values are totalled. If the total is divisible by 10, the credit card number is valid.

For example, performing the algorithm on the credit card number 5048 4801 4229 6497 gives the following results: digit position check value
5 0 4 8 4 8 0 1 4 2 2 9 6 4 9 7 even odd even odd even odd even odd even odd even odd even odd even odd 5*2 = 10 - 9 = 1 0 4*2 = 8 8 4*2 = 8 8 2*0 = 0 1 4*2 = 8 2 2*2 = 4 9 6*2 = 12 - 9 = 3 4 9*2 = 18 - 9 = 9 7

Total 80 (valid) Create a Java program that accepts as input a 16-digit number and implements the above algorithm to check for a valid credit card number. Since a 16-digit number is too large to fit into an int, you will need to work with long integer values.
1

## COMP 1010 Fall 2013 Assignment 2

Page 3 of 6

Input: Ask the user for a credit card number with 16 digits (Note: leading zeros are NOT permitted). Use System.in (using the Scanner class) for input and echo the input using System.out.println. You must validate the input by checking that 1) the input is a valid long integer (hint: use one of the Scanner "has-" methods), and 2) that the input has 16 digits (hint: the number must be in the range 1,000,000,000,000,000 input < 10,000,000,000,000,000). Use a loop to accept and validate input so that the user must enter valid input before proceeding to the calculation step. Print meaningful messages when invalid input is entered (see the sample output below). Calculate: calculate the check value using the algorithm described above. You will use a loop to perform this calculation. (Which is the most appropriate type?) Output: Print (using System.out) the total check value and a message indicating whether the credit card number is valid or not. Here is sample output from the program. Scanner input is shown in bold italic. Enter a 16-digit credit card number: something else Input of 'something else' is not valid. Enter a 16-digit credit card number : 0000111122223333 Input of 111122223333 is not 16 digits. Enter a 16-digit credit card number: 12345678901234567 Input of 12345678901234567 is not 16 digits. Enter a 16-digit credit card number: 5048480142296497 Your card number is 5048480142296497 Checksum is 80 Card number is valid. Programmed by Stew Dent ** End of processing. ** Extra feature (not required): Echo the credit card number as four groups of 4 digits. Hand in: Your Java source code, according to the instructions on page 1. Also, submit two sets of output using the following input values: 5048 4801 4229 6497 and 1234 5678 9009 8765. Your output should also show that invalid input is detected and an appropriate message is printed by entering: 1) non-numeric input, 2) a number with less than 16 digits and 3) a number with more than 16 digits. Put all your output into one file. (Submit exactly 2 files, one .java file and one .txt file, for this question.)

Page 4 of 6

## Question 2 - File Input and String processing (45%)

One of the reasons we use computers is their ability to quickly process large data sets. In this question you will get a chance to do just that, by reading information from a text file and processing it. Boilerplate code for the file input using the Scanner class is provided; you will provide the required processing. We will define something called an ascending word for the purposes of processing. An ascending word is one where each letter is either equal to or occurs later in the alphabet than the preceding letter. That is, the letters in the word are found in order in the alphabet. For example, the word "envy" is an ascending word. The word "invest" is not ascending, since the letters 'v' and 'e' are not in order. Input: You will read data from the file dictionary.txt, which contains 20,069 words, one per line. Download this file and place it in the same folder as your Java program. The boilerplate code for performing file input is shown highlighted in red below:
import java.io.*; // defines FileNotFoundException public static void main(String[] args) throws FileNotFoundException{ String theWord; Scanner inputFile = new Scanner(new File("dictionary.txt")); while(inputFile.hasNextLine()){ theWord = inputFile.nextLine(); // your code goes here }

Calculate: Find all the ascending words that are at least 2 letters long (the single letter words are not interesting), count them and calculate the average length and longest length. Output: Print the list of ascending words. Group the list by first letter, separating each group with a blank line. Print as many words on each line as will fit into an 80-character width, but do not go over 80 characters. Separate the words with a single blank (but don't put a blank at the beginning of a line). Print the total number of ascending words found, their average length and the longest length. Hand in: Your Java source code, according to the instructions on page 1. Also, submit the output from running your program. (Submit exactly 2 files, one .java file and one .txt file, for this question.) Here is sample output from the program, with some values obscured. Since everyones output should be identical, we dont want to give away the correct values.
Searching for ascending words from the input file. abbe abbey abbot abc abet abort accent accept access accost ace act ad add adept ado adopt aegis affix afoot aft ago ah ahoy ail aim air airy all allot allow // more output here Found *** ascending words. Average word length is ******. Longest word length is *.

Page 5 of 6