Advertisement
Guest User

DigitCombo.java

a guest
Jul 30th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package crossnum;
  2.  
  3. /**
  4.  * Represents a number in any specified base. Instead of digits being
  5.  * zero to base-minus-one, they can be from any specified start digit
  6.  * to start-plus-base-minus-one.
  7.  *
  8.  * Supports operations for incrementing a number and summing its digits.
  9.  *
  10.  * @author dub_nerd
  11.  *
  12.  */
  13. public class DigitCombo {
  14.  
  15.     /**
  16.      * Minimum value a number in the current base can have
  17.      */
  18.     final private int minDigit;
  19.  
  20.     /**
  21.      * Maximum value a number in the current base can have
  22.      */
  23.     final private int maxDigit;
  24.    
  25.     /**
  26.      * Overflow indicator, set if number incremented beyond its limit
  27.      */
  28.     private boolean overflow;
  29.  
  30.     /**
  31.      * The digits of the number.
  32.      */
  33.     final private int[] digits;
  34.    
  35.     /**
  36.      * Construct a new DigitCombo. All the digits are initially set to the
  37.      * specified start digit.
  38.      *
  39.      * @param numDigits
  40.      *            the number of digits in the number to be stored.
  41.      * @param base
  42.      *            the number base for this number.
  43.      * @param startDigit
  44.      *            the minimum value for a digit, not necessarily zero.
  45.      */
  46.     public DigitCombo(int numDigits, int base, int startDigit) {
  47.         minDigit = startDigit;
  48.         maxDigit = startDigit + base - 1;
  49.         digits = new int[numDigits];
  50.         for (int i = 0; i < numDigits; i++) {
  51.             digits[i] = 1;
  52.         }
  53.     }
  54.    
  55.     /**
  56.      * Get the digits of this number.
  57.      * @return digits
  58.      */
  59.     public int[] digits() {
  60.         return digits;
  61.     }
  62.    
  63.    
  64.     /**
  65.      * Increment the currently stored number.
  66.      */
  67.     public void increment() {
  68.         // Bump the value of the least significant (i.e. last) digit.
  69.         increment(digits.length - 1);
  70.     }
  71.    
  72.     /**
  73.      * Increment the value of the digit in the specified position.
  74.      * Calls itself recursively to implement digit carry.
  75.      * @param position the position of the digit to be incremented.
  76.      */
  77.     private void increment(int position) {
  78.         if (digits[position] < maxDigit) {
  79.             // ok -- no carry required
  80.             digits[position]++;
  81.         } else {
  82.             // reset current digit to minimum, and carry over
  83.             digits[position] = minDigit;
  84.             if (position > 0) {
  85.                 // increment the next most significant digit
  86.                 increment(position - 1);
  87.             } else {
  88.                 // overflow in most significant digit
  89.                 overflow = true;
  90.             }
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Get the sum of all the digits.
  96.      * @return digit sum
  97.      */
  98.     public int summedDigits() {
  99.         int total = 0;
  100.         for (int n : digits) {
  101.             total += n;
  102.         }
  103.         return total;
  104.     }
  105.  
  106.     /**
  107.      * Check if number has overflowed.
  108.      * @return the overflow indicator.
  109.      */
  110.     public boolean isOverflow() {
  111.         return overflow;
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement