Let's start with analytical solution. The paragraph just below listing 16 says:
The analytic answer is $\frac 4 3 \pi = 4.188790204786391$ — if you remember enough advanced calc, check me!
I remember very little advanced calc, but let me try...
NB: I've updated this post, which was initially using wrong formulas to compute surface integral.
General equation to compute an integral of function $f$ over surface $S$ is:
$$I_S = \iint_S f \cdot dS$$
Using spherical coordinates, surface $S$ can be described in terms of a parameterized vector function $\vec v(\theta, \phi)$, and it can be proven (I won't do that here) that:
$$dS = \bigg| \frac {\partial \vec v}{\partial \theta} \times \frac {\partial \vec v}{\partial \phi} \bigg| d\theta d\phi$$
For a sphere of radius $r$, we can write the following parametric equations:

$$\displaylines{
x = r \cdot \sin \theta \cdot \cos \phi \\
y = r \cdot \sin \theta \cdot \sin \phi \\
z = r \cdot \sin \theta
}$$
$$\vec v(\theta, \phi) = (x, y, z) = (r \cdot \sin \theta \cdot \cos \phi, r \cdot \sin \theta \cdot \sin \phi, r \cdot \sin \theta)$$
If you take partial derivatives $\frac {\partial \vec v}{\partial \theta}$ and $\frac {\partial \vec v}{\partial \phi}$, and compute their cross-product, you will arrive at the following equation:
$$dS = r^2 \sin (\theta) d\theta d\phi$$
And, since we have a unit sphere, it becomes: $dS = \sin (\theta) d\theta d\phi$
So now, if we integrate our function $f(\theta, \phi) = \cos^2(\theta)$ over this unit sphere, we get:
$$I_S = \iint_S \cos^2(\theta) \sin(\theta) d\theta d\phi = \int_0^{2\pi} \bigg( \int_0^\pi \cos^2(\theta) \sin(\theta) d\theta \bigg) d\phi$$
Let's integrate over $\theta$ first:
$$I_\theta = \int_0^\pi \cos^2(\theta) \sin(\theta) d\theta$$
Using u-substitution with $u = \cos(\theta)$, we get $\frac {du}{d\theta} = -\sin(\theta)$ or $d\theta = -\frac {du}{\sin(\theta)}$. Substitute $u$ and $d\theta$ into the above formula, and we get:
$$I_\theta = \int_0^\pi -u^2 \sin(\theta) \frac {du}{\sin(\theta)} = \int_0^\pi -u^2 du$$
$$I_\theta = -\frac {u^3} 3 \bigg|_0^\pi = -\frac {\cos^3(\theta)} 3 \bigg|_0^\pi = \frac 2 3$$
Now we integrate over $\phi$:
$$I = \int_0^{2\pi} I_\theta d\phi = \int_0^{2\pi} \frac 2 3 d\phi = \frac 2 3 \phi \bigg|_0^{2\pi} = \frac 4 3 \pi$$
QED