assert Exercises

  < Previous  Next >
  1. Write a simple assert statement with just a test expression
  2. Write an assert statement with a test expression and an error message
  3. Replace the /* add assert statement HERE */ with an appropriate assert statement:
    final int MAX_LOOP_COUNT = 10;
    final int COUNTER_THRESHOLD = 50;
    int i = 0;
    int counter = 0;
    while( i < MAX_LOOP_COUNT ){
        int j = 0;
        while( j < MAX_LOOP_COUNT ){
            ++counter;
            if( counter > 50 ){
                return;
            }
            ++j;
        }
        ++i;
    }
    
    // Should never reach this line
    	    
    /* add assert statement HERE */
    
  4. Add appropriate assert statements for the assumptions in the following helper method:
    /**
     * Processes the survey results
     * @param name Participant's last name
     * @param age Participant's age
     * @return true if survey results were successful processed, false otherwise
     */
    private boolean processSurvey( String name, int age ){
    
        // Note: It's assumed that the name is at least 2 characters long
        // Note: Can only legally process participants 18 years and older
        
    }