GIVENS: digits (reference to array of digits) // first version: only the first digits need to be 0 n (number of elements in the public static boolean isZero(int [ ] digits) { RESULT: flag (TRUE if first digit is zero, FALSE otherwise) INTERMEDIATE: (none) boolean flag; // result flag = digits[0] == 0; HEADER: flag isZero(digits, n) return(flag); BODY: } //second version: all 16 digits need to be 0 public static boolean isZero(int [ ] digits) { } GIVENS: (none) /* This method asks the user to a credit card number ITERMEDIATE: as 4 integers, that will be placed in an array. This method RESULT: intArray (reference to integer array) calls readIntLine( ) from the class ITI1120 to read the n (number of elements in the array array in integers. */ HEADER: intArray, n readDigits( ) public static int [ ] readDigits( ) BODY: { int [ ] intArray; // reference to array System.out.println ("Please input the credit card number as four "); System.out.println ("numbers of four digits, separated by spaces;"); System.out.println("or press 0 to finish."); intArray = ITI1120.readIntLine( ); } GIVENS: x (integer number) // Returns the sum of the digits of a number x RESULT: sum (sum of all digits in the integer number) public static int sumDigits(int x) INTERMEDIATE: { int sum; // result sum of digits HEADER: sum numDigits(x) – // Body BODY: sum = 0; while (x != 0) { } return sum; }
GIVENS: digits (reference to array of digits) n (number of elements in the RESULT: flag (TRUE if first digit is zero, FALSE otherwise) INTERMEDIATE: (none) HEADER: flag isZero(digits, n) BODY: // first version: only the first digits need to be 0 public static boolean isZero(int [ ] digits) { boolean flag; // result flag = digits[0] == 0; return(flag); } //second version: all 16 digits need to be 0 public static boolean isZero(int [ ] digits) { } GIVENS: (none) ITERMEDIATE: RESULT: intArray (reference to integer array) n (number of elements in the array HEADER: intArray, n readDigits( ) BODY: /* This method asks the user to a credit card number as 4 integers, that will be placed in an array. This method calls readIntLine( ) from the class ITI1120 to read the array in integers. */ public static int [ ] readDigits( ) { int [ ] intArray; // reference to array System.out.println ("Please input the credit card number as four "); System.out.println ("numbers of four digits, separated by spaces;"); System.out.println("or press 0 to finish."); intArray = ITI1120.readIntLine( ); } GIVENS: x (integer number) RESULT: sum (sum of all digits in the integer number) INTERMEDIATE: HEADER: sum numDigits(x) BODY: // Returns the sum of the digits of a number x public static int sumDigits(int x) { int sum; // result – sum of digits // Body sum = 0; while (x != 0) { } return sum; }
ITI1120WorkBook.docx