Debugging and Testing Exercises

  < Previous  Next >
  1. Discuss with someone else the difference between debugging and testing
  2. Discuss with someone else:
    1. What is the relationship between 100% code coverage in your unit tests and bugs?
    2. What is regression testing and when should it be applied?
  3. Write appropriate unit tests for getTax() in the following code. Run the tests and fix the code.
    /**
     * Simple linear income tax calculator based on preset rates and threshold levels.
     * <p>
     * Note: Intentionally contains bugs to illustrate the importance of unit testing.
     * </p>
     * @author Hyrum D. Carroll
     * @version 0.3, 09/08/20
     */
    public class TaxCalculator{
        private double tax;  // the amount of tax due
    
        // annual income in dollars
        private final double LEVEL_1_INCOME = 10_000.00;
        private final double LEVEL_2_INCOME = 50_000.00;
        private final double LEVEL_3_INCOME = 100_000.00;
    
        private final double TAX_RATE_1 = 0.10; // % of income
        private final double TAX_RATE_2 = 0.15; // % of income
        private final double TAX_RATE_3 = 0.20; // % of income
        private final double TAX_RATE_4 = 0.30; // % of income
    
        /**
         * No-argument constructor
         */
        public TaxCalculator( ){
    	tax = 0.0;
        }
    
        /**
         * Calculates the amount of tax due
         * <p>
         * Calculations are as follows:
         <ul>
         <li>if income is less than $10,000, 10% of income</li>
         <li>if income is less than $50,000, 15% of income</li>
         <li>if income is less than $100,000, 20% of income</li>
         <li>otherwise, 30% of income</li>
         </ul>
         * </p>
         * @param income the amount of income in dollars (and cents)
         * @return the amount of tax due
         */
        public double getTax( double income ){
    	if( income < LEVEL_1_INCOME ){
    	    tax = income * TAX_RATE_1;
    	}else if( income < LEVEL_2_INCOME ){
    	    tax = income * TAX_RATE_2;
    	}else if( income < LEVEL_2_INCOME ){
    	    tax = income * TAX_RATE_3;
    	}else{
    	    tax = income * TAX_RATE_4;
    	}
    	tax = Math.round(tax * 100.0) / 100.0; // round to 2 decimal places
    	return tax;
        }
    }
    
  4. Write appropriate unit tests for each of the methods below. Run the tests and fix the code.
    /**
     * Stores a pair of whole numbers
     * <p>
     * Caution: Class intentionally has one or more errors (for pedagogical purposes)
     * </p>
     * @author Hyrum D. Carroll
     * @version 0.2, 09/08/20
     */
    public class NumberPairs {
        /** first number */
        private int value1;
    
        /** second number */
        private int value2;
    
        /**
         * Set the first value
         * @param valueVal the number for the first value
         */
        public void setValue1( int valueVal ){
            value1 = valueVal;
        }
    
        /**
         * Set the first value
         * @param valueVal the number for the second value
         */
        public void setValue2( int valueVal ){
            value2 = valueVal;
        }
    
        /**
         * Accessor for first value
         * @return the first value
         */
        public int getValue1(){
            return value1;
        }
    
        /**
         * Accessor for second value
         * @return the second value
         */
        public int getValue2(){
            return value1;
        }
    
        /**
         * Calculate the average of the two values
         * @return the average of the two values
         */
        public int averagePairs(){
            return value1 + value2 / 2;
        }
    }